import { useRemoteConnectionProgress, RemoteConnectionMinimizedProgress, } from "@/remote-connection-flow/ui/RemoteConnectionProgress.js"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog.js"; import { useOptionalPlatform } from "@/hooks/usePlatform.js"; import { DesktopCommandIds } from "@zcode/shared"; /* eslint-disable max-lines -- Source/Profile/迁移三类管理操作共享同一 Host 快照和错误状态。 */ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ArrowUpRight, ChevronDown, ChevronRight, Ellipsis, FileWarning, Folder, ListTree, Loader2, Pencil, Plus, Server, Box, TerminalSquare, Copy, Trash2, } from "lucide-react"; import { resolveWorkspaceKey } from "@zcode/shared"; import type { ConnectionProfile, AppSettings, SourceServerManagementSnapshot, SourceServerMigrationReport, SourceServerRecord, SourceServerWorkspaceHistoryEntry, } from "@zcode/shared"; import type { ISourceServerManagementService } from "@zcode/services"; import { getPathLeaf } from "@/lib/path.js"; import { toast } from "@/components/ui/toast.js"; import { getSourceServerManagementErrorMessage } from "@/lib/sourceServerManagementError.js"; import { Button } from "@/components/ui/button.js"; import { Switch } from "@/components/ui/switch.js"; import { cn } from "@/components/lib/utils.js"; import { useConfirmDialog } from "@/hooks/useConfirmDialog.js"; import { useSourceServerManagementSnapshot } from "@/hooks/useSourceServerManagementSnapshot.js"; import { useSourceServerOperationLogs } from "@/hooks/useSourceServerOperationLogs.js"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { SourceServerMigrationReportPanel } from "@/settings/SourceServerMigrationReportPanel.js"; import { SourceServerLogList, SourceServerUpdateActivity, } from "@/settings/SourceServerUpdateActivity.js"; import { formatRemoteConnectionError } from "@/lib/remoteConnectionPresentation.js"; import { resolveRemoteConnectionPresentation, getRemoteDevicePresentationInput, classifyRemoteConnectionError, type RemoteConnectionPresentation, } from "@/lib/remoteConnectionPresentation.js"; import { sourceServerUpdatePreferences, sourceServerUpdateKey, useDeferredSourceServerUpdates, } from "@/lib/sourceServerUpdatePreferences.js"; import { getSourceServerForceUpdateDescription, runSourceServerUpdate, } from "@/lib/sourceServerUpdateAction.js"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu.js"; import { EditConnectionProfileDialog } from "@/remote-connection/EditConnectionProfileDialog.js"; import { formatConnectionProfileTarget, RemoteConnectionKindTag, RemoteConnectionStatusTag, } from "@/remote-connection/RemoteConnectionTags.js"; import type { WorkspaceTabState } from "@/store/tabStore.js"; import { getWorkspaceSourceServerId } from "@/conversation-workspace-target/sourceIdentity.js"; import { buildRemoteWorkspaceProjection } from "@/remote/remoteWorkspaceProjection.js"; function formatTimestamp(timestamp: number, locale: string): string { return new Date(timestamp).toLocaleString(locale, { dateStyle: "medium", timeStyle: "short", }); } function formatRelativeTimestamp( timestamp: number, locale: string, formatMessage: (descriptor: { id: string }, values?: Record) => string, ): string { const elapsed = Math.max(0, Date.now() - timestamp); if (elapsed < 60_000) return formatMessage({ id: "common.justNow" }); if (elapsed < 60 * 60_000) { return formatMessage({ id: "common.minutesAgo" }, { count: Math.floor(elapsed / 60_000) }); } if (elapsed < 24 * 60 * 60_000) { return formatMessage({ id: "common.hoursAgo" }, { count: Math.floor(elapsed / (60 * 60_000)) }); } if (elapsed < 48 * 60 * 60_000) return formatMessage({ id: "common.yesterday" }); return new Date(timestamp).toLocaleDateString(locale, { month: "short", day: "numeric", }); } export function getProjectCount( snapshot: SourceServerManagementSnapshot, sourceServerId: string, remoteConversationWorkspaceBySourceServerId?: AppSettings["remoteConversationWorkspaceBySourceServerId"], ): number { return new Set( snapshot.workspaceHistory .filter( (entry) => entry.sourceServerId === sourceServerId && remoteConversationWorkspaceBySourceServerId?.[sourceServerId]?.workspacePath !== entry.workspacePath, ) .map((entry) => resolveWorkspaceKey(entry)), ).size; } const COLLAPSED_PROJECT_COUNT = 3; function toggleSetItem(current: Set, item: string): Set { const next = new Set(current); if (next.has(item)) next.delete(item); else next.add(item); return next; } function profileOperationScope(profile: ConnectionProfile): string { return profile.sourceServerId ? `source:${profile.sourceServerId}` : `profile:${profile.profileId}`; } export function SourceServerManagementPanel({ management, targetSourceServerId, targetSourceServerDetails = false, onTargetSourceServerFocused, onCreateConnection, onConnectProfile, onDisconnectSource, onShowInSidebar, onOpenProject, onShowUpdateDetails, workspaceTabs = [], settings, onUpdateSettings, }: { management: ISourceServerManagementService; targetSourceServerId?: string; targetSourceServerDetails?: boolean; onTargetSourceServerFocused?: () => void; onCreateConnection?: () => void; /** 用户点击连接时交给 Root 的全局弹窗,以 preferredProfileId 直达第 4 步。 */ onConnectProfile?: (profileId: string) => Promise | void; /** 用户断开设备时交给 Root 的统一 RemoteWorkspaceController。 */ onDisconnectSource?: (sourceServerId: string) => Promise; /** 「在侧边栏中显示」菜单项:返回工作区主界面(侧边栏设备分组所在处)。 */ onShowInSidebar?: (sourceServerId: string) => void; onOpenProject?: (sourceServerId: string, path: string, profileId?: string) => Promise; onShowUpdateDetails?: (sourceServerId: string) => void; workspaceTabs?: WorkspaceTabState[]; /** 「新任务」按钮(原型 L763):激活该设备已打开的 workspace tab 并开新草稿。 */ /** 有已打开 workspace tab 的设备 id 集合;「新任务」仅对这些设备渲染。 */ settings?: Pick< AppSettings, "remoteStartupAutoReconnectEnabled" | "remoteConversationWorkspaceBySourceServerId" > | null; onUpdateSettings?: (patch: Partial) => Promise | void; }) { const background = useRemoteConnectionProgress(); const { intl, locale } = useZCodeIntl(); const confirmDialog = useConfirmDialog(); const platform = useOptionalPlatform(); const deferredUpdates = useDeferredSourceServerUpdates(); const [startupAutoReconnect, setStartupAutoReconnect] = useState( settings?.remoteStartupAutoReconnectEnabled ?? true, ); useEffect(() => { setStartupAutoReconnect(settings?.remoteStartupAutoReconnectEnabled ?? true); }, [settings?.remoteStartupAutoReconnectEnabled]); const [editingConnection, setEditingConnection] = useState<{ profile: ConnectionProfile; server?: SourceServerRecord; focusField?: "host" | "auth"; } | null>(null); const [errorDetailsProfile, setErrorDetailsProfile] = useState(null); const [expandedSavedConnectionSources, setExpandedSavedConnectionSources] = useState>( new Set(), ); const [expandedProjectSources, setExpandedProjectSources] = useState>(new Set()); // 只记录用户手动切换过的日志展开状态;未切换时由设备是否失败决定默认值。 const [logExpansionBySource, setLogExpansionBySource] = useState>( new Map(), ); // 这里只拥有面板操作的防重状态;业务状态与快照版本仍由 Host 和快照 hook 负责。 const operationsRef = useRef(new Map()); const [busyOperations, setBusyOperations] = useState>( new Map(), ); const busyScopes = new Set([...busyOperations.values()].flat()); if (background?.sourceServerId) busyScopes.add(`source:${background.sourceServerId}`); else if (background?.profileId) busyScopes.add(`profile:${background.profileId}`); const hasBusyScope = useCallback( (scope: string) => [...operationsRef.current.values()].some((scopes) => scopes.includes(scope)), [], ); const mountedRef = useRef(true); useEffect(() => { mountedRef.current = true; return () => { mountedRef.current = false; }; }, []); // 相对时间必须随时钟更新,不能依赖连接事件才重绘。 const [, setClockTick] = useState(0); useEffect(() => { const timer = setInterval(() => setClockTick((tick) => tick + 1), 60_000); return () => clearInterval(timer); }, []); const [error, setError] = useState(null); const [migrationReports, setMigrationReports] = useState< Record >({}); const { snapshot, ready, applySnapshot, refresh: refreshSnapshot, } = useSourceServerManagementSnapshot(management, { onError: (cause) => setError(getSourceServerManagementErrorMessage(cause, intl.formatMessage)), }); const focusedTargetRef = useRef(undefined); const deviceCardsRef = useRef(new Map()); useEffect(() => { if (!targetSourceServerId) { focusedTargetRef.current = undefined; return; } const targetKey = `${targetSourceServerId}:${targetSourceServerDetails}`; if (!ready || focusedTargetRef.current === targetKey) return; const card = deviceCardsRef.current.get(targetSourceServerId); if (!card) return; card.scrollIntoView({ block: "center" }); card.focus({ preventScroll: true }); const failedProfileId = snapshot?.states.find( (state) => state.sourceServerId === targetSourceServerId && state.state === "failed", )?.profileId; const failedProfile = snapshot?.profiles.find( (profile) => profile.profileId === failedProfileId, ); if (targetSourceServerDetails && failedProfile) setErrorDetailsProfile(failedProfile); focusedTargetRef.current = targetKey; onTargetSourceServerFocused?.(); }, [ ready, snapshot, targetSourceServerId, targetSourceServerDetails, onTargetSourceServerFocused, ]); const { logsByProfileId } = useSourceServerOperationLogs( management, snapshot?.profiles.map((profile) => profile.profileId), ); const projection = useMemo( () => buildRemoteWorkspaceProjection({ managementSnapshot: snapshot, tabs: [], remoteConversationWorkspaceBySourceServerId: settings?.remoteConversationWorkspaceBySourceServerId, }), [snapshot, settings?.remoteConversationWorkspaceBySourceServerId], ); const refresh = useCallback(async () => { refreshSnapshot(); }, [refreshSnapshot]); const stateByProfile = useMemo( () => new Map(snapshot?.states.map((state) => [state.profileId, state]) ?? []), [snapshot?.states], ); const run = useCallback( async ( key: string, operation: () => Promise, scopes: readonly string[] = [key], ) => { // 同设备的不同连接互斥;无关设备和日志查看独立,不用全局锁阻塞长时间更新。 if (operationsRef.current.has(key) || scopes.some(hasBusyScope)) return; operationsRef.current.set(key, scopes); setBusyOperations(new Map(operationsRef.current)); setError(null); try { const nextSnapshot = await operation(); if (nextSnapshot) applySnapshot(nextSnapshot); else refreshSnapshot(); } catch (cause) { setError(getSourceServerManagementErrorMessage(cause, intl.formatMessage)); } finally { operationsRef.current.delete(key); setBusyOperations(new Map(operationsRef.current)); } }, [applySnapshot, refreshSnapshot, intl, hasBusyScope], ); const removeProfile = useCallback( async (profile: ConnectionProfile) => { if (!management) return; const confirmed = await confirmDialog({ title: intl.formatMessage({ id: "settings.source.removeProfileTitle" }), description: intl.formatMessage( { id: "settings.source.removeProfileDescription" }, { name: profile.displayName ?? profile.transport }, ), confirmLabel: intl.formatMessage({ id: "settings.source.removeProfile", }), confirmVariant: "destructive", }); if (!confirmed) return; await run(`remove:${profile.profileId}`, () => management.removeProfile(profile.profileId), [ profileOperationScope(profile), ]); }, [confirmDialog, intl, management, run], ); const removeSourceServer = useCallback( async (server: SourceServerRecord) => { if (hasBusyScope(`source:${server.sourceServerId}`)) return; const affectedProjects = workspaceTabs.filter( (tab) => getWorkspaceSourceServerId(tab, { workspaceHistory: snapshot?.workspaceHistory }) === server.sourceServerId, ); const projectNotice = affectedProjects.length > 0 ? intl.formatMessage( { id: "settings.source.removeOpenProjects" }, { projects: affectedProjects .map((tab) => `${tab.label} (${tab.workspacePath})`) .join("\n"), }, ) : intl.formatMessage({ id: "settings.source.noOpenProjects" }); const confirmed = await confirmDialog({ title: intl.formatMessage({ id: "settings.source.removeSourceServer", }), description: intl.formatMessage( { id: "settings.source.removeSourceServerDescription" }, { name: server.displayName }, ) + "\n\n" + projectNotice, confirmLabel: intl.formatMessage({ id: "settings.source.removeSourceServer", }), confirmVariant: "destructive", }); if (!confirmed) return; await run( `remove-source:${server.sourceServerId}`, () => management.removeSourceServer(server.sourceServerId), [`source:${server.sourceServerId}`], ); }, [confirmDialog, intl, management, run, snapshot?.workspaceHistory, workspaceTabs, hasBusyScope], ); const loadMigrationReport = useCallback( async (sourceServerId: string) => { if (!management) return; await run(`report:${sourceServerId}`, async () => { const report = await management.getMigrationReport(sourceServerId); setMigrationReports((current) => ({ ...current, [sourceServerId]: report })); }); }, [management, run], ); const toggleMigrationReport = useCallback( (sourceServerId: string) => { if (migrationReports[sourceServerId]) { setMigrationReports((current) => { const next = { ...current }; delete next[sourceServerId]; return next; }); return; } void loadMigrationReport(sourceServerId); }, [loadMigrationReport, migrationReports], ); const runMigrationOperation = useCallback( async ( key: string, sourceServerId: string, operation: () => Promise, ) => { await run(key, async () => { const report = await operation(); setMigrationReports((current) => ({ ...current, [sourceServerId]: report })); }, [`source:${sourceServerId}`]); }, [run], ); const updateProfile = useCallback( async (profile: ConnectionProfile) => { await run( `update:${profile.profileId}`, async () => { let next: SourceServerManagementSnapshot | null; try { next = await runSourceServerUpdate({ management, profileId: profile.profileId, confirmForce: async () => await confirmDialog({ title: intl.formatMessage({ id: "settings.source.forceUpdateTitle", }), description: await getSourceServerForceUpdateDescription( management, profile.profileId, (id) => intl.formatMessage({ id }), ), confirmLabel: intl.formatMessage({ id: "settings.source.forceUpdate", }), confirmVariant: "destructive", }), }); } catch (cause) { toast( intl.formatMessage( { id: "sourceServer.updateFailedToast" }, { source: profile.displayName ?? profile.transport }, ), { variant: "warning", actionLabel: intl.formatMessage({ id: "remote.action.details" }), onAction: () => { // 更新可跨面板生命周期完成,不能让全局 toast 捕获已卸载组件的 setter。 if (mountedRef.current) setErrorDetailsProfile(profile); else if (profile.sourceServerId) onShowUpdateDetails?.(profile.sourceServerId); }, }, ); throw cause; } if (next) toast( intl.formatMessage( { id: "sourceServer.updateCompletedToast" }, { source: profile.displayName ?? profile.transport }, ), { variant: "source-server-update" }, ); return next ?? undefined; }, [profileOperationScope(profile)], ); }, [confirmDialog, intl, management, onShowUpdateDetails, run], ); const servers = snapshot?.servers ?? []; const profiles = snapshot?.profiles ?? []; const activeProfiles = profiles.filter((profile) => profile.lifecycleState !== "retired"); const retiredProfiles = profiles.filter((profile) => profile.lifecycleState === "retired"); const unboundProfiles = activeProfiles.filter((profile) => !profile.sourceServerId); const renderIdentityConfirmation = (profile: ConnectionProfile, size: "sm" | "xs") => ( ); const handleProfileAction = ( profile: ConnectionProfile, presentation: RemoteConnectionPresentation, server?: SourceServerRecord, ) => { if ( !["security", "details"].includes(presentation.primaryAction) && hasBusyScope(profileOperationScope(profile)) ) return; switch (presentation.primaryAction) { case "update-desktop": void run("desktop-update", async () => { await platform?.executeDesktopCommand(DesktopCommandIds.CheckForUpdates); }); return; case "update-server": void updateProfile(profile); return; case "disconnect": if (profile.sourceServerId && onDisconnectSource) void run( `disconnect:${profile.sourceServerId}`, () => onDisconnectSource(profile.sourceServerId!), [profileOperationScope(profile)], ); return; case "credentials": case "edit-auth": setEditingConnection({ profile, server, focusField: "auth" }); return; case "edit-host": setEditingConnection({ profile, server, focusField: "host" }); return; case "security": case "details": setErrorDetailsProfile(profile); return; case "none": return; default: void run( `connect:${profile.profileId}`, async () => { await onConnectProfile?.(profile.profileId); }, [profileOperationScope(profile)], ); } }; const renderProfile = (profile: ConnectionProfile, server?: SourceServerRecord) => { const state = stateByProfile.get(profile.profileId); const isOnline = state?.state === "online"; const identityChangePending = state?.state === "identity-change-pending"; const profileName = profile.displayName ?? profile.transport; const presentation = resolveRemoteConnectionPresentation({ ...state, target: profile.target }); const mutationBusy = busyScopes.has(profileOperationScope(profile)) || presentation.status === "connecting"; const actionBusy = mutationBusy && !["security", "details"].includes(presentation.primaryAction); const devicePresentation = server ? resolveRemoteConnectionPresentation( getRemoteDevicePresentationInput(snapshot, server.sourceServerId), ) : null; const connectionErrorText = presentation.reason && devicePresentation?.profileId !== profile.profileId ? intl.formatMessage({ id: presentation.reason.titleId }, presentation.reason.values) : null; return (
{!server ? : null} {profileName} {formatConnectionProfileTarget(profile)} {server?.preferredProfileId === profile.profileId ? ( {intl.formatMessage({ id: "settings.source.defaultProfile" })} ) : null} {connectionErrorText ? ( {connectionErrorText} ) : profile.lastConnectedAt ? ( {intl.formatMessage( { id: "settings.source.lastConnected" }, { time: formatRelativeTimestamp( profile.lastConnectedAt, locale, intl.formatMessage, ), }, )} ) : null} {!isOnline && !identityChangePending ? ( ) : null} {identityChangePending ? renderIdentityConfirmation(profile, "xs") : null} {server && server.preferredProfileId !== profile.profileId ? ( ) : null}
); }; return (
0}>

