forked from openstatusHQ/openstatus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-import.tsx
More file actions
480 lines (470 loc) · 18.3 KB
/
form-import.tsx
File metadata and controls
480 lines (470 loc) · 18.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
"use client";
import { Note } from "@/components/common/note";
import {
FormCard,
FormCardContent,
FormCardDescription,
FormCardFooter,
FormCardHeader,
FormCardSeparator,
FormCardTitle,
} from "@/components/forms/form-card";
import { useTRPC } from "@/lib/trpc/client";
import { zodResolver } from "@hookform/resolvers/zod";
import {
BetterstackIcon,
InstatusIcon,
StatuspageIcon,
} from "@openstatus/icons";
import type { ImportSummary } from "@openstatus/importers/types";
import { Badge } from "@openstatus/ui/components/ui/badge";
import { Button } from "@openstatus/ui/components/ui/button";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@openstatus/ui/components/ui/form";
import { Input } from "@openstatus/ui/components/ui/input";
import {
RadioGroup,
RadioGroupItem,
} from "@openstatus/ui/components/ui/radio-group";
import { Switch } from "@openstatus/ui/components/ui/switch";
import { useMutation } from "@tanstack/react-query";
import { isTRPCClientError } from "@trpc/client";
import { AlertTriangle } from "lucide-react";
import { useTransition } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
const schema = z.object({
provider: z.enum(["statuspage", "betterstack", "instatus"]),
apiKey: z.string().min(1, "API key is required"),
statuspagePageId: z.string().optional(),
betterstackStatusPageId: z.string().optional(),
instatusPageId: z.string().optional(),
includeMonitors: z.boolean(),
includeStatusReports: z.boolean(),
includeSubscribers: z.boolean(),
includeComponents: z.boolean(),
});
export type ImportFormValues = z.input<typeof schema>;
function getPhaseCount(preview: ImportSummary, phase: string): number {
return preview.phases.find((p) => p.phase === phase)?.resources.length ?? 0;
}
const PHASE_LABELS: Record<string, string> = {
monitors: "Monitors",
componentGroups: "Component Groups",
monitorGroups: "Monitor Groups",
sections: "Sections",
components: "Components",
incidents: "Status Reports",
maintenances: "Maintenances",
subscribers: "Subscribers",
};
export function FormImport({
pageId,
onSubmit,
}: {
pageId: number;
onSubmit: (values: ImportFormValues) => Promise<ImportSummary>;
}) {
const form = useForm<ImportFormValues>({
resolver: zodResolver(schema),
defaultValues: {
provider: undefined,
apiKey: "",
statuspagePageId: "",
betterstackStatusPageId: "",
instatusPageId: "",
includeMonitors: true,
includeStatusReports: true,
includeSubscribers: false,
includeComponents: true,
},
});
const [isPending, startTransition] = useTransition();
const trpc = useTRPC();
const watchProvider = form.watch("provider");
const watchApiKey = form.watch("apiKey");
const watchStatuspagePageId = form.watch("statuspagePageId");
const watchBetterstackStatusPageId = form.watch("betterstackStatusPageId");
const watchInstatusPageId = form.watch("instatusPageId");
const previewMutation = useMutation(
trpc.import.preview.mutationOptions({
onError: (error) => {
if (isTRPCClientError(error)) {
toast.error(error.message);
} else {
toast.error("Failed to preview import");
}
},
}),
);
async function runPreview() {
const apiKey = form.getValues("apiKey");
if (!apiKey) {
form.setError("apiKey", { message: "API key is required" });
return;
}
previewMutation.mutate({
provider: watchProvider,
apiKey: watchApiKey,
statuspagePageId:
watchProvider === "statuspage"
? watchStatuspagePageId || undefined
: undefined,
betterstackStatusPageId:
watchProvider === "betterstack"
? watchBetterstackStatusPageId || undefined
: undefined,
instatusPageId:
watchProvider === "instatus"
? watchInstatusPageId || undefined
: undefined,
pageId,
});
}
function submitAction(values: ImportFormValues) {
if (isPending || !previewMutation.data) return;
startTransition(async () => {
try {
const promise = onSubmit(values);
toast.promise(promise, {
loading: "Importing...",
success: (result) => {
if (result.status === "partial")
return "Import completed with warnings";
return "Import completed";
},
error: (error) => {
if (isTRPCClientError(error)) {
return error.message;
}
return "Import failed";
},
});
await promise;
} catch (error) {
console.error(error);
}
});
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(submitAction)}>
<FormCard>
<FormCardHeader>
<FormCardTitle>Import</FormCardTitle>
<FormCardDescription>
Import components, incidents, and subscribers from an external
status page provider.
</FormCardDescription>
</FormCardHeader>
<FormCardSeparator />
<FormCardContent>
<FormField
control={form.control}
name="provider"
render={({ field }) => (
<FormItem>
<FormLabel>Provider</FormLabel>
<FormControl>
<RadioGroup
onValueChange={field.onChange}
defaultValue={field.value}
className="grid grid-cols-2 gap-4 sm:grid-cols-4"
>
<FormItem className="relative flex cursor-pointer flex-row items-center gap-3 rounded-md border border-input px-2 py-3 text-center shadow-xs outline-none transition-[color,box-shadow] has-data-[state=checked]:border-primary/50 has-focus-visible:border-ring has-focus-visible:ring-[3px] has-focus-visible:ring-ring/50">
<FormControl>
<RadioGroupItem
value="statuspage"
className="sr-only"
/>
</FormControl>
<StatuspageIcon
className="size-4 shrink-0 text-foreground"
aria-hidden="true"
/>
<FormLabel className="cursor-pointer font-medium text-foreground text-xs leading-none after:absolute after:inset-0">
Atlassian Statuspage
</FormLabel>
</FormItem>
<FormItem className="relative flex cursor-pointer flex-row items-center gap-3 rounded-md border border-input px-2 py-3 text-center shadow-xs outline-none transition-[color,box-shadow] has-data-[state=checked]:border-primary/50 has-focus-visible:border-ring has-focus-visible:ring-[3px] has-focus-visible:ring-ring/50">
<FormControl>
<RadioGroupItem
value="betterstack"
className="sr-only"
/>
</FormControl>
<BetterstackIcon
className="size-4 shrink-0 text-foreground"
aria-hidden="true"
/>
<FormLabel className="cursor-pointer font-medium text-foreground text-xs leading-none after:absolute after:inset-0">
Better Stack
</FormLabel>
</FormItem>
<FormItem className="relative flex cursor-pointer flex-row items-center gap-3 rounded-md border border-input px-2 py-3 text-center shadow-xs outline-none transition-[color,box-shadow] has-data-[state=checked]:border-primary/50 has-focus-visible:border-ring has-focus-visible:ring-[3px] has-focus-visible:ring-ring/50">
<FormControl>
<RadioGroupItem
value="instatus"
className="sr-only"
/>
</FormControl>
<InstatusIcon
className="size-4 shrink-0 text-foreground"
aria-hidden="true"
/>
<FormLabel className="cursor-pointer font-medium text-foreground text-xs leading-none after:absolute after:inset-0">
Instatus
</FormLabel>
</FormItem>
<div className="col-span-1 self-end text-muted-foreground text-xs sm:place-self-end">
Missing a provider?{" "}
<a href="mailto:ping@openstatus.dev">Contact us</a>
</div>
</RadioGroup>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</FormCardContent>
{watchProvider ? (
<>
<FormCardSeparator />
<FormCardContent className="grid gap-4">
<FormField
control={form.control}
name="apiKey"
render={({ field }) => (
<FormItem>
<FormLabel>API Key</FormLabel>
<FormControl>
<Input
type="password"
placeholder={
watchProvider === "betterstack"
? "Bearer token"
: watchProvider === "instatus"
? "Bearer API key"
: "OAuth API key"
}
{...field}
/>
</FormControl>
<FormMessage />
<FormDescription>
{watchProvider === "betterstack"
? "Your Better Stack API token. Found in Better Stack \u2192 API tokens."
: watchProvider === "instatus"
? "Your Instatus API key. Found in your Instatus account under Settings > API."
: "Your Statuspage API key. Found in your Statuspage account under Manage Account > API."}
</FormDescription>
</FormItem>
)}
/>
{watchProvider === "statuspage" ? (
<FormField
control={form.control}
name="statuspagePageId"
render={({ field }) => (
<FormItem>
<FormLabel>Page ID (optional)</FormLabel>
<FormControl>
<Input placeholder="e.g. abc123def456" {...field} />
</FormControl>
<FormDescription>
Import a specific page. Leave empty to import across
pages.
</FormDescription>
</FormItem>
)}
/>
) : null}
{watchProvider === "betterstack" ? (
<FormField
control={form.control}
name="betterstackStatusPageId"
render={({ field }) => (
<FormItem>
<FormLabel>Status Page ID (optional)</FormLabel>
<FormControl>
<Input placeholder="e.g. 123456789" {...field} />
</FormControl>
<FormDescription>
Import a specific status page. Leave empty to use the
first available.
</FormDescription>
</FormItem>
)}
/>
) : null}
{watchProvider === "instatus" ? (
<FormField
control={form.control}
name="instatusPageId"
render={({ field }) => (
<FormItem>
<FormLabel>Page ID (optional)</FormLabel>
<FormControl>
<Input placeholder="e.g. clx1abc2def3" {...field} />
</FormControl>
<FormDescription>
Import a specific page. Leave empty to import all
pages.
</FormDescription>
</FormItem>
)}
/>
) : null}
<Button
type="button"
variant="secondary"
onClick={runPreview}
disabled={previewMutation.isPending}
>
{previewMutation.isPending
? "Loading preview..."
: "Preview Import"}
</Button>
</FormCardContent>
</>
) : null}
{previewMutation.data ? (
<>
<FormCardSeparator />
<FormCardContent className="grid gap-4">
<div>
<FormLabel>Preview</FormLabel>
<div className="mt-2 flex flex-wrap gap-2">
{Object.entries(PHASE_LABELS).map(([key, label]) => {
const count = getPhaseCount(previewMutation.data, key);
if (count === 0) return null;
return (
<Badge key={key} variant="secondary">
{label}: {count}
</Badge>
);
})}
</div>
</div>
{previewMutation.data.errors.length > 0 ? (
<Note color="error" size="sm">
<AlertTriangle />
<p className="text-sm">
{previewMutation.data.errors.join(" ")}
</p>
</Note>
) : null}
{watchProvider === "betterstack" ? (
<FormField
control={form.control}
name="includeMonitors"
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between">
<div className="space-y-0.5">
<FormLabel>Monitors</FormLabel>
<FormDescription>
Import monitors with their URL, frequency, and
regions.
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
) : null}
<FormField
control={form.control}
name="includeStatusReports"
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between">
<div className="space-y-0.5">
<FormLabel>Status Reports & Maintenances</FormLabel>
<FormDescription>
Import incidents as status reports and scheduled
maintenances.
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="includeComponents"
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between">
<div className="space-y-0.5">
<FormLabel>Components</FormLabel>
<FormDescription>
Import components and groups.
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
{watchProvider !== "betterstack" ? (
<FormField
control={form.control}
name="includeSubscribers"
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between">
<div className="space-y-0.5">
<FormLabel>Subscribers</FormLabel>
<FormDescription>
Import email subscribers.
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
/>
</FormControl>
</FormItem>
)}
/>
) : null}
</FormCardContent>
</>
) : null}
<FormCardFooter>
<Button
type="submit"
disabled={
!previewMutation.data ||
isPending ||
previewMutation.data.errors.length > 0
}
>
{isPending ? "Importing..." : "Import"}
</Button>
</FormCardFooter>
</FormCard>
</form>
</Form>
);
}