MCPcopy Create free account
hub / github.com/dailydotdev/apps / useRecordRecentPages

Function useRecordRecentPages

packages/shared/src/hooks/useRecentPages.ts:43–79  ·  view source on GitHub ↗
(enabled: boolean)

Source from the content-addressed store, hash-verified

41// reading History. Reads document.title after a short delay so the page's
42// SEO effect has set it.
43export const useRecordRecentPages = (enabled: boolean): void => {
44 const router = useRouter();
45
46 useEffect(() => {
47 if (!enabled || typeof window === 'undefined' || !router?.events) {
48 return undefined;
49 }
50
51 let timeoutId: number | undefined;
52 const capture = (url: string) => {
53 if (router.pathname === '/posts/[id]') {
54 return;
55 }
56 const path = url.split('?')[0].split('#')[0];
57 // Only the latest navigation matters — drop any pending capture so
58 // rapid navigation doesn't stack timers or record stale titles.
59 window.clearTimeout(timeoutId);
60 timeoutId = window.setTimeout(() => {
61 const title = cleanTitle(document.title);
62 if (title) {
63 recordRecentPage({
64 path,
65 title,
66 type: typeForPathname(router.pathname),
67 });
68 }
69 }, 250);
70 };
71
72 capture(router.asPath);
73 router.events.on('routeChangeComplete', capture);
74 return () => {
75 window.clearTimeout(timeoutId);
76 router.events.off('routeChangeComplete', capture);
77 };
78 }, [enabled, router]);
79};

Callers 1

MainLayoutComponentFunction · 0.90

Calls 2

captureFunction · 0.85
useRouterFunction · 0.50

Tested by

no test coverage detected