1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useInsights/useInsights.ts
Jaanus Sellin fc86f5b2fe
feat: align insights charts (#7984)
Now the left widget will use the same that as the last data point on the
right chart.
With this change, flags/users object never needs to be passed in.

**Next step is to remove the flags/users from the endpoint.**

Previous


![image](https://github.com/user-attachments/assets/b938e568-e32e-4ae8-beb9-d8de8aa2c5ec)

Now


![image](https://github.com/user-attachments/assets/182dfbbb-bf2e-42f9-a024-0c545c604f74)
2024-08-27 11:06:08 +03:00

52 lines
1.3 KiB
TypeScript

import useSWR, { mutate, type SWRConfiguration } from 'swr';
import { useCallback } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import type { InstanceInsightsSchema } from 'openapi';
interface IUseInsightsDataOutput {
insights: InstanceInsightsSchema;
refetchInsights: () => void;
loading: boolean;
error?: Error;
}
export const useInsights = (
from = '',
to = '',
options?: SWRConfiguration,
): IUseInsightsDataOutput => {
const path = formatApiPath(`api/admin/insights?from=${from}&to=${to}`);
const { data, error } = useSWR<InstanceInsightsSchema>(
path,
fetchExecutiveDashboard,
options,
);
const refetchInsights = useCallback(() => {
mutate(path).catch(console.warn);
}, [path]);
return {
insights: data || {
userTrends: [],
flagTrends: [],
projectFlagTrends: [],
metricsSummaryTrends: [],
environmentTypeTrends: [],
},
refetchInsights,
loading: !error && !data,
error,
};
};
const fetchExecutiveDashboard = (
path: string,
): Promise<InstanceInsightsSchema> => {
return fetch(path)
.then(handleErrorResponses('Executive Dashboard Data'))
.then((res) => res.json());
};