import { BaseView } from './BaseView'; import { ContainerConfigData, ConfigViewData } from '../types/ViewTypes'; // Git 分支接口 interface GitBranch { name: string; isCurrent: boolean; selected?: boolean; } interface BranchTreeNode { name: string; fullName: string; isLeaf: boolean; children: BranchTreeNode[]; level: number; expanded?: boolean; branch?: GitBranch; } // Git 文件树接口 interface GitFileTree { name: string; type: 'file' | 'folder'; path: string; children?: GitFileTree[]; } // Git 仓库接口 interface GitRepo { id: string; name: string; url: string; localPath: string; branch: string; lastSync: string; containerId: string; } // 合并文件夹接口 interface MergedFolder { id: string; displayName: string; // 配置栏显示的名称 folderName: string; // 实际文件夹名称 path: string; fileCount: number; containerId: string; originalConfigIds: string[]; createdAt: string; } // 树状分支节点接口 interface BranchTreeNode { name: string; fullName: string; isLeaf: boolean; children: BranchTreeNode[]; level: number; expanded?: boolean; branch?: GitBranch; } export class ConfigView extends BaseView { render(data?: ContainerConfigData & { gitRepos?: GitRepo[]; currentGitRepo?: GitRepo; gitFileTree?: GitFileTree[]; gitLoading?: boolean; gitBranches?: GitBranch[]; gitRepoUrl?: string; mergedFolders?: MergedFolder[]; }): string { const container = data?.container; const configs = data?.configs || []; const gitRepos = data?.gitRepos || []; const currentGitRepo = data?.currentGitRepo; const gitFileTree = data?.gitFileTree || []; const gitLoading = data?.gitLoading || false; const gitBranches = data?.gitBranches || []; const gitRepoUrl = data?.gitRepoUrl || ''; const mergedFolders = data?.mergedFolders || []; // 生成配置列表的 HTML - 包含配置文件和 Git 仓库 const configsHtml = configs.map((config: ConfigViewData) => ` 🔧 ${config.name} 📄 ${config.fileName} `).join(''); // 生成合并文件夹的 HTML - 显示在配置列表中 const mergedFoldersHtml = mergedFolders.map((folder: MergedFolder) => ` 📁 ${folder.displayName}
包含 ${folder.fileCount} 个文件
${folder.folderName} `).join(''); // 生成 Git 仓库列表的 HTML - 修改显示方式,Git 仓库不可勾选 const gitReposHtml = gitRepos.map(repo => { // 提取仓库名称(从URL中获取或使用name) const repoName = repo.name.split(' (')[0]; // 移除分支名部分 // 提取分支名 const branchMatch = repo.name.match(/\(([^)]+)\)/); const branchName = branchMatch ? branchMatch[1] : repo.branch; return ` 📁 ${repoName}
${branchName} `; }).join(''); // 生成分支选择的 HTML - 使用树状结构 const branchesHtml = gitBranches.length > 0 ? this.generateBranchesTreeHtml(gitBranches) : ''; return ` 配置管理 ${this.getStyles()}

⚙️ 配置管理 - ${container?.name || '未知容器'}

📋 配置文件管理

${configsHtml} ${mergedFoldersHtml} ${gitReposHtml}
配置 文件 操作

📚 Git 仓库管理

🔗 添加 Git 仓库

${branchesHtml}
`; } private generateBranchesTreeHtml(branches: GitBranch[]): string { if (branches.length === 0) return ''; // 构建分支树 const branchTree = this.buildBranchTree(branches); let html = '
'; html += '

🌳 分支树

'; html += '
'; html += ''; html += ''; html += '
'; html += '
'; html += this.renderBranchTreeNodes(branchTree, 0); html += '
'; html += '
'; html += ''; html += ''; html += '
'; return html; } /** * 构建分支树状结构 */ private buildBranchTree(branches: GitBranch[]): BranchTreeNode[] { const root: BranchTreeNode[] = []; branches.forEach(branch => { const parts = branch.name.split('/'); let currentLevel = root; for (let i = 0; i < parts.length; i++) { const part = parts[i]; const isLeaf = i === parts.length - 1; const fullName = parts.slice(0, i + 1).join('/'); let node = currentLevel.find(n => n.name === part); if (!node) { node = { name: part, fullName: fullName, isLeaf: isLeaf, children: [], level: i }; currentLevel.push(node); } if (isLeaf) { node.branch = branch; } else { node.expanded = node.expanded || true; // 默认展开 } currentLevel = node.children; } }); return root; } /** * 渲染分支树节点 */ private renderBranchTreeNodes(nodes: BranchTreeNode[], level: number): string { let html = ''; nodes.forEach(node => { const indent = level * 20; const nodeId = node.fullName.replace(/[^a-zA-Z0-9-]/g, '-'); if (node.isLeaf) { // 叶子节点(实际分支) html += '
'; html += ''; html += '🌿'; html += ''; html += node.name; html += (node.branch && node.branch.isCurrent ? ' ⭐' : ''); html += ''; html += '
'; } else { // 文件夹节点 html += '
'; html += ''; html += (node.expanded ? '🪵' : '🪵'); html += ''; html += '🪵'; html += '' + node.name + ''; html += '
'; if (node.expanded && node.children && node.children.length > 0) { html += '
'; html += this.renderBranchTreeNodes(node.children, level + 1); html += '
'; } } }); return html; } /** * 计算叶子节点数量 */ private countLeafNodes(nodes: BranchTreeNode[]): number { let count = 0; nodes.forEach(node => { if (node.isLeaf) { count++; } else { count += this.countLeafNodes(node.children); } }); return count; } }