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 (
<ConditionallyRender
condition={
!new Set(
Array.isArray(globalStore.hiddenEnvironments)
? globalStore.hiddenEnvironments
: [],
).has(env.name)
}
condition={!new Set(globalStore.hiddenEnvironments).has(env.name)}
show={
<StyledFeatureOverviewEnvironment enabled={env.enabled}>
<StyledAccordion

View File

@ -2,7 +2,7 @@ import { createLocalStorage } from 'utils/createLocalStorage';
interface IGlobalStore {
favorites?: boolean;
hiddenEnvironments?: Set<string>;
hiddenEnvironments?: Array<string>;
}
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 {
value,
value: parsedValue,
setValue,
};
};

View File

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

View File

@ -117,4 +117,12 @@ describe('localStorage with TTL', () => {
);
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
: 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) {
console.warn(err);
}
@ -59,7 +64,12 @@ export function setSessionStorageItem<T>(
? new Date().getTime() + timeToLive
: 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) {
console.warn(err);
}