Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add notification settings page and sidebar/header nav entries

Cursor Agent18bd3c78188c
diff --git a/components/settings/settings-header.tsx b/components/settings/settings-header.tsx
index 2756fd81..eeefff99 100644
--- a/components/settings/settings-header.tsx
+++ b/components/settings/settings-header.tsx
@@ -57,6 +57,11 @@ export function SettingsHeader() {
href: `/settings/webhooks`,
segment: "webhooks",
},
+ {
+ label: "Notifications",
+ href: `/settings/notifications`,
+ segment: "notifications",
+ },
{
label: "Slack",
href: `/settings/slack`,
diff --git a/components/sidebar/app-sidebar.tsx b/components/sidebar/app-sidebar.tsx
index 9471855c..fa2508e4 100644
--- a/components/sidebar/app-sidebar.tsx
+++ b/components/sidebar/app-sidebar.tsx
@@ -195,6 +195,11 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
url: "/settings/domains",
current: router.pathname.includes("settings/domains"),
},
+ {
+ title: "Notifications",
+ url: "/settings/notifications",
+ current: router.pathname.includes("settings/notifications"),
+ },
{
title: "Webhooks",
url: "/settings/webhooks",
diff --git a/lib/swr/use-notification-preferences.ts b/lib/swr/use-notification-preferences.ts
new file mode 100644
index 00000000..afaef2b5
--- /dev/null
+++ b/lib/swr/use-notification-preferences.ts
@@ -0,0 +1,60 @@
+import { useTeam } from "@/context/team-context";
+import useSWR from "swr";
+
+import type {
+ TeamNotificationFrequency,
+ TeamNotificationType,
+} from "@/lib/zod/schemas/notifications";
+import { fetcher } from "@/lib/utils";
+
+interface NotificationPreferencesResponse {
+ preferences: Record<TeamNotificationType, TeamNotificationFrequency>;
+ role: string;
+}
+
+export function useNotificationPreferences() {
+ const teamInfo = useTeam();
+ const teamId = teamInfo?.currentTeam?.id;
+
+ const { data, error, mutate } = useSWR<NotificationPreferencesResponse>(
+ teamId
+ ? `/api/teams/${teamId}/notifications/preferences`
+ : null,
+ fetcher,
+ {
+ dedupingInterval: 10000,
+ },
+ );
+
+ const updatePreferences = async (
+ preferences: { type: TeamNotificationType; frequency: TeamNotificationFrequency }[],
+ ) => {
+ if (!teamId) return;
+
+ const response = await fetch(
+ `/api/teams/${teamId}/notifications/preferences`,
+ {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ preferences }),
+ },
+ );
+
+ if (!response.ok) {
+ throw new Error("Failed to update notification preferences");
+ }
+
+ const result = await response.json();
+ mutate({ ...data, preferences: result.preferences } as NotificationPreferencesResponse, false);
+ return result;
+ };
+
+ return {
+ preferences: data?.preferences,
+ role: data?.role,
+ isLoading: !data && !error,
+ error,
+ mutate,
+ updatePreferences,
+ };
+}
diff --git a/pages/settings/notifications.tsx b/pages/settings/notifications.tsx
new file mode 100644
index 00000000..9b2d89c6
--- /dev/null
+++ b/pages/settings/notifications.tsx
@@ -0,0 +1,239 @@
+import {
+ EyeIcon,
+ FileUpIcon,
+ InfoIcon,
+ MessageSquareIcon,
+ ServerIcon,
+ ShieldAlertIcon,
+} from "lucide-react";
+import { toast } from "sonner";
+
+import { useNotificationPreferences } from "@/lib/swr/use-notification-preferences";
+import type {
+ TeamNotificationFrequency,
+ TeamNotificationType,
+} from "@/lib/zod/schemas/notifications";
+
+import AppLayout from "@/components/layouts/app";
+import { SettingsHeader } from "@/components/settings/settings-header";
+import { Alert, AlertDescription } from "@/components/ui/alert";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
+
+const FREQUENCY_LABELS: Record<TeamNotificationFrequency, string> = {
+ IMMEDIATE: "Immediate",
+ DAILY: "Daily digest",
+ WEEKLY: "Weekly digest",
+ NEVER: "Off",
+};
+
+type NotificationConfig = {
+ type: TeamNotificationType;
+ title: string;
+ descriptionAdmin: string;
+ descriptionMember: string;
+ icon: React.ElementType;
+};
+
+type NotificationCategory = {
+ label: string;
+ items: NotificationConfig[];
+};
+
+const NOTIFICATION_CATEGORIES: NotificationCategory[] = [
+ {
+ label: "Documents",
+ items: [
+ {
+ type: "DOCUMENT_VIEW",
+ title: "Document viewed",
+ descriptionAdmin: "When someone views any document link",
+ descriptionMember: "When someone views a document link you own",
+ icon: EyeIcon,
+ },
+ ],
+ },
+ {
+ label: "Data rooms",
+ items: [
+ {
+ type: "DATAROOM_VIEW",
+ title: "Data room visited",
+ descriptionAdmin: "When someone visits any data room link",
+ descriptionMember: "When someone visits a data room link you own",
+ icon: ServerIcon,
+ },
+ {
+ type: "DATAROOM_UPLOAD",
+ title: "File uploaded",
+ descriptionAdmin:
+ "When a viewer uploads files to any data room",
+ descriptionMember:
+ "When a viewer uploads files to a data room you own",
+ icon: FileUpIcon,
+ },
+ ],
+ },
+ {
+ label: "Security",
+ items: [
+ {
+ type: "BLOCKED_ACCESS",
+ title: "Blocked access attempt",
+ descriptionAdmin: "When a viewer is denied access to any link",
+ descriptionMember:
+ "When a viewer is denied access to a link you own",
+ icon: ShieldAlertIcon,
+ },
+ ],
+ },
+ {
+ label: "Conversations",
+ items: [
+ {
+ type: "CONVERSATION_MESSAGE",
+ title: "New message",
+ descriptionAdmin:
+ "When a new message is posted in any conversation",
+ descriptionMember:
+ "When a new message is posted in your conversations",
+ icon: MessageSquareIcon,
+ },
+ ],
+ },
+];
+
+export default function NotificationsSettings() {
+ const { preferences, role, isLoading, updatePreferences } =
+ useNotificationPreferences();
+
+ const isMember = role === "MEMBER";
+
+ const handleFrequencyChange = async (
+ type: TeamNotificationType,
+ frequency: TeamNotificationFrequency,
+ ) => {
+ try {
+ await updatePreferences([{ type, frequency }]);
+ toast.success("Notification preference updated");
+ } catch {
+ toast.error("Failed to update notification preference");
+ }
+ };
+
+ return (
+ <AppLayout>
+ <main className="relative mx-2 mb-10 mt-4 space-y-8 overflow-hidden px-1 sm:mx-3 md:mx-5 md:mt-5 lg:mx-7 lg:mt-8 xl:mx-10">
+ <SettingsHeader />
+
+ <div className="space-y-2">
+ <h3 className="text-lg font-semibold tracking-tight text-foreground">
+ Notifications
+ </h3>
+ <p className="text-sm text-muted-foreground">
+ Choose how and when you want to be notified about activity in this
+ team.
+ </p>
+ </div>
+
+ {isMember ? (
+ <Alert>
+ <InfoIcon className="h-4 w-4" />
+ <AlertDescription>
+ As a team member, you&apos;ll only receive notifications for
+ documents and links you own. Admins and managers receive
+ notifications for all team activity.
+ </AlertDescription>
+ </Alert>
+ ) : null}
+
+ {isLoading ? (
+ <div className="space-y-6">
+ {[1, 2, 3].map((i) => (
+ <div key={i} className="space-y-4">
+ <div className="h-5 w-32 animate-pulse rounded bg-muted" />
+ <div className="h-16 w-full animate-pulse rounded-lg bg-muted" />
+ </div>
+ ))}
+ </div>
+ ) : (
+ <div className="space-y-8">
+ {NOTIFICATION_CATEGORIES.map((category) => (
+ <div key={category.label} className="space-y-3">
+ <h4 className="text-sm font-medium text-muted-foreground">
+ {category.label}
+ </h4>
+ <div className="divide-y rounded-lg border">
+ {category.items.map((item) => (
+ <NotificationRow
+ key={item.type}
+ item={item}
+ isMember={isMember}
+ frequency={preferences?.[item.type] ?? "IMMEDIATE"}
+ onFrequencyChange={(frequency) =>
+ handleFrequencyChange(item.type, frequency)
+ }
+ />
+ ))}
+ </div>
+ </div>
+ ))}
+ </div>
+ )}
+ </main>
+ </AppLayout>
+ );
+}
+
+function NotificationRow({
+ item,
+ isMember,
+ frequency,
+ onFrequencyChange,
+}: {
+ item: NotificationConfig;
+ isMember: boolean;
+ frequency: TeamNotificationFrequency;
+ onFrequencyChange: (frequency: TeamNotificationFrequency) => void;
+}) {
+ const Icon = item.icon;
+ const description = isMember
+ ? item.descriptionMember
+ : item.descriptionAdmin;
+
+ return (
+ <div className="flex items-center justify-between gap-4 px-4 py-3">
+ <div className="flex items-center gap-3">
+ <div className="flex h-9 w-9 shrink-0 items-center justify-center rounded-full bg-muted">
+ <Icon className="h-4 w-4 text-muted-foreground" />
+ </div>
+ <div className="min-w-0">
+ <p className="text-sm font-medium text-foreground">{item.title}</p>
+ <p className="text-xs text-muted-foreground">{description}</p>
+ </div>
+ </div>
+ <Select
+ value={frequency}
+ onValueChange={(value) =>
+ onFrequencyChange(value as TeamNotificationFrequency)
+ }
+ >
+ <SelectTrigger className="w-[150px] shrink-0">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {Object.entries(FREQUENCY_LABELS).map(([value, label]) => (
+ <SelectItem key={value} value={value}>
+ {label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ );
+}