The VS Code of video editing, built for agents.
TypeScript
Enhance project deletion functionality in dashboard components
konstantin-paulus48c6672f5183
diff --git a/apps/web/src/components/dashboard/delete-project-dialog.tsx b/apps/web/src/components/dashboard/delete-project-dialog.tsxnew file mode 100644index 00000000..d71df389--- /dev/null+++ b/apps/web/src/components/dashboard/delete-project-dialog.tsx@@ -0,0 +1,82 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++import {+ AlertDialog,+ AlertDialogContent,+ AlertDialogDescription,+ AlertDialogFooter,+ AlertDialogHeader,+ AlertDialogTitle,+} from "@/components/ui/alert-dialog";+import { Button } from "@/components/ui/button";+import { createSignal } from "solid-js";+import { toast } from "somoto";+import { trashProject } from "./shared";++import type { ProjectInfo } from "@/projects";+++type DeleteProjectDialogProps = {+ /** The project being deleted; `null` while the dialog is closed. */+ project: ProjectInfo | null;+ /** Asked for whenever the dialog should close, confirmed or not. */+ onClose(): void;+ /** The project is in the Trash — for the list that showed it to catch up. */+ onDeleted(project: ProjectInfo): void;+};++/**+ * The confirmation a project's deletion goes through. The view holds which+ * project is pending and renders this on it; the delete itself, and being+ * unclosable while it runs, are the dialog's own.+ */+export function DeleteProjectDialog(props: DeleteProjectDialogProps) {+ const [deleting, setDeleting] = createSignal(false);++ const confirm = async () => {+ const project = props.project;+ if (!project || deleting()) return;++ setDeleting(true);+ try {+ await trashProject(project);+ props.onDeleted(project);+ } catch (e) {+ toast.error("Failed to delete project", {+ description: (e as Error).message,+ });+ } finally {+ setDeleting(false);+ props.onClose();+ }+ };++ return (+ <AlertDialog+ open={props.project !== null}+ onOpenChange={(open) => {+ if (!open && !deleting()) props.onClose();+ }}+ >+ <AlertDialogContent>+ <AlertDialogHeader>+ <AlertDialogTitle>Delete project</AlertDialogTitle>+ <AlertDialogDescription>+ {`"${props.project?.displayName ?? ""}" will be moved to the Trash. `}+ You can restore it from there until the Trash is emptied.+ </AlertDialogDescription>+ </AlertDialogHeader>+ <AlertDialogFooter>+ <Button variant="secondary" disabled={deleting()} onClick={props.onClose}>+ Cancel+ </Button>+ <Button variant="destructive" disabled={deleting()} onClick={confirm}>+ {deleting() ? "Deleting..." : "Delete"}+ </Button>+ </AlertDialogFooter>+ </AlertDialogContent>+ </AlertDialog>+ );+}diff --git a/apps/web/src/components/dashboard/home-view.tsx b/apps/web/src/components/dashboard/home-view.tsxindex a18dc783..2954601a 100644--- a/apps/web/src/components/dashboard/home-view.tsx+++ b/apps/web/src/components/dashboard/home-view.tsx@@ -22,7 +22,6 @@ import {createResource,createSignal,onCleanup,- type JSX,} from "solid-js";import { isServer } from "solid-js/web";import { toast } from "somoto";@@ -62,11 +61,13 @@ import {type ProjectInfo,} from "@/projects";+import { DeleteProjectDialog } from "./delete-project-dialog";import {DashboardCardButton,DashboardCardMeta,DashboardCardPreview,DashboardProjectThumbnail,+ createBackgroundClickHandler,} from "./shared";import { formatEditedAt, parseTimestamp } from "./utils";@@ -230,13 +231,17 @@ export function DashboardHomeView() {void handleSubmit();};- // Only the grid's own background clears the selection — a click that landed- // on a card is that card's to handle.- const clearSelection: JSX.EventHandlerUnion<HTMLDivElement, MouseEvent> = (- event,- ) => {- if (event.target !== event.currentTarget) return;- setSelectedProject(null);+ // Anything outside a card clears the selection — the grid's gaps, the space+ // around it, and the composer above it. A click on a card is that card's.+ const clearSelection = createBackgroundClickHandler(() =>+ setSelectedProject(null),+ );++ const [pendingDelete, setPendingDelete] = createSignal<ProjectInfo | null>(null);++ const handleDeleted = (project: ProjectInfo) => {+ setSelectedProject((current) => (current === project.dir ? null : current));+ refetchProjects();};const openProject = (project: ProjectInfo) => {@@ -271,7 +276,10 @@ export function DashboardHomeView() {return (<>- <div class="flex min-h-0 flex-1 flex-col overflow-y-auto">+ <div+ class="flex min-h-0 flex-1 flex-col overflow-y-auto"+ onClick={clearSelection}+ ><div class="flex flex-1 flex-col items-center justify-center gap-6.5 px-6 py-8 pt-[15%]"><h1 class="w-full text-center text-5xl leading-normal font-450 tracking-[0.0864px] text-muted-foreground">What should we edit?@@ -377,7 +385,6 @@ export function DashboardHomeView() {</div><divdata-slot="card-grid"- onClick={clearSelection}class="grid grid-cols-5 items-start gap-x-0.5 gap-y-3 px-4 pb-4"><DashboardCardButton onClick={handleCreateProject}>@@ -396,6 +403,7 @@ export function DashboardHomeView() {onClick={() => setSelectedProject(project.dir)}onDoubleClick={() => openProject(project)}onEscape={() => setSelectedProject(null)}+ onDelete={() => setPendingDelete(project)}><DashboardCardPreview><DashboardProjectThumbnail dir={project.dir} />@@ -410,6 +418,12 @@ export function DashboardHomeView() {</div></div></div>++ <DeleteProjectDialog+ project={pendingDelete()}+ onClose={() => setPendingDelete(null)}+ onDeleted={handleDeleted}+ /></>);}diff --git a/apps/web/src/components/dashboard/projects-view.tsx b/apps/web/src/components/dashboard/projects-view.tsxindex f2bbc201..1e27b913 100644--- a/apps/web/src/components/dashboard/projects-view.tsx+++ b/apps/web/src/components/dashboard/projects-view.tsx@@ -2,7 +2,7 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-import { forgetProjectBundle, generateProjectName } from "@/lib/db";+import { generateProjectName } from "@/lib/db";import {ContextMenu,ContextMenuContent,@@ -12,15 +12,6 @@ import {ContextMenuTrigger,} from "@/components/ui/context-menu";import { TextField, TextFieldInput } from "@/components/ui/text-field";-import { Button } from "@/components/ui/button";-import {- AlertDialog,- AlertDialogContent,- AlertDialogDescription,- AlertDialogFooter,- AlertDialogHeader,- AlertDialogTitle,-} from "@/components/ui/alert-dialog";import { toast } from "somoto";import { For, Show, batch, createMemo, createResource, createSignal } from "solid-js";import { useNavigate } from "@solidjs/router";@@ -40,6 +31,7 @@ import {DashboardProjectThumbnail,DashboardViewSection,} from "./shared";+import { DeleteProjectDialog } from "./delete-project-dialog";import { formatEditedAt, parseTimestamp } from "./utils";import { DashboardSearchPanel } from "./search-bar";import { DashboardProjectsFolderBar } from "./projects-folder-bar";@@ -48,7 +40,6 @@ import { Icon } from "../ui/icon";import { track } from "@/lib/analytics";import {createProject,- deleteProject,duplicateProject,ensureProjectsRoot,isDesktop,@@ -69,7 +60,6 @@ export function DashboardProjectsView() {const [selectedProject, setSelectedProject] = createSignal<string | null>(null);const [creating, setCreating] = createSignal(false);const [pendingDelete, setPendingDelete] = createSignal<ProjectInfo | null>(null);- const [deleting, setDeleting] = createSignal(false);const [renamingProject, setRenamingProject] = createSignal<string | null>(null);const [renameDraft, setRenameDraft] = createSignal("");@@ -118,29 +108,9 @@ export function DashboardProjectsView() {});};- const handleDelete = async (project: ProjectInfo) => {- try {- await deleteProject(project.dir);- forgetProjectBundle(project.id);- track('project_deleted');- setSelectedProject((current) => (current === project.dir ? null : current));- refetchProjects();- } catch (e) {- toast.error("Failed to delete project", { description: (e as Error).message });- }- };-- const confirmDelete = async () => {- const project = pendingDelete();- if (!project || deleting()) return;-- setDeleting(true);- try {- await handleDelete(project);- } finally {- setDeleting(false);- setPendingDelete(null);- }+ const handleDeleted = (project: ProjectInfo) => {+ setSelectedProject((current) => (current === project.dir ? null : current));+ refetchProjects();};const handleDuplicate = async (project: ProjectInfo) => {@@ -357,34 +327,11 @@ export function DashboardProjectsView() {</DashboardViewSection><DashboardProjectsFolderBar />- <AlertDialog- open={pendingDelete() !== null}- onOpenChange={(open) => {- if (!open && !deleting()) setPendingDelete(null);- }}- >- <AlertDialogContent>- <AlertDialogHeader>- <AlertDialogTitle>Delete project</AlertDialogTitle>- <AlertDialogDescription>- {`"${pendingDelete()?.displayName ?? ""}" will be moved to the Trash. `}- You can restore it from there until the Trash is emptied.- </AlertDialogDescription>- </AlertDialogHeader>- <AlertDialogFooter>- <Button- variant="secondary"- disabled={deleting()}- onClick={() => setPendingDelete(null)}- >- Cancel- </Button>- <Button variant="destructive" disabled={deleting()} onClick={confirmDelete}>- {deleting() ? "Deleting..." : "Delete"}- </Button>- </AlertDialogFooter>- </AlertDialogContent>- </AlertDialog>+ <DeleteProjectDialog+ project={pendingDelete()}+ onClose={() => setPendingDelete(null)}+ onDeleted={handleDeleted}+ /></DashboardSearchPanel>);}diff --git a/apps/web/src/components/dashboard/shared.tsx b/apps/web/src/components/dashboard/shared.tsxindex 56153e86..0b4f3d3d 100644--- a/apps/web/src/components/dashboard/shared.tsx+++ b/apps/web/src/components/dashboard/shared.tsx@@ -8,10 +8,21 @@ import { Icon } from "@/components/ui/icon";import { useAuth } from "@/context/auth";import { cx } from "@/lib/cva";import { trpc } from "@/lib/trpc";-import { For, Show, children, createMemo, createResource, onCleanup, type JSX } from "solid-js";+import {+ For,+ Show,+ children,+ createMemo,+ createResource,+ onCleanup,+ type JSX,+} from "solid-js";import { projectCoverKey, readProjectCover } from "@/projects";import { Separator } from "../ui/separator";import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";+import { track } from "@/lib/analytics";+import { forgetProjectBundle } from "@/lib/db";+import { deleteProject, type ProjectInfo } from "@/projects";type DashboardLabelValueProps = {@@ -442,6 +453,7 @@ export function DashboardCardButton(props: DashboardCardButtonProps) {return (<div+ data-slot="card-button"role="button"tabIndex={0}onClick={props.onClick}@@ -500,6 +512,31 @@ export function DashboardCardMeta(props: DashboardCardMetaProps) {);}+/**+ * A click handler for whatever surrounds a grid of {@link DashboardCardButton}s:+ * anything that did not land on a card is the background, and clears the+ * selection. Cards keep their own clicks.+ */+export function createBackgroundClickHandler(onCLick: () => void): JSX.EventHandlerUnion<HTMLDivElement, MouseEvent> {+ return (event) => {+ const target = event.target as HTMLElement | null;+ if (target?.closest('[data-slot="card-button"]')) return;++ onCLick();+ };+}++/**+ * Moves `project` to the Trash, and drops the bundle we were holding for it —+ * the folder is gone, so the cached copy of it is stale.+ */+export async function trashProject(project: ProjectInfo): Promise<void> {+ await deleteProject(project.dir);+ forgetProjectBundle(project.id);+ track("project_deleted");+}++type DashboardViewSectionProps = {title: string;controls: JSX.Element;@@ -509,12 +546,9 @@ type DashboardViewSectionProps = {};export function DashboardViewSection(props: DashboardViewSectionProps) {- const handleClick: JSX.EventHandlerUnion<HTMLDivElement, MouseEvent> = (event) => {- const target = event.target as HTMLElement;- if (target !== event.currentTarget && target.dataset.slot !== "card-grid") return;-- props.onBackgroundClick?.();- };+ const handleClick = createBackgroundClickHandler(() =>+ props.onBackgroundClick?.(),+ );return (<div class={cx("flex min-h-0 flex-1 flex-col gap-3 pt-4", props.class)}>