@keith9681/dsh-sidebar 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +0 -0
  3. package/README_EN.md +0 -0
  4. package/cordis.patch.yml +49 -0
  5. package/lib/client-editor.js +34651 -0
  6. package/lib/client-mermaid.js +200629 -0
  7. package/lib/client-registry.js +13388 -0
  8. package/lib/client-terminal.js +8904 -0
  9. package/lib/client.js +13388 -0
  10. package/lib/index.js +3959 -0
  11. package/lib/invariant.js +22 -0
  12. package/lib/types/agent-pty.d.ts +226 -0
  13. package/lib/types/browser-probe.d.ts +15 -0
  14. package/lib/types/bundle-route.d.ts +12 -0
  15. package/lib/types/client/BrowserView.d.ts +22 -0
  16. package/lib/types/client/DiffTab.d.ts +6 -0
  17. package/lib/types/client/DiffView.d.ts +51 -0
  18. package/lib/types/client/EditorHost.d.ts +12 -0
  19. package/lib/types/client/FileTree.d.ts +36 -0
  20. package/lib/types/client/GitView.d.ts +8 -0
  21. package/lib/types/client/OrphanedTab.d.ts +11 -0
  22. package/lib/types/client/PdfView.d.ts +6 -0
  23. package/lib/types/client/RenderBoundary.d.ts +32 -0
  24. package/lib/types/client/SandboxStatusBar.d.ts +10 -0
  25. package/lib/types/client/SideCardSection.d.ts +74 -0
  26. package/lib/types/client/SideChatView.d.ts +16 -0
  27. package/lib/types/client/Sidebar.d.ts +6 -0
  28. package/lib/types/client/SubagentView.d.ts +16 -0
  29. package/lib/types/client/TabBar.d.ts +43 -0
  30. package/lib/types/client/TerminalView.d.ts +24 -0
  31. package/lib/types/client/TextEditor.d.ts +10 -0
  32. package/lib/types/client/TreePanel.d.ts +23 -0
  33. package/lib/types/client/UploadOverlay.d.ts +20 -0
  34. package/lib/types/client/add-plugin-modal.d.ts +15 -0
  35. package/lib/types/client/api.d.ts +268 -0
  36. package/lib/types/client/binary-download.d.ts +12 -0
  37. package/lib/types/client/breakpoints.d.ts +12 -0
  38. package/lib/types/client/browser.d.ts +50 -0
  39. package/lib/types/client/builtins/index.d.ts +18 -0
  40. package/lib/types/client/builtins/tabs.d.ts +11 -0
  41. package/lib/types/client/builtins/viewers.d.ts +3 -0
  42. package/lib/types/client/chunk-loader.d.ts +124 -0
  43. package/lib/types/client/chunks/editor.d.ts +9 -0
  44. package/lib/types/client/chunks/mermaid.d.ts +10 -0
  45. package/lib/types/client/chunks/terminal.d.ts +9 -0
  46. package/lib/types/client/cm-themes.d.ts +24 -0
  47. package/lib/types/client/conversation-draft.d.ts +14 -0
  48. package/lib/types/client/desktop-env.d.ts +35 -0
  49. package/lib/types/client/editable-state.d.ts +27 -0
  50. package/lib/types/client/editor-load.d.ts +66 -0
  51. package/lib/types/client/frame-batcher.d.ts +24 -0
  52. package/lib/types/client/icons.d.ts +57 -0
  53. package/lib/types/client/image-types.d.ts +3 -0
  54. package/lib/types/client/ime-guard.d.ts +36 -0
  55. package/lib/types/client/index.d.ts +19 -0
  56. package/lib/types/client/intercept.d.ts +33 -0
  57. package/lib/types/client/lang.d.ts +12 -0
  58. package/lib/types/client/lazy-chunk.d.ts +25 -0
  59. package/lib/types/client/link-intercept.d.ts +42 -0
  60. package/lib/types/client/locales.d.ts +366 -0
  61. package/lib/types/client/mermaid-blocks.d.ts +41 -0
  62. package/lib/types/client/mermaid-sanitize.d.ts +2 -0
  63. package/lib/types/client/mermaid.d.ts +12 -0
  64. package/lib/types/client/open-when-sized.d.ts +20 -0
  65. package/lib/types/client/open-with-settings.d.ts +4 -0
  66. package/lib/types/client/open-with.d.ts +87 -0
  67. package/lib/types/client/openpath-intercept.d.ts +41 -0
  68. package/lib/types/client/paths.d.ts +35 -0
  69. package/lib/types/client/pdf-types.d.ts +2 -0
  70. package/lib/types/client/plugin-settings.d.ts +8 -0
  71. package/lib/types/client/plugins-shared.d.ts +27 -0
  72. package/lib/types/client/plugins-tabs.d.ts +3 -0
  73. package/lib/types/client/plugins-viewers.d.ts +3 -0
  74. package/lib/types/client/prefs.d.ts +41 -0
  75. package/lib/types/client/produced-files.d.ts +23 -0
  76. package/lib/types/client/selection-payload.d.ts +27 -0
  77. package/lib/types/client/service.d.ts +440 -0
  78. package/lib/types/client/settings-nav-icon.d.ts +19 -0
  79. package/lib/types/client/shell-presets.d.ts +48 -0
  80. package/lib/types/client/sidechat-transcript.d.ts +103 -0
  81. package/lib/types/client/split-pane.d.ts +30 -0
  82. package/lib/types/client/state.d.ts +359 -0
  83. package/lib/types/client/subagent-detect.d.ts +54 -0
  84. package/lib/types/client/subagent-jobs.d.ts +63 -0
  85. package/lib/types/client/tab-content-memo.d.ts +33 -0
  86. package/lib/types/client/terminal-font.d.ts +20 -0
  87. package/lib/types/client/theme.d.ts +45 -0
  88. package/lib/types/client/titlebar-strip.d.ts +23 -0
  89. package/lib/types/client/upload.d.ts +69 -0
  90. package/lib/types/client/wco.d.ts +47 -0
  91. package/lib/types/config.d.ts +64 -0
  92. package/lib/types/context-types.d.ts +603 -0
  93. package/lib/types/fs-operations.d.ts +28 -0
  94. package/lib/types/fs-search.d.ts +24 -0
  95. package/lib/types/fs-tree.d.ts +51 -0
  96. package/lib/types/git.d.ts +82 -0
  97. package/lib/types/html-route.d.ts +59 -0
  98. package/lib/types/index.d.ts +47 -0
  99. package/lib/types/invariant.d.ts +15 -0
  100. package/lib/types/jobs-routes.d.ts +45 -0
  101. package/lib/types/open-external.d.ts +24 -0
  102. package/lib/types/prefs-shared.d.ts +225 -0
  103. package/lib/types/pty-deps.d.ts +78 -0
  104. package/lib/types/pty-manager.d.ts +168 -0
  105. package/lib/types/sidechat-core.d.ts +172 -0
  106. package/lib/types/sidechat-routes.d.ts +30 -0
  107. package/lib/types/subagent-activity.d.ts +44 -0
  108. package/lib/types/subagent-live-route.d.ts +44 -0
  109. package/lib/types/tools.d.ts +31 -0
  110. package/lib/types/trust-fence.d.ts +24 -0
  111. package/lib/types/wire.d.ts +38 -0
  112. package/package.json +173 -0
  113. package/scripts/install.ps1 +289 -0
  114. package/scripts/install.sh +287 -0
  115. package/src/agent-pty.ts +527 -0
  116. package/src/browser-probe.ts +26 -0
  117. package/src/bundle-route.ts +129 -0
  118. package/src/client/BrowserView.tsx +253 -0
  119. package/src/client/DiffTab.tsx +110 -0
  120. package/src/client/DiffView.tsx +305 -0
  121. package/src/client/EditorHost.tsx +525 -0
  122. package/src/client/FileTree.tsx +653 -0
  123. package/src/client/GitView.tsx +558 -0
  124. package/src/client/OrphanedTab.tsx +27 -0
  125. package/src/client/PdfView.tsx +110 -0
  126. package/src/client/RenderBoundary.tsx +49 -0
  127. package/src/client/SandboxStatusBar.tsx +60 -0
  128. package/src/client/SideCardSection.module.css +960 -0
  129. package/src/client/SideCardSection.tsx +1152 -0
  130. package/src/client/SideChatView.module.css +437 -0
  131. package/src/client/SideChatView.tsx +644 -0
  132. package/src/client/Sidebar.tsx +1148 -0
  133. package/src/client/SubagentView.module.css +547 -0
  134. package/src/client/SubagentView.tsx +883 -0
  135. package/src/client/TabBar.tsx +305 -0
  136. package/src/client/TerminalView.tsx +383 -0
  137. package/src/client/TextEditor.tsx +500 -0
  138. package/src/client/TreePanel.tsx +273 -0
  139. package/src/client/UploadOverlay.tsx +62 -0
  140. package/src/client/add-plugin-modal.tsx +199 -0
  141. package/src/client/api.ts +335 -0
  142. package/src/client/binary-download.tsx +23 -0
  143. package/src/client/breakpoints.ts +51 -0
  144. package/src/client/browser.ts +119 -0
  145. package/src/client/builtins/index.ts +37 -0
  146. package/src/client/builtins/tabs.tsx +331 -0
  147. package/src/client/builtins/viewers.tsx +125 -0
  148. package/src/client/chunk-loader.ts +351 -0
  149. package/src/client/chunks/editor.tsx +9 -0
  150. package/src/client/chunks/mermaid.tsx +10 -0
  151. package/src/client/chunks/terminal.tsx +9 -0
  152. package/src/client/cm-themes.ts +129 -0
  153. package/src/client/conversation-draft.ts +29 -0
  154. package/src/client/css-modules.d.ts +5 -0
  155. package/src/client/desktop-env.ts +72 -0
  156. package/src/client/editable-state.ts +70 -0
  157. package/src/client/editor-load.ts +92 -0
  158. package/src/client/frame-batcher.ts +56 -0
  159. package/src/client/icons.tsx +161 -0
  160. package/src/client/image-types.ts +8 -0
  161. package/src/client/ime-guard.ts +47 -0
  162. package/src/client/index.tsx +369 -0
  163. package/src/client/intercept.tsx +110 -0
  164. package/src/client/lang.ts +103 -0
  165. package/src/client/layout.css +94 -0
  166. package/src/client/lazy-chunk.tsx +89 -0
  167. package/src/client/link-intercept.ts +73 -0
  168. package/src/client/locales.ts +734 -0
  169. package/src/client/mermaid-blocks.ts +110 -0
  170. package/src/client/mermaid-sanitize.ts +93 -0
  171. package/src/client/mermaid.tsx +401 -0
  172. package/src/client/open-when-sized.ts +43 -0
  173. package/src/client/open-with-settings.tsx +135 -0
  174. package/src/client/open-with.ts +221 -0
  175. package/src/client/openpath-intercept.ts +62 -0
  176. package/src/client/paths.ts +47 -0
  177. package/src/client/pdf-types.ts +4 -0
  178. package/src/client/plugin-settings.ts +43 -0
  179. package/src/client/plugins-shared.ts +29 -0
  180. package/src/client/plugins-tabs.ts +87 -0
  181. package/src/client/plugins-viewers.ts +30 -0
  182. package/src/client/prefs.ts +207 -0
  183. package/src/client/produced-files.ts +91 -0
  184. package/src/client/selection-payload.ts +86 -0
  185. package/src/client/service.ts +838 -0
  186. package/src/client/settings-nav-icon.ts +45 -0
  187. package/src/client/shell-presets.ts +83 -0
  188. package/src/client/sidebar.module.css +2577 -0
  189. package/src/client/sidechat-transcript.ts +321 -0
  190. package/src/client/split-pane.tsx +322 -0
  191. package/src/client/state.ts +1175 -0
  192. package/src/client/subagent-detect.ts +137 -0
  193. package/src/client/subagent-jobs.ts +161 -0
  194. package/src/client/tab-content-memo.ts +46 -0
  195. package/src/client/terminal-font.ts +28 -0
  196. package/src/client/theme.ts +110 -0
  197. package/src/client/titlebar-strip.ts +37 -0
  198. package/src/client/upload.ts +187 -0
  199. package/src/client/wco.ts +120 -0
  200. package/src/config.ts +155 -0
  201. package/src/context-types.ts +610 -0
  202. package/src/fs-operations.ts +99 -0
  203. package/src/fs-search.ts +86 -0
  204. package/src/fs-tree.ts +158 -0
  205. package/src/git.ts +244 -0
  206. package/src/html-route.ts +106 -0
  207. package/src/index.ts +1132 -0
  208. package/src/invariant.ts +32 -0
  209. package/src/jobs-routes.ts +257 -0
  210. package/src/open-external.ts +90 -0
  211. package/src/prefs-shared.ts +268 -0
  212. package/src/pty-deps.ts +240 -0
  213. package/src/pty-manager.ts +376 -0
  214. package/src/sidechat-core.ts +468 -0
  215. package/src/sidechat-routes.ts +344 -0
  216. package/src/subagent-activity.ts +91 -0
  217. package/src/subagent-live-route.ts +93 -0
  218. package/src/tools.ts +479 -0
  219. package/src/trust-fence.ts +77 -0
  220. package/src/wire.ts +100 -0
