From 39e0a2eb6d8b08af209db037e165243809ff3282 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Jun 2026 15:12:58 +0300 Subject: [PATCH] remember last used workspace when creating a note --- components/home-components/AppSidebar.tsx | 134 ++++++++++++++++++---- package.json | 2 +- 2 files changed, 115 insertions(+), 21 deletions(-) diff --git a/components/home-components/AppSidebar.tsx b/components/home-components/AppSidebar.tsx index f41a2c6..e59a72f 100644 --- a/components/home-components/AppSidebar.tsx +++ b/components/home-components/AppSidebar.tsx @@ -100,9 +100,7 @@ const noteTitleSchema = z .min(1, "Title cannot be empty") .max(55, "Title must be 55 characters or less"); -type PreferredAction = "note" | "DiffNote"; - -const STORAGE_KEY = "notevo_create_note_action"; +const CREATE_NOTE_WORKSPACE_STORAGE_KEY = "notevo_create_note_workspace_id"; const PINNED_NOTES_EXPANDED_STORAGE_KEY = "notevo_sidebar_pinned_notes_expanded"; const PINNED_UPLOADS_EXPANDED_STORAGE_KEY = @@ -147,6 +145,56 @@ const SidebarHeaderSection = memo(function SidebarHeaderSection({ handleCreateWorkingSpace, loading, }: SidebarHeaderSectionProps) { + const [savedWorkspaceId, setSavedWorkspaceId] = useState(null); + + useEffect(() => { + if (typeof window === "undefined") return; + setSavedWorkspaceId( + window.localStorage.getItem(CREATE_NOTE_WORKSPACE_STORAGE_KEY), + ); + }, []); + + const savedWorkspace = useMemo(() => { + if (!savedWorkspaceId) return undefined; + return getWorkingSpaces?.find( + (workingSpace) => String(workingSpace._id) === savedWorkspaceId, + ); + }, [getWorkingSpaces, savedWorkspaceId]); + + useEffect(() => { + if ( + typeof window === "undefined" || + !savedWorkspaceId || + getWorkingSpaces === undefined || + savedWorkspace + ) { + return; + } + + window.localStorage.removeItem(CREATE_NOTE_WORKSPACE_STORAGE_KEY); + setSavedWorkspaceId(null); + }, [getWorkingSpaces, savedWorkspace, savedWorkspaceId]); + + const createNoteInWorkspace = useCallback( + async (workingSpace: Doc<"workingSpaces">) => { + const workspaceId = String(workingSpace._id); + + setSavedWorkspaceId(workspaceId); + if (typeof window !== "undefined") { + window.localStorage.setItem( + CREATE_NOTE_WORKSPACE_STORAGE_KEY, + workspaceId, + ); + } + + await handleCreateNote( + workingSpace._id as any, + workingSpace.slug as string, + ); + }, + [handleCreateNote], + ); + return (
@@ -160,7 +208,8 @@ const SidebarHeaderSection = memo(function SidebarHeaderSection({
{getWorkingSpaces?.length === 0 && ( )) + ) : savedWorkspace ? ( +
+ + + + + + + + {getWorkingSpaces?.map((workingSpace) => ( + void createNoteInWorkspace(workingSpace)} + disabled={loading} + > + + {formatWorkspaceName(workingSpace.name)} + + ))} + + + +
) : ( @@ -223,12 +322,7 @@ const SidebarHeaderSection = memo(function SidebarHeaderSection({ - handleCreateNote( - workingSpace._id as any, - workingSpace.slug as string, - ) - } + onSelect={() => void createNoteInWorkspace(workingSpace)} disabled={loading} > diff --git a/package.json b/package.json index e7ee6f2..6a94740 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "cmdk": "^1.0.0", "cobe": "^0.6.3", "convex": "^1.31.3", - "convex-helpers": "^0.1.79", + "convex-helpers": "^0.1.118", "docx": "^9.5.1", "file-saver": "^2.0.5", "framer-motion": "^12.0.5",