- BrowserView: 即使 host 在白名单,GUI 自身 origin 永不给 allow-same-origin(裸 host 条目覆盖所有端口时可提权) - intercept: revealInExplorer 相对路径经 resolveSidebarPath 解析后再 revealPaths(交付物路径是相对的) - intercept: ctx.betterSidebar 改为 ctx.get(#357 纤维链读取约定) - FileTree/EditorHost/TreePanel/tabs: 把 revealed 贯通到树行渲染 + 高亮类 + scrollIntoView(此前高亮永不渲染) - state.revealPaths: 重建祖先目录时保留前导分隔符(POSIX/UNC 此前目录永远折叠) - state.schedulePersist: 仅活动会话写入全局宽度(targeted open 不再用非活动会话的过期宽度覆盖拖拽值) - browser.probe: GET 回退显式取消 body(否则流式/大响应挂住 socket) - terminal-font: leading generic 前只放 symbols-only 字体;fully-patched 放 generic 后(避免 hijack 度量字体) - sidebar.module.css: 恢复拖拽反馈(#130 的 silent resize 与 PR 描述冲突,取 HEAD) - Sidebar: 剩余 4 处 ctx.betterSidebar 改 ctx.get - tests: 补 revealPaths/iframeSandboxFor/terminal-font 回归测试 + EditorHost/FileTree 测试补 revealed prop
276 lines
11 KiB
TypeScript
276 lines
11 KiB
TypeScript
/**
|
|
* The files window's tree surface: a global file-name search box on top
|
|
* (300ms debounce; an in-flight search is aborted by the next keystroke)
|
|
* over either the shared controlled FileTree (empty query) or the flat
|
|
* result list (relative paths; click opens through the caller's mode-aware
|
|
* open). Owns its refresh tick: the icon next to the search input clears
|
|
* the tree cache. EditorHost docks it as the tab's right panel (wrapped in
|
|
* a drag-resize handle) and provides the file context-menu open escapes.
|
|
*
|
|
* Uploads (header pickers, the tree's drag-drop and "upload here" menu)
|
|
* all funnel through here: one session at a time, shown in a full-window
|
|
* progress overlay with cancel, followed by a tree refresh and a one-line
|
|
* hint under the search row (success fades, failures and cancels stay).
|
|
* OS file drags are shielded at the panel host (see Sidebar.tsx), so a
|
|
* drop over the file window uploads here and never reaches DSH's chat
|
|
* intake.
|
|
*/
|
|
import { useEffect, useRef, useState, type InputHTMLAttributes } from 'react'
|
|
import clsx from 'clsx'
|
|
import { IconFolderOpen16, IconRefreshOutline16 } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
import { api } from './api.ts'
|
|
import { FileTree } from './FileTree.tsx'
|
|
import { IconUploadOutline16 } from './icons.tsx'
|
|
import type { OpenWithTarget } from './open-with.ts'
|
|
import { t } from './locales.ts'
|
|
import { resolveSidebarPath } from './produced-files.ts'
|
|
import { UploadOverlay } from './UploadOverlay.tsx'
|
|
import {
|
|
summarizeResults, uploadHintText, uploadItemsFromFiles, uploadToDir,
|
|
UPLOAD_HINT_MS, type UploadItem,
|
|
} from './upload.ts'
|
|
import css from './sidebar.module.css'
|
|
|
|
/** One in-flight upload session (the overlay's progress source). */
|
|
interface UploadSession {
|
|
dir: string
|
|
done: number
|
|
total: number
|
|
/** Relative path of the file being uploaded ('' when none is in flight). */
|
|
current: string
|
|
controller: AbortController
|
|
}
|
|
|
|
export function TreePanel(props: {
|
|
sessionId: string
|
|
cwd: string | undefined
|
|
expanded: string[]
|
|
revealed: string[]
|
|
onToggle: (path: string) => void
|
|
onOpenFile: (path: string) => void
|
|
/** File context-menu "open in a new tab" (passed through to FileTree). */
|
|
onOpenFileNewTab?: (path: string) => void
|
|
/** File context-menu "open to the side" (passed through to FileTree). */
|
|
onOpenFileSide?: (path: string) => void
|
|
/** The "open with" menu surface (passed through to FileTree; absent →
|
|
* the whole section is hidden). */
|
|
openWithTargets?: OpenWithTarget[]
|
|
openWithPinned?: string[]
|
|
openWithSsh?: boolean
|
|
onOpenWith?: (targetId: string, path: string) => void
|
|
onToggleOpenWithPin?: (targetId: string) => void
|
|
onReferenceFile: (path: string) => void
|
|
/** Full-window presentation: the panel fills its host instead of docking
|
|
* at a fixed width. */
|
|
full?: boolean
|
|
}) {
|
|
const { sessionId, cwd, expanded, revealed, onToggle, onOpenFile, onOpenFileNewTab, onOpenFileSide, openWithTargets, openWithPinned, openWithSsh, onOpenWith, onToggleOpenWithPin, onReferenceFile, full } = props
|
|
const [query, setQuery] = useState('')
|
|
const [results, setResults] = useState<{ matches: string[]; truncated: boolean } | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [refreshTick, setRefreshTick] = useState(0)
|
|
/** One-line upload status under the search row ('' hides the hint). */
|
|
const [uploadStatus, setUploadStatus] = useState('')
|
|
/** Whether the status line is a failure/cancel (error color, stays visible). */
|
|
const [uploadFailed, setUploadFailed] = useState(false)
|
|
/** The in-flight upload session (null → no overlay, buttons enabled). */
|
|
const [upload, setUpload] = useState<UploadSession | null>(null)
|
|
/** True between the cancel click and the session settling (button disabled). */
|
|
const [cancelling, setCancelling] = useState(false)
|
|
/** Set by cancelUpload; the settle path shows 'upload cancelled' instead of
|
|
* summarizing the partial results. */
|
|
const cancelledRef = useRef(false)
|
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
|
const folderInputRef = useRef<HTMLInputElement>(null)
|
|
|
|
/** Start one upload session into `dir` (absolute, inside the workspace). */
|
|
const startUpload = (dir: string, items: UploadItem[]): void => {
|
|
if (items.length === 0 || cwd === undefined || upload !== null) return
|
|
cancelledRef.current = false
|
|
const controller = new AbortController()
|
|
setUploadFailed(false)
|
|
setUploadStatus(uploadHintText(0, items.length, '', dir, t))
|
|
setUpload({ dir, done: 0, total: items.length, current: '', controller })
|
|
void uploadToDir({ sessionId, cwd }, dir, items, (done, total, current) => {
|
|
if (current !== '') setUploadStatus(uploadHintText(done, total, current, dir, t))
|
|
setUpload(session => session === null ? session : { ...session, done, total, current })
|
|
}, controller.signal).then((results) => {
|
|
setUpload(null)
|
|
setCancelling(false)
|
|
// Reload the tree whatever the outcome: files may have landed before a
|
|
// cancel, and failures leave whatever did succeed visible.
|
|
setRefreshTick(tick => tick + 1)
|
|
if (cancelledRef.current) {
|
|
setUploadStatus(t('uploadCancelled'))
|
|
setUploadFailed(true)
|
|
return
|
|
}
|
|
const status = summarizeResults(results, t)
|
|
setUploadStatus(status)
|
|
setUploadFailed(results.some(result => !result.ok))
|
|
// Success messages are transient; failures stay until the next action.
|
|
if (results.every(result => result.ok)) {
|
|
window.setTimeout(() => {
|
|
setUploadStatus(current => current === status ? '' : current)
|
|
}, UPLOAD_HINT_MS)
|
|
}
|
|
})
|
|
}
|
|
|
|
/** Cancel the in-flight upload (aborts the request; the host drops its temp). */
|
|
const cancelUpload = (): void => {
|
|
if (upload === null || cancelling) return
|
|
cancelledRef.current = true
|
|
setCancelling(true)
|
|
upload.controller.abort()
|
|
}
|
|
|
|
const folderInputProps = { webkitdirectory: '' } as InputHTMLAttributes<HTMLInputElement>
|
|
|
|
const needle = query.trim()
|
|
useEffect(() => {
|
|
if (needle === '') {
|
|
setResults(null)
|
|
setError(null)
|
|
return
|
|
}
|
|
const controller = new AbortController()
|
|
const timer = window.setTimeout(() => {
|
|
api.fsSearch({ sessionId, cwd }, needle, controller.signal).then((found) => {
|
|
setResults(found)
|
|
setError(null)
|
|
}).catch((failure: unknown) => {
|
|
if (controller.signal.aborted) return
|
|
setResults(null)
|
|
setError(failure instanceof Error ? failure.message : String(failure))
|
|
})
|
|
}, 300)
|
|
return () => {
|
|
window.clearTimeout(timer)
|
|
controller.abort()
|
|
}
|
|
}, [sessionId, cwd, needle])
|
|
|
|
const busy = upload !== null
|
|
|
|
return (
|
|
<div className={clsx(css.editorTreePanel, full === true && css.editorTreePanelFull)}>
|
|
<div className={css.editorTreeSearch}>
|
|
<input
|
|
className={css.editorSearchInput}
|
|
value={query}
|
|
placeholder={t('editorSearchPlaceholder')}
|
|
spellCheck={false}
|
|
onChange={(event) => { setQuery(event.target.value) }}
|
|
/>
|
|
<button
|
|
type="button"
|
|
className={css.iconButton}
|
|
aria-label={t('refresh')}
|
|
title={t('refresh')}
|
|
onClick={() => { setRefreshTick(tick => tick + 1) }}
|
|
>
|
|
<IconRefreshOutline16 size={14} />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={css.iconButton}
|
|
aria-label={t('uploadFiles')}
|
|
title={t('uploadFiles')}
|
|
disabled={busy}
|
|
onClick={() => { fileInputRef.current?.click() }}
|
|
>
|
|
<IconUploadOutline16 size={14} />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={css.iconButton}
|
|
aria-label={t('uploadFolder')}
|
|
title={t('uploadFolder')}
|
|
disabled={busy}
|
|
onClick={() => { folderInputRef.current?.click() }}
|
|
>
|
|
<IconFolderOpen16 size={14} />
|
|
</button>
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
multiple
|
|
style={{ display: 'none' }}
|
|
onChange={(event) => {
|
|
if (cwd !== undefined) startUpload(cwd, uploadItemsFromFiles(event.target.files ?? []))
|
|
event.target.value = ''
|
|
}}
|
|
/>
|
|
<input
|
|
ref={folderInputRef}
|
|
type="file"
|
|
multiple
|
|
{...folderInputProps}
|
|
style={{ display: 'none' }}
|
|
onChange={(event) => {
|
|
if (cwd !== undefined) startUpload(cwd, uploadItemsFromFiles(event.target.files ?? []))
|
|
event.target.value = ''
|
|
}}
|
|
/>
|
|
</div>
|
|
{uploadStatus !== '' && (
|
|
<div className={clsx(css.editorSearchHint, uploadFailed && css.editorError)} title={uploadStatus}>{uploadStatus}</div>
|
|
)}
|
|
{needle === '' ? (
|
|
<FileTree
|
|
sessionId={sessionId}
|
|
cwd={cwd}
|
|
expanded={expanded}
|
|
revealed={revealed}
|
|
onToggle={onToggle}
|
|
onOpenFile={onOpenFile}
|
|
onOpenFileNewTab={onOpenFileNewTab}
|
|
onOpenFileSide={onOpenFileSide}
|
|
openWithTargets={openWithTargets}
|
|
openWithPinned={openWithPinned}
|
|
openWithSsh={openWithSsh}
|
|
onOpenWith={onOpenWith}
|
|
onToggleOpenWithPin={onToggleOpenWithPin}
|
|
onReferenceFile={onReferenceFile}
|
|
refreshTick={refreshTick}
|
|
onUploadRequest={startUpload}
|
|
busy={busy}
|
|
/>
|
|
) : (
|
|
<div className={css.explorerBody}>
|
|
{error !== null && <div className={clsx(css.editorSearchHint, css.editorError)}>{error}</div>}
|
|
{error === null && results === null && <div className={css.editorSearchHint}>{t('loading')}</div>}
|
|
{error === null && results !== null && results.matches.length === 0 && (
|
|
<div className={css.editorSearchHint}>{t('editorSearchNoResults')}</div>
|
|
)}
|
|
{error === null && results !== null && results.matches.map(rel => (
|
|
<button
|
|
key={rel}
|
|
type="button"
|
|
className={css.editorSearchResult}
|
|
title={rel}
|
|
onClick={() => { onOpenFile(resolveSidebarPath(cwd, rel)) }}
|
|
>
|
|
{rel}
|
|
</button>
|
|
))}
|
|
{error === null && results?.truncated === true && (
|
|
<div className={css.editorSearchHint}>{t('editorSearchTruncated')}</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
{upload !== null && (
|
|
<UploadOverlay
|
|
dir={upload.dir}
|
|
done={upload.done}
|
|
total={upload.total}
|
|
current={upload.current}
|
|
onCancel={cancelUpload}
|
|
cancelling={cancelling}
|
|
/>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|