feat: right-click menus everywhere, workspace/session cost, long-term DB safety

Right-click menus: one portal shell (components/ContextMenu.tsx) for every
item with actions - sidebar/Recents/archived session rows (SessionMenu),
console tabs, pane headers, the Changes panel's files/tree/commits, the
workspace home, sessions outside a workspace (SessionRowMenu: Sessions
table, Dashboard, Activity, Workflow runs), prompt images, every Settings
list and every Claude Code/Codex config list (FileItemMenu). Buttons stay;
a delete keeps its button's confirm; the shell stops portal clicks from
reaching the card that opened it.

Status bar: Workspace $ and Session $ on the Workspace page, from
?lane= on GET /api/pricing/cost and the focused/replayed session's cost.

Database, no data dropped: synchronous=NORMAL, journal_size_limit 64 MB,
hourly PASSIVE checkpoint + PRAGMA optimize, idx_events_session_created
(replacing the redundant idx_events_session), watchdog reads
sessions.transcript_path, transcript parsed before the hook's write
transaction, GET /api/sessions/:id?events=0 for the web page (reloads only
on its own session, debounced), streamed export, transactional clear-data
and chunked cleanup, subagent import reads tool_use_ids once, runtime logs
rotated past 5 MB.
This commit is contained in:
2026-10-05 09:37:48 +07:00
parent 65fe680d38
commit ad6e80192d
80 changed files with 2914 additions and 489 deletions
+135
View File
@@ -0,0 +1,135 @@
/**
* @file ContextMenu — the one shell every right-click (and `⋯`) menu opens
* in: a fixed list at a viewport point, rendered into `document.body` so no
* transformed or clipped ancestor moves it, nudged back inside the viewport
* once measured, closed by Escape, a press outside it, or after an item runs.
* `useContextMenu` keeps "which item, where" for a list of rows; `menuPoint`
* turns a mouse event into that point (and stops the browser's own menu).
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
import { useCallback, useEffect, useLayoutEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import type { MouseEvent as ReactMouseEvent } from "react";
export type MenuPoint = { x: number; y: number };
/** The pointer of a right-click, the browser's own menu suppressed. */
export function menuPoint(e: ReactMouseEvent): MenuPoint {
e.preventDefault();
e.stopPropagation();
return { x: e.clientX, y: e.clientY };
}
/** Menu state for a list of rows: which row's menu is open, and where. */
export function useContextMenu<T>() {
const [menu, setMenu] = useState<(MenuPoint & { item: T }) | null>(null);
const open = useCallback((e: ReactMouseEvent, item: T) => {
setMenu({ ...menuPoint(e), item });
}, []);
const close = useCallback(() => setMenu(null), []);
return { menu, open, close };
}
const MARGIN = 8;
const stop = (e: { stopPropagation: () => void }) => e.stopPropagation();
export function ContextMenu({
x,
y,
onClose,
testId,
className = "w-56",
children,
}: MenuPoint & {
onClose: () => void;
testId?: string;
className?: string;
children: ReactNode;
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState({ left: x, top: y });
useLayoutEffect(() => {
const r = ref.current?.getBoundingClientRect();
if (!r) return;
setPos({
left: Math.max(MARGIN, Math.min(x, window.innerWidth - r.width - MARGIN)),
top: Math.max(MARGIN, Math.min(y, window.innerHeight - r.height - MARGIN)),
});
}, [x, y]);
useEffect(() => {
const onDown = (e: MouseEvent) => {
if (!ref.current?.contains(e.target as Node)) onClose();
};
const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose();
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
window.addEventListener("blur", onClose);
window.addEventListener("resize", onClose);
return () => {
document.removeEventListener("mousedown", onDown);
document.removeEventListener("keydown", onKey);
window.removeEventListener("blur", onClose);
window.removeEventListener("resize", onClose);
};
}, [onClose]);
return createPortal(
<div
ref={ref}
role="menu"
data-testid={testId}
style={pos}
// A portal still bubbles React events to the component that rendered
// it — a menu opened from a clickable card must not click the card too.
onClick={stop}
onDoubleClick={stop}
onMouseDown={stop}
onPointerDown={stop}
onContextMenu={(e) => {
e.preventDefault();
e.stopPropagation();
}}
className={`fixed z-50 rounded-lg border border-border bg-surface-2 p-1 shadow-lg ${className}`}
>
{children}
</div>,
document.body
);
}
export const MENU_ITEM =
"flex w-full items-center gap-2 rounded px-2 py-1 text-left text-xs text-fg-secondary hover:bg-surface-4 hover:text-fg-primary disabled:pointer-events-none disabled:opacity-50";
export function MenuItem({
onSelect,
danger,
disabled,
testId,
children,
}: {
onSelect: () => void;
danger?: boolean;
disabled?: boolean;
testId?: string;
children: ReactNode;
}) {
return (
<button
type="button"
role="menuitem"
data-testid={testId}
disabled={disabled}
className={`${MENU_ITEM}${danger ? " !text-status-danger" : ""}`}
onClick={onSelect}
>
{children}
</button>
);
}
export function MenuSeparator() {
return <div role="separator" className="my-1 border-t border-border" />;
}