import { routes as aiRoutes } from '@help-center/pages/AIChat';
import { routes as dashRoutes } from '@help-center/pages/Dashboard';
import { routes as kbRoutes } from '@help-center/pages/KnowledgeBase';
import { routes as tourRoutes } from '@help-center/pages/Tours';
import { safeParseJson } from '@shared/lib/parsing';
import { useActivityStore } from '@shared/state/activity';
import apiFetch from '@wordpress/api-fetch';
import { useCallback, useEffect } from '@wordpress/element';
import { create } from 'zustand';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
const pages = [...dashRoutes, ...kbRoutes, ...tourRoutes, ...aiRoutes];
const state = (set, get) => ({
// initialize the state with default values
...(safeParseJson(window.extHelpCenterData.userData.routerData)?.state ?? {}),
if (get().history.length < 2) return;
const nextPage = get().history[1];
useActivityStore.getState().incrementActivity(`hc-${nextPage.slug}-back`);
history: state.history.slice(1),
// If history is the same, dont add (they pressed the same button)
if (get().history[0]?.slug === page.slug) return;
const lastViewedAt = new Date().toISOString();
const firstViewedAt = lastViewedAt;
const visited = state.viewedPages.find((a) => a.slug === page.slug);
// Remove the page if it's already in the list
...state.viewedPages.filter((a) => a.slug !== page.slug),
// Either add the page or update the count
? { ...visited, count: Number(visited.count) + 1, lastViewedAt }
// Persist the detailed history to the server (don't wait for response)
path: '/extendify/v1/help-center/router-data',
data: { state: { viewedPages } },
history: [page, ...state.history].filter(Boolean),
reset: () => set({ ...initialState }),
const useRouterState = create(
persist(devtools(state, { name: 'Extendify Help Center Router' }), {
name: `extendify-help-center-router-${window.extSharedData.siteId}`,
storage: createJSONStorage(() => sessionStorage),
partialize: ({ history, current }) => {
// remove the component from the current page
return { history, current: { ...current, component: null } };
export const useRouter = () => {
const { current, setCurrent, history, goBack, reset } = useRouterState();
pages.find((a) => a.slug === current?.slug)?.component ??
}, [current, setCurrent]);
CurrentPage: useCallback(
<section aria-live="polite" className="h-full">
{/* Announce to SR on change */}
<h1 className="sr-only">{current?.title}</h1>
const page = pages.find((a) => a.slug === slug);
if (!page) return setCurrent(pages[0]);
useActivityStore.getState().incrementActivity(`hc-${page.slug}`);