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 { Lock, Add, Close } from "@openstatus/icons";
import { Button } from "@openstatus/ui/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@openstatus/ui/components/ui/form";
import { Input } from "@openstatus/ui/components/ui/input";
import { headerPairSchema } from "@openstatus/utils";
import NextLink from "next/link";
import { useTransition } from "react";
import { useFieldArray, useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { Link } from "@/components/common/link";
import {
FormCard,
FormCardContent,
FormCardDescription,
FormCardFooter,
FormCardFooterInfo,
FormCardHeader,
FormCardTitle,
FormCardUpgrade,
} from "@/components/forms/form-card";
// TODO: add headers
const schema = z.object({
endpoint: z.url("Please enter a valid URL"),
headers: z.array(headerPairSchema).prefault([]),
});
type FormValues = z.input<typeof schema>;
export function FormOtel({
locked,
defaultValues,
onSubmit,
...props
}: Omit<React.ComponentProps<"form">, "onSubmit"> & {
locked?: boolean;
defaultValues?: FormValues;
onSubmit: (values: FormValues) => Promise<void>;
}) {
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: defaultValues ?? { endpoint: "", headers: [] },
});
// Stable row ids so removing a row remounts the shifted controllers.
const {
fields: headerFields,
append: appendHeader,
remove: removeHeader,
} = useFieldArray({ control: form.control, name: "headers" });
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>
{locked ? <FormCardUpgrade /> : null}
<FormCardHeader>
<FormCardTitle>OpenTelemetry</FormCardTitle>
<FormCardDescription>
Configure your OpenTelemetry Exporter.
</FormCardDescription>
</FormCardHeader>
<FormCardContent className="grid gap-4 sm:grid-cols-3">
<FormField
control={form.control}
name="endpoint"
render={({ field }) => (
<FormItem className="sm:col-span-2">
<FormLabel>Endpoint</FormLabel>
<FormControl>
<Input
placeholder="https://otel.openstatus.dev/api/v1/metrics"
disabled={locked}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormItem className="col-span-full">
<FormLabel>Request Headers</FormLabel>
{headerFields.map((row, index) => (
<div key={row.id} className="grid gap-2 sm:grid-cols-5">
<FormField
control={form.control}
name={`headers.${index}.key`}
render={({ field }) => (
<FormItem className="col-span-2">
<FormControl>
<Input
placeholder="Key"
{...field}
disabled={locked}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`headers.${index}.value`}
render={({ field }) => (
<FormItem className="col-span-2">
<FormControl>
<Input
placeholder="Value"
{...field}
disabled={locked}
/>
</FormControl>
</FormItem>
)}
/>
<Button
size="icon"
variant="ghost"
type="button"
aria-label="Remove header"
disabled={locked}
onClick={() => removeHeader(index)}
>
<Close />
</Button>
</div>
))}
<div>
<Button
size="sm"
variant="outline"
type="button"
disabled={locked}
onClick={() => appendHeader({ key: "", value: "" })}
>
<Add />
Add Header
</Button>
</div>
</FormItem>
</FormCardContent>
<FormCardFooter>
<FormCardFooterInfo>
Learn more about{" "}
<Link
href="https://www.openstatus.dev/docs/reference/http-monitor/#opentelemetry"
rel="noreferrer"
target="_blank"
>
OTel
</Link>
.
</FormCardFooterInfo>
{locked ? (
<Button data-track="paywall_viewed" data-limit="otel" asChild>
<NextLink href="/settings/billing">
<Lock className="size-4" />
Upgrade
</NextLink>
</Button>
) : (
<Button type="submit" disabled={isPending}>
{isPending ? "Submitting..." : "Submit"}
</Button>
)}
</FormCardFooter>
</FormCard>
</form>
</Form>
);
}