1import { useMemo } from 'react';
2import { DataFrame } from '@grafana/data';
3
4/**
5 * As we need 2 dataframes for the service map, one with nodes and one with edges we have to figure out which is which.
6 * Right now we do not have any metadata for it so we just check preferredVisualisationType and then column names.
7 * TODO: maybe we could use column labels to have a better way to do this
8 */
9export function useCategorizeFrames(series: DataFrame[]) {
10  return useMemo(() => {
11    return series.reduce(
12      (acc, frame) => {
13        const sourceField = frame.fields.filter((f) => f.name === 'source');
14        if (sourceField.length) {
15          acc.edges.push(frame);
16        } else {
17          acc.nodes.push(frame);
18        }
19        return acc;
20      },
21      { edges: [], nodes: [] } as { nodes: DataFrame[]; edges: DataFrame[] }
22    );
23  }, [series]);
24}
25