feat(ui): 添加编辑器和文件浏览器状态持久化
- FileExplorer: 保存展开的目录路径到 localStorage - IDE: 保存打开的标签页和活动标签,刷新后自动恢复 - App: 调整 IDE 面板默认宽度为 70%
This commit is contained in:
@@ -132,11 +132,21 @@ function TreeNode({ node, depth, onFileSelect, expandedPaths, onToggleExpand }:
|
||||
);
|
||||
}
|
||||
|
||||
const STORAGE_KEY_EXPANDED = 'ai-assistant-file-explorer-expanded';
|
||||
|
||||
export function FileExplorer({ onFileSelect, className, workingDirectory }: FileExplorerProps) {
|
||||
const [tree, setTree] = useState<FileTreeNode[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [expandedPaths, setExpandedPaths] = useState<Set<string>>(new Set());
|
||||
// 从 localStorage 恢复展开状态
|
||||
const [expandedPaths, setExpandedPaths] = useState<Set<string>>(() => {
|
||||
try {
|
||||
const saved = localStorage.getItem(STORAGE_KEY_EXPANDED);
|
||||
return saved ? new Set(JSON.parse(saved)) : new Set();
|
||||
} catch {
|
||||
return new Set();
|
||||
}
|
||||
});
|
||||
|
||||
// 加载文件树
|
||||
const loadTree = useCallback(async () => {
|
||||
@@ -146,13 +156,7 @@ export function FileExplorer({ onFileSelect, className, workingDirectory }: File
|
||||
const result = await getFileTree('.', 4);
|
||||
if (result.success) {
|
||||
setTree(result.data.tree);
|
||||
// 默认展开第一层
|
||||
const firstLevel = new Set(
|
||||
result.data.tree
|
||||
.filter((n) => n.type === 'directory')
|
||||
.map((n) => n.path)
|
||||
);
|
||||
setExpandedPaths(firstLevel);
|
||||
// 保持已保存的展开状态(不重置)
|
||||
} else {
|
||||
setError('Failed to load file tree');
|
||||
}
|
||||
@@ -175,6 +179,8 @@ export function FileExplorer({ onFileSelect, className, workingDirectory }: File
|
||||
} else {
|
||||
next.add(path);
|
||||
}
|
||||
// 保存到 localStorage
|
||||
localStorage.setItem(STORAGE_KEY_EXPANDED, JSON.stringify([...next]));
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user