Skip to content

Open-source status page, incident response and uptime monitoring

feat: add notification channel limit (#564)

Maximilian Kaske384236eedc48
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/page.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/page.tsx
index e527b79d8..ac38713c3 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/page.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/notifications/page.tsx
@@ -1,7 +1,7 @@
import * as React from "react";
import Link from "next/link";
-import { Button } from "@openstatus/ui";
+import { ButtonWithDisableTooltip } from "@openstatus/ui";
import { Header } from "@/components/dashboard/header";
import { HelpCallout } from "@/components/dashboard/help-callout";
@@ -10,13 +10,15 @@ import { DataTable } from "@/components/data-table/notification/data-table";
import { api } from "@/trpc/server";
import { EmptyState } from "./_components/empty-state";
-export default async function MonitorPage({
+export default async function NotificationPage({
params,
}: {
params: { workspaceSlug: string };
}) {
const notifications =
await api.notification.getNotificationsByWorkspace.query();
+ const isLimitReached =
+ await api.notification.isNotificationLimitReached.query();
return (
<div className="grid min-h-full grid-cols-1 grid-rows-[auto,1fr,auto] gap-6 md:grid-cols-2 md:gap-8">
@@ -24,9 +26,13 @@ export default async function MonitorPage({
title="Notifications"
description="Overview of all your notification channels."
actions={
- <Button asChild>
+ <ButtonWithDisableTooltip
+ tooltip="You reached the limits"
+ asChild={!isLimitReached}
+ disabled={isLimitReached}
+ >
<Link href="./notifications/edit">Create</Link>
- </Button>
+ </ButtonWithDisableTooltip>
}
/>
{notifications && notifications.length > 0 ? (
diff --git a/apps/web/src/components/forms/monitor-form.tsx b/apps/web/src/components/forms/monitor-form.tsx
index 920701272..cd1c8da83 100644
--- a/apps/web/src/components/forms/monitor-form.tsx
+++ b/apps/web/src/components/forms/monitor-form.tsx
@@ -18,7 +18,7 @@ import {
monitorMethodsSchema,
monitorPeriodicitySchema,
} from "@openstatus/db/src/schema";
-import { allPlans } from "@openstatus/plans";
+import { getLimit } from "@openstatus/plans";
import {
Accordion,
AccordionContent,
@@ -207,7 +207,11 @@ export function MonitorForm({
});
};
- const limit = allPlans[plan].limits.periodicity;
+ const periodicityLimit = getLimit(plan, "periodicity");
+ const notificationLimit = getLimit(plan, "notification-channels");
+ const notificationLimitReached = notifications
+ ? notifications.length >= notificationLimit
+ : false;
return (
<Dialog open={openDialog} onOpenChange={(val) => setOpenDialog(val)}>
@@ -446,7 +450,7 @@ export function MonitorForm({
<SelectItem
key={value}
value={value}
- disabled={!limit.includes(value)}
+ disabled={!periodicityLimit.includes(value)}
>
{label}
</SelectItem>
@@ -662,7 +666,10 @@ export function MonitorForm({
<FormMessage />
<div className="sm:col-span-2 sm:col-start-1">
<DialogTrigger asChild>
- <Button variant="outline">
+ <Button
+ variant="outline"
+ disabled={notificationLimitReached}
+ >
Add Notifications
</Button>
</DialogTrigger>
diff --git a/apps/web/src/components/forms/notification-form.tsx b/apps/web/src/components/forms/notification-form.tsx
index 62d1c30cf..5fc63c0fe 100644
--- a/apps/web/src/components/forms/notification-form.tsx
+++ b/apps/web/src/components/forms/notification-form.tsx
@@ -56,6 +56,7 @@ function getProviderMetaData(provider: NotificationProvider) {
placeholder: "[email protected]",
setupDocLink: null,
sendTest: null,
+ plans: ["free", "starter", "pro", "team"],
};
case "slack":
@@ -65,6 +66,7 @@ function getProviderMetaData(provider: NotificationProvider) {
setupDocLink:
"https://api.slack.com/messaging/webhooks#getting_started",
sendTest: sendTestSlackMessage,
+ plans: ["free", "starter", "pro", "team"],
};
case "discord":
@@ -73,6 +75,7 @@ function getProviderMetaData(provider: NotificationProvider) {
placeholder: "https://discord.com/api/webhooks/{channelId}/xxx...",
setupDocLink: "https://support.discord.com/hc/en-us/articles/228383668",
sendTest: sendTestDiscordMessage,
+ plans: ["free", "starter", "pro", "team"],
};
case "sms":
return {
@@ -80,7 +83,7 @@ function getProviderMetaData(provider: NotificationProvider) {
placeholder: "+123456789",
setupDocLink: null,
sendTest: null,
- isPro: true,
+ plans: ["pro", "team"],
};
default:
@@ -89,6 +92,7 @@ function getProviderMetaData(provider: NotificationProvider) {
placeholder: "xxxx",
setupDocLink: `https://docs.openstatus.dev/integrations/${provider}`,
send: null,
+ plans: ["free", "starter", "pro", "team"],
};
}
}
@@ -194,15 +198,22 @@ export function NotificationForm({
</SelectTrigger>
</FormControl>
<SelectContent>
- {notificationProvider.map((provider) => (
- <SelectItem
- key={provider}
- value={provider}
- className="capitalize"
- >
- {provider}
- </SelectItem>
- ))}
+ {notificationProvider.map((provider) => {
+ const isIncluded =
+ getProviderMetaData(provider).plans?.includes(
+ workspacePlan,
+ );
+ return (
+ <SelectItem
+ key={provider}
+ value={provider}
+ className="capitalize"
+ disabled={!isIncluded}
+ >
+ {provider}
+ </SelectItem>
+ );
+ })}
</SelectContent>
</Select>
<FormDescription>
@@ -244,7 +255,7 @@ export function NotificationForm({
placeholder={providerMetaData.placeholder}
{...field}
disabled={
- providerMetaData.isPro && workspacePlan !== "pro"
+ !providerMetaData.plans?.includes(workspacePlan)
}
/>
</FormControl>
diff --git a/packages/api/src/router/notification.ts b/packages/api/src/router/notification.ts
index de9e43121..dd195361e 100644
--- a/packages/api/src/router/notification.ts
+++ b/packages/api/src/router/notification.ts
@@ -1,3 +1,4 @@
+import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { and, eq } from "@openstatus/db";
@@ -6,6 +7,7 @@ import {
notification,
selectNotificationSchema,
} from "@openstatus/db/src/schema";
+import { getLimit } from "@openstatus/plans";
import { trackNewNotification } from "../analytics";
import { createTRPCRouter, protectedProcedure } from "../trpc";
@@ -16,6 +18,25 @@ export const notificationRouter = createTRPCRouter({
.mutation(async (opts) => {
const { ...data } = opts.input;
+ const notificationLimit = getLimit(
+ opts.ctx.workspace.plan,
+ "notification-channels",
+ );
+
+ const notificationNumber = (
+ await opts.ctx.db.query.notification.findMany({
+ where: eq(notification.workspaceId, opts.ctx.workspace.id),
+ })
+ ).length;
+
+ // the user has reached the limits
+ if (notificationNumber >= notificationLimit) {
+ throw new TRPCError({
+ code: "FORBIDDEN",
+ message: "You reached your notification limits.",
+ });
+ }
+
const _notification = await opts.ctx.db
.insert(notification)
.values({ ...data, workspaceId: opts.ctx.workspace.id })
@@ -88,4 +109,18 @@ export const notificationRouter = createTRPCRouter({
return z.array(selectNotificationSchema).parse(notifications);
}),
+
+ isNotificationLimitReached: protectedProcedure.query(async (opts) => {
+ const notificationLimit = getLimit(
+ opts.ctx.workspace.plan,
+ "notification-channels",
+ );
+ const notificationNumbers = (
+ await opts.ctx.db.query.notification.findMany({
+ where: eq(notification.workspaceId, opts.ctx.workspace.id),
+ })
+ ).length;
+
+ return notificationNumbers >= notificationLimit;
+ }),
});
diff --git a/packages/plans/src/utils.ts b/packages/plans/src/utils.ts
index 2016eeea9..8a51d170d 100644
--- a/packages/plans/src/utils.ts
+++ b/packages/plans/src/utils.ts
@@ -4,6 +4,9 @@ import { allPlans } from "./index";
import type { Limits } from "./types";
// TODO: use getLimit utils function
-export function getLimit(plan: WorkspacePlan, limit: keyof Limits) {
+export function getLimit<T extends keyof Limits>(
+ plan: WorkspacePlan,
+ limit: T,
+) {
return allPlans[plan].limits[limit];
}