-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppContext.tsx
More file actions
79 lines (75 loc) · 2.11 KB
/
Copy pathAppContext.tsx
File metadata and controls
79 lines (75 loc) · 2.11 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
'use client'
import { ProductMappingItemType } from '@/db/schema/qbProductSync'
import { Token, WorkspaceResponse } from '@/type/common'
import { IntuitAPITokensType } from '@/utils/intuitAPI'
import { createContext, useContext, useState, ReactNode } from 'react'
type AppContextType = {
token: string
tokenPayload: Token
syncFlag: boolean
reconnect: boolean
portalConnectionStatus: boolean | null
isEnabled: boolean
lastSyncTimestamp: string | null
showProductConfirm?: boolean
initialProductMap?: ProductMappingItemType[] // initial product mapped value
enableAppIndicator?: boolean // flag to indicate whether to enable the "Enable App" button
initialInvoiceSettingMapFlag?: boolean // flag to determine the initial invoice setting flag
initialProductSettingMapFlag?: boolean // flag to determine the initial product setting flag
workspace: WorkspaceResponse
nonUsCompany?: boolean
qbTokens?: IntuitAPITokensType
}
const AppContext = createContext<
| (AppContextType & {
setAppParams: React.Dispatch<React.SetStateAction<AppContextType>>
})
| null
>(null)
export const AppProvider = ({
token,
tokenPayload,
syncFlag,
reconnect,
portalConnectionStatus,
isEnabled,
lastSyncTimestamp,
showProductConfirm = false,
initialProductMap = [],
enableAppIndicator = false,
initialInvoiceSettingMapFlag = false,
initialProductSettingMapFlag = false,
workspace,
children,
}: AppContextType & { children: ReactNode }) => {
const [appParams, setAppParams] = useState<AppContextType>({
token,
tokenPayload,
syncFlag,
reconnect,
portalConnectionStatus,
lastSyncTimestamp,
isEnabled,
showProductConfirm,
initialProductMap,
enableAppIndicator,
initialInvoiceSettingMapFlag,
initialProductSettingMapFlag,
workspace,
})
return (
<AppContext.Provider
value={{
...appParams,
setAppParams,
}}
>
{children}
</AppContext.Provider>
)
}
export const useApp = () => {
const context = useContext(AppContext)
if (!context) throw new Error('useApp must be used within AppProvider')
return context
}