annotatedTextErrors.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import capitalize from 'lodash/capitalize';
  4. import List from 'sentry/components/list';
  5. import ListItem from 'sentry/components/list/listItem';
  6. import Tooltip from 'sentry/components/tooltip';
  7. import {IconWarning} from 'sentry/icons';
  8. import space from 'sentry/styles/space';
  9. import {MetaError} from 'sentry/types';
  10. function formatErrorKind(kind: string) {
  11. return capitalize(kind.replace(/_/g, ' '));
  12. }
  13. function ErrorMessage({error}: {error?: MetaError}) {
  14. if (Array.isArray(error)) {
  15. if (!error[0] && !error[1]?.reason) {
  16. return null;
  17. }
  18. const errorMessage: React.ReactNode[] = [];
  19. if (error[0]) {
  20. errorMessage.push(<strong>{formatErrorKind(error[0])}</strong>);
  21. }
  22. if (error[1]?.reason) {
  23. errorMessage.push(<div>{capitalize(error[1].reason)}</div>);
  24. }
  25. return <div>{errorMessage}</div>;
  26. }
  27. if (!error) {
  28. return null;
  29. }
  30. return <Fragment>{formatErrorKind(error)}</Fragment>;
  31. }
  32. export function AnnotatedTextErrors({errors = []}: {errors: Array<MetaError>}) {
  33. if (!errors.length) {
  34. return null;
  35. }
  36. return (
  37. <StyledTooltip
  38. title={
  39. errors.length === 1 ? (
  40. <ErrorMessage error={errors[0]} />
  41. ) : (
  42. <Errors symbol="bullet">
  43. {errors.map((error, index) => (
  44. <Error key={index}>
  45. <ErrorMessage error={error} />
  46. </Error>
  47. ))}
  48. </Errors>
  49. )
  50. }
  51. >
  52. <StyledIconWarning color="red300" />
  53. </StyledTooltip>
  54. );
  55. }
  56. const StyledTooltip = styled(Tooltip)`
  57. margin-left: ${space(0.75)};
  58. `;
  59. const StyledIconWarning = styled(IconWarning)`
  60. vertical-align: middle;
  61. `;
  62. const Errors = styled(List)``;
  63. const Error = styled(ListItem)`
  64. text-align: left;
  65. `;