1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

refactor: cleanup fix for persistent set (#6060)

This commit is contained in:
Mateusz Kwasniewski 2024-01-29 12:33:01 +01:00 committed by GitHub
parent c1046079dd
commit 8a7e65eaa6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 22 deletions

View File

@ -134,13 +134,7 @@ const FeatureOverviewEnvironment = ({
return ( return (
<ConditionallyRender <ConditionallyRender
condition={ condition={!new Set(globalStore.hiddenEnvironments).has(env.name)}
!new Set(
Array.isArray(globalStore.hiddenEnvironments)
? globalStore.hiddenEnvironments
: [],
).has(env.name)
}
show={ show={
<StyledFeatureOverviewEnvironment enabled={env.enabled}> <StyledFeatureOverviewEnvironment enabled={env.enabled}>
<StyledAccordion <StyledAccordion

View File

@ -2,7 +2,7 @@ import { createLocalStorage } from 'utils/createLocalStorage';
interface IGlobalStore { interface IGlobalStore {
favorites?: boolean; favorites?: boolean;
hiddenEnvironments?: Set<string>; hiddenEnvironments?: Array<string>;
} }
export const useGlobalLocalStorage = () => { export const useGlobalLocalStorage = () => {
@ -11,8 +11,14 @@ export const useGlobalLocalStorage = () => {
{}, {},
); );
// fix incorrect values introduced by a bug
const parsedValue = {
...value,
hiddenEnvironments: Array.from(value.hiddenEnvironments || []),
};
return { return {
value, value: parsedValue,
setValue, setValue,
}; };
}; };

View File

@ -9,21 +9,12 @@ export const useHiddenEnvironments = () => {
useGlobalLocalStorage(); useGlobalLocalStorage();
const [hiddenEnvironments, setStoredHiddenEnvironments] = useState< const [hiddenEnvironments, setStoredHiddenEnvironments] = useState<
Set<string> Set<string>
>( >(new Set(globalStore.hiddenEnvironments));
new Set(
Array.isArray(globalStore.hiddenEnvironments)
? globalStore.hiddenEnvironments
: [],
),
);
const setHiddenEnvironments = (environment: string) => { const setHiddenEnvironments = (environment: string) => {
// @ts-expect-error
setGlobalStore((params) => { setGlobalStore((params) => {
const hiddenEnvironments = new Set( const hiddenEnvironments = new Set(
Array.isArray(globalStore.hiddenEnvironments) Array.from(params.hiddenEnvironments || []),
? globalStore.hiddenEnvironments
: [],
); );
if (hiddenEnvironments.has(environment)) { if (hiddenEnvironments.has(environment)) {
hiddenEnvironments.delete(environment); hiddenEnvironments.delete(environment);

View File

@ -117,4 +117,12 @@ describe('localStorage with TTL', () => {
); );
expect(retrievedObject).toBeUndefined(); expect(retrievedObject).toBeUndefined();
}); });
test('should handle set with any level of nesting', () => {
setLocalStorageItem(
'testKey',
new Set([{ nestedSet: new Set([1, 2]) }]),
);
expect(getLocalStorageItem('testKey')).toEqual([{ nestedSet: [1, 2] }]);
});
}); });

View File

@ -39,7 +39,12 @@ export function setLocalStorageItem<T>(
? new Date().getTime() + timeToLive ? new Date().getTime() + timeToLive
: null, : null,
}; };
window.localStorage.setItem(key, JSON.stringify(item)); window.localStorage.setItem(
key,
JSON.stringify(item, (_key, value) =>
value instanceof Set ? [...value] : value,
),
);
} catch (err: unknown) { } catch (err: unknown) {
console.warn(err); console.warn(err);
} }
@ -59,7 +64,12 @@ export function setSessionStorageItem<T>(
? new Date().getTime() + timeToLive ? new Date().getTime() + timeToLive
: null, : null,
}; };
window.sessionStorage.setItem(key, JSON.stringify(item)); window.sessionStorage.setItem(
key,
JSON.stringify(item, (_key, value) =>
value instanceof Set ? [...value] : value,
),
);
} catch (err: unknown) { } catch (err: unknown) {
console.warn(err); console.warn(err);
} }