-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_app.admin.users.tsx
More file actions
300 lines (297 loc) · 9.3 KB
/
Copy path_app.admin.users.tsx
File metadata and controls
300 lines (297 loc) · 9.3 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import {
Banner,
Button,
Card,
EmptyState,
FormLayout,
HStack,
Selector,
Stack,
Table,
Text,
TextInput,
} from '@astryxdesign/core'
import { useMutation, useQuery } from '@tanstack/react-query'
import { createFileRoute, redirect, useRouter } from '@tanstack/react-router'
import {
createColumnHelper,
createSortedRowModel,
rowSortingFeature,
tableFeatures,
useTable,
} from '@tanstack/react-table'
import { useEffect, useState } from 'react'
import { adminUsersApi } from '../lib/api'
import { authClient, getSession, isAdmin } from '../lib/auth'
import { adminUserKeys, clearProtectedQueries, queryClient } from '../lib/query'
import { ApiError } from '../lib/types'
import type { AdminUser } from '../lib/types'
export const Route = createFileRoute('/_app/admin/users')({
beforeLoad: async () => {
if (!isAdmin(await getSession())) throw redirect({ to: '/jobs' })
},
component: UsersPage,
})
const pageSize = 20
type PendingAction = { user: AdminUser; type: 'role' | 'ban' }
const features = tableFeatures({
rowSortingFeature,
sortedRowModel: createSortedRowModel(),
})
const helper = createColumnHelper<typeof features, AdminUser>()
function UsersPage() {
const router = useRouter()
const [search, setSearch] = useState('')
const [searchField, setSearchField] = useState<'email' | 'name'>('email')
const [page, setPage] = useState(0)
const [pendingAction, setPendingAction] = useState<PendingAction>()
const [actionError, setActionError] = useState('')
const reconcileAuthorization = () => {
clearProtectedQueries()
void authClient.getSession().then(() => router.invalidate())
}
const query = useQuery({
queryKey: adminUserKeys.list(search, searchField, page * pageSize),
queryFn: () =>
adminUsersApi.listUsers(search, searchField, page * pageSize),
retry: false,
})
const mutation = useMutation({
mutationFn: async (action: PendingAction) => {
if (action.type === 'role')
return adminUsersApi.setRole(
action.user.id,
action.user.role === 'admin' ? 'user' : 'admin',
)
return action.user.banned
? adminUsersApi.unbanUser(action.user.id)
: adminUsersApi.banUser(action.user.id)
},
onSuccess: async () => {
setPendingAction(undefined)
setActionError('')
await queryClient.invalidateQueries({ queryKey: adminUserKeys.all })
},
onError: (error) => {
if (error instanceof ApiError && error.kind === 'forbidden')
reconcileAuthorization()
setActionError(
error instanceof Error
? error.message
: 'The user could not be updated.',
)
},
})
const table = useTable({
features,
data: query.data?.users ?? [],
columns: helper.columns([
helper.accessor('name', { header: 'Name' }),
helper.accessor('email', { header: 'Email' }),
helper.accessor('role', {
header: 'Role',
cell: (info) => info.getValue() ?? 'user',
}),
helper.accessor('banned', {
header: 'Ban status',
cell: (info) => (info.getValue() ? 'Banned' : 'Active'),
}),
helper.display({
id: 'actions',
header: 'Actions',
enableSorting: false,
cell: (info) => {
const user = info.row.original
return (
<HStack gap={2}>
<Button
size="sm"
variant="secondary"
label={user.role === 'admin' ? 'Demote' : 'Promote'}
onClick={() => {
setActionError('')
setPendingAction({ user, type: 'role' })
}}
/>
<Button
size="sm"
variant="secondary"
label={user.banned ? 'Unban' : 'Ban'}
onClick={() => {
setActionError('')
setPendingAction({ user, type: 'ban' })
}}
/>
</HStack>
)
},
}),
]),
})
const authorizationDenied =
query.error instanceof ApiError && query.error.kind === 'forbidden'
useEffect(() => {
if (!authorizationDenied) return
reconcileAuthorization()
}, [authorizationDenied, router])
const applySearch = () => {
setPage(0)
void query.refetch()
}
if (authorizationDenied)
return (
<EmptyState
title="Administrator access denied"
description="The server refused this request. Your session has been refreshed."
/>
)
if (query.isPending)
return (
<EmptyState
title="Loading users"
description="Retrieving the administrator user list…"
/>
)
if (query.isError)
return (
<EmptyState
title="Users could not be loaded"
description={query.error.message}
actions={<Button label="Retry" onClick={() => void query.refetch()} />}
/>
)
const totalPages = Math.max(1, Math.ceil(query.data.total / pageSize))
return (
<Stack gap={5}>
<div className="page-heading">
<div>
<div className="eyebrow">Administration</div>
<h1>User manager</h1>
<p>
Manage user roles and access. Changes are authorized by the server.
</p>
</div>
</div>
<Card className="form-panel">
<FormLayout direction="horizontal">
<Selector
label="Search field"
options={['email', 'name']}
value={searchField}
onChange={(value) => setSearchField(value as 'email' | 'name')}
/>
<TextInput
label="Search users"
value={search}
onChange={setSearch}
placeholder={`Search by ${searchField}`}
/>
<Button label="Search" onClick={applySearch} />
</FormLayout>
</Card>
{actionError && <Banner status="error" title={actionError} />}
{query.data.users.length === 0 ? (
<EmptyState
title="No matching users"
description="Try a different search."
/>
) : (
<div className="data-panel">
<Table>
<thead>
{table.getHeaderGroups().map((group) => (
<tr key={group.id}>
{group.headers.map((header) => (
<th key={header.id}>
{header.column.getCanSort() ? (
<button
onClick={header.column.getToggleSortingHandler()}
>
<table.FlexRender header={header} />
<span aria-hidden="true">
{header.column.getIsSorted() === 'asc'
? ' ↑'
: header.column.getIsSorted() === 'desc'
? ' ↓'
: ''}
</span>
</button>
) : (
<table.FlexRender header={header} />
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.map((row) => (
<tr key={row.id}>
{row.getAllCells().map((cell) => (
<td
key={cell.id}
data-label={String(cell.column.columnDef.header)}
>
<table.FlexRender cell={cell} />
</td>
))}
</tr>
))}
</tbody>
</Table>
</div>
)}
<HStack justify="between">
<Text type="supporting">
{query.data.total} users · Page {page + 1} of {totalPages}
</Text>
<HStack gap={2}>
<Button
label="Previous"
variant="secondary"
isDisabled={page === 0}
onClick={() => setPage((current) => current - 1)}
/>
<Button
label="Next"
variant="secondary"
isDisabled={page + 1 >= totalPages}
onClick={() => setPage((current) => current + 1)}
/>
</HStack>
</HStack>
{pendingAction && (
<dialog open aria-labelledby="confirmation-title">
<Card>
<Stack gap={3}>
<h2 id="confirmation-title">
Confirm{' '}
{pendingAction.type === 'role'
? pendingAction.user.role === 'admin'
? 'demotion'
: 'promotion'
: pendingAction.user.banned
? 'unban'
: 'ban'}
</h2>
<Text>Apply this change to {pendingAction.user.email}?</Text>
<HStack justify="end" gap={2}>
<Button
label="Cancel"
variant="secondary"
isDisabled={mutation.isPending}
onClick={() => setPendingAction(undefined)}
/>
<Button
label="Confirm"
isLoading={mutation.isPending}
onClick={() => mutation.mutate(pendingAction)}
/>
</HStack>
</Stack>
</Card>
</dialog>
)}
</Stack>
)
}