mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
29 lines
821 B
TypeScript
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,
|
|
};
|
|
};
|