-
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathpage.tsx
More file actions
370 lines (348 loc) · 12.8 KB
/
page.tsx
File metadata and controls
370 lines (348 loc) · 12.8 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"use client";
import { useEffect, useState } from "react";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { Button } from "@usesend/ui/src/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@usesend/ui/src/form";
import { Input } from "@usesend/ui/src/input";
import { Switch } from "@usesend/ui/src/switch";
import Spinner from "@usesend/ui/src/spinner";
import { toast } from "@usesend/ui/src/toaster";
import { Badge } from "@usesend/ui/src/badge";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@usesend/ui/src/select";
import { formatDistanceToNow } from "date-fns";
import { aggregateDomainStatus } from "~/lib/domain-aggregate-status";
import { api } from "~/trpc/react";
import type { AppRouter } from "~/server/api/root";
import type { inferRouterOutputs } from "@trpc/server";
import { isCloud } from "~/utils/common";
const searchSchema = z.object({
query: z
.string({ required_error: "Enter a team ID, name, domain, member email, or subscription ID" })
.trim()
.min(1, "Enter a team ID, name, domain, member email, or subscription ID"),
});
type SearchInput = z.infer<typeof searchSchema>;
type RouterOutputs = inferRouterOutputs<AppRouter>;
type TeamAdmin = NonNullable<RouterOutputs["admin"]["findTeam"]>;
const updateSchema = z.object({
apiRateLimit: z.coerce.number().int().min(1).max(10_000),
dailyEmailLimit: z.coerce.number().int().min(0).max(10_000_000),
isBlocked: z.boolean(),
plan: z.enum(["FREE", "BASIC"]),
});
type UpdateInput = z.infer<typeof updateSchema>;
export default function AdminTeamsPage() {
const [team, setTeam] = useState<TeamAdmin | null>(null);
const [hasSearched, setHasSearched] = useState(false);
const searchForm = useForm<SearchInput>({
resolver: zodResolver(searchSchema),
defaultValues: { query: "" },
});
const updateForm = useForm<UpdateInput>({
resolver: zodResolver(updateSchema),
defaultValues: {
apiRateLimit: 1,
dailyEmailLimit: 0,
isBlocked: false,
plan: "FREE",
},
});
useEffect(() => {
if (team) {
updateForm.reset({
apiRateLimit: team.apiRateLimit,
dailyEmailLimit: team.dailyEmailLimit,
isBlocked: team.isBlocked,
plan: team.plan,
});
}
}, [team, updateForm]);
if (!isCloud()) {
return (
<div className="rounded-lg border bg-muted/30 p-6 text-sm text-muted-foreground">
Team administration tools are available only in the cloud deployment.
</div>
);
}
const findTeam = api.admin.findTeam.useMutation({
onSuccess: (data) => {
setHasSearched(true);
if (!data) {
setTeam(null);
toast.info("No team found for that query");
return;
}
setTeam(data);
},
onError: (error) => {
toast.error(error.message ?? "Unable to search for team");
},
});
const updateTeam = api.admin.updateTeamSettings.useMutation({
onSuccess: (updated) => {
setTeam(updated);
updateForm.reset({
apiRateLimit: updated.apiRateLimit,
dailyEmailLimit: updated.dailyEmailLimit,
isBlocked: updated.isBlocked,
plan: updated.plan,
});
toast.success("Team settings updated");
},
onError: (error) => {
toast.error(error.message ?? "Unable to update team settings");
},
});
const onSearchSubmit = (values: SearchInput) => {
setTeam(null);
setHasSearched(false);
findTeam.mutate(values);
};
const onUpdateSubmit = (values: UpdateInput) => {
if (!team) return;
updateTeam.mutate({ teamId: team.id, ...values });
};
return (
<div className="space-y-8">
<div className="rounded-lg border p-6 shadow-sm">
<Form {...searchForm}>
<form
onSubmit={searchForm.handleSubmit(onSearchSubmit)}
className="space-y-4"
noValidate
>
<FormField
control={searchForm.control}
name="query"
render={({ field }) => (
<FormItem>
<FormLabel>Team lookup</FormLabel>
<FormControl>
<Input
placeholder="Team ID, team name, domain, member email, or subscription ID"
autoComplete="off"
{...field}
value={field.value}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" disabled={findTeam.isPending}>
{findTeam.isPending ? (
<>
<Spinner className="mr-2 h-4 w-4" /> Searching...
</>
) : (
"Lookup team"
)}
</Button>
</form>
</Form>
</div>
{findTeam.isPending ? null : hasSearched && !team ? (
<div className="rounded-lg border border-dashed p-6 text-sm text-muted-foreground">
No team matched that query. Try another search.
</div>
) : null}
{team ? (
<div className="space-y-6 rounded-lg border p-6 shadow-sm">
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<p className="text-sm text-muted-foreground">Team</p>
<p className="text-xl font-semibold">{team.name}</p>
<p className="text-xs text-muted-foreground">
ID #{team.id} • Created {formatDistanceToNow(new Date(team.createdAt), { addSuffix: true })}
</p>
</div>
<div className="flex flex-wrap items-center gap-2">
<Badge variant="outline">Plan: {team.plan}</Badge>
<Badge variant={team.isBlocked ? "destructive" : "outline"}>
{team.isBlocked ? "Blocked" : "Active"}
</Badge>
</div>
</div>
<div className="grid gap-4 lg:grid-cols-2">
<div className="space-y-3">
<h3 className="text-sm font-medium text-muted-foreground">Members</h3>
<div className="space-y-2 rounded-lg border bg-muted/20 p-3">
{team.teamUsers.length ? (
team.teamUsers.map((member) => (
<div
key={member.user.id}
className="flex items-center justify-between rounded-md bg-background px-3 py-2 text-sm"
>
<div>
<p className="font-medium">{member.user.name ?? member.user.email}</p>
<p className="text-xs text-muted-foreground">{member.user.email}</p>
</div>
<Badge variant="outline">{member.role}</Badge>
</div>
))
) : (
<p className="text-xs text-muted-foreground">No members found.</p>
)}
</div>
</div>
<div className="space-y-3">
<h3 className="text-sm font-medium text-muted-foreground">Domains</h3>
<div className="space-y-2 rounded-lg border bg-muted/20 p-3">
{team.domains.length ? (
team.domains.map((domain) => {
const agg = aggregateDomainStatus(domain);
return (
<div
key={domain.id}
className="flex items-center justify-between rounded-md bg-background px-3 py-2 text-sm"
>
<span>{domain.name}</span>
<Badge variant={agg === "SUCCESS" ? "outline" : "secondary"}>
{agg === "SUCCESS"
? "Verified"
: agg.toLowerCase()}
</Badge>
</div>
);
})
) : (
<p className="text-xs text-muted-foreground">No domains connected.</p>
)}
</div>
</div>
</div>
<div className="rounded-lg border bg-muted/10 p-4">
<p className="text-sm text-muted-foreground">
Billing contact: {team.billingEmail ?? "Not set"}
</p>
</div>
<div className="rounded-lg border p-6">
<Form {...updateForm}>
<form onSubmit={updateForm.handleSubmit(onUpdateSubmit)} className="grid gap-6 lg:grid-cols-2">
<FormField
control={updateForm.control}
name="apiRateLimit"
render={({ field }) => (
<FormItem>
<FormLabel>API rate limit</FormLabel>
<FormControl>
<Input
type="number"
min={1}
max={10000}
{...field}
value={Number.isNaN(field.value) ? 1 : field.value}
onChange={(event) =>
field.onChange(Number(event.target.value))
}
disabled={updateTeam.isPending}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={updateForm.control}
name="dailyEmailLimit"
render={({ field }) => (
<FormItem>
<FormLabel>Daily email limit</FormLabel>
<FormControl>
<Input
type="number"
min={0}
max={10_000_000}
{...field}
value={Number.isNaN(field.value) ? 0 : field.value}
onChange={(event) =>
field.onChange(Number(event.target.value))
}
disabled={updateTeam.isPending}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={updateForm.control}
name="plan"
render={({ field }) => (
<FormItem>
<FormLabel>Plan</FormLabel>
<FormControl>
<Select
value={field.value}
onValueChange={field.onChange}
disabled={updateTeam.isPending}
>
<SelectTrigger>
<SelectValue placeholder="Select plan" />
</SelectTrigger>
<SelectContent>
<SelectItem value="FREE">Free</SelectItem>
<SelectItem value="BASIC">Basic</SelectItem>
</SelectContent>
</Select>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={updateForm.control}
name="isBlocked"
render={({ field }) => (
<FormItem>
<FormLabel>Blocked</FormLabel>
<FormControl>
<div className="flex items-center gap-3 rounded-md border px-3 py-2">
<Switch
checked={field.value}
onCheckedChange={field.onChange}
disabled={updateTeam.isPending}
/>
<span className="text-sm text-muted-foreground">
{field.value ? "Team is blocked" : "Team is active"}
</span>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="lg:col-span-2 flex justify-end">
<Button type="submit" disabled={updateTeam.isPending}>
{updateTeam.isPending ? (
<>
<Spinner className="mr-2 h-4 w-4" /> Saving...
</>
) : (
"Update team"
)}
</Button>
</div>
</form>
</Form>
</div>
</div>
) : null}
</div>
);
}