components.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {Fragment, ReactNode, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {KeyValueTable, KeyValueTableRow} from 'sentry/components/keyValueTable';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {IconChevron} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. export const Indent = styled('div')`
  9. padding-left: ${space(4)};
  10. `;
  11. const NotFoundText = styled('span')`
  12. color: ${p => p.theme.subText};
  13. font-size: ${p => p.theme.fontSizeSmall};
  14. `;
  15. const WarningText = styled('span')`
  16. color: ${p => p.theme.errorText};
  17. `;
  18. export function Warning({warnings}: {warnings: undefined | string[]}) {
  19. if (warnings?.includes('JSON_TRUNCATED') || warnings?.includes('TEXT_TRUNCATED')) {
  20. return (
  21. <WarningText>{t('Truncated (~~) due to exceeding 150k characters')}</WarningText>
  22. );
  23. }
  24. if (warnings?.includes('INVALID_JSON')) {
  25. return <WarningText>{t('Invalid JSON')}</WarningText>;
  26. }
  27. return null;
  28. }
  29. export function SizeTooltip({children}: {children: ReactNode}) {
  30. return (
  31. <Tooltip
  32. title={t('It is possible the network transfer size is smaller due to compression.')}
  33. >
  34. {children}
  35. </Tooltip>
  36. );
  37. }
  38. export function keyValueTableOrNotFound(
  39. data: undefined | Record<string, string | ReactNode>,
  40. notFoundText: string
  41. ) {
  42. return data ? (
  43. <StyledKeyValueTable noMargin>
  44. {Object.entries(data).map(([key, value]) => (
  45. <KeyValueTableRow key={key} keyName={key} value={<span>{value}</span>} />
  46. ))}
  47. </StyledKeyValueTable>
  48. ) : (
  49. <Indent>
  50. <NotFoundText>{notFoundText}</NotFoundText>
  51. </Indent>
  52. );
  53. }
  54. const SectionTitle = styled('dt')``;
  55. const SectionTitleExtra = styled('span')`
  56. flex-grow: 1;
  57. text-align: right;
  58. font-weight: normal;
  59. `;
  60. const SectionData = styled('dd')`
  61. font-size: ${p => p.theme.fontSizeExtraSmall};
  62. `;
  63. const ToggleButton = styled('button')`
  64. background: ${p => p.theme.background};
  65. border: 0;
  66. color: ${p => p.theme.headingColor};
  67. font-size: ${p => p.theme.fontSizeSmall};
  68. font-weight: 600;
  69. line-height: ${p => p.theme.text.lineHeightBody};
  70. width: 100%;
  71. display: flex;
  72. align-items: center;
  73. justify-content: flex-start;
  74. gap: ${space(1)};
  75. padding: ${space(0.5)} ${space(1)};
  76. :hover {
  77. background: ${p => p.theme.backgroundSecondary};
  78. }
  79. `;
  80. export function SectionItem({
  81. children,
  82. title,
  83. titleExtra,
  84. }: {
  85. children: ReactNode;
  86. title: ReactNode;
  87. titleExtra?: ReactNode;
  88. }) {
  89. const [isOpen, setIsOpen] = useState(true);
  90. return (
  91. <Fragment>
  92. <SectionTitle>
  93. <ToggleButton aria-label={t('toggle section')} onClick={() => setIsOpen(!isOpen)}>
  94. <IconChevron direction={isOpen ? 'down' : 'right'} size="xs" />
  95. {title}
  96. {titleExtra ? <SectionTitleExtra>{titleExtra}</SectionTitleExtra> : null}
  97. </ToggleButton>
  98. </SectionTitle>
  99. <SectionData>{isOpen ? children : null}</SectionData>
  100. </Fragment>
  101. );
  102. }
  103. const StyledKeyValueTable = styled(KeyValueTable)`
  104. & > dt {
  105. font-size: ${p => p.theme.fontSizeSmall};
  106. padding-left: ${space(4)};
  107. }
  108. & > dd {
  109. ${p => p.theme.overflowEllipsis};
  110. font-size: ${p => p.theme.fontSizeSmall};
  111. display: flex;
  112. justify-content: flex-end;
  113. white-space: normal;
  114. text-align: right;
  115. }
  116. `;