1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-08 01:15:49 +02:00
unleash.unleash/frontend/src/hooks/api/getters/useFeature/useFeatureImmutable.ts
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

40 lines
1.2 KiB
TypeScript

import useSWRImmutable from 'swr/immutable';
import { useCallback } from 'react';
import { emptyFeature } from './emptyFeature';
import {
IUseFeatureOutput,
IFeatureResponse,
featureFetcher,
formatFeatureApiPath,
useFeature,
} from 'hooks/api/getters/useFeature/useFeature';
// useFeatureImmutable is like useFeature, except it won't refetch data on
// focus/reconnect/remount. Useful for <form>s that need a stable copy of
// the data. In particular, the lastSeenAt field may often change.
export const useFeatureImmutable = (
projectId: string,
featureId: string,
): IUseFeatureOutput => {
const { refetchFeature } = useFeature(projectId, featureId);
const path = formatFeatureApiPath(projectId, featureId);
const { data, error, mutate } = useSWRImmutable<IFeatureResponse>(
['useFeatureImmutable', path],
() => featureFetcher(path),
);
const refetch = useCallback(async () => {
await mutate();
await refetchFeature();
}, [mutate, refetchFeature]);
return {
feature: data?.body || emptyFeature,
refetchFeature: refetch,
loading: !error && !data,
status: data?.status,
error,
};
};