tachybase_todo/packages/plugins/charts/src/client/hooks/index.ts
2023-06-05 13:13:06 +08:00

20 lines
593 B
TypeScript

import { useChartQueryMetadataContext } from '../ChartQueryMetadataProvider';
import { useEffect, useState } from 'react';
const useFieldsById = (queryId: number) => {
const [fields, setFields] = useState([]);
const ctx = useChartQueryMetadataContext();
useEffect(() => {
const chartQueryList = ctx?.data;
if (chartQueryList && Array.isArray(chartQueryList)) {
const currentQuery = chartQueryList.find((chartQuery) => chartQuery.id === queryId);
setFields(currentQuery?.fields || []);
}
}, [queryId]);
return {
fields,
};
};
export { useFieldsById };