utils.ts 663 B

12345678910111213141516171819202122232425
  1. import { useMemo } from 'react';
  2. import { DataFrame, getFieldDisplayName } from '@grafana/data';
  3. export function useAllFieldNamesFromDataFrames(input: DataFrame[]): string[] {
  4. return useMemo(() => {
  5. if (!Array.isArray(input)) {
  6. return [];
  7. }
  8. return Object.keys(
  9. input.reduce((names, frame) => {
  10. if (!frame || !Array.isArray(frame.fields)) {
  11. return names;
  12. }
  13. return frame.fields.reduce((names, field) => {
  14. const t = getFieldDisplayName(field, frame, input);
  15. names[t] = true;
  16. return names;
  17. }, names);
  18. }, {} as Record<string, boolean>)
  19. );
  20. }, [input]);
  21. }