"use client"; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { FormCard, FormCardContent, FormCardDescription, FormCardFooter, FormCardHeader, FormCardTitle, } from "@/components/forms/form-card"; import { Button } from "@/components/ui/button"; import { Form } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { zodResolver } from "@hookform/resolvers/zod"; import { useTransition } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const DEGRADED = 30_000; const TIMEOUT = 45_000; const schema = z.object({ degradedAfter: z.coerce.number().optional(), timeout: z.coerce.number(), }); type FormValues = z.input; export function FormResponseTime({ defaultValues, onSubmit, ...props }: Omit, "onSubmit"> & { defaultValues?: FormValues; onSubmit: (values: FormValues) => Promise; }) { const form = useForm({ 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 (
Response Time Thresholds Configure your degraded and timeout thresholds. ( Degraded (in ms.) Time after which the endpoint is considered degraded. )} /> ( Timeout (in ms.) Max. time allowed for request to complete. )} />
); }