/* eslint-disable @typescript-eslint/no-explicit-any */ import React, { Suspense, lazy } from 'react'; import { AlertTriangle, Download, Trash2 } from 'lucide-react'; import { activeTabStore, toEditorTabId } from '../state/activeTabStore'; import { editorTabStore } from '../state/editorTabStore'; import { releaseEditorTabSaveCoordinator, saveEditorTab } from '../state/editorTabSave'; import { TopTabs } from '../../components/TopTabs'; import { VaultView } from '../../components/VaultView'; import { QuickAddSnippetDialog } from '../../components/QuickAddSnippetDialog'; import { AddToWorkspaceDialog } from '../../components/workspace/AddToWorkspaceDialog'; import { KeyboardInteractiveModal } from '../../components/KeyboardInteractiveModal'; import { PassphraseModal } from '../../components/PassphraseModal'; import { TextEditorTabView } from '../../components/editor/TextEditorTabView'; import { UnsavedChangesProvider } from '../../components/editor/UnsavedChangesDialog'; import { SnippetExecutionProvider } from '../../components/SnippetExecutionProvider'; import { Button } from '../../components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '../../components/ui/dialog'; import { Input } from '../../components/ui/input'; import { Label } from '../../components/ui/label'; import { toast } from '../../components/ui/toast'; import { AppHostTreeLayer } from './AppHostTreeLayer'; const LazyProtocolSelectDialog = lazy(() => import('../../components/ProtocolSelectDialog')); const LazyQuickSwitcher = lazy(() => import('../../components/QuickSwitcher').then((m) => ({ default: m.QuickSwitcher })), ); const LazyCreateWorkspaceDialog = lazy(() => import('../../components/CreateWorkspaceDialog').then((m) => ({ default: m.CreateWorkspaceDialog })), ); type AppViewContext = Record; export function AppView({ ctx }: { ctx: AppViewContext }) { const { accentMode, addShellHistoryEntry, addSessionToWorkspace, addToWorkspaceDialog, appendHostToWorkspace, appendLocalTerminalToWorkspace, clearAndRemoveSource, clearAndRemoveSources, clearUnsavedConnectionLogs, closeLogView, closeSession, closeTabsBatch, closeWorkspace, copySessionToNewWindowWithCurrentShell, copySessionWithCurrentShell, connectionLogs, convertKnownHostToHost, createWorkspaceFromSessions, createWorkspaceFromTargets, createWorkspaceWithHosts, customAccent, customGroups, currentTerminalTheme, deleteConnectionLog, draggingSessionId, effectiveKnownHosts, editorTabs, editorWordWrap, emptyVaultConflict, followAppTerminalTheme, groupConfigs, handleAddKnownHost, handleConnectSerial, handleConnectToHost, handleCreateLocalTerminal, handleDeleteHost, handleEndSessionDrag, handleHostConnectWithProtocolCheck, handleHotkeyAction, handleKeyboardInteractiveCancel, handleKeyboardInteractiveSubmit, handleOpenQuickSwitcher, handleOpenSettings, handleRootContextMenu, handlePassphraseCancel, handlePassphraseSkip, handlePassphraseSubmit, handleProtocolSelect, handleRequestCloseEditorTabRef, handleSessionStatusChange, handleSyncNowManual, handleTerminalDataCapture, handleToggleTheme, handleUpdateHostFromTerminal, hostById, hosts, hotkeyScheme, identities, importOrReuseKey, isBroadcastEnabled, isCreateWorkspaceOpen, isMacClient, isQuickSwitcherOpen, keyBindings, keyboardInteractiveQueue, keys, logViews, managedSources, navigateToSection, openLogView, orderedTabsWithEditors, orphanSessions, passphraseQueue, protocolSelectHost, proxyProfiles, quickResults, quickSearch, reorderWorkTabs, reorderWorkspaceSessions, resetSessionRename, resetWorkspaceRename, resolveEmptyVaultConflict, resolvedTheme, runSnippet, sessionLogsDir, sessionLogsEnabled, sessionLogsFormat, sessionLogsTimestampsEnabled, sessionRenameTarget, sshDebugLogsEnabled, sessionRenameValue, sessions, setActiveTabId, setAddToWorkspaceDialog, setDraggingSessionId, setEditorWordWrap, setIsCreateWorkspaceOpen, setIsQuickSwitcherOpen, setNavigateToSection, setProtocolSelectHost, setQuickSearch, setSessionRenameValue, setTerminalFontFamilyId, setTerminalFontSize, setTerminalThemeId, updateSessionFontSize, clearSessionFontSizeOverride, setWorkspaceFocusedSession, setWorkspaceRenameValue, settings, sftpAutoOpenSidebar, sftpFollowTerminalCwd, setSftpFollowTerminalCwd, sftpAutoSync, sftpDefaultViewMode, sftpDoubleClickBehavior, sftpShowHiddenFiles, sftpUseCompressedUpload, shellHistory, snippetPackages, snippets, splitSessionWithCurrentShell, startSessionRename, startWorkspaceRename, submitSessionRename, submitWorkspaceRename, t, terminalFontFamilyId, terminalFontSize, terminalSettings, terminalThemeId, toggleBroadcast, toggleConnectionLogSaved, toggleScriptsSidePanelRef, toggleSidePanelRef, toggleWorkspaceViewMode, unmanageSource, updateConnectionLog, updateCustomGroups, updateGroupConfigs, updateHostDistro, updateHosts, updateIdentities, updateKeys, updateKnownHosts, updateManagedSources, updateProxyProfiles, updateSnippetPackages, updateSnippets, updateSplitSizes, updateTerminalSetting, workspaceRenameTarget, workspaceRenameValue, workspaces, VaultViewContainer, SftpViewMount, TerminalLayerMount, LogViewWrapper, } = ctx; return ( {({ prompt }) => { // Helper: close an editor tab and activate the neighbor (left-preference), or vault. const closeEditorAndActivateNeighbor = (id: string) => { const closingTabId = toEditorTabId(id); const list = orderedTabsWithEditors; const idx = list.indexOf(closingTabId); releaseEditorTabSaveCoordinator(id); editorTabStore.close(id); if (activeTabStore.getActiveTabId() !== closingTabId) return; const next = list[idx - 1] ?? list[idx + 1] ?? 'vault'; activeTabStore.setActiveTabId(next === closingTabId ? 'vault' : next); }; // Real dirty-confirm close handler. const handleRequestCloseEditorTab = async (id: string): Promise => { const tab = editorTabStore.getTab(id); if (!tab) return false; const dirty = tab.content !== tab.baselineContent; if (!dirty) { closeEditorAndActivateNeighbor(id); return true; } const choice = await prompt(tab.fileName); if (choice === 'cancel') return false; if (choice === 'discard') { closeEditorAndActivateNeighbor(id); return true; } if (choice === 'save') { const ok = await saveEditorTab(id); if (!ok) { const msg = editorTabStore.getTab(id)?.saveError ?? 'Save failed'; toast.error(msg, 'SFTP'); return false; } const latest = editorTabStore.getTab(id); if (!latest || latest.content !== latest.baselineContent) return false; closeEditorAndActivateNeighbor(id); return true; } return false; }; // Expose to the hotkey dispatcher (Cmd/Ctrl+W). handleRequestCloseEditorTabRef.current = handleRequestCloseEditorTab; return (
setNavigateToSection(null)} terminalSettings={terminalSettings} /> updateTerminalSetting('fontWeight', w)} onCloseSession={closeSession} onUpdateSessionStatus={handleSessionStatusChange} onUpdateHostDistro={updateHostDistro} onUpdateHost={handleUpdateHostFromTerminal} onAddKnownHost={handleAddKnownHost} onCommandExecuted={(command, hostId, hostLabel, sessionId) => { addShellHistoryEntry({ command, hostId, hostLabel, sessionId }); }} shellHistory={shellHistory} onTerminalDataCapture={handleTerminalDataCapture} onCreateWorkspaceFromSessions={createWorkspaceFromSessions} onAddSessionToWorkspace={addSessionToWorkspace} onRequestAddToWorkspace={(workspaceId) => setAddToWorkspaceDialog({ mode: 'append', workspaceId }) } onUpdateSplitSizes={updateSplitSizes} onSetDraggingSessionId={setDraggingSessionId} onToggleWorkspaceViewMode={toggleWorkspaceViewMode} onSetWorkspaceFocusedSession={setWorkspaceFocusedSession} onReorderWorkspaceSessions={reorderWorkspaceSessions} onSplitSession={splitSessionWithCurrentShell} onConnectToHost={handleConnectToHost} onCreateLocalTerminal={handleCreateLocalTerminal} isBroadcastEnabled={isBroadcastEnabled} onToggleBroadcast={toggleBroadcast} updateHosts={updateHosts} updateSnippets={updateSnippets} updateSnippetPackages={updateSnippetPackages} sftpDefaultViewMode={sftpDefaultViewMode} sftpDoubleClickBehavior={sftpDoubleClickBehavior} sftpAutoSync={sftpAutoSync} sftpShowHiddenFiles={sftpShowHiddenFiles} sftpUseCompressedUpload={sftpUseCompressedUpload} sftpAutoOpenSidebar={sftpAutoOpenSidebar} sftpFollowTerminalCwd={sftpFollowTerminalCwd} setSftpFollowTerminalCwd={setSftpFollowTerminalCwd} editorWordWrap={editorWordWrap} setEditorWordWrap={setEditorWordWrap} sessionLogsEnabled={sessionLogsEnabled} sessionLogsDir={sessionLogsDir} sessionLogsFormat={sessionLogsFormat} sessionLogsTimestampsEnabled={sessionLogsTimestampsEnabled} sshDebugLogsEnabled={sshDebugLogsEnabled} showHostTreeSidebar={settings.showHostTreeSidebar} toggleScriptsSidePanelRef={toggleScriptsSidePanelRef} toggleSidePanelRef={toggleSidePanelRef} /> {/* Log Views - readonly terminal replays */} {logViews.map(logView => { // Get the latest log data from connectionLogs to reflect updates const latestLog = connectionLogs.find(l => l.id === logView.connectionLogId) || logView.log; return ( closeLogView(logView.id)} onUpdateLog={updateConnectionLog} /> ); })} {/* Editor Tabs — kept mounted for Monaco instance persistence; visibility toggled via CSS */} {editorTabs.map((tab) => ( handleRequestCloseEditorTabRef.current(id)} /> ))}
{/* Global "quick add / edit snippet" dialog, triggered by the netcatty:snippets:add and :edit window events (from ScriptsSidePanel "+" button and right-click menu). Delete is handled by a sibling useEffect above — it does not need a dialog. */} updateSnippets([...snippets, snippet])} onUpdateSnippet={(snippet) => updateSnippets(snippets.map((s) => (s.id === snippet.id ? snippet : s))) } onCreatePackage={(pkg) => updateSnippetPackages(Array.from(new Set([...snippetPackages, pkg]))) } /> {/* Root-mounted AddToWorkspaceDialog — triggered by the focus-mode "+" button (mode='append') or QuickSwitcher's "New Workspace" button (mode='create'). Single instance so dialog state and styling stay consistent across entry points. */} {addToWorkspaceDialog && ( { if (!open) setAddToWorkspaceDialog(null); }} // Filter serial hosts only in append mode — appendHostToWorkspace // has no serial code path. Create mode goes through // createWorkspaceFromTargets, which builds a SerialConfig-backed // session for serial hosts, so those should remain pickable. hosts={addToWorkspaceDialog.mode === 'append' ? hosts.filter((h) => h.protocol !== 'serial') : hosts} workspaceTitle={ addToWorkspaceDialog.mode === 'append' ? workspaces.find((w) => w.id === addToWorkspaceDialog.workspaceId)?.title : 'New Workspace' } onAdd={(targets) => { if (addToWorkspaceDialog.mode === 'append') { // Match the workspace root's current split direction so // the new panes peer the existing siblings instead of // wrapping the whole tree into one side of a fresh split // (which would happen if we always passed the helper's // default 'vertical'). const ws = workspaces.find((w) => w.id === addToWorkspaceDialog.workspaceId); const rootDir = ws && ws.root.type === 'split' ? ws.root.direction : 'vertical'; for (const target of targets) { if (target.kind === 'local') { appendLocalTerminalToWorkspace(addToWorkspaceDialog.workspaceId, undefined, rootDir); } else { appendHostToWorkspace(addToWorkspaceDialog.workspaceId, target.host, rootDir); } } } else { createWorkspaceFromTargets(targets); } }} /> )} {isQuickSwitcherOpen && ( { setActiveTabId(tabId); setIsQuickSwitcherOpen(false); setQuickSearch(''); }} onCreateLocalTerminal={(shell) => { handleCreateLocalTerminal(shell); setIsQuickSwitcherOpen(false); setQuickSearch(''); }} onCreateWorkspace={() => { setIsQuickSwitcherOpen(false); setQuickSearch(''); setAddToWorkspaceDialog({ mode: 'create' }); }} onClose={() => { setIsQuickSwitcherOpen(false); setQuickSearch(''); }} keyBindings={keyBindings} /> )} { if (!open) { resetSessionRename(); } }}> {t('dialog.renameSession.title')}
setSessionRenameValue(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') submitSessionRename(); }} autoFocus placeholder={t('placeholder.sessionName')} />
{ if (!open) { resetWorkspaceRename(); } }}> {t('dialog.renameWorkspace.title')}
setWorkspaceRenameValue(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') submitWorkspaceRename(); }} autoFocus placeholder={t('placeholder.workspaceName')} />
{isCreateWorkspaceOpen && ( setIsCreateWorkspaceOpen(false)} hosts={hosts} onCreate={createWorkspaceWithHosts} /> )} {/* Protocol Select Dialog for QuickSwitcher */} {protocolSelectHost && ( setProtocolSelectHost(null)} /> )} {/* Global Keyboard-Interactive Authentication Modal (2FA/MFA) - processes queue */} {/* Indicator when more 2FA requests are pending */} {keyboardInteractiveQueue.length > 1 && (
{keyboardInteractiveQueue.length - 1} more pending
)} {/* Global Passphrase Modal for encrypted SSH keys */} {/* Empty vault vs cloud data confirmation dialog (#679). This dialog intentionally cannot be dismissed — the user MUST choose "Restore" or "Keep Empty" before the sync flow can proceed. hideCloseButton removes the X button, onOpenChange is a no-op so ESC also does nothing, and onInteractOutside prevents click-away. */} { /* intentionally non-dismissable */ }}> e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()}> {t('sync.autoSync.emptyVaultConflict.title')} {t('sync.autoSync.emptyVaultConflict.description')} {emptyVaultConflict && (
{t('sync.autoSync.emptyVaultConflict.cloudLabel')}
{t('sync.autoSync.emptyVaultConflict.cloudSummary', { hosts: emptyVaultConflict.hostCount, keys: emptyVaultConflict.keyCount, snippets: emptyVaultConflict.snippetCount, proxyProfiles: emptyVaultConflict.proxyProfileCount, })}
)}
); }}
); }