1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-12 13:48:35 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useApplication/useApplication.ts
Christopher Kolstad 53354224fc
chore: Bump biome and configure husky (#6589)
Upgrades biome to 1.6.1, and updates husky pre-commit hook.

Most changes here are making type imports explicit.
2024-03-18 13:58:05 +01:00

64 lines
1.6 KiB
TypeScript

import useSWR, { mutate, type SWRConfiguration } from 'swr';
import { useState, useEffect } from 'react';
import { formatApiPath } from 'utils/formatPath';
import handleErrorResponses from '../httpErrorResponseHandler';
import type { IApplication } from 'interfaces/application';
interface IUseApplicationOutput {
application: IApplication;
refetchApplication: () => void;
loading: boolean;
error?: Error;
APPLICATION_CACHE_KEY: string;
}
const useApplication = (
name: string,
options: SWRConfiguration = {},
): IUseApplicationOutput => {
const path = formatApiPath(`api/admin/metrics/applications/${name}`);
const fetcher = async () => {
return fetch(path, {
method: 'GET',
})
.then(handleErrorResponses('Application'))
.then((res) => res.json());
};
const APPLICATION_CACHE_KEY = `api/admin/metrics/applications/${name}`;
const { data, error } = useSWR(APPLICATION_CACHE_KEY, fetcher, {
...options,
});
const [loading, setLoading] = useState(!error && !data);
const refetchApplication = () => {
mutate(APPLICATION_CACHE_KEY);
};
useEffect(() => {
setLoading(!error && !data);
}, [data, error]);
return {
application: data || {
appName: name,
color: '',
createdAt: '2022-02-02T21:04:00.268Z',
description: '',
instances: [],
strategies: [],
seenToggles: [],
url: '',
},
error,
loading,
refetchApplication,
APPLICATION_CACHE_KEY,
};
};
export default useApplication;