-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathuseInboxApiWithUi.tsx
172 lines (158 loc) · 4.87 KB
/
useInboxApiWithUi.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
import { ClearAll, Delete, Refresh, Settings } from '@mui/icons-material'
import clsx from 'clsx'
import { useRouter } from 'next/router'
import { useCallback, useEffect, useState } from 'react'
import toast from 'react-hot-toast'
import { useTranslation } from 'react-i18next'
import { IconButton, Tooltip, useAppContext } from '@dao-dao/stateless'
import { InboxApiWithUi, InboxLoadedItem } from '@dao-dao/types'
import { processError } from '@dao-dao/utils'
import { IconButtonLink } from '../components'
import { useInboxApi } from './useInboxApi'
import { useWallet } from './useWallet'
export type UseInboxApiWithUiOptions = {
/**
* Whether or not we are currently on the notifications page or in the popup.
*/
mode: 'page' | 'popup'
}
export const useInboxApiWithUi = ({
mode,
}: UseInboxApiWithUiOptions): InboxApiWithUi => {
const { t } = useTranslation()
const api = useInboxApi()
const {
query: { code },
isReady,
replace,
} = useRouter()
const { isWalletConnected } = useWallet()
const { inbox } = useAppContext()
// Type-check, should always be loaded for dapp.
if (!inbox) {
throw new Error(t('error.loadingData'))
}
const { ready, verify: doVerify } = api
const verify = useCallback(async () => {
if (ready && isReady) {
if (typeof code === 'string') {
if (await doVerify(code)) {
toast.success(t('info.emailVerified'))
}
} else {
toast.error(t('error.invalidCode'))
}
replace('/notifications/settings', undefined, { shallow: true })
}
}, [code, isReady, replace, ready, t, doVerify])
const [refreshSpinning, setRefreshSpinning] = useState(false)
// Start spinning refresh icon if refreshing sets to true. Turn off once the
// iteration completes (in `onAnimationIteration` below).
const shouldBeSpinningRefresh = isWalletConnected && inbox.refreshing
useEffect(() => {
shouldBeSpinningRefresh && setRefreshSpinning(true)
}, [shouldBeSpinningRefresh])
const [checked, setChecked] = useState({} as Record<string, boolean>)
const countChecked = Object.values(checked).filter(Boolean).length
const onCheck = useCallback(
(item: InboxLoadedItem) =>
setChecked((prev) => ({
...prev,
[item.chainId + ':' + item.id]: !prev[item.chainId + ':' + item.id],
})),
[]
)
const [checking, setChecking] = useState(false)
const clearChecked = useCallback(async () => {
setChecking(true)
try {
// If none checked, clear all.
const toClear = !countChecked
? inbox.items
: Object.entries(checked).flatMap(([key, checked]) =>
checked
? {
chainId: key.split(':')[0],
id: key.split(':')[1],
}
: []
)
if (toClear.length && (await api.clear(toClear))) {
setChecked({})
}
} catch (err) {
console.error(err)
toast.error(processError(err))
} finally {
setChecking(false)
}
}, [api, checked, countChecked, inbox.items])
const refreshButton = (
<Tooltip title={t('button.refresh')}>
<IconButton
Icon={Refresh}
disabled={!api.ready}
iconClassName={clsx(refreshSpinning && 'animate-spin-medium')}
// If spinning but no longer refreshing, stop after iteration.
onAnimationIteration={
refreshSpinning && !shouldBeSpinningRefresh
? () => setRefreshSpinning(false)
: undefined
}
onClick={() => {
// Perform one spin even if refresh completes immediately. It will
// stop after 1 iteration if `refreshing` does not become true.
setRefreshSpinning(true)
inbox.refresh()
}}
size={mode === 'popup' ? 'sm' : undefined}
variant="ghost"
/>
</Tooltip>
)
const clearButton = (
// Matches clear button in InboxMainItemRenderer
<Tooltip
title={
countChecked
? t('button.clearSelected', {
count: countChecked,
})
: t('button.clearAll')
}
>
<IconButton
Icon={countChecked ? Delete : ClearAll}
disabled={!api.ready || api.updating || !inbox.items.length}
loading={checking}
onClick={clearChecked}
size={mode === 'popup' ? 'sm' : undefined}
variant={countChecked ? 'brand' : 'ghost'}
/>
</Tooltip>
)
const settingsButton = (
<Tooltip title={t('button.settings')}>
<IconButtonLink
Icon={Settings}
disabled={!api.ready}
href="/notifications/settings"
replace={mode === 'page'}
shallow
size={mode === 'popup' ? 'sm' : undefined}
variant="ghost"
/>
</Tooltip>
)
return {
api,
checked,
onCheck,
verify,
buttons: {
refresh: refreshButton,
clear: clearButton,
settings: settingsButton,
},
}
}