From ddbf12693ca44bfb907803b6d519773fe8c1d5a7 Mon Sep 17 00:00:00 2001 From: QQ Date: Wed, 11 Nov 2026 22:31:08 +0800 Subject: [PATCH] Import src/client/FoldersDialog.tsx --- src/client/FoldersDialog.tsx | 486 +++++++++++++++++++++++++++++++++++ 1 file changed, 486 insertions(+) create mode 100644 src/client/FoldersDialog.tsx diff --git a/src/client/FoldersDialog.tsx b/src/client/FoldersDialog.tsx new file mode 100644 index 0000000..139f1de --- /dev/null +++ b/src/client/FoldersDialog.tsx @@ -0,0 +1,486 @@ +/** + * The folder dialog of one workspace row: the base folder first, then every + * extra folder in stored order. A row is chosen as the root (the folder new + * sessions default to); open/remove act per row, and the base folder is never + * removable because the workspace's canonical directory cannot be dropped. + * + * Folders come in two kinds, and the add area switches between them: + * · local — a directory picked on this machine (`pickDirectory`); + * · cloud — a directory on an SSH host, browsed through the SSH plugin's own + * pooled session, then registered as a mirror-backed workspace. + * + * Cloud browsing lives here rather than behind a second modal: the dialog body + * swaps to the remote tree, so no stacked overlays are needed and the current + * location stays readable at the top. + */ +import { useEffect, useState } from 'react' +import { + Button, + IconFolderOpenOutlineRegular, + IconTrashOutlineRegular, + Modal, +} from '@deepseek-ai/dsh-client-ui-primitives' +import type { WorkspaceView } from '@deepseek-ai/dsh-api-workspace-controller/client' +import { effectiveFolders, memberOf, rootOf, samePath, type WorkspaceFolderEntry } from '../core/folders.ts' +import type { GroupsBrowserProps } from './contract.ts' +import { folderLabel } from './tree.ts' +import { + listRemoteDirectory, + remoteDirJoin, + remoteDirParent, + type RemoteDirEntry, + type RemoteProfileSummary, +} from './folder-directory.ts' + +/** Which kind of folder the add area currently offers. */ +type AddMode = 'local' | 'cloud' + +/** Relative path of `picked` inside `root`: '' for the root itself, null outside. */ +function subfolderOf(root: string | null, picked: string): string | null { + if (root === null) return null + const base = root.trim().replace(/\/+$/u, '') + if (base === '') return null + if (picked === base) return '' + return picked.startsWith(`${base}/`) ? picked.slice(base.length + 1) : null +} + +export function FoldersDialog({ workspace, entry, t, busy, canOpen, remote, ownerRemotePath, profiles, profilesError, knownWorkspaceIds, onClose, onAddFolder, onAddRemote, onAddSubpath, onSetRoot, onRemove, onOpenFolder }: { + workspace: WorkspaceView + entry: WorkspaceFolderEntry | undefined + t: GroupsBrowserProps['t'] + busy: boolean + canOpen: boolean + /** Remote (mirror-backed) workspace: extra folders can also be typed subpaths. */ + remote: boolean + /** Remote root of a cloud owner, so a browsed subdirectory joins as a subfolder. */ + ownerRemotePath: string | null + /** SSH connections, `null` while loading or when the remote-ssh plugin is absent. */ + profiles: readonly RemoteProfileSummary[] | null + profilesError: string | null + /** Live workspace ids, so a member whose workspace is gone can say so. */ + knownWorkspaceIds: readonly string[] + onClose: () => void + /** Pick any directory (local or an already-mirrored cloud folder). */ + onAddFolder: () => void + /** Register one cloud folder: returns true once it is stored. */ + onAddRemote: (profileId: string, remotePath: string) => Promise + onAddSubpath: (relative: string) => Promise + onSetRoot: (path: string) => void + onRemove: (path: string, deleteRecord: boolean) => void + onOpenFolder: (path: string) => void +}) { + const folders = effectiveFolders(workspace.path, entry) + const root = rootOf(workspace.path, entry) + const [addMode, setAddMode] = useState('local') + // Path of the remote member whose removal awaits the delete-record choice. + const [confirmRemovePath, setConfirmRemovePath] = useState(null) + const [profileId, setProfileId] = useState('') + const [remotePath, setRemotePath] = useState('') + const [browsing, setBrowsing] = useState(false) + const [notice, setNotice] = useState(null) + const profileChoices = profiles ?? [] + const knownIds = new Set(knownWorkspaceIds) + const selectedProfile = profileChoices.some(profile => profile.id === profileId) + ? profileId + : (profileChoices[0]?.id ?? '') + const currentProfile = profileChoices.find(profile => profile.id === selectedProfile) + /** One field, two shapes: a bare name creates a subdirectory inside THIS + * project's remote root, anything with a separator registers a new cloud + * workspace. Keeping them in one field removed a whole extra row from a + * ~330px wide dialog. */ + const typed = remotePath.trim() + const asSubfolder = remote && typed !== '' && !/[/\\~]/u.test(typed) + const submitAdd = (): void => { + if (typed === '' || busy) return + if (asSubfolder) { + void onAddSubpath(typed).then(success => { if (success) setRemotePath('') }) + return + } + if (selectedProfile === '') return + void onAddRemote(selectedProfile, typed).then((success) => { + if (!success) return + setRemotePath('') + setBrowsing(false) + }) + } + + if (browsing && currentProfile !== undefined) { + return ( + { + const relative = subfolderOf(ownerRemotePath, picked) + if (relative === '' && ownerRemotePath !== null) { + // Already this project's base folder: nothing to add, say so. + setNotice(t('folder.alreadyRoot')) + setBrowsing(false) + return + } + if (relative !== null) { + // Inside the owner's remote root: one folder of this mirror, no second copy. + void onAddSubpath(relative).then((success) => { + if (success) { + setNotice(null) + setBrowsing(false) + } + }) + return + } + setRemotePath(picked) + setBrowsing(false) + }} + onCancel={() => { setBrowsing(false) }} + /> + ) + } + + const cloudAvailable = profiles !== null + const extraFolders = folders.length > 1 + // A cloud project's directory is a generated mirror id, so the header names the + // place a human recognises: the remote root, when we managed to read it. + const ownerPath = remote ? (ownerRemotePath ?? workspace.path) : workspace.path + return ( + {t('folder.done')} + )} + > +
+
+ {workspace.title} + {ownerPath} +
+ +
+
{t('folder.sectionList')}
+
+ {folders.map((path, index) => { + const isBase = index === 0 + const isRoot = samePath(path, root) + const member = memberOf(entry, path) + // A cloud member's directory is a generated mirror id, so the dialog + // names it by its remote path/title, exactly like the sidebar row does. + // The base folder is named by the workspace title for the same reason. + const label = isBase + ? workspace.title + : member?.kind === 'remote' + ? (member.title ?? folderLabel(member.remotePath ?? path)) + : folderLabel(path) + const displayPath = member === undefined + ? (isBase ? ownerPath : path) + : (member.remotePath ?? member.path) + // A member whose own workspace disappeared (deleted outside this + // overlay, or an SSH record the plugin dropped) keeps its row so the + // user can still remove it, but says that it cannot run sessions. + const memberGone = member !== undefined && !knownIds.has(member.workspaceId) + const canDeleteRecord = member?.kind === 'remote' && member.recordId !== undefined && member.recordId !== '' + return ( +
+
+ + + {label} + {displayPath} + + + {isBase && {t('folder.base')}} + {isRoot && {t('folder.rootBadge')}} + {member !== undefined && ( + + {member.kind === 'remote' ? t('folder.cloudTag') : t('folder.localTag')} + + )} + {memberGone && ( + {t('folder.memberMissing')} + )} + + + {canOpen && ( + + )} + {!isBase && ( + + )} + +
+ {confirmRemovePath === path && ( +
+ {t('folder.removeRemotePrompt')} + + + + + +
+ )} +
+ ) + })} +
+ {!extraFolders &&

{t('folder.emptyHint')}

} +
+ +
+
{t('folder.sectionAdd')}
+
+ + {cloudAvailable && ( + + )} +
+ {notice !== null &&

{notice}

} + + {addMode === 'local' + ? ( +
+ {/* Full width, own line: the action used to share a row with the + consequence text and the label wrapped onto two lines. */} + +

{remote ? t('folder.localIntoRemote') : t('folder.localNote')}

+
+ ) + : ( + <> + {/* One control per line: the dialog body is only ~330px wide, so a + profile select, a path field and a labelled button cannot share + a row without squeezing one of them. Browsing therefore docks + into the path field as an icon instead of its own button. */} +
+ {profileChoices.length === 0 + ?

{t('folder.noProfiles')}

+ : ( + + )} +
+ setRemotePath(event.target.value)} + onKeyDown={(event) => { + if (event.key !== 'Enter') return + event.preventDefault() + submitAdd() + }} + /> + +
+
+ +
+
+

{t(remote ? 'folder.cloudNoteBoth' : 'folder.cloudNote')}

+ + )} + {profilesError !== null && ( +

{t('folder.profilesError')}: {profilesError}

+ )} +
+
+
+ ) +} + +/** + * Browse one SSH host and pick a directory. Entries come from the SSH plugin's + * `listDir`, so the local machine never runs a copy of the remote tree and the + * connection's own auth (key, password, ProxyJump) is reused unchanged. + */ +function RemoteFolderPicker({ profile, startPath, t, busy, onPick, onCancel }: { + profile: RemoteProfileSummary + startPath: string + t: GroupsBrowserProps['t'] + busy: boolean + onPick: (path: string) => void + onCancel: () => void +}) { + const [path, setPath] = useState(startPath) + const [resolved, setResolved] = useState(startPath) + const [entries, setEntries] = useState([]) + const [error, setError] = useState(null) + const [loading, setLoading] = useState(true) + const [reloadKey, setReloadKey] = useState(0) + + useEffect(() => { + let live = true + setLoading(true) + setError(null) + void listRemoteDirectory(profile.id, path).then( + (listing) => { + if (!live) return + setEntries(listing.entries) + setResolved(listing.resolved) + setLoading(false) + // The shell resolved `~` or a relative name: browse the real place next + // so "up" and the children join behave like an absolute path. + if (listing.resolved !== path) setPath(listing.resolved) + }, + (reason: unknown) => { + if (!live) return + setError(reason instanceof Error ? reason.message : String(reason)) + setLoading(false) + }, + ) + return () => { live = false } + }, [profile.id, path, reloadKey]) + + const parent = remoteDirParent(resolved) + const directories = entries.filter(entry => entry.type === 'directory') + const choose = (): void => { onPick(resolved) } + return ( + + + + + )} + > +
+ {resolved} + + + + + +
+
+ {loading &&

{t('folder.loading')}

} + {!loading && error !== null && ( +

{t('folder.browseFailed')}: {error}

+ )} + {!loading && error === null && directories.length === 0 && ( +

{t('folder.emptyDir')}

+ )} + {!loading && error === null && directories.map(entry => ( + + ))} +
+

{t('folder.browseNote')}

+
+ ) +}