components.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 type KeyValueTuple = {
  39. key: string;
  40. value: string | ReactNode;
  41. type?: 'warning' | 'error';
  42. };
  43. export function keyValueTableOrNotFound(data: KeyValueTuple[], notFoundText: string) {
  44. return data.length ? (
  45. <StyledKeyValueTable noMargin>
  46. {data.map(({key, value, type}) => (
  47. <KeyValueTableRow
  48. key={key}
  49. keyName={key}
  50. type={type}
  51. value={<span>{value}</span>}
  52. />
  53. ))}
  54. </StyledKeyValueTable>
  55. ) : (
  56. <Indent>
  57. <NotFoundText>{notFoundText}</NotFoundText>
  58. </Indent>
  59. );
  60. }
  61. const SectionTitle = styled('dt')``;
  62. const SectionTitleExtra = styled('span')`
  63. flex-grow: 1;
  64. text-align: right;
  65. font-weight: normal;
  66. `;
  67. const SectionData = styled('dd')`
  68. font-size: ${p => p.theme.fontSizeExtraSmall};
  69. `;
  70. const ToggleButton = styled('button')`
  71. background: ${p => p.theme.background};
  72. border: 0;
  73. color: ${p => p.theme.headingColor};
  74. font-size: ${p => p.theme.fontSizeSmall};
  75. font-weight: 600;
  76. line-height: ${p => p.theme.text.lineHeightBody};
  77. width: 100%;
  78. display: flex;
  79. align-items: center;
  80. justify-content: flex-start;
  81. gap: ${space(1)};
  82. padding: ${space(0.5)} ${space(1)};
  83. :hover {
  84. background: ${p => p.theme.backgroundSecondary};
  85. }
  86. `;
  87. export function SectionItem({
  88. children,
  89. title,
  90. titleExtra,
  91. }: {
  92. children: ReactNode;
  93. title: ReactNode;
  94. titleExtra?: ReactNode;
  95. }) {
  96. const [isOpen, setIsOpen] = useState(true);
  97. return (
  98. <Fragment>
  99. <SectionTitle>
  100. <ToggleButton aria-label={t('toggle section')} onClick={() => setIsOpen(!isOpen)}>
  101. <IconChevron direction={isOpen ? 'down' : 'right'} size="xs" />
  102. {title}
  103. {titleExtra ? <SectionTitleExtra>{titleExtra}</SectionTitleExtra> : null}
  104. </ToggleButton>
  105. </SectionTitle>
  106. <SectionData>{isOpen ? children : null}</SectionData>
  107. </Fragment>
  108. );
  109. }
  110. const StyledKeyValueTable = styled(KeyValueTable)`
  111. & > dt {
  112. font-size: ${p => p.theme.fontSizeSmall};
  113. padding-left: ${space(4)};
  114. }
  115. & > dd {
  116. ${p => p.theme.overflowEllipsis};
  117. font-size: ${p => p.theme.fontSizeSmall};
  118. display: flex;
  119. justify-content: flex-end;
  120. white-space: normal;
  121. text-align: right;
  122. }
  123. `;