DataLinks.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { GrafanaTheme2, VariableOrigin, DataLinkBuiltInVars } from '@grafana/data';
  4. import { Button, useStyles2 } from '@grafana/ui';
  5. import { DataLinkConfig } from '../types';
  6. import { DataLink } from './DataLink';
  7. const getStyles = (theme: GrafanaTheme2) => {
  8. return {
  9. infoText: css`
  10. padding-bottom: ${theme.spacing(2)};
  11. color: ${theme.colors.text.secondary};
  12. `,
  13. dataLink: css`
  14. margin-bottom: ${theme.spacing(1)};
  15. `,
  16. };
  17. };
  18. type Props = {
  19. value?: DataLinkConfig[];
  20. onChange: (value: DataLinkConfig[]) => void;
  21. };
  22. export const DataLinks = (props: Props) => {
  23. const { value, onChange } = props;
  24. const styles = useStyles2(getStyles);
  25. return (
  26. <>
  27. <h3 className="page-heading">Data links</h3>
  28. <div className={styles.infoText}>
  29. Add links to existing fields. Links will be shown in log row details next to the field value.
  30. </div>
  31. {value && value.length > 0 && (
  32. <div className="gf-form-group">
  33. {value.map((field, index) => {
  34. return (
  35. <DataLink
  36. className={styles.dataLink}
  37. key={index}
  38. value={field}
  39. onChange={(newField) => {
  40. const newDataLinks = [...value];
  41. newDataLinks.splice(index, 1, newField);
  42. onChange(newDataLinks);
  43. }}
  44. onDelete={() => {
  45. const newDataLinks = [...value];
  46. newDataLinks.splice(index, 1);
  47. onChange(newDataLinks);
  48. }}
  49. suggestions={[
  50. {
  51. value: DataLinkBuiltInVars.valueRaw,
  52. label: 'Raw value',
  53. documentation: 'Raw value of the field',
  54. origin: VariableOrigin.Value,
  55. },
  56. ]}
  57. />
  58. );
  59. })}
  60. </div>
  61. )}
  62. <Button
  63. type="button"
  64. variant={'secondary'}
  65. className={css`
  66. margin-right: 10px;
  67. `}
  68. icon="plus"
  69. onClick={(event) => {
  70. event.preventDefault();
  71. const newDataLinks = [...(value || []), { field: '', url: '' }];
  72. onChange(newDataLinks);
  73. }}
  74. >
  75. Add
  76. </Button>
  77. </>
  78. );
  79. };