mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-19 00:15:43 +01:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
|
import useSWR, { mutate } from 'swr';
|
||
|
import { useState, useEffect } from 'react';
|
||
|
|
||
|
import { formatApiPath } from '../../../../utils/format-path';
|
||
|
import { IFeatureToggle } from '../../../../interfaces/featureToggle';
|
||
|
import { defaultFeature } from './defaultFeature';
|
||
|
|
||
|
const useFeature = (projectId: string, id: string) => {
|
||
|
const fetcher = () => {
|
||
|
const path = formatApiPath(
|
||
|
`api/admin/projects/${projectId}/features/${id}`
|
||
|
);
|
||
|
return fetch(path, {
|
||
|
method: 'GET',
|
||
|
}).then(res => res.json());
|
||
|
};
|
||
|
|
||
|
const KEY = `api/admin/projects/${projectId}/features/${id}`;
|
||
|
|
||
|
const { data, error } = useSWR<IFeatureToggle>(KEY, fetcher);
|
||
|
const [loading, setLoading] = useState(!error && !data);
|
||
|
|
||
|
const refetch = () => {
|
||
|
mutate(KEY);
|
||
|
};
|
||
|
|
||
|
useEffect(() => {
|
||
|
setLoading(!error && !data);
|
||
|
}, [data, error]);
|
||
|
|
||
|
let feature = defaultFeature;
|
||
|
if (data) {
|
||
|
if (data.environments) {
|
||
|
feature = data;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
feature,
|
||
|
error,
|
||
|
loading,
|
||
|
refetch,
|
||
|
};
|
||
|
};
|
||
|
|
||
|
export default useFeature;
|