Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
import { zodResolver } from "@hookform/resolvers/zod";
import { ArrowUpRight } from "@openstatus/icons";
import { THEMES, THEME_KEYS } from "@openstatus/theme-store";
import { Button } from "@openstatus/ui/components/ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@openstatus/ui/components/ui/dialog";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@openstatus/ui/components/ui/form";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@openstatus/ui/components/ui/select";
import { cn } from "@openstatus/ui/lib/utils";
import { isTRPCClientError } from "@trpc/client";
import { parseAsStringLiteral, useQueryStates } from "nuqs";
import { useEffect, useState, useTransition } from "react";
import { type UseFormReturn, useForm } from "react-hook-form";
import { toast } from "sonner";
import { z } from "zod";
import { Link } from "@/components/common/link";
import { Note } from "@/components/common/note";
import {
FormCard,
FormCardContent,
FormCardDescription,
FormCardFooter,
FormCardFooterInfo,
FormCardHeader,
FormCardSeparator,
FormCardTitle,
} from "@/components/forms/form-card";
const schema = z.object({
configuration: z.record(
z.string(),
z.string().or(z.boolean()).or(z.number()).nullish().optional(),
),
});
const configurationSchema = z
.object({
type: z.enum(["manual", "absolute"]),
value: z.enum(["duration", "requests", "manual"]).nullish(),
uptime: z.boolean().or(z.literal("true").or(z.literal("false"))),
theme: z.enum(THEME_KEYS as [string, ...string[]]),
days: z
.union([z.literal(30), z.literal(45)])
.or(z.literal("30").or(z.literal("45")))
.nullish(),
})
.refine(
(data) => {
// If type is "manual", value must be "manual"
if (data.type === "manual") return data.value === "manual";
return true;
},
{
error: "Value must be manual when type is manual",
path: ["value"],
},
);
type FormValues = z.infer<typeof schema>;
export function FormConfiguration({
defaultValues,
onSubmit,
configLink,
hasMonitorComponents = true,
}: {
defaultValues?: FormValues;
onSubmit: (values: FormValues) => Promise<void>;
configLink: string;
hasMonitorComponents?: boolean;
}) {
const [isPending, startTransition] = useTransition();
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: defaultValues ?? {
configuration: {},
},
});
const watchConfigurationType = form.watch("configuration.type") as
| "manual"
| "absolute";
const watchConfigurationValue = form.watch("configuration.value") as
| "duration"
| "requests";
const watchConfigurationUptime = form.watch("configuration.uptime") as
| "true"
| "false";
const watchConfigurationDays = form.watch("configuration.days") ?? 45;
useEffect(() => {
if (watchConfigurationType === "manual") {
form.setValue("configuration.value", "manual");
} else {
form.setValue("configuration.value", "duration");
form.setValue("configuration.type", "absolute");
if (!watchConfigurationUptime) {
form.setValue("configuration.uptime", "true");
}
}
}, [watchConfigurationType, watchConfigurationUptime, form]);
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);
}
});
}
return (
<>
<Form {...form}>
<form id="redesign" onSubmit={form.handleSubmit(submitAction)}>
<FormCard>
<FormCardHeader>
<FormCardTitle>Components Configuration</FormCardTitle>
<FormCardDescription>
Configure which data should be shown for your components.
</FormCardDescription>
</FormCardHeader>
<FormCardSeparator />
<FormCardContent
className={cn(
"grid gap-4 sm:grid-cols-3",
!hasMonitorComponents && "pointer-events-none opacity-50",
)}
>
<FormField
control={form.control}
name="configuration.type"
render={({ field }) => (
<FormItem>
<FormLabel>Status source</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={String(field.value) ?? "absolute"}
disabled={!hasMonitorComponents}
>
<FormControl>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select a source" />
</SelectTrigger>
</FormControl>
<SelectContent>
{(["absolute", "manual"] as const).map((type) => (
<SelectItem key={type} value={type}>
{configurationLabels.type[type]}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="configuration.value"
render={({ field }) => (
<FormItem>
<FormLabel>Card shows</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={String(field.value) ?? "duration"}
disabled={
!hasMonitorComponents ||
watchConfigurationType === "manual"
}
>
<FormControl>
<SelectTrigger className="w-full">
{watchConfigurationType === "manual" ? (
<span className="text-muted-foreground">
Not available for reports only
</span>
) : (
<SelectValue placeholder="Select a value" />
)}
</SelectTrigger>
</FormControl>
<SelectContent>
{(["duration", "requests"] as const).map((value) => (
<SelectItem key={value} value={value}>
{configurationLabels.value[value]}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<Note className="col-span-full">
{hasMonitorComponents ? (
<ul className="list-inside list-disc">
<li>
<span>Status source </span>
<span className="font-medium">
{configurationLabels.type[watchConfigurationType]}
</span>
: <span>{message.type[watchConfigurationType]}</span>
</li>
<li>
<span>Card shows </span>
<span className="font-medium">
{configurationLabels.value[watchConfigurationValue] ??
configurationLabels.value.manual}
</span>
:{" "}
<span>
{message.value[watchConfigurationValue] ??
message.value.default}
</span>
</li>
</ul>
) : (
<span>
Add a monitor component to your status page to configure how
its bars and cards display data.
</span>
)}
</Note>
</FormCardContent>
<FormCardSeparator />
<FormCardContent className="grid gap-4 sm:grid-cols-3">
<FormField
control={form.control}
name="configuration.uptime"
render={({ field }) => (
<FormItem>
<FormLabel>Show Uptime</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={String(field.value) ?? "true"}
>
<FormControl>
<SelectTrigger className="w-full capitalize">
<SelectValue placeholder="Select a type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{["true", "false"].map((type) => (
<SelectItem
key={type}
value={type}
className="capitalize"
>
{type}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="configuration.days"
render={({ field }) => (
<FormItem>
<FormLabel>History</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={String(field.value ?? 45)}
>
<FormControl>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select a range" />
</SelectTrigger>
</FormControl>
<SelectContent>
{["30", "45"].map((days) => (
<SelectItem key={days} value={days}>
{days} days
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<Note className="col-span-full">
<ul className="list-inside list-disc">
<li>
<span>Show Uptime </span>
<span className="font-medium capitalize">
{String(watchConfigurationUptime)}
</span>
: <span>{message.uptime[watchConfigurationUptime]}</span>
</li>
<li>
<span>History </span>
<span className="font-medium">
{String(watchConfigurationDays)} days
</span>
:{" "}
<span>
shows the last {String(watchConfigurationDays)} days of
component history.
</span>
</li>
</ul>
</Note>
</FormCardContent>
<FormCardFooter className="flex-wrap">
<FormCardFooterInfo>
Learn more about{" "}
<Link
href="https://www.openstatus.dev/docs/tutorial/how-to-configure-status-page"
rel="noreferrer"
target="_blank"
>
Configuration
</Link>
.
</FormCardFooterInfo>
<div className="flex items-center gap-2">
<Button type="button" variant="ghost" asChild>
<Link
href={configLink}
rel="noreferrer"
target="_blank"
className="inline-flex items-center gap-1"
>
View and configure status page{" "}
<ArrowUpRight className="h-4 w-4" />
</Link>
</Button>
<Button type="submit" disabled={isPending}>
{isPending ? "Submitting..." : "Submit"}
</Button>
</div>
</FormCardFooter>
</FormCard>
</form>
</Form>
<FormConfigurationDialog
defaultValues={defaultValues}
form={form}
onSubmit={async (e) => {
await onSubmit(e);
// NOTE: make sure to sync the form with the new values
form.reset(e);
}}
/>
</>
);
}
// UI labels only - the stored enum values stay `absolute`/`manual` and
// `duration`/`requests`/`manual` (DB, API and URL params).
export const configurationLabels = {
type: {
absolute: "Monitor checks",
manual: "Reports only",
},
value: {
duration: "Time per status",
requests: "Number of checks",
manual: "Worst status of the day",
},
} as const;
const message = {
type: {
absolute: "bars show your monitor's real results for each day.",
manual:
"bars only change when you post a status report or maintenance. Monitor results are hidden.",
},
value: {
duration:
"cards show how long the monitor was operational, degraded or down.",
requests: "cards show how many checks succeeded, degraded or failed.",
default: "cards show only the worst status of the day.",
},
uptime: {
true: "shares the uptime percentage and current status of your endpoint.",
false: "shares only the current status.",
},
} as const;
// ?type=manual&value=manual&uptime=true&theme=default&days=45
const searchParams = {
type: parseAsStringLiteral(["manual", "absolute"]),
value: parseAsStringLiteral(["duration", "requests", "manual"]),
uptime: parseAsStringLiteral(["true", "false"]),
theme: parseAsStringLiteral(Object.keys(THEMES)),
days: parseAsStringLiteral(["30", "45"]),
};
function FormConfigurationDialog({
defaultValues,
onSubmit,
}: {
defaultValues?: FormValues;
onSubmit: (values: FormValues) => Promise<void>;
form: UseFormReturn<FormValues>;
}) {
const [open, setOpen] = useState(false);
const [isPending, startTransition] = useTransition();
const [{ type, value, uptime, theme, days }, setSearchParams] =
useQueryStates(searchParams);
useEffect(() => {
if (type) setOpen(true);
}, [type]);
function submitAction(values: FormValues) {
if (isPending) return;
const data = configurationSchema.safeParse(values.configuration);
if (!data.success) {
toast.error(data.error.message);
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;
await setSearchParams({
type: null,
value: null,
uptime: null,
theme: null,
days: null,
});
setOpen(false);
} catch (error) {
console.error(error);
} finally {
if (typeof window !== "undefined") {
window.location.reload();
}
}
});
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Status Page Configuration</DialogTitle>
<DialogDescription>
Do you want to update the status page based on the configured
settings? You can always change the settings later.
</DialogDescription>
</DialogHeader>
<div className="flex flex-col gap-2">
<pre className="bg-muted/50 font-commit-mono rounded-md border px-3 py-2 text-sm">
{JSON.stringify({ type, value, uptime, theme, days }, null, 2)}
</pre>
</div>
<DialogFooter>
<DialogClose asChild>
<Button variant="outline">Cancel</Button>
</DialogClose>
<Button
type="button"
onClick={() =>
submitAction({
...defaultValues,
configuration: {
type: type ?? undefined,
value: value ?? undefined,
uptime: uptime ?? undefined,
theme: theme ?? undefined,
// fall back to the page's stored value when the URL omits
// `days` (e.g. links generated before it was added).
days: days ?? defaultValues?.configuration?.days ?? undefined,
},
})
}
disabled={isPending}
>
{isPending ? "Saving..." : "Save"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}