VariableUsagesButton.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React, { FC, useMemo } from 'react';
  2. import { reportInteraction } from '@grafana/runtime';
  3. import { IconButton } from '@grafana/ui';
  4. import { NetworkGraphModal } from './NetworkGraphModal';
  5. import { UsagesToNetwork } from './utils';
  6. interface Props {
  7. id: string;
  8. usages: UsagesToNetwork[];
  9. isAdhoc: boolean;
  10. }
  11. export const VariableUsagesButton: FC<Props> = ({ id, usages, isAdhoc }) => {
  12. const network = useMemo(() => usages.find((n) => n.variable.id === id), [usages, id]);
  13. if (usages.length === 0 || isAdhoc || !network) {
  14. return null;
  15. }
  16. const nodes = network.nodes.map((n) => {
  17. if (n.label.includes(`$${id}`)) {
  18. return { ...n, color: '#FB7E81' };
  19. }
  20. return n;
  21. });
  22. return (
  23. <NetworkGraphModal show={false} title={`Showing usages for: $${id}`} nodes={nodes} edges={network.edges}>
  24. {({ showModal }) => {
  25. return (
  26. <IconButton
  27. onClick={() => {
  28. reportInteraction('Show variable usages');
  29. showModal();
  30. }}
  31. name="code-branch"
  32. title="Show usages"
  33. />
  34. );
  35. }}
  36. </NetworkGraphModal>
  37. );
  38. };