1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00
unleash.unleash/frontend/src/hooks/useLastViewedProject.ts

29 lines
821 B
TypeScript
Raw Normal View History

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`;
2024-05-28 15:47:26 +02:00
const [lastViewed, setLastViewed] = useState<string | undefined>(() => {
return getLocalStorageItem(key);
});
const { emitEvent } = useCustomEvent('lastViewedProjectUpdated', () => {
setLastViewed(getLocalStorageItem(key));
});
useEffect(() => {
if (lastViewed) {
setLocalStorageItem(key, lastViewed);
emitEvent();
}
2024-05-29 16:01:52 +02:00
}, [lastViewed, key, emitEvent]);
return {
lastViewed,
setLastViewed,
};
};