import { useCallback, useRef } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import { Archive, Code2, Copy, ExternalLink, GitBranch, GitFork, Pencil, Pin, PinOff, Play } from "@/components/app-icons"; import type { OpenTarget } from "../../shared/editor-handoff"; import { resumeAgentOnOpen, useCanResumeAgent } from "../hooks/useCanResumeAgent"; import { conversationQueryKey } from "../hooks/useConversation"; import { editorHandoffErrorMessage, useEditorHandoffState, useOpenSessionTarget } from "../hooks/useEditorHandoff"; import { usePinSession, useUnpinSession } from "../hooks/usePinSession"; import { workspaceQueryKeyForHost } from "../hooks/useWorkspaceQuery"; import { aoBridge } from "../lib/bridge"; import { sessionBranchPresentation } from "../lib/session-branch"; import { useUiStore } from "../stores/ui-store"; import { isOrchestratorSession, primaryPR, type WorkspaceSession } from "../types/workspace"; import { MenuEntries, type MenuEntry } from "./menu-model"; import { OpenTargetIcon } from "./OpenTargetIcon"; import { useForkSessionFromMenu, useSessionForkOptions } from "./SessionForkControls"; /** Copy from a menu, which has no inline place to confirm the copy. */ export function useCopyToClipboard() { const { t } = useTranslation(); const showGlobalToast = useUiStore((state) => state.showGlobalToast); return useCallback( (text: string) => { void aoBridge.clipboard.writeText(text).then( () => showGlobalToast(t("shell.copied")), () => showGlobalToast(t("shell.copyFailed"), undefined, "error"), ); }, [showGlobalToast, t], ); } export type SessionMenuOptions = { /** Absent where the session cannot be archived from here. */ onArchive?: () => void; /** Absent where the title is not editable here; the row shows disabled. */ onRename?: () => void; /** Key hint for the rename row, where a focused trigger takes one. */ renameShortcut?: string; }; /** * A session's actions as menu entries: open in and copy path, rename and pin, * resume, PR and branch links, then archive. Call it from menu content, which * Radix mounts only while open, so the lookups run on demand rather than once * per sidebar row. An absent session yields no entries. */ export function useSessionMenuEntries(session: WorkspaceSession | undefined, { onArchive, onRename, renameShortcut }: SessionMenuOptions = {}): MenuEntry[] { const { t } = useTranslation(); const queryClient = useQueryClient(); const showGlobalToast = useUiStore((state) => state.showGlobalToast); const copyToClipboard = useCopyToClipboard(); const { mutate: pinSession } = usePinSession(); const { mutate: unpinSession } = useUnpinSession(); // A local worker whose agent exited and that is not switching interfaces; // orchestrators and remote sessions resume elsewhere, so they skip the check. const canResume = useCanResumeAgent(!session || session.hostId || isOrchestratorSession(session) ? undefined : session); // Remote and Cloud sessions have no local workspace for Electron to hand off. const hasLocalWorkspace = Boolean(session && !session.hostId && !session.cloud); const workspaceEntries = useSessionWorkspaceEntries(hasLocalWorkspace ? session : undefined); const forkSession = useForkSessionFromMenu(); // Cloud sessions can't be forked here; for the rest the daemon says // whether this one can, and why not. const forkable = Boolean(session && !session.cloud); const forkOptions = useSessionForkOptions(session, forkable); if (!session) return []; const branch = sessionBranchPresentation(session).branch; const showBranch = Boolean(branch) && !isOrchestratorSession(session) && branch !== `session/${session.id}`; const pr = primaryPR(session); // Rename and pin go to whoever owns the session: its daemon, or the control // plane for a Cloud session. A session shared from another organization is // the sharer's to name and lists outside this sidebar's pins. const canRenameAndPin = !session.cloud?.shared; const resume = () => { void resumeAgentOnOpen(session.id) .catch((error: unknown) => { showGlobalToast(t("inspector.resumeAgent"), error instanceof Error ? error.message : undefined, "error"); }) .finally(() => { void queryClient.invalidateQueries({ queryKey: workspaceQueryKeyForHost(session.hostId) }); void queryClient.invalidateQueries({ queryKey: conversationQueryKey(session.id) }); }); }; const openPullRequest = (url: string, title: string) => { aoBridge.app.openExternal(url).catch((error: unknown) => { showGlobalToast(title, error instanceof Error ? error.message : undefined, "error"); }); }; const entries: MenuEntry[] = [...workspaceEntries, { kind: "separator", id: "session-workspace-sep" }]; if (canRenameAndPin) { entries.push( { kind: "item", id: "session-rename", ariaLabel: t("shell.renameSession", { title: session.title }), disabled: !onRename, icon: