Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.7 kB ยท 127 lines
TSX
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128"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 { useTransition } from "react";import { useForm } from "react-hook-form";import { toast } from "sonner";import { z } from "zod";
import { FormCard, FormCardContent, FormCardDescription, FormCardFooter, FormCardHeader, FormCardTitle,} from "@/components/forms/form-card";
const DEGRADED = 30_000;const TIMEOUT = 45_000;
const schema = z.object({ degradedAfter: z.coerce.number<number>().optional(), timeout: z.coerce.number<number>(),});
type FormValues = z.input<typeof schema>;
export function FormResponseTime({ defaultValues, onSubmit, ...props}: Omit<React.ComponentProps<"form">, "onSubmit"> & { defaultValues?: FormValues; onSubmit: (values: FormValues) => Promise<void>;}) { const form = useForm<FormValues>({ resolver: zodResolver(schema), defaultValues: defaultValues ?? { degradedAfter: DEGRADED, timeout: TIMEOUT, }, }); const [isPending, startTransition] = useTransition();
function submitAction(values: FormValues) { if (isPending) return;
startTransition(async () => { try { const promise = onSubmit(values); toast.promise(promise, { loading: "Saving...", success: () => "Saved", error: "Failed to save", }); await promise; } catch (error) { console.error(error); } }); }
return ( <Form {...form}> <form onSubmit={form.handleSubmit(submitAction)} {...props}> <FormCard> <FormCardHeader> <FormCardTitle>Response Time Thresholds</FormCardTitle> <FormCardDescription> Configure your degraded and timeout thresholds. </FormCardDescription> </FormCardHeader> <FormCardContent className="grid gap-4 sm:grid-cols-2"> <FormField control={form.control} name="degradedAfter" render={({ field }) => ( <FormItem className="self-start"> <FormLabel>Degraded (in ms.)</FormLabel> <FormControl> <Input placeholder="30000" type="number" {...field} /> </FormControl> <FormDescription> Time after which the endpoint is considered degraded. </FormDescription> <FormMessage /> </FormItem> )} /> <FormField control={form.control} name="timeout" render={({ field }) => ( <FormItem className="self-start"> <FormLabel>Timeout (in ms.)</FormLabel> <FormControl> <Input placeholder="45000" type="number" {...field} /> </FormControl> <FormDescription> Max. time allowed for request to complete. </FormDescription> <FormMessage /> </FormItem> )} /> </FormCardContent> <FormCardFooter> <Button type="submit" disabled={isPending}> {isPending ? "Submitting..." : "Submit"} </Button> </FormCardFooter> </FormCard> </form> </Form> );}