{intl.formatMessage({ id: "settings.remoteDevicesDescription" })}

{error ? (

{error}

) : null}
{onCreateConnection && (!ready || profiles.length > 0 || servers.length > 0) ? ( ) : null}
{background && !servers.some((server) => server.sourceServerId === background.sourceServerId) && !unboundProfiles.some((profile) => profile.profileId === background.profileId) ? (

{intl.formatMessage({ id: "settings.source.unfinishedConnections" })}

) : null} {!ready && !error ? (

) : null}
{servers.length > 0 ? (

{intl.formatMessage({ id: "settings.source.devicesHeading" })} {servers.length}

) : null} {servers.map((server) => { const device = projection.devices.find( (candidate) => candidate.sourceServerId === server.sourceServerId, ); const serverProfiles = device?.profiles ?? []; const preferredProfile = serverProfiles.find((profile) => profile.profileId === server.preferredProfileId) ?? serverProfiles[0]; // 旧 Source 在确认前仍可能是 uninstalled;恢复入口不能藏在安装状态或首选线路折叠后面。 const pendingIdentityProfiles = serverProfiles.filter( (profile) => stateByProfile.get(profile.profileId)?.state === "identity-change-pending", ); const identityChangeProfile = pendingIdentityProfiles.find((profile) => profile === preferredProfile) ?? pendingIdentityProfiles[0]; const currentProfile = serverProfiles.find( (profile) => stateByProfile.get(profile.profileId)?.state === "online", ); const displayedProfile = currentProfile ?? preferredProfile; const ConnectionIcon = displayedProfile?.transport === "docker" ? Box : displayedProfile?.transport === "wsl" ? TerminalSquare : Server; // 同一项目可能有多条历史(不同连接),保留最近一次,并按最近打开倒序展示。 const latestProjectByKey = new Map(); for (const project of device?.knownProjects ?? []) { const key = resolveWorkspaceKey(project); const existing = latestProjectByKey.get(key); if (!existing || project.lastOpenedAt >= existing.lastOpenedAt) latestProjectByKey.set(key, project); } const projects = [...latestProjectByKey.values()].sort( (left, right) => right.lastOpenedAt - left.lastOpenedAt, ); const projectsExpanded = expandedProjectSources.has(server.sourceServerId); const visibleProjects = projectsExpanded ? projects : projects.slice(0, COLLAPSED_PROJECT_COUNT); const openedProjectPaths = new Set( workspaceTabs .filter( (tab) => getWorkspaceSourceServerId(tab, { workspaceHistory: snapshot?.workspaceHistory, }) === server.sourceServerId, ) .map((tab) => tab.workspacePath), ); const otherProfiles = displayedProfile ? serverProfiles.filter( (profile) => profile !== displayedProfile && !pendingIdentityProfiles.includes(profile), ) : []; const online = device?.aggregatedStatus === "online"; const migrationReport = migrationReports[server.sourceServerId]; const migrationCount = snapshot?.migrationCounts.find( (count) => count.sourceServerId === server.sourceServerId, ); const showServerLines = server.installState !== "uninstalled"; // 覆盖可更新/必须升级/更新中/更新失败四态:更新中不再携带 updateAvailability,也要选中。 const profileStates = serverProfiles .map((profile) => stateByProfile.get(profile.profileId)) .filter((state) => state !== undefined); const presentation = resolveRemoteConnectionPresentation( getRemoteDevicePresentationInput(snapshot, server.sourceServerId), ); const actionProfile = serverProfiles.find((profile) => profile.profileId === presentation.profileId) ?? preferredProfile; const deviceBusy = busyScopes.has(`source:${server.sourceServerId}`); const actionBusy = busyOperations.has(`disconnect:${server.sourceServerId}`) || busyOperations.has(`connect:${actionProfile?.profileId}`) || busyOperations.has(`update:${actionProfile?.profileId}`) || (busyOperations.has("desktop-update") && presentation.primaryAction === "update-desktop"); const preferredConnectionErrorText = presentation.reason ? intl.formatMessage({ id: presentation.reason.titleId }, presentation.reason.values) : null; const serverState = profileStates.find((state) => state.state === "updating") ?? profileStates.find( (state) => (state.updateAvailability && state.updateAvailability !== "none") || state.state === "updating", ); const updateFailed = serverState?.state === "failed"; const updateKey = sourceServerUpdateKey( server.sourceServerId, serverState?.availableVersion ?? "", ); const showUpdate = serverState && !( serverState.updateAvailability === "available" && serverState.state !== "updating" && deferredUpdates.has(updateKey) ); const updateFailureText = updateFailed && serverState ? formatRemoteConnectionError({ fallback: intl.formatMessage({ id: "settings.source.operationFailed" }), reasonCode: serverState.errorCode, retryable: serverState.retryable, recoveryAttempt: serverState.recoveryAttempt, recoveryLimit: serverState.recoveryLimit, formatMessage: intl.formatMessage, }) : null; const updateExplainsConnectionError = Boolean( showUpdate && serverState?.profileId === presentation.profileId && (serverState?.state === "updating" || (updateFailed && updateFailureText === preferredConnectionErrorText) || (serverState?.updateAvailability === "required" && presentation.reason?.category === "source-server-upgrade-required")), ); const operationLogs = logsByProfileId[serverState?.profileId ?? actionProfile?.profileId ?? ""] ?? []; const showOtherProfiles = showServerLines && otherProfiles.length > 0; const otherProfilesExpanded = showOtherProfiles && expandedSavedConnectionSources.has(server.sourceServerId); // 更新中由更新横幅展示进度与日志,页脚不重复。 const showRecentLog = operationLogs.length > 0 && serverState?.state !== "updating"; const logExpanded = showRecentLog && (logExpansionBySource.get(server.sourceServerId) ?? presentation.status === "failed"); return (
{ if (element) deviceCardsRef.current.set(server.sourceServerId, element); else deviceCardsRef.current.delete(server.sourceServerId); }} className="overflow-hidden rounded-xl border border-card-border bg-card focus-visible:outline-2 focus-visible:outline-ring" > {background?.sourceServerId === server.sourceServerId ? ( ) : null}

{server.displayName}

{displayedProfile ? ( <> {formatConnectionProfileTarget(displayedProfile)} {/* 只有一条连接时“当前 / 默认”没有区分意义,不显示。 */} {serverProfiles.length > 1 && currentProfile ? ( {intl.formatMessage({ id: "settings.source.currentProfile", })} ) : null} {serverProfiles.length > 1 && displayedProfile.profileId === server.preferredProfileId ? ( {intl.formatMessage({ id: "settings.source.defaultProfile" })} ) : null} {displayedProfile.profileId !== server.preferredProfileId ? ( ) : null} ) : null} {server.lastSeenVersion ? ( {displayedProfile ? "· " : ""}v{server.lastSeenVersion.replace(/^v/i, "")} ) : null} {server.lastOnlineAt && !online ? ( {displayedProfile || server.lastSeenVersion ? "· " : ""} {intl.formatMessage( { id: "settings.source.lastOnline" }, { time: formatRelativeTimestamp( server.lastOnlineAt, locale, intl.formatMessage, ), }, )} ) : null}
{projects.length === 0 ? (

{intl.formatMessage({ id: "settings.source.projectCount" }, { count: 0 })}

) : ( <>

{intl.formatMessage({ id: "settings.source.recentProjects" })}

{/* Bug 原因:旧实现超出三个只显示不可点击的 +N,其余项目在此处无法打开。 */} {projects.length > COLLAPSED_PROJECT_COUNT ? ( ) : null}
{visibleProjects.map((project) => { const projectName = server.declaredWorkspaces?.find( (item) => item.workspacePath === project.workspacePath, )?.label ?? getPathLeaf(project.workspacePath); return ( ); })}
)}
{presentation.primaryAction === "confirm-identity" && identityChangeProfile ? ( renderIdentityConfirmation(identityChangeProfile, "sm") ) : presentation.primaryAction !== "none" ? ( ) : null} {presentation.primaryAction === "update-desktop" && preferredProfile ? ( ) : null} {displayedProfile ? ( ) : null} { {/* Bug 原因:「移除全部连接」曾被包在 online 分支里,简化 UI 删掉离线态的 「重命名 / 迁移报告」后离线菜单变成空壳,离线设备再无任何移除入口。 移除只删本机记录与凭据,Host removeSourceServer 会先幂等断开再删记录, 不依赖远端在线;因此对所有已安装状态都渲染,只有「在侧边栏中显示」保留在线条件。 */} {onShowInSidebar && workspaceTabs.some( (tab) => getWorkspaceSourceServerId(tab, { workspaceHistory: snapshot?.workspaceHistory, }) === server.sourceServerId, ) ? ( <> onShowInSidebar(server.sourceServerId)} > {intl.formatMessage({ id: "settings.source.showInSidebar", })} ) : null}
{[ ["version", server.lastSeenVersion], ["hostname", server.hostname], ["osUser", server.osUser], ["deviceId", server.sourceServerId], ].map(([key, value]) => (

{intl.formatMessage({ id: `settings.source.info.${key}` })}:{" "} {value ?? intl.formatMessage({ id: "remote.infoUnavailable" })}

))}
void run(`copy:${server.sourceServerId}`, async () => { await navigator.clipboard.writeText(server.sourceServerId); }) } > {intl.formatMessage({ id: "settings.source.copyDeviceId" })} void removeSourceServer(server)} > {intl.formatMessage({ id: "settings.source.removeSourceServer", })}
}
{/* 更新失败只替代同一连接的相同原因,其他连接和可选更新不能吞掉连接错误。 */} {preferredConnectionErrorText && !updateExplainsConnectionError ? (
{preferredConnectionErrorText}
) : null} {showUpdate && serverState ? (
{serverState.state === "updating" ? intl.formatMessage({ id: "settings.source.updateInProgress", }) : updateFailed ? updateFailureText : serverState.updateAvailability === "required" ? intl.formatMessage({ id: "settings.source.updateRequired", }) : intl.formatMessage( { id: "settings.source.updateAvailableVersion" }, { version: serverState.availableVersion?.replace(/^v/i, "") ?? "", }, )} {serverState.updateAvailability === "available" && serverState.state !== "updating" ? ( ) : null} {serverState.state !== "updating" && presentation.primaryAction !== "update-server" && presentation.primaryAction !== "update-desktop" ? ( ) : null} {serverState.state === "updating" ? ( ) : serverState.currentVersion && serverState.minimumVersion ? (

{intl.formatMessage( { id: "settings.source.versionDetails" }, { current: serverState.currentVersion.replace(/^v/i, ""), minimum: serverState.minimumVersion.replace(/^v/i, ""), }, )}

) : null}
) : null} {pendingIdentityProfiles.length > 1 ? (
{pendingIdentityProfiles .filter((profile) => profile !== identityChangeProfile) .map((profile) => renderProfile(profile, server))}
) : null} {showOtherProfiles || showRecentLog ? (
{showOtherProfiles ? ( ) : null} {showRecentLog ? ( ) : null}
{otherProfilesExpanded ? (
{otherProfiles.map((profile) => renderProfile(profile, server))}
) : null} {logExpanded ? (
) : null}
) : null} {/* 产品边界:迁移账本是内部兼容机制,仅在存在待处理问题时以用户可理解的连接历史提醒暴露。 */} {showServerLines && migrationCount?.status === "available" && migrationCount.failedPathCount + migrationCount.abandonedPathCount > 0 ? (
{migrationReport ? ( { void confirmDialog({ title: intl.formatMessage({ id: "settings.source.abandonMigrationTitle", }), description: intl.formatMessage({ id: "settings.source.abandonMigrationDescription", }), confirmLabel: intl.formatMessage({ id: "settings.source.abandonMigration", }), confirmVariant: "destructive", }).then((confirmed) => { if (!confirmed) return; void runMigrationOperation( `abandon:${server.sourceServerId}:${migrationId}`, server.sourceServerId, () => management.abandonWorkspaceMigration( server.sourceServerId, workspacePath, migrationId, ), ); }); }} onRetry={(workspacePath, migrationId) => void runMigrationOperation( `retry:${server.sourceServerId}:${migrationId}`, server.sourceServerId, () => management.retryWorkspaceMigration( server.sourceServerId, workspacePath, migrationId, ), ) } /> ) : null}
) : null}
); })} {unboundProfiles.length > 0 ? (
profile.profileId === background.profileId) ? true : undefined } data-unfinished-connections className="group/unfinished border-t border-border pt-4" > {intl.formatMessage({ id: "settings.source.unfinishedConnections" })} {unboundProfiles.length}
{unboundProfiles.map((profile) => (
{renderProfile(profile)} {background && !background.sourceServerId && background.profileId === profile.profileId ? ( ) : null} {(logsByProfileId[profile.profileId]?.length ?? 0) > 0 ? ( ) : null}
))}
) : null} {retiredProfiles.length > 0 ? (
{intl.formatMessage( { id: "settings.source.retiredProfilesWithCount" }, { count: retiredProfiles.length }, )} {intl.formatMessage({ id: "settings.source.retiredProfilesDescription", })}
{retiredProfiles.map((profile) => (
{profile.displayName ?? profile.transport} {intl.formatMessage({ id: "settings.source.retiredNameSuffix", })} {profile.retiredAt ? `${intl.formatMessage( { id: "settings.source.archivedAt" }, { time: formatRelativeTimestamp( profile.retiredAt, locale, intl.formatMessage, ), }, )} · ${intl.formatMessage({ id: "settings.source.retiredEntryNote", })}` : intl.formatMessage({ id: "settings.source.retiredEntryNote", })}
))}
) : null}

