Open source CapCut alternative.
TypeScript
feat(site): multi-select and delete in Camera Roll
David Leaedc2c5391c1
diff --git a/site/src/cut/components/CameraRollView.tsx b/site/src/cut/components/CameraRollView.tsxindex ad14d73a..300abcef 100644--- a/site/src/cut/components/CameraRollView.tsx+++ b/site/src/cut/components/CameraRollView.tsx@@ -1,26 +1,22 @@"use client";-import { useCallback, useState } from "react";+import { useCallback, useRef, useState } from "react";import { useQueryClient } from "@tanstack/react-query";-import { Camera, Loader2 } from "lucide-react";-import {- AlertDialog,- AlertDialogAction,- AlertDialogCancel,- AlertDialogContent,- AlertDialogDescription,- AlertDialogFooter,- AlertDialogHeader,- AlertDialogTitle,-} from "@/components/ui/alert-dialog";+import { Camera, Loader2, Trash2 } from "lucide-react";+import { DropdownMenuItem } from "@/components/ui/dropdown-menu";+import { useDeleteKey } from "@/cut/hooks/useDeleteKey";import { useTabTitle } from "@/cut/hooks/useTabTitle";+import { setObjectDragImage } from "@/cut/lib/assetDrag";import { useClipTitles } from "@/cut/lib/clipTitle";+import { additiveClick } from "@/cut/lib/hostKeys";import { deleteFromLibrary, type LibraryAsset } from "@/cut/lib/library";import { lightboxItemFromLibrary, useLightbox } from "@/cut/lib/lightbox";import { patchLibrary, refetchLibrary, renameInLibrary, useLibrary } from "@/cut/lib/queries";import { formatDate } from "@/cut/lib/time";import { shapeBand } from "@/cut/lib/types";+import { Marquee, useTilePicks } from "./desktopFolders";import { Lightbox } from "./Lightbox";+import { DeleteConfirm, SelectionMenu, pickLabel, useSelectionMenu } from "./selectionMenu";import { LibraryCard } from "@/cut/components/LibraryCard";/** The phone's recordings, synced up from the iOS app: every cloud library@@ -28,7 +24,8 @@ import { LibraryCard } from "@/cut/components/LibraryCard";* assets — the editor's Library panel offers them to any project — this page* is where they are watched and pruned. The listing stays live while the tab* is open, so a recording made on the phone arrives on its own, and names- * itself off what is said in it. */+ * itself off what is said in it. Pruning picks the way every tile grid does:+ * click, ⌘ and ⇧ clicks, a sweep, then ⌫ or the right-click menu. */export function CameraRollView() {const client = useQueryClient();// The phone fills this page, so it re-reads on a timer and whenever the@@ -36,7 +33,8 @@ export function CameraRollView() {// place in the grid — a shimmering tile first, the clip a moment later —// with nothing to reload.const library = useLibrary({ live: true });- const [deleting, setDeleting] = useState<LibraryAsset | null>(null);+ const [deleting, setDeleting] = useState<LibraryAsset[] | null>(null);+ const rootRef = useRef<HTMLDivElement>(null);const clips = (library.data?.assets ?? []).filter((a) => a.origin === "camera");const patch = useCallback((fn: Parameters<typeof patchLibrary>[1]) => patchLibrary(client, fn),@@ -45,12 +43,14 @@ export function CameraRollView() {useClipTitles(clips, patch);useTabTitle("Camera Roll");- const remove = async () => {- if (!deleting) return;- const { id, residency } = deleting;+ const remove = async (set: LibraryAsset[]) => {setDeleting(null);- patch((d) => ({ ...d, assets: d.assets.filter((a) => a.id !== id) }));- await deleteFromLibrary(residency, id).catch(() => void refetchLibrary(client));+ setSelected(new Set());+ const gone = new Set(set.map((a) => a.id));+ patch((d) => ({ ...d, assets: d.assets.filter((a) => !gone.has(a.id)) }));+ await Promise.all(set.map((a) => deleteFromLibrary(a.residency, a.id))).catch(+ () => void refetchLibrary(client),+ );};// Same banding as the Library grid: tiles of one shape share a row, every@@ -64,8 +64,30 @@ export function CameraRollView() {}const TILE_AREA = 180 * 320;+ // What can be picked, in the order a ⇧-range runs: the grid as it is laid out.+ const order = [...bands.values()].flat().map((a) => a.id);+ const { picked: selected, setPicked: setSelected, pick: pickTile } = useTilePicks(order);+ const pick = clips.filter((a) => selected.has(a.id));+ // A card inside the pick carries the whole set; outside it, itself.+ const setOf = (a: LibraryAsset) => (selected.has(a.id) ? pick : [a]);+ useDeleteKey(rootRef, pick.length > 0 ? () => setDeleting(pick) : null);+ const ctx = useSelectionMenu({ picked: selected, setPicked: setSelected, shown: order });+ const ctxSet = ctx.menu ? clips.filter((a) => ctx.menu!.ids.includes(a.id)) : [];+ // A drag out of a pick carries the pick, under the one ghost every drag wears.+ const onCardDragExtra = (e: React.DragEvent, a: LibraryAsset) => {+ if (!selected.has(a.id)) setSelected(new Set([a.id]));+ const ids = setOf(a).map((x) => x.id);+ setObjectDragImage(e, ids.length, [a.id, ...ids.filter((x) => x !== a.id)], () =>+ setSelected(new Set()),+ );+ };+return (- <div className="mx-auto w-full max-w-6xl px-10 py-9">+ <div+ ref={rootRef}+ className="mx-auto w-full max-w-6xl px-10 py-9"+ onContextMenu={ctx.onContextMenu}+ ><div className="mb-5 flex items-center justify-between gap-4"><h1 className="text-lg font-semibold tracking-tight">Camera Roll</h1></div>@@ -85,7 +107,11 @@ export function CameraRollView() {</div></div>) : (- <div className="flex flex-col gap-8">+ <Marquee+ className="flex min-h-[40vh] flex-col content-start gap-8"+ selected={selected}+ setSelected={setSelected}+ >{[...bands.values()].map((band, i) => (<div key={i} className="flex flex-wrap items-start gap-4">{band.map((a) => (@@ -94,41 +120,45 @@ export function CameraRollView() {asset={a}area={TILE_AREA}caption={formatDate(a.addedAt)}- onClick={() =>- useLightbox.getState().open(lightboxItemFromLibrary(a, true))- }- onDelete={() => setDeleting(a)}+ selected={selected.has(a.id)}+ dragGroup={pick}+ onClick={(e) => {+ if (additiveClick(e)) {+ e.preventDefault();+ pickTile(e, a.id, order);+ } else useLightbox.getState().open(lightboxItemFromLibrary(a, true));+ }}+ onDelete={() => setDeleting(setOf(a))}onRename={(name) => renameInLibrary(client, a, name)}+ onDragStartExtra={(e) => onCardDragExtra(e, a)}/>))}</div>))}- </div>+ </Marquee>)}- <AlertDialog open={!!deleting} onOpenChange={(o) => !o && setDeleting(null)}>- <AlertDialogContent>- <AlertDialogHeader>- <AlertDialogTitle>Delete “{deleting?.title || deleting?.name}”?</AlertDialogTitle>- <AlertDialogDescription>- The clip leaves your cloud Camera Roll and the phone it was shot on. Projects that- already use it keep their own copy.- </AlertDialogDescription>- </AlertDialogHeader>- <AlertDialogFooter>- <AlertDialogCancel>Cancel</AlertDialogCancel>- <AlertDialogAction- className="bg-destructive/10 text-destructive hover:bg-destructive/20"- onClick={(e) => {- e.preventDefault();- void remove();- }}- >- Delete- </AlertDialogAction>- </AlertDialogFooter>- </AlertDialogContent>- </AlertDialog>+ <SelectionMenu menu={ctxSet.length > 0 ? ctx.menu : null} onClose={ctx.close}>+ <DropdownMenuItem variant="destructive" onClick={() => setDeleting(ctxSet)}>+ <Trash2 /> Delete+ </DropdownMenuItem>+ </SelectionMenu>++ <DeleteConfirm+ open={!!deleting}+ title={+ deleting+ ? `Delete ${pickLabel([], deleting.map((a) => ({ name: a.title || a.name })), ["clip", "clips"], 0)}?`+ : ""+ }+ description={+ deleting?.length === 1+ ? "The clip leaves your cloud Camera Roll and the phone it was shot on. Projects that already use it keep their own copy."+ : "The clips leave your cloud Camera Roll and the phone they were shot on. Projects that already use them keep their own copy."+ }+ onClose={() => setDeleting(null)}+ onConfirm={() => deleting && void remove(deleting)}+ /><Lightbox /></div>diff --git a/site/src/cut/components/selectionMenu.tsx b/site/src/cut/components/selectionMenu.tsxindex 1cd2ba67..72713c78 100644--- a/site/src/cut/components/selectionMenu.tsx+++ b/site/src/cut/components/selectionMenu.tsx@@ -22,7 +22,10 @@ const OWN_MENU = "button,a,input,textarea,[role='button'],[role='menuitem'],[dat/*** The mechanics every tile grid shares — the projects home, the Library page,- * and the editor's Media panel and Library shelf. A right-click over a tile+ * Camera Roll, and the editor's Media panel and Library shelf. Every grid of+ * tiles picks and deletes the same way, a new tab included: `useTilePicks`+ * and `Marquee` pick, `useDeleteKey` and this menu act, `DeleteConfirm` asks+ * first. A right-click over a tile* (marked `data-sel-id`, the mark `Marquee` sweeps; a folder tile carries one* too) opens the selection menu at the pointer, with the tile joining the* pick if it wasn't in it, so the browser's own media menu never shows over a