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

29 lines
821 B
TypeScript

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