dsh-coding-sidebar 1.0.0

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