AnnotationDetailsField.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { Tooltip, useStyles } from '@grafana/ui';
  5. import { Annotation, annotationLabels } from '../utils/constants';
  6. import { DetailsField } from './DetailsField';
  7. import { Well } from './Well';
  8. const wellableAnnotationKeys = ['message', 'description'];
  9. interface Props {
  10. annotationKey: string;
  11. value: string;
  12. }
  13. export const AnnotationDetailsField: FC<Props> = ({ annotationKey, value }) => {
  14. const label = annotationLabels[annotationKey as Annotation] ? (
  15. <Tooltip content={annotationKey} placement="top" theme="info">
  16. <span>{annotationLabels[annotationKey as Annotation]}</span>
  17. </Tooltip>
  18. ) : (
  19. annotationKey
  20. );
  21. return (
  22. <DetailsField label={label} horizontal={true}>
  23. <AnnotationValue annotationKey={annotationKey} value={value} />
  24. </DetailsField>
  25. );
  26. };
  27. const AnnotationValue: FC<Props> = ({ annotationKey, value }) => {
  28. const styles = useStyles(getStyles);
  29. const needsWell = wellableAnnotationKeys.includes(annotationKey);
  30. const needsLink = value && value.startsWith('http');
  31. if (needsWell) {
  32. return <Well className={styles.well}>{value}</Well>;
  33. }
  34. if (needsLink) {
  35. return (
  36. <a href={value} target="__blank" className={styles.link}>
  37. {value}
  38. </a>
  39. );
  40. }
  41. return <>{value}</>;
  42. };
  43. export const getStyles = (theme: GrafanaTheme) => ({
  44. well: css`
  45. word-break: break-all;
  46. `,
  47. link: css`
  48. word-break: break-all;
  49. color: ${theme.colors.textBlue};
  50. `,
  51. });