DashboardLinks.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { FC } from 'react';
  2. import { useEffectOnce } from 'react-use';
  3. import { sanitizeUrl } from '@grafana/data/src/text/sanitize';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { TimeRangeUpdatedEvent } from '@grafana/runtime';
  6. import { Icon, IconName, Tooltip, useForceUpdate } from '@grafana/ui';
  7. import { getLinkSrv } from '../../../panel/panellinks/link_srv';
  8. import { DashboardModel } from '../../state';
  9. import { DashboardLink } from '../../state/DashboardModel';
  10. import { linkIconMap } from '../LinksSettings/LinkSettingsEdit';
  11. import { DashboardLinksDashboard } from './DashboardLinksDashboard';
  12. export interface Props {
  13. dashboard: DashboardModel;
  14. links: DashboardLink[];
  15. }
  16. export const DashboardLinks: FC<Props> = ({ dashboard, links }) => {
  17. const forceUpdate = useForceUpdate();
  18. useEffectOnce(() => {
  19. const sub = dashboard.events.subscribe(TimeRangeUpdatedEvent, forceUpdate);
  20. return () => sub.unsubscribe();
  21. });
  22. if (!links.length) {
  23. return null;
  24. }
  25. return (
  26. <>
  27. {links.map((link: DashboardLink, index: number) => {
  28. const linkInfo = getLinkSrv().getAnchorInfo(link);
  29. const key = `${link.title}-$${index}`;
  30. if (link.type === 'dashboards') {
  31. return <DashboardLinksDashboard key={key} link={link} linkInfo={linkInfo} dashboardId={dashboard.id} />;
  32. }
  33. const linkElement = (
  34. <a
  35. className="gf-form-label gf-form-label--dashlink"
  36. href={sanitizeUrl(linkInfo.href)}
  37. target={link.targetBlank ? '_blank' : undefined}
  38. rel="noreferrer"
  39. data-testid={selectors.components.DashboardLinks.link}
  40. >
  41. <Icon aria-hidden name={linkIconMap[link.icon] as IconName} style={{ marginRight: '4px' }} />
  42. <span>{linkInfo.title}</span>
  43. </a>
  44. );
  45. return (
  46. <div key={key} className="gf-form" data-testid={selectors.components.DashboardLinks.container}>
  47. {link.tooltip ? <Tooltip content={linkInfo.tooltip}>{linkElement}</Tooltip> : linkElement}
  48. </div>
  49. );
  50. })}
  51. </>
  52. );
  53. };