PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Draft / hooks / useRouter.js

useRouter.js in Extendify 3.1.5, at src/Draft/hooks/useRouter.js

104 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { routes as aiRoutes } from '@draft/pages/GenerateImage';
2 import { routes as generalRoutes } from '@draft/pages/Home';
3 import { routes as unsplashRoutes } from '@draft/pages/Unsplash';
4 import { useActivityStore } from '@shared/state/activity';
5 import apiFetch from '@wordpress/api-fetch';
6 import { useCallback, useEffect } from '@wordpress/element';
7 import { create } from 'zustand';
8 import { createJSONStorage, devtools, persist } from 'zustand/middleware';
9
10 const pages = [...generalRoutes, ...aiRoutes, ...unsplashRoutes];
11
12 const state = (set, get) => ({
13 history: [],
14 viewedPages: [],
15 current: null,
16 goBack: () => {
17 if (get().history.length < 2) return;
18 const nextPage = get().history[1];
19 useActivityStore
20 .getState()
21 .incrementActivity(`draft-${nextPage.slug}-back`);
22 set((state) => ({
23 history: state.history.slice(1),
24 current: nextPage,
25 }));
26 },
27 setCurrent: (page) => {
28 if (!page) return;
29 // If history is the same, dont add (they pressed the same button)
30 if (get().history[0]?.slug === page.slug) return;
31 set((state) => {
32 const lastViewedAt = new Date().toISOString();
33 const firstViewedAt = lastViewedAt;
34 const visited = state.viewedPages.find((a) => a.slug === page.slug);
35 return {
36 history: [page, ...state.history].filter(Boolean),
37 current: page,
38 viewedPages: [
39 // Remove the page if it's already in the list
40 ...state.viewedPages.filter((a) => a.slug !== page.slug),
41 // Either add the page or update the count
42 visited
43 ? { ...visited, count: visited.count + 1, lastViewedAt }
44 : {
45 slug: page.slug,
46 firstViewedAt,
47 lastViewedAt,
48 count: 1,
49 },
50 ],
51 };
52 });
53 },
54 });
55
56 const path = '/extendify/v1/draft/router-data';
57 const storage = {
58 getItem: async () => await apiFetch({ path }),
59 setItem: async (_name, state) =>
60 await apiFetch({ path, method: 'POST', data: { state } }),
61 };
62
63 const useRouterState = create(
64 persist(devtools(state, { name: 'Extendify Draft Router' }), {
65 name: 'extendify-draft-router',
66 storage: createJSONStorage(() => storage),
67 skipHydration: true,
68 partialize: ({ viewedPages }) => ({ viewedPages }),
69 }),
70 );
71
72 export const navigateTo = (slug) => {
73 const page = pages.find((a) => a.slug === slug);
74 if (!page) return useRouterState.getState().setCurrent(pages[0]);
75
76 useActivityStore.getState().incrementActivity(`draft-${page.slug}`);
77 useRouterState.getState().setCurrent(page);
78 };
79
80 export const useRouter = () => {
81 const { current, setCurrent, history, goBack } = useRouterState();
82 const Component = current?.component ?? (() => null);
83 useEffect(() => {
84 if (current) return;
85 setCurrent(pages[0]);
86 }, [current, setCurrent]);
87 return {
88 current,
89 CurrentPage: useCallback(
90 () => (
91 <section aria-live="polite" className="h-full">
92 {/* Announce to SR on change */}
93 <h1 className="sr-only">{current?.title}</h1>
94 <Component />
95 </section>
96 ),
97 [current],
98 ),
99 navigateTo,
100 goBack,
101 history,
102 };
103 };
104