Skip to content

Open-source status page, incident response and uptime monitoring

feat: improve notifications (#910)

Maximilian Kaske77b0c6296d84
diff --git a/apps/web/src/components/data-table/data-table-badges.tsx b/apps/web/src/components/data-table/data-table-badges.tsx
new file mode 100644
index 000000000..ca4aa2c61
--- /dev/null
+++ b/apps/web/src/components/data-table/data-table-badges.tsx
@@ -0,0 +1,41 @@
+import {
+ Badge,
+ Tooltip,
+ TooltipContent,
+ TooltipProvider,
+ TooltipTrigger,
+} from "@openstatus/ui";
+
+export function DataTableBadges({ names }: { names: string[] }) {
+ const [first, second, ...rest] = names;
+
+ if (names.length === 0) return null;
+
+ return (
+ <div className="flex items-center gap-2">
+ <span className="flex max-w-[150px] gap-2 truncate font-medium lg:max-w-[250px] sm:max-w-[200px] xl:max-w-[350px]">
+ <Badge variant="outline">{first}</Badge>
+ {second ? <Badge variant="outline">{second}</Badge> : null}
+ </span>
+ {rest.length > 0 ? (
+ <TooltipProvider>
+ <Tooltip delayDuration={200}>
+ <TooltipTrigger>
+ <Badge variant="secondary" className="border">
+ +{rest.length}
+ </Badge>
+ </TooltipTrigger>
+ <TooltipContent side="top" className="flex gap-2">
+ {rest.map((name, i) => (
+ // biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
+ <Badge key={i} variant="outline">
+ {name}
+ </Badge>
+ ))}
+ </TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ ) : null}
+ </div>
+ );
+}
diff --git a/apps/web/src/components/data-table/monitor/columns.tsx b/apps/web/src/components/data-table/monitor/columns.tsx
index 2a566d962..ac240bf8b 100644
--- a/apps/web/src/components/data-table/monitor/columns.tsx
+++ b/apps/web/src/components/data-table/monitor/columns.tsx
@@ -213,7 +213,8 @@ export const columns: ColumnDef<{
sortingFn: (rowA, rowB, columnId) => {
const valueA = rowA.getValue(columnId) as number | undefined;
const valueB = rowB.getValue(columnId) as number | undefined;
- if (!valueA || !valueB) return 0;
+ if (!valueB) return valueA || 1;
+ if (!valueA) return -valueB;
return valueA - valueB;
},
},
@@ -231,7 +232,8 @@ export const columns: ColumnDef<{
sortingFn: (rowA, rowB, columnId) => {
const valueA = rowA.getValue(columnId) as number | undefined;
const valueB = rowB.getValue(columnId) as number | undefined;
- if (!valueA || !valueB) return 0;
+ if (!valueB) return valueA || 1;
+ if (!valueA) return -valueB;
return valueA - valueB;
},
},
@@ -249,7 +251,8 @@ export const columns: ColumnDef<{
sortingFn: (rowA, rowB, columnId) => {
const valueA = rowA.getValue(columnId) as number | undefined;
const valueB = rowB.getValue(columnId) as number | undefined;
- if (!valueA || !valueB) return 0;
+ if (!valueB) return valueA || 1;
+ if (!valueA) return -valueB;
return valueA - valueB;
},
},
@@ -267,7 +270,8 @@ export const columns: ColumnDef<{
sortingFn: (rowA, rowB, columnId) => {
const valueA = rowA.getValue(columnId) as number | undefined;
const valueB = rowB.getValue(columnId) as number | undefined;
- if (!valueA || !valueB) return 0;
+ if (!valueB) return valueA || 1;
+ if (!valueA) return -valueB;
return valueA - valueB;
},
},
@@ -285,7 +289,8 @@ export const columns: ColumnDef<{
sortingFn: (rowA, rowB, columnId) => {
const valueA = rowA.getValue(columnId) as number | undefined;
const valueB = rowB.getValue(columnId) as number | undefined;
- if (!valueA || !valueB) return 0;
+ if (!valueB) return valueA || 1;
+ if (!valueA) return -valueB;
return valueA - valueB;
},
},
diff --git a/apps/web/src/components/data-table/monitor/data-table-toolbar.tsx b/apps/web/src/components/data-table/monitor/data-table-toolbar.tsx
index da1a0a0ad..b3f2ee472 100644
--- a/apps/web/src/components/data-table/monitor/data-table-toolbar.tsx
+++ b/apps/web/src/components/data-table/monitor/data-table-toolbar.tsx
@@ -71,9 +71,10 @@ export function DataTableToolbar<TData>({
</Button>
)}
</div>
- <div className="flex h-8 items-center self-end rounded-lg border border-dashed bg-muted/50 px-3">
+ <div className="flex items-center self-end rounded-lg border border-dashed bg-muted/50 px-3 py-2">
<p className="text-muted-foreground text-xs">
- Quantiles and Uptime are aggregated data from the last 24h.
+ Quantiles and Uptime are aggregated data from the{" "}
+ <span className="text-foreground">last 24h</span>.
</p>
</div>
</div>
diff --git a/apps/web/src/components/data-table/notification/columns.tsx b/apps/web/src/components/data-table/notification/columns.tsx
index 02edd649d..4436a3904 100644
--- a/apps/web/src/components/data-table/notification/columns.tsx
+++ b/apps/web/src/components/data-table/notification/columns.tsx
@@ -2,17 +2,36 @@
import type { ColumnDef } from "@tanstack/react-table";
-import type { Notification } from "@openstatus/db/src/schema";
+import type { Monitor, Notification } from "@openstatus/db/src/schema";
import { Badge } from "@openstatus/ui";
import { DataTableRowActions } from "./data-table-row-actions";
+import { z } from "zod";
+import { DataTableBadges } from "../data-table-badges";
+import Link from "next/link";
// TODO: use the getProviderMetaData function from the notification form to access the data
-export const columns: ColumnDef<Notification>[] = [
+export const columns: ColumnDef<
+ Notification & { monitor: { monitor: Monitor }[] }
+>[] = [
{
accessorKey: "name",
header: "Name",
+ cell: ({ row }) => {
+ const { name } = row.original;
+ return (
+ <div className="flex gap-2">
+ <Link
+ href={`./notifications/${row.original.id}/edit`}
+ className="group flex max-w-full items-center gap-2"
+ prefetch={false}
+ >
+ <span className="truncate group-hover:underline">{name}</span>
+ </Link>
+ </div>
+ );
+ },
},
{
accessorKey: "provider",
@@ -25,10 +44,22 @@ export const columns: ColumnDef<Notification>[] = [
);
},
},
- // {
- // accessorKey: "data",
- // header: "Data",
- // },
+ {
+ accessorKey: "monitor",
+ header: "Monitors",
+ cell: ({ row }) => {
+ const monitor = row.getValue("monitor");
+ const monitors = z
+ .object({ monitor: z.object({ name: z.string() }) })
+ .array()
+ .parse(monitor);
+ return (
+ <DataTableBadges
+ names={monitors.map((monitor) => monitor.monitor.name)}
+ />
+ );
+ },
+ },
{
id: "actions",
cell: ({ row }) => {
diff --git a/apps/web/src/components/data-table/status-page/columns.tsx b/apps/web/src/components/data-table/status-page/columns.tsx
index bedffdaa5..db66a4087 100644
--- a/apps/web/src/components/data-table/status-page/columns.tsx
+++ b/apps/web/src/components/data-table/status-page/columns.tsx
@@ -7,7 +7,6 @@ import * as z from "zod";
import type { Maintenance, Page } from "@openstatus/db/src/schema";
import {
- Badge,
Tooltip,
TooltipContent,
TooltipProvider,
@@ -16,6 +15,7 @@ import {
import { ArrowUpRight, Check } from "lucide-react";
import { DataTableRowActions } from "./data-table-row-actions";
+import { DataTableBadges } from "../data-table-badges";
export const columns: ColumnDef<
Page & {
@@ -28,35 +28,14 @@ export const columns: ColumnDef<
header: "Title",
cell: ({ row }) => {
return (
- <div className="flex items-center gap-1">
- <Link
- href={`./status-pages/${row.original.id}/edit`}
- className="group flex items-center gap-2"
- >
- <span className="max-w-[125px] truncate group-hover:underline">
- {row.getValue("title")}
- </span>
- </Link>
- <TooltipProvider>
- <Tooltip delayDuration={100}>
- <TooltipTrigger>
- <a
- href={
- process.env.NODE_ENV === "production"
- ? `https://${row.original.slug}.openstatus.dev`
- : `/status-page/${row.original.slug}`
- }
- target="_blank"
- rel="noreferrer"
- className="text-muted-foreground hover:text-foreground"
- >
- <ArrowUpRight className="h-4 w-4 flex-shrink-0" />
- </a>
- </TooltipTrigger>
- <TooltipContent>Visit page</TooltipContent>
- </Tooltip>
- </TooltipProvider>
- </div>
+ <Link
+ href={`./status-pages/${row.original.id}/edit`}
+ className="group flex items-center gap-2"
+ >
+ <span className="max-w-[125px] truncate group-hover:underline">
+ {row.getValue("title")}
+ </span>
+ </Link>
);
},
},
@@ -64,7 +43,30 @@ export const columns: ColumnDef<
accessorKey: "slug",
header: "Slug",
cell: ({ row }) => {
- return <span className="font-mono">{row.getValue("slug")}</span>;
+ return (
+ <TooltipProvider>
+ <Tooltip delayDuration={100}>
+ <TooltipTrigger>
+ <a
+ href={
+ process.env.NODE_ENV === "production"
+ ? `https://${row.original.slug}.openstatus.dev`
+ : `/status-page/${row.original.slug}`
+ }
+ target="_blank"
+ rel="noreferrer"
+ className="group flex items-center gap-1"
+ >
+ <span className="max-w-[125px] truncate font-mono group-hover:underline">
+ {row.getValue("slug")}
+ </span>
+ <ArrowUpRight className="h-4 w-4 flex-shrink-0 text-muted-foreground group-hover:text-foreground" />
+ </a>
+ </TooltipTrigger>
+ <TooltipContent>Visit page</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ );
},
},
{
@@ -76,38 +78,10 @@ export const columns: ColumnDef<
.object({ monitor: z.object({ name: z.string() }) })
.array()
.parse(monitorsToPages);
- const firstMonitors = monitors.splice(0, 2);
- const lastMonitors = monitors;
return (
- <div className="flex items-center gap-2">
- <span className="flex max-w-[150px] gap-2 truncate font-medium lg:max-w-[250px] sm:max-w-[200px] xl:max-w-[350px]">
- {firstMonitors.map(({ monitor: { name } }, i) => (
- // biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
- <Badge key={i} variant="outline">
- {name}
- </Badge>
- ))}
- </span>
- {lastMonitors.length > 0 ? (
- <TooltipProvider>
- <Tooltip delayDuration={200}>
- <TooltipTrigger>
- <Badge variant="secondary" className="border">
- +{lastMonitors.length}
- </Badge>
- </TooltipTrigger>
- <TooltipContent side="top" className="flex gap-2">
- {lastMonitors.map(({ monitor: { name } }, i) => (
- // biome-ignore lint/suspicious/noArrayIndexKey: <explanation>
- <Badge key={i} variant="outline">
- {name}
- </Badge>
- ))}
- </TooltipContent>
- </Tooltip>
- </TooltipProvider>
- ) : null}
- </div>
+ <DataTableBadges
+ names={monitors.map((monitor) => monitor.monitor.name)}
+ />
);
},
},
diff --git a/packages/api/src/router/notification.ts b/packages/api/src/router/notification.ts
index 7b0ac4595..f34e5873f 100644
--- a/packages/api/src/router/notification.ts
+++ b/packages/api/src/router/notification.ts
@@ -6,6 +6,7 @@ import {
NotificationDataSchema,
insertNotificationSchema,
notification,
+ selectMonitorSchema,
selectNotificationSchema,
} from "@openstatus/db/src/schema";
import { getLimit } from "@openstatus/plans";
@@ -23,7 +24,7 @@ export const notificationRouter = createTRPCRouter({
const notificationLimit = getLimit(
opts.ctx.workspace.plan,
- "notification-channels"
+ "notification-channels",
);
const notificationNumber = (
@@ -85,8 +86,8 @@ export const notificationRouter = createTRPCRouter({
.where(
and(
eq(notification.id, opts.input.id),
- eq(notification.workspaceId, opts.ctx.workspace.id)
- )
+ eq(notification.workspaceId, opts.ctx.workspace.id),
+ ),
)
.returning()
.get();
@@ -100,8 +101,8 @@ export const notificationRouter = createTRPCRouter({
.where(
and(
eq(notification.id, opts.input.id),
- eq(notification.id, opts.input.id)
- )
+ eq(notification.id, opts.input.id),
+ ),
)
.run();
}),
@@ -116,8 +117,8 @@ export const notificationRouter = createTRPCRouter({
and(
eq(notification.id, opts.input.id),
eq(notification.id, opts.input.id),
- eq(notification.workspaceId, opts.ctx.workspace.id)
- )
+ eq(notification.workspaceId, opts.ctx.workspace.id),
+ ),
)
.get();
@@ -125,19 +126,29 @@ export const notificationRouter = createTRPCRouter({
}),
getNotificationsByWorkspace: protectedProcedure.query(async (opts) => {
- const notifications = await opts.ctx.db
- .select()
- .from(notification)
- .where(eq(notification.workspaceId, opts.ctx.workspace.id))
- .all();
-
- return z.array(selectNotificationSchema).parse(notifications);
+ const notifications = await opts.ctx.db.query.notification.findMany({
+ where: and(eq(notification.workspaceId, opts.ctx.workspace.id)),
+ with: {
+ // FIXME: first should be plurals!
+ monitor: { with: { monitor: true } },
+ },
+ });
+
+ const schema = selectNotificationSchema.extend({
+ monitor: z.array(
+ z.object({
+ monitor: selectMonitorSchema,
+ }),
+ ),
+ });
+
+ return z.array(schema).parse(notifications);
}),
isNotificationLimitReached: protectedProcedure.query(async (opts) => {
const notificationLimit = getLimit(
opts.ctx.workspace.plan,
- "notification-channels"
+ "notification-channels",
);
const notificationNumbers = (
await opts.ctx.db.query.notification.findMany({
diff --git a/packages/db/src/schema/shared.ts b/packages/db/src/schema/shared.ts
index e1a8570a7..5cb511402 100644
--- a/packages/db/src/schema/shared.ts
+++ b/packages/db/src/schema/shared.ts
@@ -1,10 +1,7 @@
import { z } from "zod";
import { selectIncidentSchema } from "./incidents/validation";
-import {
- maintenancesToMonitors,
- selectMaintenanceSchema,
-} from "./maintenances";
+import { selectMaintenanceSchema } from "./maintenances";
import { selectMonitorSchema } from "./monitors";
import { selectPageSchema } from "./pages";
import {