{intl.formatMessage({ id: "settings.source.connectionPreferences", })}

{intl.formatMessage({ id: "settings.source.startupAutoReconnect", })}

{intl.formatMessage({ id: "settings.source.startupAutoReconnectDescription", })}

{ const previous = startupAutoReconnect; setStartupAutoReconnect(checked); void run("startup-reconnect", async () => { try { await onUpdateSettings?.({ remoteStartupAutoReconnectEnabled: checked }); } catch (cause) { setStartupAutoReconnect(previous); throw cause; } }); }} aria-label={intl.formatMessage({ id: "settings.source.startupAutoReconnect", })} />
{ready && profiles.length === 0 && servers.length === 0 ? (

{intl.formatMessage({ id: "settings.source.empty" })}

{onCreateConnection ? ( ) : null}
) : null}
{ if (!open) setErrorDetailsProfile(null); }} > {errorDetailsProfile ? (() => { const state = stateByProfile.get(errorDetailsProfile.profileId); const classified = classifyRemoteConnectionError({ errorCode: state?.errorCode, error: state?.error, target: errorDetailsProfile.target, }); return ( <> {intl.formatMessage({ id: classified.titleId })} {intl.formatMessage({ id: classified.reasonId })}
{intl.formatMessage({ id: "remote.error.details" })}
                        {state?.error ?? errorDetailsProfile.lastConnectError}
                      
); })() : null}
{ if (!open) setEditingConnection(null); }} onSaved={refresh} />
); }