1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00
unleash.unleash/frontend/src/hooks/useLastViewedProject.ts
Fredrik Strand Oseberg 1f6db91fde
Fix/redirect woes (#2899)
This PR fixes two problems: 

(1) The initial redirect put us into an infinite loop when redirecting,
because trying to go back to the root would always trigger the initial
redirect component. Throwing you back to project screen.

(2) Using UI config in the useLastViewedProject to get the basePath
introduced a race condition where you needed data from the uiConfig in
order to fetch the correct key from local storage. The fix here was to
use the basePath coded into the HTML file, so we can synchronously
retrieve the correct key at startup.

Co-authored-by: kwasniew <kwasniewski.mateusz@gmail.com>
2023-01-17 13:33:52 +01:00

23 lines
579 B
TypeScript

import { useEffect, useState } from 'react';
import { getLocalStorageItem, setLocalStorageItem } from '../utils/storage';
import { basePath } from 'utils/formatPath';
export const useLastViewedProject = () => {
const key = `${basePath}:unleash-lastViewedProject`;
const [lastViewed, setLastViewed] = useState(() => {
return getLocalStorageItem(key);
});
useEffect(() => {
if (lastViewed) {
setLocalStorageItem(key, lastViewed);
}
}, [lastViewed, key]);
return {
lastViewed,
setLastViewed,
};
};