|
| 1 | +import { createSignal, Show } from "solid-js" |
| 2 | +import { ContextMenu } from "@deepagent-code/ui/context-menu" |
| 3 | +import type { FileNode } from "@deepagent-code/sdk/v2" |
| 4 | +import { useFile } from "@/context/file" |
| 5 | +import { useLanguage } from "@/context/language" |
| 6 | +import { showToast } from "@/utils/toast" |
| 7 | +import { desktopApi, isLocalFilesystemOp } from "@/utils/desktop-api" |
| 8 | +import { canExtract, canOpenTimeline, canPaste, parentPath } from "./file-tree-menu" |
| 9 | + |
| 10 | +// Process-local clipboard for copy/cut → paste between file-tree nodes. Lives for the renderer |
| 11 | +// lifetime; cut entries are cleared after a successful paste. |
| 12 | +type Clip = { mode: "copy" | "cut"; absolute: string } |
| 13 | +const [clip, setClip] = createSignal<Clip | null>(null) |
| 14 | + |
| 15 | +export function FileTreeMenuContent(props: { |
| 16 | + node: FileNode |
| 17 | + onRename: () => void |
| 18 | + onOpenTimeline: (node: FileNode) => void |
| 19 | +}) { |
| 20 | + const file = useFile() |
| 21 | + const language = useLanguage() |
| 22 | + |
| 23 | + // File-ops and the git timeline run in the desktop main process against the local filesystem. |
| 24 | + // They are only available on the desktop build AND when the connected sidecar is local |
| 25 | + // (loopback). On the web build or against a remote Server Edition, these menu items degrade to |
| 26 | + // disabled so the user never triggers an operation that would fail or touch the wrong host. |
| 27 | + const localFs = () => isLocalFilesystemOp({ desktop: Boolean(desktopApi()), localSidecar: file.isLocalSidecar() }) |
| 28 | + const root = () => file.directory() |
| 29 | + |
| 30 | + const refresh = () => { |
| 31 | + void file.tree.refresh(parentPath(props.node.path)) |
| 32 | + if (props.node.type === "directory") void file.tree.refresh(props.node.path) |
| 33 | + } |
| 34 | + |
| 35 | + const report = ( |
| 36 | + res: { ok: boolean; error?: string } | undefined, |
| 37 | + okKey: string, |
| 38 | + errKey: string, |
| 39 | + ) => { |
| 40 | + if (!res) return |
| 41 | + if (res.ok) { |
| 42 | + showToast({ variant: "success", title: language.t(okKey) }) |
| 43 | + refresh() |
| 44 | + return |
| 45 | + } |
| 46 | + showToast({ variant: "error", title: language.t(errKey), description: res.error }) |
| 47 | + } |
| 48 | + |
| 49 | + const copyText = (text: string) => { |
| 50 | + void navigator.clipboard?.writeText(text).then(() => |
| 51 | + showToast({ variant: "success", title: language.t("fileTree.copied") }), |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + const markClip = (mode: "copy" | "cut") => { |
| 56 | + setClip({ mode, absolute: props.node.absolute }) |
| 57 | + showToast({ variant: "success", title: language.t(mode === "copy" ? "fileTree.copied" : "fileTree.cut") }) |
| 58 | + } |
| 59 | + |
| 60 | + const paste = async () => { |
| 61 | + const current = clip() |
| 62 | + if (!current) return |
| 63 | + const destDir = props.node.absolute |
| 64 | + const res = |
| 65 | + current.mode === "copy" |
| 66 | + ? await desktopApi()?.fileOps?.copy(root(), current.absolute, destDir) |
| 67 | + : await desktopApi()?.fileOps?.move(root(), current.absolute, destDir) |
| 68 | + if (!res) return |
| 69 | + if (res.ok) { |
| 70 | + if (current.mode === "cut") setClip(null) |
| 71 | + showToast({ variant: "success", title: language.t("fileTree.pasted") }) |
| 72 | + refresh() |
| 73 | + return |
| 74 | + } |
| 75 | + showToast({ variant: "error", title: language.t("fileTree.pasteFailed"), description: res.error }) |
| 76 | + } |
| 77 | + |
| 78 | + const remove = async () => { |
| 79 | + if (!window.confirm(language.t("fileTree.deleteConfirm", { name: props.node.name }))) return |
| 80 | + report(await desktopApi()?.fileOps?.remove(root(), props.node.absolute), "fileTree.deleted", "fileTree.deleteFailed") |
| 81 | + } |
| 82 | + |
| 83 | + const archive = async () => { |
| 84 | + report(await desktopApi()?.fileOps?.archive(root(), props.node.absolute), "fileTree.archived", "fileTree.archiveFailed") |
| 85 | + } |
| 86 | + |
| 87 | + const extract = async () => { |
| 88 | + report(await desktopApi()?.fileOps?.extract(root(), props.node.absolute), "fileTree.extracted", "fileTree.extractFailed") |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <> |
| 93 | + <ContextMenu.Item onSelect={() => copyText(props.node.path)}> |
| 94 | + <ContextMenu.ItemLabel>{language.t("fileTree.copyRelativePath")}</ContextMenu.ItemLabel> |
| 95 | + </ContextMenu.Item> |
| 96 | + <ContextMenu.Item onSelect={() => copyText(props.node.absolute)}> |
| 97 | + <ContextMenu.ItemLabel>{language.t("fileTree.copyAbsolutePath")}</ContextMenu.ItemLabel> |
| 98 | + </ContextMenu.Item> |
| 99 | + <ContextMenu.Separator /> |
| 100 | + <ContextMenu.Item onSelect={() => markClip("copy")} disabled={!localFs()}> |
| 101 | + <ContextMenu.ItemLabel>{language.t("fileTree.copy")}</ContextMenu.ItemLabel> |
| 102 | + </ContextMenu.Item> |
| 103 | + <ContextMenu.Item onSelect={() => markClip("cut")} disabled={!localFs()}> |
| 104 | + <ContextMenu.ItemLabel>{language.t("fileTree.cut")}</ContextMenu.ItemLabel> |
| 105 | + </ContextMenu.Item> |
| 106 | + <Show when={canPaste({ nodeType: props.node.type, hasClip: Boolean(clip()), localFs: localFs() })}> |
| 107 | + <ContextMenu.Item onSelect={paste}> |
| 108 | + <ContextMenu.ItemLabel>{language.t("fileTree.paste")}</ContextMenu.ItemLabel> |
| 109 | + </ContextMenu.Item> |
| 110 | + </Show> |
| 111 | + <ContextMenu.Separator /> |
| 112 | + <ContextMenu.Item onSelect={remove} disabled={!localFs()}> |
| 113 | + <ContextMenu.ItemLabel>{language.t("common.delete")}</ContextMenu.ItemLabel> |
| 114 | + </ContextMenu.Item> |
| 115 | + <ContextMenu.Item onSelect={() => props.onRename()} disabled={!localFs()}> |
| 116 | + <ContextMenu.ItemLabel>{language.t("common.rename")}</ContextMenu.ItemLabel> |
| 117 | + </ContextMenu.Item> |
| 118 | + <ContextMenu.Separator /> |
| 119 | + <ContextMenu.Item onSelect={archive} disabled={!localFs()}> |
| 120 | + <ContextMenu.ItemLabel>{language.t("fileTree.archive")}</ContextMenu.ItemLabel> |
| 121 | + </ContextMenu.Item> |
| 122 | + <Show when={canExtract({ nodeType: props.node.type, name: props.node.name })}> |
| 123 | + <ContextMenu.Item onSelect={extract} disabled={!localFs()}> |
| 124 | + <ContextMenu.ItemLabel>{language.t("fileTree.extract")}</ContextMenu.ItemLabel> |
| 125 | + </ContextMenu.Item> |
| 126 | + </Show> |
| 127 | + <Show when={canOpenTimeline({ nodeType: props.node.type, localFs: localFs() })}> |
| 128 | + <ContextMenu.Separator /> |
| 129 | + <ContextMenu.Item onSelect={() => props.onOpenTimeline(props.node)}> |
| 130 | + <ContextMenu.ItemLabel>{language.t("fileTree.openTimeline")}</ContextMenu.ItemLabel> |
| 131 | + </ContextMenu.Item> |
| 132 | + </Show> |
| 133 | + </> |
| 134 | + ) |
| 135 | +} |
0 commit comments