-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDappLayout.tsx
229 lines (211 loc) · 7 KB
/
DappLayout.tsx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import uniqBy from 'lodash.uniqby'
import { useRouter } from 'next/router'
import { ReactNode, useCallback, useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import { useTranslation } from 'react-i18next'
import { useRecoilState, useRecoilValue, waitForAll } from 'recoil'
import {
betaWarningAcceptedAtom,
commandModalVisibleAtom,
followingDaoDropdownInfosSelector,
mountedInBrowserAtom,
navigationCompactAtom,
proposalCreatedCardPropsAtom,
walletChainIdAtom,
} from '@dao-dao/state'
import {
BetaWarningModal,
ChainProvider,
ProposalCreatedModal,
DappLayout as StatelessDappLayout,
useAppContext,
useCachedLoading,
usePlatform,
} from '@dao-dao/stateless'
import { getSupportedChains, maybeGetChainForChainId } from '@dao-dao/utils'
import { CommandModal } from '../command'
import { useAutoRefreshData, useProfile, useWallet } from '../hooks'
import { daoCreatedCardPropsAtom } from '../recoil'
import { ButtonLink } from './ButtonLink'
import { DaoCreatedModal } from './DaoCreatedModal'
import { LinkWrapper } from './LinkWrapper'
import { DockWallet } from './NavWallet'
import { StatefulPageHeader } from './PageHeader'
import { WalletModals } from './wallet'
export const DappLayout = ({ children }: { children: ReactNode }) => {
const { t } = useTranslation()
const router = useRouter()
const mountedInBrowser = useRecoilValue(mountedInBrowserAtom)
const walletChainId = useRecoilValue(walletChainIdAtom)
// Prevent hydration mismatch error by loading chain ID from local storage
// after mounting.
let chainId = mountedInBrowser
? walletChainId
: getSupportedChains()[0].chainId
// Fallback to default if chain ID invalid.
if (!maybeGetChainForChainId(chainId)) {
chainId = getSupportedChains()[0].chainId
}
const [betaWarningAccepted, setBetaWarningAccepted] = useRecoilState(
betaWarningAcceptedAtom
)
const [commandModalVisible, setCommandModalVisible] = useRecoilState(
commandModalVisibleAtom
)
const [compact, setCompact] = useRecoilState(navigationCompactAtom)
// DAO creation modal that persists when navigating from create page to DAO
// page.
const [daoCreatedCardProps, setDaoCreatedCardProps] = useRecoilState(
daoCreatedCardPropsAtom
)
const [proposalCreatedCardProps, setProposalCreatedCardProps] =
useRecoilState(proposalCreatedCardPropsAtom)
const { rootCommandContextMaker, inbox } = useAppContext()
// Type-check, should always be loaded for dapp.
if (!inbox) {
throw new Error(t('error.loadingData'))
}
const { openView, isWalletConnected } = useWallet()
//! COMMAND MODAL
// Hide modal when we nav away.
useEffect(() => {
setCommandModalVisible(false)
}, [router.asPath, setCommandModalVisible])
// Detect if Mac for checking keypress.
const { isMac } = usePlatform()
// Handle keypress to show command modal or not.
const handleKeyPress = useCallback(
(event) => {
if ((!isMac && event.ctrlKey) || event.metaKey) {
if (event.key === 'k') {
setCommandModalVisible((showSearch) => !showSearch)
}
}
},
[isMac, setCommandModalVisible]
)
// Setup command modal keypress.
useEffect(() => {
document.addEventListener('keydown', handleKeyPress)
return () => document.removeEventListener('keydown', handleKeyPress)
}, [handleKeyPress])
//! Inbox
// Inbox notifications
const [lastInboxCount, setLastInboxCount] = useState(inbox.items.length)
useEffect(() => {
if (inbox.items.length > lastInboxCount) {
setTimeout(
() =>
toast.success(
t('info.notificationsInInbox', {
count: inbox.items.length,
})
),
// 3 second delay.
3 * 1000
)
}
setLastInboxCount(inbox.items.length)
}, [inbox.items.length, lastInboxCount, t])
//! Auto refresh various data used across the UI
useAutoRefreshData()
//! Following DAOs
const { uniquePublicKeys } = useProfile()
const followingDaoDropdownInfos = useCachedLoading(
!uniquePublicKeys.loading
? waitForAll(
uniquePublicKeys.data.map(({ publicKey }) =>
followingDaoDropdownInfosSelector({
walletPublicKey: publicKey,
// If not compact, remove any SubDAO from the top level that
// exists as a SubDAO of another followed DAO at the top level.
// When compact, SubDAOs aren't visible, so we should show
// followed SubDAOs in the top level.
removeTopLevelSubDaos: !compact,
})
)
)
: undefined,
[]
)
return (
// Default wrap Dapp in chain provider. Used in DappNavigation for default
// governance tab.
<ChainProvider chainId={chainId}>
<StatelessDappLayout
ButtonLink={ButtonLink}
DockWallet={DockWallet}
PageHeader={StatefulPageHeader}
connect={openView}
inboxCount={
inbox.loading ||
// Prevent hydration errors by loading until mounted.
!mountedInBrowser
? {
loading: true,
}
: {
loading: false,
data: inbox.items.length,
}
}
navigationProps={{
walletConnected: isWalletConnected,
LinkWrapper,
setCommandModalVisible: () => setCommandModalVisible(true),
followingDaos: mountedInBrowser
? followingDaoDropdownInfos.loading
? { loading: true }
: {
loading: false,
data: uniqBy(
followingDaoDropdownInfos.data.flat(),
(d) => d.chainId + d.coreAddress
)
// Alphabetize.
.sort((a, b) => a.name.localeCompare(b.name)),
}
: // Prevent hydration errors by loading until mounted.
{ loading: true },
compact,
setCompact,
mountedInBrowser,
}}
>
{children}
{/* Modals */}
<BetaWarningModal
onClose={() => setBetaWarningAccepted(true)}
visible={mountedInBrowser && !betaWarningAccepted}
/>
{rootCommandContextMaker && (
<CommandModal
makeRootContext={rootCommandContextMaker}
setVisible={setCommandModalVisible}
visible={commandModalVisible}
/>
)}
{daoCreatedCardProps && (
<DaoCreatedModal
itemProps={daoCreatedCardProps}
modalProps={{
onClose: () => setDaoCreatedCardProps(undefined),
}}
/>
)}
{proposalCreatedCardProps && (
<ProposalCreatedModal
itemProps={{
...proposalCreatedCardProps,
LinkWrapper,
}}
modalProps={{
onClose: () => setProposalCreatedCardProps(undefined),
}}
/>
)}
<WalletModals />
</StatelessDappLayout>
</ChainProvider>
)
}