@@ -0,0 +1,653 @@
1
+ /**
2
+ * The controlled file tree behind the files window's tree panel (TreePanel
3
+ * wraps it with the search box): a lazy VSCode-style tree rooted at the
4
+ * session's working directory. Levels load on expansion (one API call per
5
+ * directory), directories sort first, hidden entries render dimmed. The
6
+ * expansion set lives in the per-session state (owned by the caller); the
7
+ * caller also owns the refresh affordance — a `refreshTick` bump wipes the
8
+ * level cache so the visible set reloads.
9
+ *
10
+ * Row actions: hovering a row reveals an @-reference button on the far
11
+ * right (appends `@<relative path>` to the composer draft), and right-click
12
+ * opens a context menu: file rows offer the caller's open escapes
13
+ * (new tab / to the side, only when the callbacks exist) and a download
14
+ * action (the host serves raw bytes, binary-safe); directory rows offer
15
+ * "upload here"; every row can copy the relative or absolute path (with a
16
+ * brief "copied" label replacing the button after a successful write).
17
+ *
18
+ * Uploads start here (drag-drop or the context menu picker) but run in the
19
+ * caller: every request is reported through `onUploadRequest(dir, items)`
20
+ * (VSCode semantics — a drop on a file row targets its parent directory),
21
+ * and `busy` gates new drags while one upload is in flight.
22
+ */
23
+ import { useCallback, useEffect, useRef, useState, type DragEvent, type MouseEvent, type ReactNode } from 'react'
24
+ import { createPortal } from 'react-dom'
25
+ import clsx from 'clsx'
26
+ import {
27
+ IconChevronRightOutline14, IconCodeOutline16, IconCopyOutline16, IconDownloadOutline16,
28
+ IconLinkOutline16, Menu, type MenuEntry, type MenuItem, writeClipboard,
29
+ } from '@deepseek-ai/dsh-client-ui-primitives'
30
+ import { SiCursor, SiZedindustries } from 'react-icons/si'
31
+ import { VscFile, VscFolder, VscFolderOpened, VscLinkExternal, VscPin, VscPinned } from 'react-icons/vsc'
32
+ import { api, downloadUrl, type FsEntry } from './api.ts'
33
+ import { IconUploadOutline16, IconVscode16 } from './icons.tsx'
34
+ import type { OpenWithTarget } from './open-with.ts'
35
+ import { relativeTo } from './paths.ts'
36
+ import { t } from './locales.ts'
37
+ import { uploadItemsFromDrop, uploadItemsFromFiles, type UploadItem } from './upload.ts'
38
+ import css from './sidebar.module.css'
39
+
40
+ interface LevelData {
41
+ entries?: FsEntry[]
42
+ error?: string
43
+ }
44
+
45
+ /** Root label: the last path segment (mirror of the host rootLabel). */
46
+ export function baseName(path: string): string {
47
+ const trimmed = path.replace(/[\\/]+$/, '')
48
+ const at = Math.max(trimmed.lastIndexOf('/'), trimmed.lastIndexOf('\\'))
49
+ return at === -1 ? trimmed : trimmed.slice(at + 1)
50
+ }
51
+
52
+ /** The containing directory of an absolute row path (never the root edge here). */
53
+ function parentOf(path: string): string {
54
+ const at = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'))
55
+ return at <= 0 ? path : path.slice(0, at)
56
+ }
57
+
58
+ /** Only OS file drags belong to the upload surface; in-app drags (tab reorder,
59
+ * split zones) must pass through untouched to the pane's tab-drop handling
60
+ * (mirror of Sidebar.tsx's panel-host shield gate). */
61
+ function isFileDrag(event: DragEvent): boolean {
62
+ return event.dataTransfer?.types.includes('Files') ?? false
63
+ }
64
+
65
+ /** How long the row's "copied" label stays after a successful write. */
66
+ const COPIED_MS = 1200
67
+
68
+ /**
69
+ * The drop overlay's hero art: an arrow rising out of a notched tray
70
+ * (upload zone — the same glyph family as the toolbar's upload icon) and a
71
+ * tilted pair of photo cards (chat zone). Hand-drawn, colored in the
72
+ * palette of DSH's own native drop illustration (#3964FE / #9CE5ED) so the
73
+ * two zones read as one family; the drop overlay is this flow's one brand
74
+ * moment, so it gets color the rest of the UI never does.
75
+ */
76
+ const UploadDropIllustration = () => (
77
+ <svg width="64" height="56" viewBox="0 0 64 56" fill="none" aria-hidden="true">
78
+ <path d="M32 28V11" stroke="#3964FE" strokeWidth="5" strokeLinecap="round" />
79
+ <path d="M23 20l9-9 9 9" stroke="#3964FE" strokeWidth="5" strokeLinecap="round" strokeLinejoin="round" />
80
+ <path
81
+ d="M10 40a4 4 0 0 1 4-4h7l3.2 4.6a5 5 0 0 0 4.1 2.2h7.4a5 5 0 0 0 4.1-2.2L43 36h7a4 4 0 0 1 4 4v2a10 10 0 0 1-10 10H20A10 10 0 0 1 10 42v-2z"
82
+ fill="#9CE5ED"
83
+ />
84
+ </svg>
85
+ )
86
+
87
+ /** The chat zone's art: two tilted photo cards, each with its own
88
+ * sun-over-mountains motif (the back card carries detail too, so it never
89
+ * reads as a bare blob). */
90
+ const ChatDropIllustration = () => (
91
+ <svg width="96" height="76" viewBox="0 0 96 76" fill="none" aria-hidden="true">
92
+ <g transform="rotate(-12 24 34)">
93
+ <rect x="6" y="16" width="36" height="36" rx="10" fill="#9CE5ED" />
94
+ <circle cx="16" cy="27" r="3.5" fill="white" />
95
+ <path d="M11 44l8-9 6 6 4-4 8 9" stroke="white" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
96
+ </g>
97
+ <g transform="rotate(8 61 35)">
98
+ <rect x="40" y="12" width="42" height="46" rx="10" fill="#3964FE" />
99
+ <circle cx="55" cy="27" r="5" fill="white" />
100
+ <path d="M46 50l10-13 7 8 6-6 9 11" stroke="white" strokeWidth="3.5" strokeLinecap="round" strokeLinejoin="round" />
101
+ </g>
102
+ </svg>
103
+ )
104
+
105
+ export function FileTree(props: {
106
+ sessionId: string
107
+ cwd: string | undefined
108
+ expanded: string[]
109
+ onToggle: (path: string) => void
110
+ onOpenFile: (path: string) => void
111
+ /** Context-menu "open in a new tab" (file rows; absent → no entry). */
112
+ onOpenFileNewTab?: (path: string) => void
113
+ /** Context-menu "open to the side" (file rows; absent → no entry). */
114
+ onOpenFileSide?: (path: string) => void
115
+ /**
116
+ * The "open with" menu: resolved external targets (already SSH-filtered
117
+ * and in menu order). Absent → the whole section is hidden.
118
+ */
119
+ openWithTargets?: OpenWithTarget[]
120
+ /** Ids of targets pinned to the menu's top level (subset of the ids). */
121
+ openWithPinned?: string[]
122
+ /** Whether the workspace is remote (appends the SSH hint to target labels). */
123
+ openWithSsh?: boolean
124
+ /** Open one target externally (reveal or URL — the caller decides). */
125
+ onOpenWith?: (targetId: string, path: string) => void
126
+ /** Toggle one target's pinned state (the submenu row's pushpin). */
127
+ onToggleOpenWithPin?: (targetId: string) => void
128
+ /** Insert `@<relative path>` into the composer draft. */
129
+ onReferenceFile: (path: string) => void
130
+ /** Bump to wipe the level cache and reload the visible set. */
131
+ refreshTick: number
132
+ /** Upload into `dir` (absolute, inside the workspace); runs in the caller. */
133
+ onUploadRequest: (dir: string, items: UploadItem[]) => void
134
+ /** True while an upload is in flight (drops are ignored). */
135
+ busy: boolean
136
+ }) {
137
+ const { sessionId, cwd, expanded, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, refreshTick, onUploadRequest, busy } = props
138
+ const [data, setData] = useState<Record<string, LevelData>>({})
139
+ const dataRef = useRef(data)
140
+ /** The row whose path was just copied ("copied" label replaces its button). */
141
+ const [copiedPath, setCopiedPath] = useState<string | null>(null)
142
+ /** Open context menu: the row path (and whether it is a directory) plus the cursor position. */
143
+ const [rowMenu, setRowMenu] = useState<{ path: string; isDir: boolean; x: number; y: number } | null>(null)
144
+ /** Whether a file drag hovers the tree (drives the portaled drop zone). */
145
+ const [dropOver, setDropOver] = useState(false)
146
+ /** The directory a drag is hovering right now (null = body, drop to root). */
147
+ const [dropTarget, setDropTarget] = useState<string | null>(null)
148
+ /**
149
+ * Enter/leave depth under the tree body. dragenter/dragleave fire per
150
+ * element along the drag path (and bubble), so a counter — DSH InputBar's
151
+ * own pattern — is the flicker-free signal; relatedTarget is unreliable
152
+ * across engines for drag events.
153
+ */
154
+ const dropDepth = useRef(0)
155
+ /** Explorer body element; its viewport rect anchors the portaled drop zone. */
156
+ const bodyRef = useRef<HTMLDivElement>(null)
157
+ /** The body's viewport rect captured at drag entry (null = not measured). */
158
+ const [dropRect, setDropRect] = useState<{ top: number; left: number; width: number; height: number } | null>(null)
159
+ /** Context-menu "upload here" target directory. */
160
+ const pendingUploadDir = useRef<string | undefined>(undefined)
161
+ const fileInputRef = useRef<HTMLInputElement>(null)
162
+
163
+ /** Reset all drag state (drop landed, the drag left, or a new drag begins). */
164
+ const resetDrop = (): void => {
165
+ dropDepth.current = 0
166
+ setDropOver(false)
167
+ setDropTarget(null)
168
+ setDropRect(null)
169
+ }
170
+
171
+ /**
172
+ * Drop handlers: always swallow the event (a dropped file must never open
173
+ * in the browser), then report the target directory to the caller. A drop
174
+ * ends the drag without further leave events, so the depth resets here.
175
+ * The payload collection is async (dropped folders are traversed through
176
+ * their entry handles — captured synchronously inside uploadItemsFromDrop
177
+ * while the dataTransfer is still live), so the request rides a then.
178
+ */
179
+ const reportDrop = (dir: string, data: DataTransfer | undefined): void => {
180
+ if (busy) return
181
+ void uploadItemsFromDrop(data).then((items) => {
182
+ if (items.length > 0) onUploadRequest(dir, items)
183
+ })
184
+ }
185
+ const handleBodyDrop = (event: DragEvent): void => {
186
+ if (!isFileDrag(event)) return
187
+ event.preventDefault()
188
+ event.stopPropagation()
189
+ resetDrop()
190
+ if (cwd !== undefined) reportDrop(cwd, event.dataTransfer)
191
+ }
192
+ const handleDirDrop = (event: DragEvent, dir: string): void => {
193
+ if (!isFileDrag(event)) return
194
+ event.preventDefault()
195
+ event.stopPropagation()
196
+ resetDrop()
197
+ reportDrop(dir, event.dataTransfer)
198
+ }
199
+ const handleFileDrop = (event: DragEvent, path: string): void => {
200
+ // VSCode semantics: dropping onto a file uploads into its directory.
201
+ handleDirDrop(event, parentOf(path))
202
+ }
203
+ const handleBodyDragEnter = (event: DragEvent): void => {
204
+ if (!isFileDrag(event)) return
205
+ event.preventDefault()
206
+ event.stopPropagation()
207
+ dropDepth.current += 1
208
+ if (busy) return
209
+ // First entry: anchor the portaled drop zone to the body's rect.
210
+ if (dropDepth.current === 1) {
211
+ const rect = bodyRef.current?.getBoundingClientRect()
212
+ setDropRect(rect === undefined ? null : { top: rect.top, left: rect.left, width: rect.width, height: rect.height })
213
+ }
214
+ setDropOver(true)
215
+ }
216
+ const handleBodyDragLeave = (): void => {
217
+ dropDepth.current = Math.max(0, dropDepth.current - 1)
218
+ if (dropDepth.current > 0) return
219
+ setDropOver(false)
220
+ setDropTarget(null)
221
+ setDropRect(null)
222
+ }
223
+ const handleBodyDragOver = (event: DragEvent): void => {
224
+ if (!isFileDrag(event)) return
225
+ event.preventDefault()
226
+ event.stopPropagation()
227
+ event.dataTransfer.dropEffect = busy ? 'none' : 'copy'
228
+ if (busy) return
229
+ // Rows stop propagation, so this only fires over non-row regions: the
230
+ // drag targets the workspace root. dragover fires continuously, making
231
+ // it the authoritative (flicker-free) place to clear the row target.
232
+ setDropTarget(null)
233
+ }
234
+ const handleRowDragOver = (event: DragEvent, dir: string): void => {
235
+ if (!isFileDrag(event)) return
236
+ event.preventDefault()
237
+ event.stopPropagation()
238
+ event.dataTransfer.dropEffect = busy ? 'none' : 'copy'
239
+ if (busy) return
240
+ setDropTarget(dir)
241
+ }
242
+
243
+ const storeLevel = useCallback((path: string, level: LevelData) => {
244
+ dataRef.current = { ...dataRef.current, [path]: level }
245
+ setData(dataRef.current)
246
+ }, [])
247
+
248
+ const loadDir = useCallback((dir: string) => {
249
+ if (dataRef.current[dir] !== undefined) return
250
+ storeLevel(dir, {})
251
+ api.fsTree({ sessionId, cwd }, dir).then((listing) => {
252
+ storeLevel(dir, { entries: listing.entries })
253
+ }).catch((error: unknown) => {
254
+ storeLevel(dir, { error: error instanceof Error ? error.message : String(error) })
255
+ })
256
+ }, [sessionId, cwd, storeLevel])
257
+
258
+ // The caller's refresh tick wipes the cache (declared BEFORE the load
259
+ // effect so the reload below sees the empty cache).
260
+ const lastTick = useRef(refreshTick)
261
+ useEffect(() => {
262
+ if (lastTick.current === refreshTick) return
263
+ lastTick.current = refreshTick
264
+ dataRef.current = {}
265
+ setData({})
266
+ }, [refreshTick])
267
+
268
+ useEffect(() => {
269
+ // Load the visible set; already-loaded levels (kept in the cache) are
270
+ // not refetched. Only the refresh tick wipes the cache.
271
+ const root = cwd
272
+ if (root === undefined) return
273
+ loadDir(root)
274
+ for (const dir of expanded) loadDir(dir)
275
+ }, [cwd, expanded, refreshTick, loadDir])
276
+
277
+ /** Copy `text`; on success flip the row's copied label for a moment. */
278
+ const copyPath = useCallback((text: string, path: string): void => {
279
+ void writeClipboard(text).then((ok) => {
280
+ if (!ok) return
281
+ setCopiedPath(path)
282
+ window.setTimeout(() => {
283
+ setCopiedPath(current => current === path ? null : current)
284
+ }, COPIED_MS)
285
+ })
286
+ }, [])
287
+
288
+ /** The row's trailing actions: the @-reference button, or the copied label. */
289
+ const rowActions = (entry: FsEntry): ReactNode => {
290
+ if (copiedPath === entry.path) {
291
+ return <span className={css.explorerCopied}>{t('copied')}</span>
292
+ }
293
+ return (
294
+ <button
295
+ type="button"
296
+ className={css.explorerRef}
297
+ aria-label={t('referenceFile')}
298
+ title={t('referenceFile')}
299
+ onClick={(event) => {
300
+ event.stopPropagation()
301
+ onReferenceFile(entry.path)
302
+ }}
303
+ >
304
+ {t('referenceFile')}
305
+ </button>
306
+ )
307
+ }
308
+
309
+ const openRowMenu = (event: MouseEvent, path: string, isDir: boolean): void => {
310
+ event.preventDefault()
311
+ event.stopPropagation()
312
+ setRowMenu({ path, isDir, x: event.clientX, y: event.clientY })
313
+ }
314
+
315
+ /** Download a file through the host route (raw bytes, binary-safe). */
316
+ const downloadFile = (path: string): void => {
317
+ const url = downloadUrl({ sessionId, cwd }, path)
318
+ const anchor = document.createElement('a')
319
+ anchor.href = url
320
+ anchor.style.display = 'none'
321
+ document.body.appendChild(anchor)
322
+ anchor.click()
323
+ anchor.remove()
324
+ }
325
+
326
+ /** The menu label of one open target: a locale key for the built-ins, the
327
+ * user's own name for custom editors, plus the SSH hint in remote mode. */
328
+ const openWithLabelOf = (target: OpenWithTarget): string => {
329
+ const name = target.nameKey !== undefined ? t(target.nameKey) : target.name
330
+ return openWithSsh === true && !target.localOnly ? `${name}${t('openWithSshSuffix')}` : name
331
+ }
332
+
333
+ /**
334
+ * The "open with" menu entries: the pinned targets as DIRECT rows, then
335
+ * the parent row with every target as a nested submenu. Both only render
336
+ * when the caller wired the feature and at least one target is visible.
337
+ */
338
+ const openWithEntries = (): MenuEntry[] => {
339
+ if (openWithTargets === undefined || onOpenWith === undefined || openWithTargets.length === 0) return []
340
+ const pinnedIds = openWithPinned ?? []
341
+ /** Brand marks for the built-ins (monochrome silhouettes, currentColor);
342
+ * reveal gets the folder glyph, custom editors a generic code mark. */
343
+ const itemIcon = (target: OpenWithTarget): ReactNode => {
344
+ if (target.kind === 'reveal') return <VscFolderOpened size={16} />
345
+ if (target.id === 'vscode') return <IconVscode16 size={16} />
346
+ if (target.id === 'cursor') return <SiCursor size={16} />
347
+ if (target.id === 'zed') return <SiZedindustries size={16} />
348
+ return <IconCodeOutline16 size={16} />
349
+ }
350
+ const pinned = openWithTargets
351
+ .filter(target => pinnedIds.includes(target.id))
352
+ .map<MenuItem>(target => ({
353
+ id: `open-with:${target.id}`,
354
+ label: openWithLabelOf(target),
355
+ icon: itemIcon(target),
356
+ }))
357
+ const submenu = openWithTargets.map<MenuItem>(target => {
358
+ const pinnedNow = pinnedIds.includes(target.id)
359
+ return {
360
+ id: `open-with:${target.id}`,
361
+ label: (
362
+ <span className={css.openWithLabel}>
363
+ <span className={css.openWithName}>{openWithLabelOf(target)}</span>
364
+ {/* The pushpin: a span (never a button — the Menu row itself is
365
+ a button, so a nested interactive element would be invalid).
366
+ Clicking it pins/unpins the target at the menu's top level
367
+ WITHOUT selecting the row: the pin stops propagation, so the
368
+ menu stays open and the icon flips on the next render. */}
369
+ <span
370
+ role="button"
371
+ tabIndex={-1}
372
+ className={clsx(css.openWithPin, pinnedNow && css.openWithPinActive)}
373
+ aria-label={pinnedNow ? t('unpinOpenWith') : t('pinOpenWith')}
374
+ title={pinnedNow ? t('unpinOpenWith') : t('pinOpenWith')}
375
+ onClick={(event) => {
376
+ event.preventDefault()
377
+ event.stopPropagation()
378
+ onToggleOpenWithPin?.(target.id)
379
+ }}
380
+ >
381
+ {pinnedNow ? <VscPinned size={14} /> : <VscPin size={14} />}
382
+ </span>
383
+ </span>
384
+ ),
385
+ icon: itemIcon(target),
386
+ }
387
+ })
388
+ return [
389
+ ...pinned,
390
+ ...(pinned.length > 0 ? [{ id: 'open-with-sep', type: 'separator' } as MenuEntry] : []),
391
+ {
392
+ id: 'open-with-menu',
393
+ // The primitives Menu renders no chevron for submenu parents — the
394
+ // trailing arrow is supplied inside the label (full-width flex row,
395
+ // right-aligned), matching how the submenu rows right-align the pin.
396
+ label: (
397
+ <span className={css.openWithLabel}>
398
+ <span className={css.openWithName}>{t('openWithMenu')}</span>
399
+ <IconChevronRightOutline14 size={14} className={css.openWithChevron} aria-hidden />
400
+ </span>
401
+ ),
402
+ icon: <VscLinkExternal size={16} />,
403
+ submenu,
404
+ },
405
+ ]
406
+ }
407
+
408
+ const root = cwd
409
+
410
+ const renderLevel = (dir: string, depth: number): ReactNode => {
411
+ const level = data[dir]
412
+ if (level === undefined) {
413
+ return <div className={css.explorerRow} style={{ paddingLeft: depth * 22 + 6 }}>{t('loading')}</div>
414
+ }
415
+ if (level.error !== undefined) {
416
+ return (
417
+ <div className={clsx(css.explorerRow, css.explorerError)} style={{ paddingLeft: depth * 22 + 6 }}>
418
+ {level.error}
419
+ </div>
420
+ )
421
+ }
422
+ const entries = level.entries ?? []
423
+ return entries.map(entry => {
424
+ if (entry.isDir) {
425
+ const isOpen = expanded.includes(entry.path)
426
+ return (
427
+ <div key={entry.path}>
428
+ <div
429
+ role="button"
430
+ tabIndex={0}
431
+ className={clsx(
432
+ css.explorerRow, css.explorerDir, entry.hidden && css.explorerHidden,
433
+ dropTarget === entry.path && css.explorerRowDropTarget,
434
+ )}
435
+ style={{ paddingLeft: depth * 22 + 6 }}
436
+ onClick={() => { onToggle(entry.path) }}
437
+ onKeyDown={(event) => {
438
+ if (event.key === 'Enter' || event.key === ' ') {
439
+ event.preventDefault()
440
+ onToggle(entry.path)
441
+ }
442
+ }}
443
+ onDragOver={(event) => { handleRowDragOver(event, entry.path) }}
444
+ onDrop={(event) => { handleDirDrop(event, entry.path) }}
445
+ onContextMenu={(event) => { openRowMenu(event, entry.path, true) }}
446
+ >
447
+ {isOpen ? <VscFolderOpened size={14} /> : <VscFolder size={14} />}
448
+ <span className={css.explorerName}>{entry.name}</span>
449
+ {entry.isSymlink && <IconLinkOutline16 size={12} className={css.explorerSymlink} />}
450
+ {rowActions(entry)}
451
+ </div>
452
+ {isOpen && renderLevel(entry.path, depth + 1)}
453
+ </div>
454
+ )
455
+ }
456
+ return (
457
+ <div
458
+ key={entry.path}
459
+ role="button"
460
+ tabIndex={0}
461
+ className={clsx(
462
+ css.explorerRow, entry.hidden && css.explorerHidden, entry.broken && css.explorerBroken,
463
+ dropTarget === parentOf(entry.path) && css.explorerRowDropTarget,
464
+ )}
465
+ style={{ paddingLeft: depth * 22 + 6 }}
466
+ title={entry.broken ? `${entry.path} — ${t('brokenSymlink')}` : entry.path}
467
+ onClick={() => { onOpenFile(entry.path) }}
468
+ onKeyDown={(event) => {
469
+ if (event.key === 'Enter' || event.key === ' ') {
470
+ event.preventDefault()
471
+ onOpenFile(entry.path)
472
+ }
473
+ }}
474
+ onDragOver={(event) => { handleRowDragOver(event, parentOf(entry.path)) }}
475
+ onDrop={(event) => { handleFileDrop(event, entry.path) }}
476
+ onContextMenu={(event) => { openRowMenu(event, entry.path, false) }}
477
+ >
478
+ <VscFile size={14} />
479
+ <span className={css.explorerName}>{entry.name}</span>
480
+ {entry.isSymlink && <IconLinkOutline16 size={12} className={css.explorerSymlink} />}
481
+ {rowActions(entry)}
482
+ </div>
483
+ )
484
+ })
485
+ }
486
+
487
+ return (
488
+ <div
489
+ ref={bodyRef}
490
+ className={css.explorerBody}
491
+ onDragEnter={handleBodyDragEnter}
492
+ onDragOver={handleBodyDragOver}
493
+ onDragLeave={handleBodyDragLeave}
494
+ onDrop={handleBodyDrop}
495
+ >
496
+ {root === undefined ? (
497
+ <div className={css.explorerEmpty}>{t('noSession')}</div>
498
+ ) : (
499
+ <>
500
+ <div
501
+ className={clsx(css.explorerRow, dropTarget === root && css.explorerRowDropTarget)}
502
+ style={{ paddingLeft: 6 }}
503
+ onDragOver={(event) => { handleRowDragOver(event, root) }}
504
+ onDrop={(event) => { handleDirDrop(event, root) }}
505
+ onContextMenu={(event) => { openRowMenu(event, root, true) }}
506
+ >
507
+ <VscFolderOpened size={14} />
508
+ <span className={css.explorerName}>{baseName(root)}</span>
509
+ {copiedPath === root
510
+ ? <span className={css.explorerCopied}>{t('copied')}</span>
511
+ : (
512
+ <button
513
+ type="button"
514
+ className={css.explorerRef}
515
+ aria-label={t('referenceFile')}
516
+ title={t('referenceFile')}
517
+ onClick={(event) => {
518
+ event.stopPropagation()
519
+ onReferenceFile(root)
520
+ }}
521
+ >
522
+ {t('referenceFile')}
523
+ </button>
524
+ )}
525
+ </div>
526
+ {data[root] !== undefined && renderLevel(root, 1)}
527
+ </>
528
+ )}
529
+ {dropOver && dropRect !== null && createPortal(
530
+ /*
531
+ * The sidebar's drop surface, portaled to document.body at z-1001 —
532
+ * above DSH's own whole-page drop mask (z-1000, see InputBar's
533
+ * document-level intake) so the two never compete. It dims the WHOLE
534
+ * viewport (the giant box-shadow spread on the zone frame is the
535
+ * mask; the zone rect itself stays clear), teaching the zone split:
536
+ * the dimmed conversation column still takes drops into the chat
537
+ * natively (this layer is pointer-inert), while the clear frame
538
+ * marks the tree as the workspace-upload zone. Deliberate exception
539
+ * to the "panel stays below the DSH float stack" rule: transient,
540
+ * and the drop always lands on the element beneath. The hint pill
541
+ * docks at the TOP edge of the zone — right under the search row,
542
+ * the first thing the eye meets — keeping the rows aimable.
543
+ */
544
+ <>
545
+ <div
546
+ className={css.uploadDropZone}
547
+ style={{
548
+ top: dropRect.top + 2,
549
+ left: dropRect.left + 2,
550
+ width: dropRect.width - 4,
551
+ height: dropRect.height - 4,
552
+ }}
553
+ >
554
+ <div className={css.uploadDropHero}>
555
+ <UploadDropIllustration />
556
+ <div className={css.uploadDropZonePill}>
557
+ <IconUploadOutline16 size={14} />
558
+ <span className={css.uploadDropZoneText}>
559
+ {dropTarget !== null ? t('uploadTo', { dir: dropTarget }) : t('uploadDropHint')}
560
+ </span>
561
+ </div>
562
+ </div>
563
+ </div>
564
+ {/* The left zone's invitation, centered in the space beside the
565
+ tree; skipped when that space is too narrow to hold it. */}
566
+ {dropRect.left >= 200 && (
567
+ <div className={css.uploadDropChatHint} style={{ width: dropRect.left }}>
568
+ <div className={css.uploadDropChatCard}>
569
+ <ChatDropIllustration />
570
+ <span>{t('uploadDropChat')}</span>
571
+ </div>
572
+ </div>
573
+ )}
574
+ </>,
575
+ document.body,
576
+ )}
577
+ {/*
578
+ The one shared context menu, positioned at the right-click cursor
579
+ (portal so the tree's overflow clip cannot crop it).
580
+ */}
581
+ <input
582
+ ref={fileInputRef}
583
+ type="file"
584
+ multiple
585
+ style={{ display: 'none' }}
586
+ onChange={(event) => {
587
+ const dir = pendingUploadDir.current ?? root
588
+ pendingUploadDir.current = undefined
589
+ if (dir !== undefined && !busy) onUploadRequest(dir, uploadItemsFromFiles(event.target.files ?? []))
590
+ event.target.value = ''
591
+ }}
592
+ />
593
+ <Menu
594
+ open={rowMenu !== null}
595
+ onClose={() => { setRowMenu(null) }}
596
+ items={[
597
+ // The open escapes head the FILE menu (dirs only get copy).
598
+ ...(rowMenu?.isDir === false && onOpenFileNewTab !== undefined
599
+ ? [{ id: 'open-new-tab', label: t('openFileNewTab'), icon: <IconCodeOutline16 size={16} /> }]
600
+ : []),
601
+ ...(rowMenu?.isDir === false && onOpenFileSide !== undefined
602
+ ? [{ id: 'open-side', label: t('openFileSide'), icon: <VscFolderOpened size={16} /> }]
603
+ : []),
604
+ ...openWithEntries(),
605
+ // Download applies to files only (the host route refuses directories).
606
+ ...(rowMenu?.isDir === false
607
+ ? [{ id: 'download', label: t('download'), icon: <IconDownloadOutline16 size={16} /> }]
608
+ : []),
609
+ // Upload into a directory (incl. the workspace root row).
610
+ ...(rowMenu?.isDir === true
611
+ ? [{ id: 'upload-here', label: t('uploadHere'), icon: <IconUploadOutline16 size={16} /> }]
612
+ : []),
613
+ { id: 'relative', label: t('copyRelative'), icon: <IconCopyOutline16 size={16} /> },
614
+ { id: 'absolute', label: t('copyAbsolute'), icon: <IconCopyOutline16 size={16} /> },
615
+ ]}
616
+ onSelect={(id) => {
617
+ const target = rowMenu
618
+ if (target === null) return
619
+ setRowMenu(null)
620
+ if (id === 'open-new-tab') {
621
+ onOpenFileNewTab?.(target.path)
622
+ return
623
+ }
624
+ if (id === 'open-side') {
625
+ onOpenFileSide?.(target.path)
626
+ return
627
+ }
628
+ if (id.startsWith('open-with:')) {
629
+ onOpenWith?.(id.slice('open-with:'.length), target.path)
630
+ return
631
+ }
632
+ if (id === 'download') {
633
+ downloadFile(target.path)
634
+ return
635
+ }
636
+ if (id === 'upload-here') {
637
+ pendingUploadDir.current = target.path
638
+ fileInputRef.current?.click()
639
+ return
640
+ }
641
+ copyPath(
642
+ id === 'relative' ? relativeTo(cwd ?? '', target.path) : target.path,
643
+ target.path,
644
+ )
645
+ }}
646
+ portal
647
+ align="start"
648
+ getAnchorRect={() => (rowMenu === null ? null : new DOMRect(rowMenu.x, rowMenu.y, 0, 0))}
649
+ anchor={<span />}
650
+ />
651
+ </div>
652
+ )
653
+ }