Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { Button } from "@openstatus/ui/components/ui/button";
import {
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@openstatus/ui/components/ui/form";
import { Form } from "@openstatus/ui/components/ui/form";
import { Input } from "@openstatus/ui/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@openstatus/ui/components/ui/select";
import { cn } from "@openstatus/ui/lib/utils";
import { useMutation } from "@tanstack/react-query";
import { isTRPCClientError } from "@trpc/client";
import { useTransition } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import {
FormCardContent,
FormCardSeparator,
} from "@/components/forms/form-card";
import { CheckboxTree } from "@/components/ui/checkbox-tree";
import { useTRPC } from "@/lib/trpc/client";
const schema = z.object({
name: z.string().trim().min(1, "Name is required"),
provider: z.literal("opsgenie"),
data: z.object({
apiKey: z.string().trim().min(1, "API key is required"),
region: z.enum(["us", "eu"], {
message: "Please select a region",
}),
}),
monitors: z.array(z.number()),
});
type FormValues = z.infer<typeof schema>;
export function FormOpsGenie({
defaultValues,
onSubmit,
className,
monitors,
...props
}: Omit<React.ComponentProps<"form">, "onSubmit"> & {
defaultValues?: FormValues;
onSubmit: (values: FormValues) => Promise<void>;
monitors: { id: number; name: string }[];
}) {
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: defaultValues ?? {
name: "",
provider: "opsgenie",
data: {
apiKey: "",
region: undefined,
},
monitors: [],
},
});
const [isPending, startTransition] = useTransition();
const trpc = useTRPC();
const sendTestMutation = useMutation(
trpc.notification.sendTest.mutationOptions(),
);
function submitAction(values: FormValues) {
if (isPending) return;
startTransition(async () => {
try {
const promise = onSubmit(values);
toast.promise(promise, {
loading: "Saving...",
success: "Saved",
error: (error) => {
if (isTRPCClientError(error)) {
return error.message;
}
return "Failed to save";
},
});
await promise;
} catch (error) {
console.error(error);
}
});
}
function testAction() {
if (isPending) return;
// Validate API key field before sending test
const data = form.getValues("data");
if (!data.apiKey || data.apiKey.trim() === "") {
toast.error("Please enter an API key before sending test");
return;
}
// Validate region field before sending test
if (!data.region) {
toast.error("Please select a region before sending test");
return;
}
startTransition(async () => {
try {
const provider = form.getValues("provider");
const promise = sendTestMutation.mutateAsync({
provider,
data: {
opsgenie: data,
},
});
toast.promise(promise, {
loading: "Sending test...",
success: "Test sent",
error: (error) => {
if (error instanceof Error) {
return error.message;
}
return "Failed to send test";
},
});
await promise;
} catch (error) {
console.error(error);
}
});
}
return (
<Form {...form}>
<form
className={cn("grid gap-4", className)}
onSubmit={form.handleSubmit(submitAction)}
{...props}
>
<FormCardContent className="grid gap-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder="My Notifier" {...field} />
</FormControl>
<FormMessage />
<FormDescription>
Enter a descriptive name for your notifier.
</FormDescription>
</FormItem>
)}
/>
<FormField
control={form.control}
name="data.apiKey"
render={({ field }) => (
<FormItem>
<FormLabel>API Key</FormLabel>
<FormControl>
<Input placeholder="your-api-key" {...field} />
</FormControl>
<FormMessage />
<FormDescription>Enter your OpsGenie API key.</FormDescription>
</FormItem>
)}
/>
<FormField
control={form.control}
name="data.region"
render={({ field }) => (
<FormItem>
<FormLabel>Region</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select a region" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="us">US</SelectItem>
<SelectItem value="eu">EU</SelectItem>
</SelectContent>
</Select>
<FormMessage />
<FormDescription>Select your OpsGenie region.</FormDescription>
</FormItem>
)}
/>
<div>
<Button
variant="outline"
size="sm"
type="button"
onClick={testAction}
>
Send Test
</Button>
</div>
</FormCardContent>
<FormCardSeparator />
<FormCardContent>
<FormField
control={form.control}
name="monitors"
render={({ field }) => (
<FormItem>
<FormLabel>Monitors</FormLabel>
<FormDescription>
Select the monitors you want to notify.
</FormDescription>
<FormControl>
<CheckboxTree
items={[
{
id: -1,
label: "Select all",
children: monitors.map((m) => ({
id: m.id,
label: m.name,
})),
},
]}
value={field.value ?? []}
onValueChange={field.onChange}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</FormCardContent>
</form>
</Form>
);
}