mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-10 17:53:36 +02:00
fix/usefeaturesearch cache (#5340)
Fixes a bug where the closure over the useFeatureSearch hook would not account for projectId and return the wrong total/initial load
This commit is contained in:
parent
142e322589
commit
db77962a72
@ -1,5 +1,5 @@
|
|||||||
import useSWR, { SWRConfiguration } from 'swr';
|
import useSWR, { SWRConfiguration } from 'swr';
|
||||||
import { useCallback } from 'react';
|
import { useCallback, useEffect } from 'react';
|
||||||
import { IFeatureToggleListItem } from 'interfaces/featureToggle';
|
import { IFeatureToggleListItem } from 'interfaces/featureToggle';
|
||||||
import { formatApiPath } from 'utils/formatPath';
|
import { formatApiPath } from 'utils/formatPath';
|
||||||
import handleErrorResponses from '../httpErrorResponseHandler';
|
import handleErrorResponses from '../httpErrorResponseHandler';
|
||||||
@ -19,6 +19,14 @@ interface IUseFeatureSearchOutput {
|
|||||||
refetch: () => void;
|
refetch: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CacheValue = {
|
||||||
|
total: number;
|
||||||
|
initialLoad: boolean;
|
||||||
|
[key: string]: number | boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type InternalCache = Record<string, CacheValue>;
|
||||||
|
|
||||||
const fallbackData: {
|
const fallbackData: {
|
||||||
features: IFeatureToggleListItem[];
|
features: IFeatureToggleListItem[];
|
||||||
total: number;
|
total: number;
|
||||||
@ -28,8 +36,28 @@ const fallbackData: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createFeatureSearch = () => {
|
const createFeatureSearch = () => {
|
||||||
let total = 0;
|
const internalCache: InternalCache = {};
|
||||||
let initialLoad = true;
|
|
||||||
|
const initCache = (projectId: string) => {
|
||||||
|
internalCache[projectId] = {
|
||||||
|
total: 0,
|
||||||
|
initialLoad: true,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const set = (projectId: string, key: string, value: number | boolean) => {
|
||||||
|
if (!internalCache[projectId]) {
|
||||||
|
initCache(projectId);
|
||||||
|
}
|
||||||
|
internalCache[projectId][key] = value;
|
||||||
|
};
|
||||||
|
|
||||||
|
const get = (projectId: string) => {
|
||||||
|
if (!internalCache[projectId]) {
|
||||||
|
initCache(projectId);
|
||||||
|
}
|
||||||
|
return internalCache[projectId];
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
offset: number,
|
offset: number,
|
||||||
@ -44,6 +72,11 @@ const createFeatureSearch = () => {
|
|||||||
limit,
|
limit,
|
||||||
searchValue,
|
searchValue,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
initCache(projectId);
|
||||||
|
}, []);
|
||||||
|
|
||||||
const { data, error, mutate, isLoading } =
|
const { data, error, mutate, isLoading } =
|
||||||
useSWR<IFeatureSearchResponse>(KEY, fetcher, options);
|
useSWR<IFeatureSearchResponse>(KEY, fetcher, options);
|
||||||
|
|
||||||
@ -51,12 +84,14 @@ const createFeatureSearch = () => {
|
|||||||
mutate();
|
mutate();
|
||||||
}, [mutate]);
|
}, [mutate]);
|
||||||
|
|
||||||
|
const cacheValues = get(projectId);
|
||||||
|
|
||||||
if (data?.total) {
|
if (data?.total) {
|
||||||
total = data.total;
|
set(projectId, 'total', data.total);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isLoading && initialLoad) {
|
if (!isLoading && cacheValues.initialLoad) {
|
||||||
initialLoad = false;
|
set(projectId, 'initialLoad', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const returnData = data || fallbackData;
|
const returnData = data || fallbackData;
|
||||||
@ -65,8 +100,8 @@ const createFeatureSearch = () => {
|
|||||||
loading: isLoading,
|
loading: isLoading,
|
||||||
error,
|
error,
|
||||||
refetch,
|
refetch,
|
||||||
total,
|
total: cacheValues.total,
|
||||||
initialLoad: isLoading && initialLoad,
|
initialLoad: isLoading && cacheValues.initialLoad,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user