1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/hooks/useLastViewedFlags.ts
2024-05-29 16:01:52 +02:00

56 lines
1.8 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage';
import { basePath } from 'utils/formatPath';
import { useCustomEvent } from './useCustomEvent';
const MAX_ITEMS = 3;
export type LastViewedFlag = { featureId: string; projectId: string };
const removeIncorrect = (flags?: any[]): LastViewedFlag[] => {
if (!Array.isArray(flags)) return [];
return flags.filter((flag) => flag.featureId && flag.projectId);
};
const localStorageItems = (key: string) => {
return removeIncorrect(getLocalStorageItem(key) || []);
};
export const useLastViewedFlags = () => {
const key = `${basePath}:unleash-lastViewedFlags`;
const [lastViewed, setLastViewed] = useState<LastViewedFlag[]>(() =>
localStorageItems(key),
);
const { emitEvent } = useCustomEvent(
'lastViewedFlagsUpdated',
useCallback(() => {
setLastViewed(localStorageItems(key));
}, [key]),
);
useEffect(() => {
if (lastViewed) {
setLocalStorageItem(key, lastViewed);
emitEvent();
}
}, [JSON.stringify(lastViewed), key, emitEvent]);
const setCappedLastViewed = useCallback(
(flag: { featureId: string; projectId: string }) => {
if (!flag.featureId || !flag.projectId) return;
if (lastViewed.find((item) => item.featureId === flag.featureId))
return;
const updatedLastViewed = removeIncorrect([...lastViewed, flag]);
setLastViewed(
updatedLastViewed.length > MAX_ITEMS
? updatedLastViewed.slice(-MAX_ITEMS)
: updatedLastViewed,
);
},
[JSON.stringify(lastViewed)],
);
return { lastViewed, setLastViewed: setCappedLastViewed };
};