SILENT KILLERPanel

Current Path: > home > codekrsu > > ameliagraphics.com > wp-content > plugins > extendify > src > HelpCenter > hooks >


Operation   : Linux premium131.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
Software     : Apache
Server IP    : 162.0.232.56 | Your IP: 216.73.216.111
Domains      : 1034 Domain(s)
Permission   : [ 0755 ]

Files and Folders in: /home/codekrsu//ameliagraphics.com/wp-content/plugins/extendify/src/HelpCenter/hooks/

NameTypeSizeLast ModifiedActions
useRouter.js File 3476 bytes March 13 2025 21:10:44.
useSearchArticles.js File 623 bytes December 19 2024 22:43:36.
useSupportArticles.js File 604 bytes December 19 2024 22:43:36.

Reading File: /home/codekrsu//ameliagraphics.com/wp-content/plugins/extendify/src/HelpCenter/hooks//useRouter.js

import apiFetch from '@wordpress/api-fetch';
import { useCallback, useEffect } from '@wordpress/element';
import { safeParseJson } from '@shared/lib/parsing';
import { useActivityStore } from '@shared/state/activity';
import { create } from 'zustand';
import { devtools, persist, createJSONStorage } from 'zustand/middleware';
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';

const pages = [...dashRoutes, ...kbRoutes, ...tourRoutes, ...aiRoutes];

const initialState = {
	history: [],
	viewedPages: [],
	current: null,
};

const state = (set, get) => ({
	...initialState,
	// initialize the state with default values
	...(safeParseJson(window.extHelpCenterData.userData.routerData)?.state ?? {}),
	goBack: () => {
		if (get().history.length < 2) return;
		const nextPage = get().history[1];
		useActivityStore.getState().incrementActivity(`hc-${nextPage.slug}-back`);
		set((state) => ({
			history: state.history.slice(1),
			current: nextPage,
		}));
	},
	setCurrent: (page) => {
		if (!page) return;
		// If history is the same, dont add (they pressed the same button)
		if (get().history[0]?.slug === page.slug) return;
		const state = get();
		const lastViewedAt = new Date().toISOString();
		const firstViewedAt = lastViewedAt;
		const visited = state.viewedPages.find((a) => a.slug === page.slug);
		const viewedPages = [
			// 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
				? { ...visited, count: Number(visited.count) + 1, lastViewedAt }
				: {
						slug: page.slug,
						firstViewedAt,
						lastViewedAt,
						count: 1,
					},
		];
		// Persist the detailed history to the server (don't wait for response)
		apiFetch({
			path: '/extendify/v1/help-center/router-data',
			method: 'POST',
			data: { state: { viewedPages } },
		});

		set({
			history: [page, ...state.history].filter(Boolean),
			current: page,
			viewedPages,
		});
	},
	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();
	const Component =
		current?.component ??
		pages.find((a) => a.slug === current?.slug)?.component ??
		(() => null);

	useEffect(() => {
		if (current) return;
		setCurrent(pages[0]);
	}, [current, setCurrent]);
	return {
		current,
		CurrentPage: useCallback(
			() => (
				<div role="region" aria-live="polite" className="h-full">
					{/* Announce to SR on change */}
					<h1 className="sr-only">{current?.title}</h1>
					<Component />
				</div>
			),
			[current],
		),
		navigateTo: (slug) => {
			const page = pages.find((a) => a.slug === slug);
			if (!page) return setCurrent(pages[0]);

			useActivityStore.getState().incrementActivity(`hc-${page.slug}`);
			setCurrent(page);
		},
		goBack,
		history,
		reset,
	};
};

SILENT KILLER Tool