| 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 |
// Mirrors Tailwind's --breakpoint-md, where WP's admin bar goes big. |
| 7 |
export const DESKTOP_MIN_WIDTH = 783; |
| 8 |
|
| 9 |
const url = new URL(window.location.href); |
| 10 |
const openAgentFromUrl = url.searchParams.has('extendify-open-agent'); |
| 11 |
if (openAgentFromUrl) { |
| 12 |
url.searchParams.delete('extendify-open-agent'); |
| 13 |
window.history.replaceState({}, '', url.toString()); |
| 14 |
} |
| 15 |
const forceOpen = window.extAgentData?.startOnboarding || openAgentFromUrl; |
| 16 |
|
| 17 |
export const useGlobalStore = create()( |
| 18 |
persist( |
| 19 |
devtools( |
| 20 |
(set, get) => ({ |
| 21 |
retryAfter: undefined, |
| 22 |
open: true, |
| 23 |
minimized: false, |
| 24 |
// e.g. floating, docked-left, docked-right ? |
| 25 |
mode: window.extAgentData?.agentPosition, |
| 26 |
queuedTour: null, |
| 27 |
scratch: {}, |
| 28 |
isMobile: window.innerWidth < DESKTOP_MIN_WIDTH, |
| 29 |
setIsMobile: (isMobile) => { |
| 30 |
if (get().isMobile === isMobile) return; |
| 31 |
set({ isMobile }); |
| 32 |
}, |
| 33 |
queueTourForRedirect: (tour) => set({ queuedTour: tour }), |
| 34 |
clearQueuedTour: () => set({ queuedTour: null }), |
| 35 |
setOpen: (open) => { |
| 36 |
if (!open) { |
| 37 |
usePositionStore.getState().resetPosition(); |
| 38 |
window.dispatchEvent( |
| 39 |
new CustomEvent('extendify-agent:cancel-workflow'), |
| 40 |
); |
| 41 |
} |
| 42 |
set({ open }); |
| 43 |
}, |
| 44 |
setMinimized: (minimized) => { |
| 45 |
if (get().minimized === minimized) return; |
| 46 |
set({ minimized }); |
| 47 |
}, |
| 48 |
toggleOpen: () => |
| 49 |
set((state) => { |
| 50 |
if (!state.open) { |
| 51 |
usePositionStore.getState().resetPosition(); |
| 52 |
} |
| 53 |
return { open: !state.open }; |
| 54 |
}), |
| 55 |
updateRetryAfter: (retryAfter) => set({ retryAfter }), |
| 56 |
isChatAvailable: () => { |
| 57 |
const { retryAfter } = get(); |
| 58 |
if (!retryAfter) return true; |
| 59 |
const stillWaiting = isInTheFuture(new Date(Number(retryAfter))); |
| 60 |
if (!stillWaiting) set({ retryAfter: undefined }); |
| 61 |
return !stillWaiting; |
| 62 |
}, |
| 63 |
setScratch: (key, value) => |
| 64 |
set((state) => ({ scratch: { ...state.scratch, [key]: value } })), |
| 65 |
getScratch: (key) => get().scratch[key] || null, |
| 66 |
deleteScratch: (key) => |
| 67 |
set((state) => { |
| 68 |
const { [key]: _, ...rest } = state.scratch; |
| 69 |
return { scratch: rest }; |
| 70 |
}), |
| 71 |
}), |
| 72 |
{ name: 'Extendify Agent Global' }, |
| 73 |
), |
| 74 |
{ |
| 75 |
name: `extendify-agent-global-${window.extSharedData.siteId}`, |
| 76 |
merge: (persistedState, currentState) => { |
| 77 |
const open = forceOpen |
| 78 |
? true |
| 79 |
: (persistedState?.open ?? currentState.open); |
| 80 |
return { ...currentState, ...persistedState, open }; |
| 81 |
}, |
| 82 |
partialize: (state) => { |
| 83 |
// mode is determined on the server |
| 84 |
const { mode, isMobile, ...rest } = state; |
| 85 |
return { ...rest }; |
| 86 |
}, |
| 87 |
}, |
| 88 |
), |
| 89 |
); |
| 90 |
|