Skip to content

Commit 6376862

Browse files
committed
Improve file tree and tab activation sync
1 parent d38ba58 commit 6376862

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

anycode/App.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,28 @@ const App: React.FC = () => {
113113
}, [editors.flushAllPendingChanges]);
114114

115115
useEffect(() => {
116-
if (!editors.activeFileId) {
117-
return;
118-
}
119-
120116
fileTree.setActiveNode(editors.activeFileId);
121117
}, [editors.activeFileId, fileTree.setActiveNode]);
122118

123119
useEffect(() => {
124120
if (!editors.activeFileId) {
121+
fileTree.clearFileSelection();
125122
return;
126123
}
127124

128125
const node = fileTree.findNodeByPath(fileTree.fileTree, editors.activeFileId);
129-
if (!node || node.isSelected) {
126+
if (!node) {
127+
fileTree.clearFileSelection();
128+
return;
129+
}
130+
131+
if (node.isSelected) {
130132
return;
131133
}
132134

133135
fileTree.selectNode(node.id);
134136
}, [editors.activeFileId, fileTree.fileTree,
135-
fileTree.findNodeByPath, fileTree.selectNode]);
137+
fileTree.findNodeByPath, fileTree.selectNode, fileTree.clearFileSelection]);
136138

137139
useEffect(() => {
138140
saveItem('terminals', terminals.terminals);

anycode/components/TreeNodeComponent.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ const TreeNodeComponentImpl: React.FC<TreeNodeComponentProps> = ({
5959

6060
const handleNameClick = (e: React.MouseEvent) => {
6161
e.stopPropagation();
62+
if (isActive) {
63+
return;
64+
}
6265
onActivate(node.id);
6366

6467
if (node.type === 'file') {
@@ -99,7 +102,7 @@ const TreeNodeComponentImpl: React.FC<TreeNodeComponentProps> = ({
99102
{hasChildren ? '▶' : ''}
100103
</div>
101104

102-
<span className="tree-name" title={title} onClick={handleNameClick}>
105+
<span className="tree-name" title={title} onClick={handleNameClick} onMouseDown={(e) => e.stopPropagation()}>
103106
{node.name}
104107
</span>
105108
</div>
@@ -159,7 +162,7 @@ const areEqual = (prev: TreeNodeComponentProps, next: TreeNodeComponentProps): b
159162
if (prev.node.type === 'directory') {
160163
const prevInside = isPathWithinNode(prev.node.path, prev.activeNodeId);
161164
const nextInside = isPathWithinNode(prev.node.path, next.activeNodeId);
162-
if (prevInside !== nextInside) {
165+
if (prevInside || nextInside) {
163166
return false;
164167
}
165168
}

anycode/components/toolbar/Toolbar.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Toolbar = ({
3636
<div
3737
key={file.id}
3838
className={`tab ${activeFileId === file.id ? 'active' : ''}`}
39-
onClick={() => onSelectFile(file.id)}
39+
onClick={() => activeFileId !== file.id && onSelectFile(file.id)}
4040
>
4141
<span className="tab-filename" title={file.id}> {file.name} </span>
4242
<button
@@ -54,7 +54,7 @@ export const Toolbar = ({
5454
<div
5555
key={`toolbar-terminal-${terminal.id}`}
5656
className={`tab tab-terminal ${activeTerminalIndex === index ? 'active' : ''}`}
57-
onClick={() => onSelectTerminal(index)}
57+
onClick={() => activeTerminalIndex !== index && onSelectTerminal(index)}
5858
>
5959
<span className="tab-filename">{`term:${terminal.name}`}</span>
6060
<button
@@ -72,7 +72,7 @@ export const Toolbar = ({
7272
<div
7373
key={`toolbar-agent-${session.agentId}`}
7474
className={`tab tab-agent ${activeAgentId === session.agentId ? 'active' : ''}`}
75-
onClick={() => onSelectAgent(session.agentId)}
75+
onClick={() => activeAgentId !== session.agentId && onSelectAgent(session.agentId)}
7676
>
7777
<span className="tab-filename">{`${session.agentName || session.agentId}`}</span>
7878
<button

anycode/hooks/useFileTree.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,19 @@ export const useFileTree = () => {
249249
}, []);
250250

251251
const clearFileSelection = useCallback(() => {
252+
const hasSelection = (nodes: TreeNode[]): boolean => {
253+
if (!Array.isArray(nodes)) return false;
254+
for (const node of nodes) {
255+
if (node.isSelected) return true;
256+
if (node.children && hasSelection(node.children)) return true;
257+
}
258+
return false;
259+
};
260+
252261
setFileTree((prevTree) => {
262+
if (!hasSelection(prevTree)) {
263+
return prevTree;
264+
}
253265
const clearSelection = (nodes: TreeNode[]): TreeNode[] => {
254266
return nodes.map((node) => {
255267
const updatedChildren = node.children ? clearSelection(node.children) : undefined;

0 commit comments

Comments
 (0)