From ee5d14c86c6e91ba14ee109cb75278e4fae9a950 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Thu, 25 Dec 2025 19:28:46 +0530 Subject: [PATCH] refactor: improve type safety and structure in editor page - Updated BlockNote document handling by defining TypeScript types for better clarity and maintainability. - Refactored extractTitleFromBlockNote function to utilize the new types, enhancing type safety. - Changed state management for editorContent to use the defined BlockNoteDocument type. - Adjusted layout classes for improved responsiveness and visual consistency in the editor interface. --- .../editor/[documentId]/page.tsx | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx index 235420fcb..8ac20a606 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/editor/[documentId]/page.tsx @@ -25,17 +25,29 @@ import { Separator } from "@/components/ui/separator"; import { notesApiService } from "@/lib/apis/notes-api.service"; import { authenticatedFetch, getBearerToken, redirectToLogin } from "@/lib/auth-utils"; +// BlockNote types +type BlockNoteInlineContent = string | { text?: string; type?: string; styles?: Record }; + +interface BlockNoteBlock { + type: string; + content?: BlockNoteInlineContent[]; + children?: BlockNoteBlock[]; + props?: Record; +} + +type BlockNoteDocument = BlockNoteBlock[] | null | undefined; + interface EditorContent { document_id: number; title: string; document_type?: string; - blocknote_document: any; + blocknote_document: BlockNoteDocument; updated_at: string | null; } // Helper function to extract title from BlockNote document // Takes the text content from the first block (should be a heading for notes) -function extractTitleFromBlockNote(blocknoteDocument: any[] | null | undefined): string { +function extractTitleFromBlockNote(blocknoteDocument: BlockNoteDocument): string { if (!blocknoteDocument || !Array.isArray(blocknoteDocument) || blocknoteDocument.length === 0) { return "Untitled"; } @@ -49,9 +61,9 @@ function extractTitleFromBlockNote(blocknoteDocument: any[] | null | undefined): // BlockNote blocks have a content array with inline content if (firstBlock.content && Array.isArray(firstBlock.content)) { const textContent = firstBlock.content - .map((item: any) => { + .map((item: BlockNoteInlineContent) => { if (typeof item === "string") return item; - if (item?.text) return item.text; + if (typeof item === "object" && item?.text) return item.text; return ""; }) .join("") @@ -73,7 +85,7 @@ export default function EditorPage() { const [document, setDocument] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); - const [editorContent, setEditorContent] = useState(null); + const [editorContent, setEditorContent] = useState(null); const [error, setError] = useState(null); const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); const [showUnsavedDialog, setShowUnsavedDialog] = useState(false); @@ -410,7 +422,7 @@ export default function EditorPage() { {/* Toolbar */}
@@ -444,7 +456,7 @@ export default function EditorPage() {
{/* Editor Container */} -
+
{error && (