Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

use-compose-note.ts

33 lines1.2 KB

1import { useNavigate, useParams } from "@tanstack/react-router";
2​
3import { createNote } from "@/db/mutations";
4import { useAccount } from "@/db/queries";
5import { isSmartFolderId } from "@/lib/notes/folder-views";
6​
7import { focusNoteWhenOpened } from "./note-editor";
8​
9/**
10 * The compose button: a new, empty note in the open folder (or the default
11 * folder, from a smart folder), opened with its title focused. `undefined`
12 * where Notes doesn't allow it (Recently Deleted).
13 */
14export function useComposeNote(): (() => void) | undefined {
15 const navigate = useNavigate();
16 const account = useAccount();
17 const { folderId: openFolderId } = useParams({ strict: false });
18​
19 if (!account || openFolderId === "recently-deleted") return undefined;
20​
21 return () => {
22 const folderId =
23 openFolderId && !isSmartFolderId(openFolderId) ? openFolderId : account.defaultFolderId;
24 const { note } = createNote(folderId);
25 focusNoteWhenOpened(note.id);
26 // Stay in All iCloud; from anywhere else, go to the folder the note is in.
27 const listId = openFolderId === "all" ? "all" : folderId;
28 void navigate({
29 to: "/folders/$folderId/notes/$noteId",
30 params: { folderId: listId, noteId: note.id },
31 });
32 };