URLPickerTab.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { css } from '@emotion/css';
  2. import React, { Dispatch, SetStateAction } from 'react';
  3. import SVG from 'react-inlinesvg';
  4. import { GrafanaTheme2 } from '@grafana/data';
  5. import { Field, Input, Label, useStyles2 } from '@grafana/ui';
  6. import { getPublicOrAbsoluteUrl } from '../resource';
  7. import { MediaType } from '../types';
  8. interface Props {
  9. newValue: string;
  10. setNewValue: Dispatch<SetStateAction<string>>;
  11. mediaType: MediaType;
  12. }
  13. export const URLPickerTab = (props: Props) => {
  14. const { newValue, setNewValue, mediaType } = props;
  15. const styles = useStyles2(getStyles);
  16. const imgSrc = getPublicOrAbsoluteUrl(newValue!);
  17. let shortName = newValue?.substring(newValue.lastIndexOf('/') + 1, newValue.lastIndexOf('.'));
  18. if (shortName.length > 20) {
  19. shortName = shortName.substring(0, 20) + '...';
  20. }
  21. return (
  22. <>
  23. <Field>
  24. <Input onChange={(e) => setNewValue(e.currentTarget.value)} value={newValue} />
  25. </Field>
  26. <div className={styles.iconContainer}>
  27. <Field label="Preview">
  28. <div className={styles.iconPreview}>
  29. {mediaType === MediaType.Icon && <SVG src={imgSrc} className={styles.img} />}
  30. {mediaType === MediaType.Image && newValue && <img src={imgSrc} className={styles.img} />}
  31. </div>
  32. </Field>
  33. <Label>{shortName}</Label>
  34. </div>
  35. </>
  36. );
  37. };
  38. const getStyles = (theme: GrafanaTheme2) => ({
  39. iconContainer: css`
  40. display: flex;
  41. flex-direction: column;
  42. width: 80%;
  43. align-items: center;
  44. align-self: center;
  45. `,
  46. iconPreview: css`
  47. width: 238px;
  48. height: 198px;
  49. border: 1px solid ${theme.colors.border.medium};
  50. display: flex;
  51. align-items: center;
  52. justify-content: center;
  53. `,
  54. img: css`
  55. width: 147px;
  56. height: 147px;
  57. fill: ${theme.colors.text.primary};
  58. `,
  59. });