Skip to content

Open source CapCut alternative.

TypeScript

fix(site): pick and delete only notes on screen

David Le562d8e55559c
diff --git a/site/src/cut/components/NotesView.tsx b/site/src/cut/components/NotesView.tsx
index 2a08f4a1..dd2465e6 100644
--- a/site/src/cut/components/NotesView.tsx
+++ b/site/src/cut/components/NotesView.tsx
@@ -3,12 +3,14 @@
import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "react";
import { useSearchParams } from "next/navigation";
import { useQueryClient, type QueryClient } from "@tanstack/react-query";
-import { FolderPlus, Loader2, Plus, StickyNote } from "lucide-react";
+import { FolderPlus, Loader2, Plus, StickyNote, Trash2 } from "lucide-react";
+import { useDeleteKey } from "@/cut/hooks/useDeleteKey";
import { useTabTitle } from "@/cut/hooks/useTabTitle";
import { noteInLibraryFolder, noteInNotesFolder, noteRef, type NoteLocation } from "@/cut/lib/noteReference";
import { setRefDragData, clearRefDrag } from "@/cut/lib/assetRef";
import type { Residency } from "@/cut/lib/residency";
import { Button } from "@/components/ui/button";
+import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import {
deleteNote,
deleteNoteFolder,
@@ -28,10 +30,11 @@ import { cutNotes } from "@/cut/lib/chatRuntime";
import { notesKey, patchNotes, useNotes } from "@/cut/lib/queries";
import { cn } from "@/lib/utils";
import { setObjectDragImage } from "@/cut/lib/assetDrag";
+import { additiveClick } from "@/cut/lib/hostKeys";
import { PICKED_RING } from "@/cut/lib/assetPick";
-import { FolderCrumb, FolderShelf, Marquee } from "./desktopFolders";
+import { FolderCrumb, FolderShelf, Marquee, useTilePicks } from "./desktopFolders";
import { CardActionsMenu } from "./CardActionsMenu";
-import { DeleteConfirm, foldersGoNote } from "./selectionMenu";
+import { DeleteConfirm, SelectionMenu, foldersGoNote, pickLabel, useSelectionMenu } from "./selectionMenu";
import { LIBRARY_SQUARE } from "./LibraryCard";
import { NoteComposer, noteChanged, type NoteDraft } from "./NoteComposer";
import { NoteBodyPreview } from "./NoteBodyPreview";
@@ -178,7 +181,6 @@ export function NotesView({ library, ref }: Props = {}) {
writes.current = run;
return run;
};
- const [selected, setSelected] = useState<Set<string>>(new Set());
const [folderCreating, setFolderCreating] = useState(false);
const [deletingFolder, setDeletingFolder] = useState<string | null>(null);
const [deletingNotes, setDeletingNotes] = useState<string[] | null>(null);
@@ -217,6 +219,15 @@ export function NotesView({ library, ref }: Props = {}) {
const shown = list.filter((n) => library
? noteInLibraryFolder(n, library.folderId, library.folderId === null ? undefined : library.residency)
: noteInNotesFolder(n, openFolder));
+ // Notes pick the way every tile grid does, and a pick reaches only the
+ // notes on screen: a note picked in another folder never rides a delete.
+ const shownIds = shown.map((n) => n.id);
+ const { picked: selected, setPicked: setSelected, pick: pickTile } = useTilePicks(shownIds);
+ const pick = shown.filter((n) => selected.has(n.id));
+ // A note inside the pick carries the whole pick; outside it, itself.
+ const setOf = (n: CutNote) => (selected.has(n.id) ? pick : [n]);
+ const ctx = useSelectionMenu({ picked: selected, setPicked: setSelected, shown: shownIds });
+ const ctxSet = ctx.menu ? shown.filter((n) => ctx.menu!.ids.includes(n.id)) : [];
// What the composer shows: the buffer while it belongs to the note the URL
// names, and the stored note otherwise. A note nobody has typed into has no
// buffer, and one still being written has no stored note.
@@ -431,7 +442,6 @@ export function NotesView({ library, ref }: Props = {}) {
...prev,
notes: prev.notes.filter((n) => !gone.has(n.id)),
}));
- setSelected((prev) => new Set([...prev].filter((id) => !gone.has(id))));
// Behind any save still queued for it, whose later stamp would bring
// the note back.
for (const id of ids) void queue(() => deleteNote(id).catch(() => reload()));
@@ -579,9 +589,18 @@ export function NotesView({ library, ref }: Props = {}) {
// The open note is portaled over the app; this is how it finds the column
// this list scrolls in and holds it still.
const pageRef = useRef<HTMLDivElement>(null);
+ useDeleteKey(pageRef, pick.length > 0 ? () => setDeletingNotes(pick.map((n) => n.id)) : null);
+ // What the confirm names: each note by its title.
+ const deletingNames = (deletingNotes ?? []).map((id) => ({
+ name: list.find((n) => n.id === id)?.title.trim() || "Untitled note",
+ }));
return (
- <div ref={pageRef} className={library ? (shown.length > 0 ? "mb-6" : undefined) : "mx-auto w-full max-w-6xl px-10 py-9"}>
+ <div
+ ref={pageRef}
+ className={library ? (shown.length > 0 ? "mb-6" : undefined) : "mx-auto w-full max-w-6xl px-10 py-9"}
+ onContextMenu={ctx.onContextMenu}
+ >
{failedDraft && <p role="alert" className="py-3 text-sm text-destructive">Could not save the note. <button className="underline" onClick={() => { edit(failedDraft); openAt(failedDraft.id); setFailedDraft(null); }}>Reopen and retry</button></p>}
{/* One note at a time, over the whole window. The list stays mounted
behind it, so closing comes back to the same scroll position. */}
@@ -711,13 +730,15 @@ export function NotesView({ library, ref }: Props = {}) {
return (
<div
key={n.id}
- data-sel-id={n.id}
className={cn(
"group relative rounded-xl transition-transform hover:-translate-y-0.5",
selected.has(n.id) && PICKED_RING,
)}
>
+ {/* The button carries the tile's mark, so a right-click on it
+ opens the selection menu. */}
<button
+ data-sel-id={n.id}
draggable
onDragStart={(e) => onCardDragStart(e, n.id)}
onDragEnd={clearRefDrag}
@@ -733,7 +754,14 @@ export function NotesView({ library, ref }: Props = {}) {
width: LIBRARY_SQUARE,
height: LIBRARY_SQUARE,
}}
- onClick={() => openNote(n)}
+ onClick={(e) => {
+ if (!additiveClick(e)) {
+ openNote(n);
+ return;
+ }
+ e.preventDefault();
+ pickTile(e, n.id, shownIds);
+ }}
>
{/* Clear of the actions button that takes the corner on hover. */}
{n.title && <div className="truncate pr-6 text-[13px] font-semibold">{n.title}</div>}
@@ -755,20 +783,21 @@ export function NotesView({ library, ref }: Props = {}) {
deletes the whole pick with it, the way a file does. */}
<CardActionsMenu
className="absolute top-1.5 right-1.5"
- onDelete={() => setDeletingNotes(selected.has(n.id) ? [...selected].filter((id) => list.some((m) => m.id === id)) : [n.id])}
+ onDelete={() => setDeletingNotes(setOf(n).map((m) => m.id))}
/>
</div>
);
})}
</Marquee>
)}
+ <SelectionMenu menu={ctxSet.length > 0 ? ctx.menu : null} onClose={ctx.close}>
+ <DropdownMenuItem variant="destructive" onClick={() => setDeletingNotes(ctxSet.map((n) => n.id))}>
+ <Trash2 /> Delete
+ </DropdownMenuItem>
+ </SelectionMenu>
<DeleteConfirm
open={deletingNotes !== null}
- title={
- deletingNotes?.length === 1
- ? `Delete “${list.find((n) => n.id === deletingNotes[0])?.title.trim() || "Untitled note"}”?`
- : `Delete ${deletingNotes?.length ?? 0} notes?`
- }
+ title={`Delete ${pickLabel([], deletingNames, ["note", "notes"], 0)}?`}
description={`${deletingNotes?.length === 1 ? "It is" : "They are"} removed from your phone too.`}
onClose={() => setDeletingNotes(null)}
onConfirm={() => {
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index dba8cd7f..e6465482 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -1050,7 +1050,7 @@ function ProjectFilesPanel({
)}
{/* The shelf scrolls with the grid, so one sweep reaches both. */}
<ScrollArea className="min-h-0 flex-1" contentClassName="pt-1 pb-3.5">
- {openFolder === null && (folders.length > 0 || creatingIds !== null) && (
+ {openFolder === null && (
<div className="shrink-0 px-3.5">
<FolderShelf
rows
@@ -1962,58 +1962,56 @@ function LibraryPanel({ projectId, onImport }: {
/>
</div>
)}
- {shelfFolders.length > 0 ? (
- <div className="shrink-0 px-3.5">
- <FolderShelf
- rows
- folders={shelfFolders}
- referenceOf={(f) => libraryFolderRef(f, folders)}
- mime={LIBRARY_MOVE_MIME}
- folderMime={LIBRARY_FOLDER_MOVE_MIME}
- statOf={(id) => ({
- count:
- all.filter((a) => folderOf(a) === id).length +
- templates.filter((t) => (t.folderId ?? null) === id).length +
- childrenOf(folders, id).length,
- })}
- badgeOf={(id) => {
- const r = folders.find((f) => f.id === id)?.residency;
- return bothShelves && r ? <ShelfBadge residency={r} /> : null;
- }}
- picked={picked}
- onPick={(e, id) => pick(e, folderSelId(id), order)}
- renaming={renamingFolder}
- onRenamingChange={setRenamingFolder}
- onOpen={(id) => setOpenFolder(id)}
- onRename={renameFolder}
- // A folder in a pick with others takes the pick to the
- // confirm; alone, itself.
- onDelete={(id) => {
- const f = folders.find((x) => x.id === id);
- if (!f) return;
- const size = pickSet.folders.length + pickSet.items.length;
- if (picked.has(folderSelId(id)) && size > 1) setDeleting(pickSet);
- else setDeleting({ folders: [f], items: [] });
- }}
- onDropIds={(ids, fid) => ids.forEach((id) => void move(id, fid))}
- onDropFolders={(ids, fid) => void moveFolders(ids, fid)}
- onRefDrop={(ref, fid) => {
- // Project media dropped on a folder tile (a Media card or a
- // timeline clip): save it to the library, filed in that folder.
- // The copy lands on the project's own shelf, so a folder on the
- // other one can't take it.
- if (ref.scope !== "project") return;
- if (folders.find((f) => f.id === fid)?.residency !== activeResidency()) return;
- const asset = useEditor.getState().assets.find((a) => a.id === ref.id);
- if (!asset) return;
- void saveAssetToLibrary(projectId, asset)
- .then((saved) => moveLibraryItem(saved.residency, saved.id, fid))
- .then(() => void reload())
- .catch(() => {});
- }}
- />
- </div>
- ) : null}
+ <div className="shrink-0 px-3.5">
+ <FolderShelf
+ rows
+ folders={shelfFolders}
+ referenceOf={(f) => libraryFolderRef(f, folders)}
+ mime={LIBRARY_MOVE_MIME}
+ folderMime={LIBRARY_FOLDER_MOVE_MIME}
+ statOf={(id) => ({
+ count:
+ all.filter((a) => folderOf(a) === id).length +
+ templates.filter((t) => (t.folderId ?? null) === id).length +
+ childrenOf(folders, id).length,
+ })}
+ badgeOf={(id) => {
+ const r = folders.find((f) => f.id === id)?.residency;
+ return bothShelves && r ? <ShelfBadge residency={r} /> : null;
+ }}
+ picked={picked}
+ onPick={(e, id) => pick(e, folderSelId(id), order)}
+ renaming={renamingFolder}
+ onRenamingChange={setRenamingFolder}
+ onOpen={(id) => setOpenFolder(id)}
+ onRename={renameFolder}
+ // A folder in a pick with others takes the pick to the
+ // confirm; alone, itself.
+ onDelete={(id) => {
+ const f = folders.find((x) => x.id === id);
+ if (!f) return;
+ const size = pickSet.folders.length + pickSet.items.length;
+ if (picked.has(folderSelId(id)) && size > 1) setDeleting(pickSet);
+ else setDeleting({ folders: [f], items: [] });
+ }}
+ onDropIds={(ids, fid) => ids.forEach((id) => void move(id, fid))}
+ onDropFolders={(ids, fid) => void moveFolders(ids, fid)}
+ onRefDrop={(ref, fid) => {
+ // Project media dropped on a folder tile (a Media card or a
+ // timeline clip): save it to the library, filed in that folder.
+ // The copy lands on the project's own shelf, so a folder on the
+ // other one can't take it.
+ if (ref.scope !== "project") return;
+ if (folders.find((f) => f.id === fid)?.residency !== activeResidency()) return;
+ const asset = useEditor.getState().assets.find((a) => a.id === ref.id);
+ if (!asset) return;
+ void saveAssetToLibrary(projectId, asset)
+ .then((saved) => moveLibraryItem(saved.residency, saved.id, fid))
+ .then(() => void reload())
+ .catch(() => {});
+ }}
+ />
+ </div>
{shownTemplates.length > 0 && (
<div className="shrink-0 px-3.5 pb-3">
<div className="grid grid-cols-2 gap-2.5">
diff --git a/site/src/cut/components/desktopFolders.tsx b/site/src/cut/components/desktopFolders.tsx
index 146292c4..30be316c 100644
--- a/site/src/cut/components/desktopFolders.tsx
+++ b/site/src/cut/components/desktopFolders.tsx
@@ -16,6 +16,7 @@ import { setObjectDragImage } from "@/cut/lib/assetDrag";
import { PICKED_RING } from "@/cut/lib/assetPick";
import { additiveClick } from "@/cut/lib/hostKeys";
import { clearRefDrag, setRefDragData, type AssetRef } from "@/cut/lib/assetRef";
+import { reportSwallowed } from "@/cut/lib/report";
import { formatBytes } from "@/lib/bytes";
import { RefDropZone } from "./RefDropZone";
import { cn } from "@/lib/utils";
@@ -515,20 +516,28 @@ export function FolderShelf<F extends DeskFolder>({
// lets the name go.
const [landing, setLanding] = useState<{ name: string; known: Set<string> } | null>(null);
if (landing && folders.some((f) => !landing.known.has(f.id))) setLanding(null);
+ // A failed create says so on the shelf until the next one, and reaches
+ // error tracking.
+ const [failed, setFailed] = useState<string | null>(null);
const commitCreate = (name: string) => {
const pending = { name, known: new Set(folders.map((f) => f.id)) };
setLanding(pending);
+ setFailed(null);
const release = () => setLanding((l) => (l === pending ? null : l));
Promise.resolve(onCreate?.(name)).then(
() => setTimeout(release, LANDING_GRACE_MS),
- release,
+ (err: unknown) => {
+ release();
+ setFailed(err instanceof Error && err.message ? err.message : "Could not create folder.");
+ reportSwallowed(`[cut] folder create failed for ${name}`, err);
+ },
);
closeCreate();
};
// Nothing filed here and nothing being made: the shelf takes no room. The
// host keeps it mounted, so a name held for a create outlives the field.
- if (folders.length === 0 && !creating && !landing) return null;
+ if (folders.length === 0 && !creating && !landing && !failed) return null;
const editRowClass = rows
? "flex items-center gap-2.5 rounded-lg px-2 py-1.5"
@@ -749,6 +758,12 @@ export function FolderShelf<F extends DeskFolder>({
/>
</div>
)}
+
+ {failed && (
+ <p role="alert" className="w-full px-2 text-xs text-destructive">
+ {failed}
+ </p>
+ )}
</div>
);
}