Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions templates/content/app/components/editor/database/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import {
import { useEffect, useState, type MouseEvent, type ReactNode } from "react";
import { Link } from "react-router";

import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import {
Collapsible,
Expand Down Expand Up @@ -727,7 +717,6 @@ function DatabaseSidebarRow({
};
}) {
const t = useT();
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
const canEdit = item.document.canEdit !== false;
const canManage =
item.document.canManage === true ||
Expand Down Expand Up @@ -858,7 +847,7 @@ function DatabaseSidebarRow({
{canManage && onDeleteItem ? (
<DropdownMenuItem
className="text-destructive focus:text-destructive"
onSelect={() => setDeleteDialogOpen(true)}
onSelect={() => onDeleteItem(item)}
>
<IconTrash className="me-2 size-4" />
{t("database.delete")}
Expand Down Expand Up @@ -903,28 +892,6 @@ function DatabaseSidebarRow({
</div>
)}
</div>

<AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t("sidebar.deletePageQuestion")}
</AlertDialogTitle>
<AlertDialogDescription>
{t("sidebar.deletePageDescription", { title })}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t("comments.cancel")}</AlertDialogCancel>
<AlertDialogAction
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
onClick={() => onDeleteItem?.(item)}
>
{t("database.delete")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,23 @@ describe("document sidebar layout", () => {
expect(sidebar).not.toContain("!localFileMode && favorites.length > 0");
});

it("keeps delete confirmation owned by the stable sidebar", () => {
const sidebar = readSidebarSource("./DocumentSidebar.tsx");
const treeItem = readSidebarSource("./DocumentTreeItem.tsx");
const databaseSidebar = readSidebarSource("../editor/database/sidebar.tsx");

expect(sidebar).toContain("const [pendingDelete, setPendingDelete]");
expect(sidebar).toContain("open={pendingDelete !== null}");
expect(sidebar).toContain("confirmedDeleteIdRef");
expect(sidebar).toContain("window.requestAnimationFrame");
expect(sidebar).toContain('document.body.style.pointerEvents === "none"');
expect(sidebar).toContain("void handleDelete(confirmedDeleteId)");
expect(treeItem).not.toContain("deleteDialogOpen");
expect(treeItem).not.toContain("<AlertDialog");
expect(databaseSidebar).not.toContain("deleteDialogOpen");
expect(databaseSidebar).not.toContain("<AlertDialog");
});

it("keeps the Content sidebar quiet while lists load", () => {
const sidebar = readSidebarSource("./DocumentSidebar.tsx");

Expand Down
80 changes: 77 additions & 3 deletions templates/content/app/components/sidebar/DocumentSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ const SIDEBAR_SECTION_COLLAPSE_STORAGE_KEY =
const TRASH_COLLAPSED_DEFAULT_MIGRATION_KEY =
"content-sidebar-trash-collapsed-default-v2";
const CONTENT_SIDEBAR_STATE_VERSION = 1 as const;

function afterBodyPointerUnlock(callback: () => void) {
const run = () => {
if (document.body.style.pointerEvents === "none") {
window.requestAnimationFrame(run);
return;
}
callback();
};
window.requestAnimationFrame(run);
}

interface ContentSidebarStateSnapshot {
version: typeof CONTENT_SIDEBAR_STATE_VERSION;
expandedWorkspaceIds: string[];
Expand Down Expand Up @@ -906,6 +918,11 @@ export function DocumentSidebar({
}, [setStoredCollapsedSections]);
const [removeLocalFilesDialogOpen, setRemoveLocalFilesDialogOpen] =
useState(false);
const [pendingDelete, setPendingDelete] = useState<{
id: string;
title: string;
} | null>(null);
const confirmedDeleteIdRef = useRef<string | null>(null);
const settingsActive = location.pathname.startsWith("/settings");
const sensors = useSensors(
useSensor(PointerSensor, {
Expand Down Expand Up @@ -1258,6 +1275,12 @@ export function DocumentSidebar({
],
);

const requestDelete = useCallback((id: string, title: string) => {
afterBodyPointerUnlock(() => {
setPendingDelete({ id, title });
});
}, []);

const handleReorderPage = useCallback(
async (id: string, overId: string) => {
if (id === overId) return;
Expand Down Expand Up @@ -1520,7 +1543,7 @@ export function DocumentSidebar({
}}
onCreateChildPage={(parentId) => handleCreatePage(parentId)}
onCreateChildDatabase={(parentId) => handleCreateDatabase(parentId)}
onDelete={handleDelete}
onDelete={requestDelete}
onToggleFavorite={handleToggleFavorite}
/>
))}
Expand Down Expand Up @@ -1800,7 +1823,12 @@ export function DocumentSidebar({
onCreateChildDatabase={(nextSpace, item) =>
void handleCreateDatabase(item.document.id, nextSpace.id)
}
onDeleteItem={(item) => void handleDelete(item.document.id)}
onDeleteItem={(item) =>
requestDelete(
item.document.id,
item.document.title || t("sidebar.untitled"),
)
}
onToggleFavorite={(item) =>
handleToggleFavorite(item.document.id, !item.document.isFavorite)
}
Expand Down Expand Up @@ -2331,7 +2359,10 @@ export function DocumentSidebar({
void handleCreateDatabase(item.document.id)
}
onDeleteItem={(item) =>
void handleDelete(item.document.id)
requestDelete(
item.document.id,
item.document.title || t("sidebar.untitled"),
)
}
onToggleFavorite={(item) =>
handleToggleFavorite(item.document.id, false)
Expand Down Expand Up @@ -2399,6 +2430,49 @@ export function DocumentSidebar({
onMouseDown={handleMouseDown}
/>
)}
<AlertDialog
open={pendingDelete !== null}
onOpenChange={(open) => {
if (open) return;
setPendingDelete(null);
const confirmedDeleteId = confirmedDeleteIdRef.current;
confirmedDeleteIdRef.current = null;
if (confirmedDeleteId) {
afterBodyPointerUnlock(() => {
void handleDelete(confirmedDeleteId);
});
}
}}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{t("sidebar.deletePageQuestion")}
</AlertDialogTitle>
<AlertDialogDescription>
{pendingDelete
? t("sidebar.deletePageDescription", {
title: pendingDelete.title,
})
: null}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>{t("comments.cancel")}</AlertDialogCancel>
<AlertDialogAction
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
disabled={
deleteDocument.isPending || deleteContentDatabase.isPending
}
onClick={() => {
confirmedDeleteIdRef.current = pendingDelete?.id ?? null;
}}
>
{t("database.delete")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog
open={removeLocalFilesDialogOpen}
onOpenChange={setRemoveLocalFilesDialogOpen}
Expand Down
Loading
Loading