Skip to content

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

feature - implemnted automatic Notion file name implementation

Ashrafb7a70292cc56
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 0ab231ac..8d483d5a 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -19,6 +19,8 @@ import { Button } from "@/components/ui/button";
import { usePlausible } from "next-plausible";
import { toast } from "sonner";
import { useTeam } from "@/context/team-context";
+import useDocuments from "@/lib/swr/use-documents";
+import { DocumentWithLinksAndLinkCountAndViewCount } from "@/lib/types";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
@@ -35,6 +37,7 @@ export function AddDocumentModal({
const [currentFile, setCurrentFile] = useState<File | null>(null);
const [notionLink, setNotionLink] = useState<string | null>(null);
const teamInfo = useTeam();
+ const { documents } = useDocuments();
const handleBrowserUpload = async (
event: FormEvent<HTMLFormElement>,
@@ -174,6 +177,28 @@ export function AddDocumentModal({
return pdf.numPages;
}
+ const createNotionFileName = () => {
+ // Extract Notion file name from the URL
+ const urlSegments = (notionLink as string).split("/")[3];
+
+ // Remove the last hyphen along with the Notion ID
+ const extractName = urlSegments.replace(/-([^/-]+)$/, "");
+ const notionFileName = extractName.replaceAll("-", " ") || "Notion Link";
+
+ // Check if a Notion file with the same name already exists
+ const existingNotionFiles = documents
+ ?.filter((item) => item.type === "notion")
+ .filter((item) =>
+ item.name.includes(notionFileName),
+ ) as DocumentWithLinksAndLinkCountAndViewCount[];
+
+ if (existingNotionFiles?.length > 0) {
+ return `${notionFileName} ${existingNotionFiles.length}`;
+ } else {
+ return notionFileName;
+ }
+ };
+
const handleNotionUpload = async (
event: FormEvent<HTMLFormElement>,
): Promise<void> => {
@@ -196,7 +221,7 @@ export function AddDocumentModal({
"Content-Type": "application/json",
},
body: JSON.stringify({
- name: "Notion Link", // TODO: get the title of the notion page
+ name: createNotionFileName(),
url: notionLink,
numPages: 1,
type: "notion",