mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-05 17:53:12 +02:00
chore: clean up useRecentlyVisited hook
This commit is contained in:
parent
dd75b17116
commit
19ab7f757c
@ -14,18 +14,8 @@ export type LastViewedPage = {
|
|||||||
pathName?: string;
|
pathName?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeIncorrect = (flags?: any[]): LastViewedPage[] => {
|
const localStorageItems = (key: string): LastViewedPage[] => {
|
||||||
if (!Array.isArray(flags)) return [];
|
return getLocalStorageItem(key) || [];
|
||||||
return flags.filter(
|
|
||||||
(flag) =>
|
|
||||||
(flag.featureId && flag.projectId) ||
|
|
||||||
flag.projectId ||
|
|
||||||
flag.pathName,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const localStorageItems = (key: string) => {
|
|
||||||
return removeIncorrect(getLocalStorageItem(key) || []);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useRecentlyVisited = () => {
|
export const useRecentlyVisited = () => {
|
||||||
@ -51,20 +41,16 @@ export const useRecentlyVisited = () => {
|
|||||||
|
|
||||||
const path = routes.find((r) => r.path === location.pathname);
|
const path = routes.find((r) => r.path === location.pathname);
|
||||||
if (path) {
|
if (path) {
|
||||||
setCappedLastVisited([{ pathName: path.path }]);
|
setCappedLastVisited({ pathName: path.path });
|
||||||
} else {
|
} else if (featureMatch?.params.featureId) {
|
||||||
if (featureMatch?.params.featureId) {
|
setCappedLastVisited({
|
||||||
setCappedLastVisited([
|
featureId: featureMatch?.params.featureId,
|
||||||
{
|
projectId: featureMatch?.params.projectId,
|
||||||
featureId: featureMatch?.params.featureId,
|
});
|
||||||
projectId: featureMatch?.params.projectId,
|
} else if (projectMatch?.params.projectId) {
|
||||||
},
|
setCappedLastVisited({
|
||||||
]);
|
projectId: projectMatch?.params.projectId,
|
||||||
} else if (projectMatch?.params.projectId) {
|
});
|
||||||
setCappedLastVisited([
|
|
||||||
{ projectId: projectMatch?.params.projectId },
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [location, featureMatch, projectMatch]);
|
}, [location, featureMatch, projectMatch]);
|
||||||
|
|
||||||
@ -76,43 +62,36 @@ export const useRecentlyVisited = () => {
|
|||||||
}, [JSON.stringify(lastVisited), key, emitEvent]);
|
}, [JSON.stringify(lastVisited), key, emitEvent]);
|
||||||
|
|
||||||
const setCappedLastVisited = useCallback(
|
const setCappedLastVisited = useCallback(
|
||||||
(pages: LastViewedPage[]) => {
|
(page: LastViewedPage) => {
|
||||||
const filtered = pages.filter((page) => {
|
if (page.featureId && !page.projectId) return;
|
||||||
if (page.featureId && !page.projectId) return false;
|
if (
|
||||||
if (
|
lastVisited.find(
|
||||||
lastVisited.find(
|
(item) =>
|
||||||
(item) =>
|
item.featureId && item.featureId === page.featureId,
|
||||||
item.featureId && item.featureId === page.featureId,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return false;
|
)
|
||||||
if (
|
return;
|
||||||
lastVisited.find(
|
if (
|
||||||
(item) =>
|
lastVisited.find(
|
||||||
item.pathName && item.pathName === page.pathName,
|
(item) => item.pathName && item.pathName === page.pathName,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return false;
|
)
|
||||||
if (
|
return;
|
||||||
lastVisited.find(
|
if (
|
||||||
(item) =>
|
lastVisited.find(
|
||||||
item.projectId &&
|
(item) =>
|
||||||
!item.featureId &&
|
item.projectId &&
|
||||||
!page.featureId &&
|
!item.featureId &&
|
||||||
item.projectId === page.projectId,
|
!page.featureId &&
|
||||||
)
|
item.projectId === page.projectId,
|
||||||
)
|
)
|
||||||
return false;
|
)
|
||||||
return true;
|
return;
|
||||||
});
|
|
||||||
|
|
||||||
const updatedLastVisited = removeIncorrect([
|
const updatedLastVisited = [page, ...lastVisited];
|
||||||
...lastVisited,
|
|
||||||
...filtered,
|
|
||||||
]);
|
|
||||||
const sliced =
|
const sliced =
|
||||||
updatedLastVisited.length > MAX_ITEMS
|
updatedLastVisited.length > MAX_ITEMS
|
||||||
? updatedLastVisited.slice(-MAX_ITEMS)
|
? updatedLastVisited.slice(0, MAX_ITEMS)
|
||||||
: updatedLastVisited;
|
: updatedLastVisited;
|
||||||
setLastVisited(sliced);
|
setLastVisited(sliced);
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user