1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useExecutiveSummary/useExecutiveSummary.ts
andreas-unleash 60754b9fca
chore: generate new orval types (#6292)
what it says on the box

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
2024-02-21 11:31:06 +02:00

51 lines
1.4 KiB
TypeScript

import useSWR, { mutate, SWRConfiguration } from 'swr';
import { useCallback } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import { ExecutiveSummarySchema } from 'openapi';
interface IUseExecutiveDashboardDataOutput {
executiveDashboardData: ExecutiveSummarySchema;
refetchExecutiveDashboard: () => void;
loading: boolean;
error?: Error;
}
export const useExecutiveDashboard = (
options?: SWRConfiguration,
): IUseExecutiveDashboardDataOutput => {
const path = formatApiPath('api/admin/dashboard/executive');
const { data, error } = useSWR<ExecutiveSummarySchema>(
path,
fetchExecutiveDashboard,
options,
);
const refetchExecutiveDashboard = useCallback(() => {
mutate(path).catch(console.warn);
}, [path]);
return {
executiveDashboardData: data || {
users: { total: 0, inactive: 0, active: 0 },
flags: { total: 0 },
userTrends: [],
flagTrends: [],
projectFlagTrends: [],
impressionsSummary: [],
},
refetchExecutiveDashboard,
loading: !error && !data,
error,
};
};
const fetchExecutiveDashboard = (
path: string,
): Promise<ExecutiveSummarySchema> => {
return fetch(path)
.then(handleErrorResponses('Executive Dashboard Data'))
.then((res) => res.json());
};