Skip to content

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

feat: add notion dark mode button

Marc Seitz704567682cd8
diff --git a/components/documents/document-header.tsx b/components/documents/document-header.tsx
index 5a2b664a..d0828195 100644
--- a/components/documents/document-header.tsx
+++ b/components/documents/document-header.tsx
@@ -10,8 +10,10 @@ import {
CloudDownloadIcon,
DownloadIcon,
FileDownIcon,
+ MoonIcon,
SheetIcon,
Sparkles,
+ SunIcon,
TrashIcon,
ViewIcon,
} from "lucide-react";
@@ -345,6 +347,36 @@ export default function DocumentHeader({
);
};
+ // Toggle dark mode for Notion documents
+ const toggleNotionDarkMode = async (darkMode: boolean) => {
+ if (prismaDocument.type !== "notion") {
+ toast.error("This feature is not available for your document type");
+ return;
+ }
+
+ toast.promise(
+ fetch(
+ `/api/teams/${teamId}/documents/${prismaDocument.id}/toggle-dark-mode`,
+ {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ darkMode: darkMode,
+ }),
+ },
+ ).then(() => {
+ mutate(`/api/teams/${teamId}/documents/${prismaDocument.id}`);
+ }),
+ {
+ loading: "Updating Notion theme...",
+ success: `Notion theme changed to ${darkMode ? "dark" : "light"} mode`,
+ error: "Failed to update Notion theme",
+ },
+ );
+ };
+
useEffect(() => {
function handleClickOutside(event: { target: any }) {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
@@ -706,6 +738,26 @@ export default function DocumentHeader({
</DropdownMenuItem>
)}
+ {prismaDocument.type === "notion" && (
+ <>
+ {primaryVersion.file.includes("mode=dark") ? (
+ <DropdownMenuItem
+ onClick={() => toggleNotionDarkMode(false)}
+ >
+ <MoonIcon className="mr-2 h-4 w-4" />
+ Disable dark mode
+ </DropdownMenuItem>
+ ) : (
+ <DropdownMenuItem
+ onClick={() => toggleNotionDarkMode(true)}
+ >
+ <SunIcon className="mr-2 h-4 w-4" />
+ Enable dark mode
+ </DropdownMenuItem>
+ )}
+ </>
+ )}
+
<DropdownMenuSeparator />
{/* Export views in CSV */}
diff --git a/pages/api/teams/[teamId]/documents/[id]/toggle-dark-mode.ts b/pages/api/teams/[teamId]/documents/[id]/toggle-dark-mode.ts
new file mode 100644
index 00000000..4d7ebd64
--- /dev/null
+++ b/pages/api/teams/[teamId]/documents/[id]/toggle-dark-mode.ts
@@ -0,0 +1,94 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth/next";
+
+import prisma from "@/lib/prisma";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "PATCH") {
+ return res.status(405).json({ message: "Method not allowed" });
+ }
+
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).json({ message: "Unauthorized" });
+ }
+
+ const userId = (session.user as CustomUser).id;
+ const { teamId, id: documentId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+ const { darkMode } = req.body as { darkMode: boolean };
+
+ try {
+ // Check if user has access to the team
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId: userId,
+ },
+ },
+ },
+ });
+
+ if (!team) {
+ return res.status(401).json({ message: "Unauthorized" });
+ }
+
+ const documentVersion = await prisma.documentVersion.findFirst({
+ where: {
+ documentId: documentId,
+ },
+ select: {
+ file: true,
+ type: true,
+ },
+ });
+
+ if (!documentVersion) {
+ return res.status(404).json({ message: "Document version not found" });
+ }
+
+ if (documentVersion.type !== "notion") {
+ return res
+ .status(400)
+ .json({ message: "Document is not a Notion document" });
+ }
+
+ const notionUrl = new URL(documentVersion.file);
+ if (darkMode) {
+ notionUrl.searchParams.set("mode", "dark");
+ } else {
+ notionUrl.searchParams.delete("mode");
+ }
+
+ // Update document version
+ await prisma.documentVersion.updateMany({
+ where: {
+ documentId: documentId,
+ },
+ data: {
+ file: notionUrl.toString(),
+ },
+ });
+
+ await fetch(
+ `${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&documentId=${documentId}`,
+ );
+
+ return res.status(200).json({
+ message: `Notion document theme changed to ${darkMode ? "dark" : "light"} mode`,
+ });
+ } catch (error) {
+ console.error(error);
+ return res.status(500).json({ message: "Error updating document" });
+ }
+}