PluginProbe
Extendify / 2.2.0
Extendify v2.2.0
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 2.2.0, at src/Draft/hooks/useRouter.js

102 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { useCallback, useEffect } from '@wordpress/element';
3 import { useActivityStore } from '@shared/state/activity';
4 import { create } from 'zustand';
5 import { devtools, persist, createJSONStorage } from 'zustand/middleware';
6 import { routes as aiRoutes } from '@draft/pages/GenerateImage';
7 import { routes as generalRoutes } from '@draft/pages/Home';
8 import { routes as unsplashRoutes } from '@draft/pages/Unsplash';
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 useRouter = () => {
73 const { current, setCurrent, history, goBack } = useRouterState();
74 const Component = current?.component ?? (() => null);
75 useEffect(() => {
76 if (current) return;
77 setCurrent(pages[0]);
78 }, [current, setCurrent]);
79 return {
80 current,
81 CurrentPage: useCallback(
82 () => (
83 <div role="region" aria-live="polite" className="h-full">
84 {/* Announce to SR on change */}
85 <h1 className="sr-only">{current?.title}</h1>
86 <Component />
87 </div>
88 ),
89 [current],
90 ),
91 navigateTo: (slug) => {
92 const page = pages.find((a) => a.slug === slug);
93 if (!page) return setCurrent(pages[0]);
94
95 useActivityStore.getState().incrementActivity(`draft-${page.slug}`);
96 setCurrent(page);
97 },
98 goBack,
99 history,
100 };
101 };
102