-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuiStore.tsx
More file actions
115 lines (99 loc) · 2.91 KB
/
Copy pathuiStore.tsx
File metadata and controls
115 lines (99 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, {
createContext,
useContext,
useRef,
useCallback,
useState,
useEffect,
useMemo
} from 'react'
import type { ReactNode } from 'react'
import { DialogProvider } from '@/components'
import { toolActions } from '~/stores/toolStore'
type UIState = {
contentComplete: boolean
appearanceComplete: boolean
activeSection: 'content' | 'appearance' | null
buildStepComplete: boolean
}
interface WalletInputRef {
focus: () => void
}
interface UIActions {
focusWalletInput: () => void
registerWalletInput: (ref: WalletInputRef) => () => void
setContentComplete: (complete: boolean) => void
setAppearanceComplete: (complete: boolean) => void
setActiveSection: (section: UIState['activeSection']) => void
}
const UIStateContext = createContext<UIState | undefined>(undefined)
const UIActionsContext = createContext<UIActions | undefined>(undefined)
interface UIProviderProps {
children: ReactNode
}
export const UIProvider: React.FC<UIProviderProps> = ({ children }) => {
const walletInputRef = useRef<WalletInputRef | null>(null)
const [shouldFocusWallet, setShouldFocusWallet] = useState(false)
const [contentComplete, setContentComplete] = useState(false)
const [appearanceComplete, setAppearanceComplete] = useState(false)
const [activeSection, setActiveSection] =
useState<UIState['activeSection']>(null)
const buildStepComplete = contentComplete && appearanceComplete
useEffect(() => {
if (shouldFocusWallet && walletInputRef.current) {
walletInputRef.current.focus()
setShouldFocusWallet(false)
}
}, [shouldFocusWallet])
useEffect(() => {
toolActions.setBuildCompleteStep(buildStepComplete ? 'filled' : 'unfilled')
}, [buildStepComplete])
const focusWalletInput = useCallback(() => {
setShouldFocusWallet(true)
}, [])
const registerWalletInput = useCallback((ref: WalletInputRef) => {
walletInputRef.current = ref
return () => {
walletInputRef.current = null
}
}, [])
const state: UIState = {
contentComplete,
activeSection,
appearanceComplete,
buildStepComplete
}
const actions: UIActions = useMemo(
() => ({
focusWalletInput,
registerWalletInput,
setContentComplete,
setAppearanceComplete,
setActiveSection
}),
[
focusWalletInput,
registerWalletInput,
setContentComplete,
setAppearanceComplete,
setActiveSection
]
)
return (
<UIStateContext.Provider value={state}>
<UIActionsContext.Provider value={actions}>
<DialogProvider>{children}</DialogProvider>
</UIActionsContext.Provider>
</UIStateContext.Provider>
)
}
export const useUIActions = (): UIActions => {
const context = useContext(UIActionsContext)
if (!context) {
throw new Error('useUIActions must be used within a UIProvider')
}
return context
}
export const useUIState = (): UIState | undefined => {
return useContext(UIStateContext)
}