Skip to content

Open-source status page, incident response and uptime monitoring

feat: incidents improvements (#280)

Maximilian Kaske74012885cd89
diff --git a/apps/web/src/app/status-page/[domain]/_components/navigation-link.tsx b/apps/web/src/app/status-page/[domain]/_components/navigation-link.tsx
new file mode 100644
index 000000000..7408ff14f
--- /dev/null
+++ b/apps/web/src/app/status-page/[domain]/_components/navigation-link.tsx
@@ -0,0 +1,41 @@
+"use client";
+
+import Link from "next/link";
+import { usePathname, useSelectedLayoutSegment } from "next/navigation";
+
+import { Button } from "@/components/ui/button";
+
+export default function NavigationLink({
+ slug,
+ children,
+}: {
+ slug: string | null;
+ children: React.ReactNode;
+}) {
+ const pathname = usePathname();
+ const segment = useSelectedLayoutSegment();
+ const isActive = slug === segment;
+
+ // REMINDER: `/status-page/${params.domain}/${slug}` won't work for subdomain
+ let href = pathname;
+
+ if (!isActive) {
+ if (segment && slug) {
+ href = `${pathname.replace(segment, slug)}`;
+ } else if (segment) {
+ href = `${pathname.replace(segment, "")}`;
+ } else {
+ href = `${pathname}${pathname.endsWith("/") ? "" : "/"}${slug}`;
+ }
+ }
+
+ return (
+ <Button
+ asChild
+ variant={isActive ? "secondary" : "ghost"}
+ className={isActive ? "font-bold" : ""}
+ >
+ <Link href={href}>{children}</Link>
+ </Button>
+ );
+}
diff --git a/apps/web/src/app/status-page/[domain]/incidents/page.tsx b/apps/web/src/app/status-page/[domain]/incidents/page.tsx
new file mode 100644
index 000000000..473607979
--- /dev/null
+++ b/apps/web/src/app/status-page/[domain]/incidents/page.tsx
@@ -0,0 +1,66 @@
+import type { Metadata } from "next";
+import { notFound } from "next/navigation";
+
+import {
+ defaultMetadata,
+ ogMetadata,
+ twitterMetadata,
+} from "@/app/shared-metadata";
+import { Header } from "@/components/dashboard/header";
+import { IncidentList } from "@/components/status-page/incident-list";
+import { api } from "@/trpc/server";
+
+type Props = {
+ params: { domain: string };
+ searchParams: { [key: string]: string | string[] | undefined };
+};
+
+export default async function Page({ params }: Props) {
+ if (!params.domain) return notFound();
+
+ const page = await api.page.getPageBySlug.query({ slug: params.domain });
+ if (!page) return notFound();
+
+ return (
+ <div className="grid gap-6">
+ <Header
+ title={page.title}
+ description={page.description}
+ className="text-left"
+ />
+ <IncidentList incidents={page.incidents} monitors={page.monitors} />
+ </div>
+ );
+}
+
+export async function generateMetadata({ params }: Props): Promise<Metadata> {
+ const page = await api.page.getPageBySlug.query({ slug: params.domain });
+ const firstMonitor = page?.monitors?.[0]; // temporary solution
+
+ return {
+ ...defaultMetadata,
+ title: page?.title,
+ description: page?.description,
+ icons: page?.icon,
+ twitter: {
+ ...twitterMetadata,
+ images: [
+ `/api/og?monitorId=${firstMonitor?.id}&title=${page?.title}&description=${
+ page?.description || `The ${page?.title} status page}`
+ }`,
+ ],
+ title: page?.title,
+ description: page?.description,
+ },
+ openGraph: {
+ ...ogMetadata,
+ images: [
+ `/api/og?monitorId=${firstMonitor?.id}&title=${page?.title}&description=${
+ page?.description || `The ${page?.title} status page}`
+ }`,
+ ],
+ title: page?.title,
+ description: page?.description,
+ },
+ };
+}
diff --git a/apps/web/src/app/status-page/[domain]/layout.tsx b/apps/web/src/app/status-page/[domain]/layout.tsx
index d7eef968b..f88b1a017 100644
--- a/apps/web/src/app/status-page/[domain]/layout.tsx
+++ b/apps/web/src/app/status-page/[domain]/layout.tsx
@@ -1,3 +1,6 @@
+import { Shell } from "@/components/dashboard/shell";
+import NavigationLink from "./_components/navigation-link";
+
export default function StatusPageLayout({
children,
}: {
@@ -5,8 +8,16 @@ export default function StatusPageLayout({
}) {
return (
<div className="flex min-h-screen w-full flex-col space-y-6 p-4 md:p-8">
- <main className="flex flex-1 flex-col items-center justify-center gap-8">
- <div className="mx-auto w-full max-w-xl text-center">{children}</div>
+ <header className="mx-auto w-full max-w-xl">
+ <Shell className="mx-auto flex items-center justify-center gap-2 p-2 px-2 md:p-3">
+ <NavigationLink slug={null}>Status</NavigationLink>
+ <NavigationLink slug="incidents">Incidents</NavigationLink>
+ </Shell>
+ </header>
+ <main className="flex h-full w-full flex-1 flex-col">
+ <Shell className="mx-auto h-full max-w-xl flex-1 px-4 py-4">
+ {children}
+ </Shell>
</main>
<footer className="z-10">
<p className="text-muted-foreground text-center text-sm">
diff --git a/apps/web/src/app/status-page/[domain]/loading.tsx b/apps/web/src/app/status-page/[domain]/loading.tsx
index 43d1e619b..b8c2a380d 100644
--- a/apps/web/src/app/status-page/[domain]/loading.tsx
+++ b/apps/web/src/app/status-page/[domain]/loading.tsx
@@ -1,20 +1,17 @@
import { Container } from "@/components/dashboard/container";
import { Header } from "@/components/dashboard/header";
-import { Shell } from "@/components/dashboard/shell";
export default function StatusPageLoading() {
return (
- <Shell>
- <div className="grid gap-6">
- <Header.Skeleton />
- <div className="grid gap-4">
- <Container.Skeleton />
- <Container.Skeleton />
- </div>
- <div className="grid gap-4">
- <Container.Skeleton />
- </div>
+ <div className="grid gap-6">
+ <Header.Skeleton />
+ <div className="grid gap-4">
+ <Container.Skeleton />
+ <Container.Skeleton />
</div>
- </Shell>
+ <div className="grid gap-4">
+ <Container.Skeleton />
+ </div>
+ </div>
);
}
diff --git a/apps/web/src/app/status-page/[domain]/page.tsx b/apps/web/src/app/status-page/[domain]/page.tsx
index e6ff610d4..c25a3cd34 100644
--- a/apps/web/src/app/status-page/[domain]/page.tsx
+++ b/apps/web/src/app/status-page/[domain]/page.tsx
@@ -7,7 +7,6 @@ import {
twitterMetadata,
} from "@/app/shared-metadata";
import { Header } from "@/components/dashboard/header";
-import { Shell } from "@/components/dashboard/shell";
import { IncidentList } from "@/components/status-page/incident-list";
import { MonitorList } from "@/components/status-page/monitor-list";
import { api } from "@/trpc/server";
@@ -26,18 +25,21 @@ export default async function Page({ params }: Props) {
return notFound();
}
return (
- <Shell>
- <div className="grid gap-6">
- <Header
- title={page.title}
- description={page.description}
- className="mx-auto max-w-lg lg:mx-auto"
+ <div className="grid gap-6">
+ <Header
+ title={page.title}
+ description={page.description}
+ className="text-left"
+ />
+ <MonitorList monitors={page.monitors} />
+ {page.monitors?.length > 0 ? (
+ <IncidentList
+ incidents={page.incidents}
+ monitors={page.monitors}
+ context="latest"
/>
- {/* Think of having a tab to switch between monitors and incidents? */}
- <MonitorList monitors={page.monitors} />
- <IncidentList incidents={page.incidents} monitors={page.monitors} />
- </div>
- </Shell>
+ ) : null}
+ </div>
);
}
diff --git a/apps/web/src/components/incidents/affected-monitors.tsx b/apps/web/src/components/incidents/affected-monitors.tsx
index c3773b2de..5ae5545fa 100644
--- a/apps/web/src/components/incidents/affected-monitors.tsx
+++ b/apps/web/src/components/incidents/affected-monitors.tsx
@@ -2,19 +2,19 @@ import type * as z from "zod";
import type { selectPublicMonitorSchema } from "@openstatus/db/src/schema";
+import { Badge } from "../ui/badge";
+
export function AffectedMonitors({
monitors,
}: {
monitors: z.infer<typeof selectPublicMonitorSchema>[];
}) {
return (
- <ul role="list">
+ <ul role="list" className="flex gap-2">
{monitors.length > 0 ? (
monitors.map(({ name, url }, i) => (
<li key={i} className="text-xs">
- <span className="text-sm font-medium">{name}</span>
- <span className="text-muted-foreground/70 mx-1">&bull;</span>
- <span className="text-muted-foreground font-mono">{url}</span>
+ <Badge variant="secondary">{name}</Badge>
</li>
))
) : (
diff --git a/apps/web/src/components/incidents/events.tsx b/apps/web/src/components/incidents/events.tsx
index 1a64fad6d..f8f837f89 100644
--- a/apps/web/src/components/incidents/events.tsx
+++ b/apps/web/src/components/incidents/events.tsx
@@ -2,14 +2,19 @@
import * as React from "react";
import { useRouter } from "next/navigation";
-import { format } from "date-fns";
+import { format, formatDistance } from "date-fns";
import type * as z from "zod";
import type { selectIncidentUpdateSchema } from "@openstatus/db/src/schema";
import { Icons } from "@/components/icons";
-import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipProvider,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
import { statusDict } from "@/data/incidents-dictionary";
import { useProcessor } from "@/hooks/use-preprocessor";
import { cn } from "@/lib/utils";
@@ -42,45 +47,61 @@ export function Events({
return (
<div className="grid gap-3">
- {slicedArray?.map((update) => {
+ {slicedArray?.map((update, i) => {
const { icon, label } = statusDict[update.status];
const StatusIcon = Icons[icon];
return (
<div
key={update.id}
className={cn(
- "group relative -m-2 grid gap-2 border border-transparent p-2",
- editable &&
- "hover:bg-accent/40 hover:border-border hover:rounded-lg",
+ "group relative -m-2 flex gap-4 border border-transparent p-2",
+ editable && "hover:bg-accent/40 hover:rounded-lg",
)}
>
- {editable ? (
- <div className="absolute right-2 top-2 hidden gap-2 group-hover:flex group-active:flex">
- <Button
- size="icon"
- variant="outline"
- className="h-7 w-7 p-0"
- onClick={() => {
- router.push(
- `./incidents/update/edit?incidentId=${update.incidentId}&id=${update.id}`,
- );
- }}
- >
- <Icons.pencil className="h-4 w-4" />
- </Button>
- <DeleteIncidentUpdateButtonIcon id={update.id} />
+ <div className="relative">
+ <div className="bg-background border-border rounded-full border p-2">
+ <StatusIcon className="h-4 w-4" />
</div>
- ) : undefined}
- <div className="text-muted-foreground flex items-center text-xs font-light">
- {format(new Date(update.date), "LLL dd, y HH:mm")}
- <span className="text-muted-foreground/70 mx-1">&bull;</span>
- <Badge variant="secondary">
- <StatusIcon className="mr-1 h-3 w-3" />
- {label}
- </Badge>
+ {i !== sortedArray.length - 1 ? (
+ <div className="bg-muted absolute inset-x-0 mx-auto h-full w-[2px]" />
+ ) : null}
+ </div>
+ <div className="mt-1 grid flex-1 gap-3">
+ {editable ? (
+ <div className="absolute bottom-2 right-2 hidden gap-2 group-hover:flex group-active:flex">
+ <Button
+ size="icon"
+ variant="outline"
+ className="h-7 w-7 p-0"
+ onClick={() => {
+ router.push(
+ `./incidents/update/edit?incidentId=${update.incidentId}&id=${update.id}`,
+ );
+ }}
+ >
+ <Icons.pencil className="h-4 w-4" />
+ </Button>
+ <DeleteIncidentUpdateButtonIcon id={update.id} />
+ </div>
+ ) : undefined}
+ <div className="flex items-center justify-between gap-4">
+ <p className="text-sm font-semibold">{label}</p>
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger className="text-muted-foreground text-xs font-light">
+ {formatDistance(new Date(update.date), new Date(), {
+ addSuffix: true,
+ })}
+ </TooltipTrigger>
+ <TooltipContent>
+ <p>{format(new Date(update.date), "LLL dd, y HH:mm")}</p>
+ </TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ </div>
+ {/* <p className="max-w-3xl text-sm">{update.message}</p> */}
+ <EventMessage message={update.message} />
</div>
- {/* <p className="max-w-3xl text-sm">{update.message}</p> */}
- <EventMessage message={update.message} />
</div>
);
})}
@@ -88,7 +109,7 @@ export function Events({
{incidentUpdates.length > 1 ? (
<div className="text-center">
<Button variant="ghost" onClick={toggle}>
- {open ? "Close all" : "All updates"}
+ {open ? "Close" : "More"}
</Button>
</div>
) : null}
diff --git a/apps/web/src/components/status-page/incident-list.tsx b/apps/web/src/components/status-page/incident-list.tsx
index 765925b82..80a828b97 100644
--- a/apps/web/src/components/status-page/incident-list.tsx
+++ b/apps/web/src/components/status-page/incident-list.tsx
@@ -15,23 +15,36 @@ import { StatusBadge } from "../incidents/status-badge";
export const IncidentList = ({
incidents,
monitors,
+ context = "all",
}: {
incidents: z.infer<typeof selectIncidentsPageSchema>;
monitors: z.infer<typeof selectPublicMonitorSchema>[];
+ context?: "all" | "latest"; // latest 7 days
}) => {
- // TBD
- // const currentIncidents = incidents.filter(
- // ({ status }) => status !== "resolved",
- // );
+ const lastWeek = Date.now() - 1000 * 60 * 60 * 24 * 7;
+
+ function getLastWeeksIncidents() {
+ return incidents.filter((incident) => {
+ return incident.incidentUpdates.some(
+ (update) => update.date.getTime() > lastWeek,
+ );
+ });
+ }
+
+ const _incidents = context === "all" ? incidents : getLastWeeksIncidents();
return (
<>
- {incidents?.length > 0 ? (
+ {_incidents.sort((a, b) => {
+ if (a.updatedAt == undefined) return 1;
+ if (b.updatedAt == undefined) return -1;
+ return b.updatedAt.getTime() - a.updatedAt.getTime();
+ })?.length > 0 ? (
<div className="grid gap-4">
<h2 className="text-muted-foreground text-lg font-light">
- All Incidents
+ {context === "all" ? "All incidents" : "Latest incidents"}
</h2>
- {incidents.map((incident) => {
+ {_incidents.map((incident) => {
return (
<div key={incident.id} className="grid gap-4 text-left">
<div className="max-w-3xl font-semibold">
@@ -39,7 +52,7 @@ export const IncidentList = ({
<StatusBadge status={incident.status} />
</div>
<div className="overflow-hidden text-ellipsis">
- <p className="text-muted-foreground mb-1 text-xs">
+ <p className="text-muted-foreground mb-2 text-xs">
Affected Monitors
</p>
<AffectedMonitors
@@ -54,7 +67,7 @@ export const IncidentList = ({
/>
</div>
<div>
- <p className="text-muted-foreground mb-1 text-xs">
+ <p className="text-muted-foreground mb-2 text-xs">
Latest Updates
</p>
<Events incidentUpdates={incident.incidentUpdates} />
@@ -64,9 +77,11 @@ export const IncidentList = ({
})}
</div>
) : (
- <h2 className="text-muted-foreground text-lg font-light">
- No Incidents
- </h2>
+ <p className="text-muted-foreground text-center text-sm font-light">
+ {context === "all"
+ ? "No incidents."
+ : "No incidents in the last week."}
+ </p>
)}
</>
);
diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts
index 892ed4afe..1a002f1a5 100644
--- a/apps/web/src/middleware.ts
+++ b/apps/web/src/middleware.ts
@@ -52,6 +52,9 @@ export const getValidSubdomain = (host?: string | null) => {
return subdomain;
};
+// used when trying subdomain slug via status.localhost:3000/incidents
+const publicRoutesDev = ["/incidents"];
+
export default authMiddleware({
publicRoutes: [
"/",
@@ -68,7 +71,8 @@ export default authMiddleware({
"/discord",
"/github",
"/oss-friends",
- ],
+ "/status-page/(.*)",
+ ].concat(process.env.NODE_ENV === "development" ? publicRoutesDev : []),
ignoredRoutes: ["/api/og", "/discord", "github"], // FIXME: we should check the `publicRoutes`
beforeAuth: before,
async afterAuth(auth, req) {
diff --git a/packages/api/src/router/incident.ts b/packages/api/src/router/incident.ts
index f75da9b3c..2aa7bbe3f 100644
--- a/packages/api/src/router/incident.ts
+++ b/packages/api/src/router/incident.ts
@@ -81,7 +81,7 @@ export const incidentRouter = createTRPCRouter({
// update parent incident with latest status
await opts.ctx.db
.update(incident)
- .set({ status: opts.input.status })
+ .set({ status: opts.input.status, updatedAt: new Date() })
.where(eq(incident.id, opts.input.incidentId))
.returning()
.get();
@@ -333,7 +333,7 @@ export const incidentRouter = createTRPCRouter({
],
},
},
- orderBy: (incident, { desc }) => [desc(incident.createdAt)],
+ orderBy: (incident, { desc }) => [desc(incident.updatedAt)],
});
return z.array(selectIncidentSchemaWithRelation).parse(result);