PluginProbe
Extendify / 3.0.6
Extendify v3.0.6
3.2.1 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 All 127 releases
extendify / src / Agent / state / global.js

global.js in Extendify 3.0.6, at src/Agent/state/global.js

87 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { usePositionStore } from '@agent/state/position';
2 import { isInTheFuture } from '@wordpress/date';
3 import { create } from 'zustand';
4 import { devtools, persist } from 'zustand/middleware';
5
6 const url = new URL(window.location.href);
7 const openAgentFromUrl = url.searchParams.has('extendify-open-agent');
8 if (openAgentFromUrl) {
9 url.searchParams.delete('extendify-open-agent');
10 window.history.replaceState({}, '', url.toString());
11 }
12 const forceOpen = window.extAgentData?.startOnboarding || openAgentFromUrl;
13
14 export const useGlobalStore = create()(
15 persist(
16 devtools(
17 (set, get) => ({
18 retryAfter: undefined,
19 open: true,
20 minimized: false,
21 // e.g. floating, docked-left, docked-right ?
22 mode: window.extAgentData.agentPosition,
23 queuedTour: null,
24 scratch: {},
25 isMobile: window.innerWidth < 768,
26 setIsMobile: (isMobile) => {
27 if (get().isMobile === isMobile) return;
28 set({ isMobile });
29 },
30 queueTourForRedirect: (tour) => set({ queuedTour: tour }),
31 clearQueuedTour: () => set({ queuedTour: null }),
32 setOpen: (open) => {
33 if (!open) {
34 usePositionStore.getState().resetPosition();
35 window.dispatchEvent(
36 new CustomEvent('extendify-agent:cancel-workflow'),
37 );
38 }
39 set({ open });
40 },
41 setMinimized: (minimized) => {
42 if (get().minimized === minimized) return;
43 set({ minimized });
44 },
45 toggleOpen: () =>
46 set((state) => {
47 if (!state.open) {
48 usePositionStore.getState().resetPosition();
49 }
50 return { open: !state.open };
51 }),
52 updateRetryAfter: (retryAfter) => set({ retryAfter }),
53 isChatAvailable: () => {
54 const { retryAfter } = get();
55 if (!retryAfter) return true;
56 const stillWaiting = isInTheFuture(new Date(Number(retryAfter)));
57 if (!stillWaiting) set({ retryAfter: undefined });
58 return !stillWaiting;
59 },
60 setScratch: (key, value) =>
61 set((state) => ({ scratch: { ...state.scratch, [key]: value } })),
62 getScratch: (key) => get().scratch[key] || null,
63 deleteScratch: (key) =>
64 set((state) => {
65 const { [key]: _, ...rest } = state.scratch;
66 return { scratch: rest };
67 }),
68 }),
69 { name: 'Extendify Agent Global' },
70 ),
71 {
72 name: `extendify-agent-global-${window.extSharedData.siteId}`,
73 merge: (persistedState, currentState) => {
74 const open = forceOpen
75 ? true
76 : (persistedState?.open ?? currentState.open);
77 return { ...currentState, ...persistedState, open };
78 },
79 partialize: (state) => {
80 // mode is determined on the server
81 const { mode, isMobile, ...rest } = state;
82 return { ...rest };
83 },
84 },
85 ),
86 );
87