2022-04-26 09:54:16 +02:00
|
|
|
import useSWR, { SWRConfiguration } from 'swr';
|
2022-11-18 12:43:24 +01:00
|
|
|
import { useCallback, useEffect } from 'react';
|
2022-03-09 14:59:24 +01:00
|
|
|
import { emptyFeature } from './emptyFeature';
|
2021-10-15 09:21:38 +02:00
|
|
|
import handleErrorResponses from '../httpErrorResponseHandler';
|
2022-03-25 12:34:20 +01:00
|
|
|
import { formatApiPath } from 'utils/formatPath';
|
2022-03-10 10:52:50 +01:00
|
|
|
import { IFeatureToggle } from 'interfaces/featureToggle';
|
2022-11-18 12:43:24 +01:00
|
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
2021-09-14 14:20:23 +02:00
|
|
|
|
2022-04-26 09:54:16 +02:00
|
|
|
export interface IUseFeatureOutput {
|
2022-03-09 14:59:24 +01:00
|
|
|
feature: IFeatureToggle;
|
|
|
|
refetchFeature: () => void;
|
|
|
|
loading: boolean;
|
2022-03-10 10:52:50 +01:00
|
|
|
status?: number;
|
2022-03-09 14:59:24 +01:00
|
|
|
error?: Error;
|
|
|
|
}
|
2021-09-14 14:20:23 +02:00
|
|
|
|
2022-04-26 09:54:16 +02:00
|
|
|
export interface IFeatureResponse {
|
2022-03-10 10:52:50 +01:00
|
|
|
status: number;
|
|
|
|
body?: IFeatureToggle;
|
|
|
|
}
|
|
|
|
|
2022-03-09 14:59:24 +01:00
|
|
|
export const useFeature = (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string,
|
|
|
|
options?: SWRConfiguration
|
|
|
|
): IUseFeatureOutput => {
|
2022-04-26 09:54:16 +02:00
|
|
|
const path = formatFeatureApiPath(projectId, featureId);
|
2022-03-09 14:59:24 +01:00
|
|
|
|
2022-11-18 12:43:24 +01:00
|
|
|
const { uiConfig } = useUiConfig();
|
|
|
|
const {
|
|
|
|
flags: { variantsPerEnvironment },
|
|
|
|
} = uiConfig;
|
|
|
|
|
2022-04-26 09:54:16 +02:00
|
|
|
const { data, error, mutate } = useSWR<IFeatureResponse>(
|
|
|
|
['useFeature', path],
|
2022-11-18 12:43:24 +01:00
|
|
|
() => featureFetcher(path, variantsPerEnvironment),
|
2022-03-09 14:59:24 +01:00
|
|
|
options
|
|
|
|
);
|
|
|
|
|
|
|
|
const refetchFeature = useCallback(() => {
|
2022-04-26 09:54:16 +02:00
|
|
|
mutate().catch(console.warn);
|
|
|
|
}, [mutate]);
|
2021-09-14 14:20:23 +02:00
|
|
|
|
2022-11-18 12:43:24 +01:00
|
|
|
useEffect(() => {
|
|
|
|
mutate();
|
|
|
|
}, [mutate, variantsPerEnvironment]);
|
|
|
|
|
2021-09-14 14:20:23 +02:00
|
|
|
return {
|
2022-03-10 10:52:50 +01:00
|
|
|
feature: data?.body || emptyFeature,
|
2022-03-09 14:59:24 +01:00
|
|
|
refetchFeature,
|
|
|
|
loading: !error && !data,
|
2022-03-10 10:52:50 +01:00
|
|
|
status: data?.status,
|
2021-09-14 14:20:23 +02:00
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-26 09:54:16 +02:00
|
|
|
export const featureFetcher = async (
|
2022-11-18 12:43:24 +01:00
|
|
|
path: string,
|
|
|
|
variantsPerEnvironment?: boolean
|
2022-04-26 09:54:16 +02:00
|
|
|
): Promise<IFeatureResponse> => {
|
2022-11-18 12:43:24 +01:00
|
|
|
const variantEnvironments = variantsPerEnvironment
|
|
|
|
? '?variantEnvironments=true'
|
|
|
|
: '';
|
|
|
|
|
|
|
|
const res = await fetch(path + variantEnvironments);
|
2022-03-10 10:52:50 +01:00
|
|
|
|
|
|
|
if (res.status === 404) {
|
|
|
|
return { status: 404 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
await handleErrorResponses('Feature toggle data')(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: res.status,
|
|
|
|
body: await res.json(),
|
|
|
|
};
|
2022-03-09 14:59:24 +01:00
|
|
|
};
|
2022-04-26 09:54:16 +02:00
|
|
|
|
|
|
|
export const formatFeatureApiPath = (
|
|
|
|
projectId: string,
|
|
|
|
featureId: string
|
|
|
|
): string => {
|
|
|
|
return formatApiPath(
|
|
|
|
`api/admin/projects/${projectId}/features/${featureId}`
|
|
|
|
);
|
|
|
|
};
|