Skip to content

Commit ec1ce58

Browse files
committed
Move file tree folder loading into hook
1 parent 124efd3 commit ec1ce58

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

anycode/App.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { normalizePath } from './utils';
3838
const App: React.FC = () => {
3939
const { wsRef, isConnected } = useSocket({});
4040

41-
const fileTree = useFileTree();
41+
const fileTree = useFileTree({ wsRef, isConnected });
4242
const editors = useEditors({ wsRef, isConnected });
4343
const terminals = useTerminals({ wsRef, isConnected });
4444
const terminalPanes = useTerminalPanes({
@@ -53,13 +53,6 @@ const App: React.FC = () => {
5353
const { currentThemeId, handleThemeChange } = useTheme({ wsRef, isConnected });
5454
const layoutActionsRef = useRef<LayoutActions | null>(null);
5555

56-
const openFolder = useMemo(() => {
57-
return (path: string) => {
58-
if (!wsRef.current || !isConnected) return;
59-
wsRef.current.emit('dir:list', { path }, fileTree.handleOpenFolderResponse);
60-
};
61-
}, [wsRef, isConnected, fileTree.handleOpenFolderResponse]);
62-
6356
useEffect(() => {
6457
const ws = wsRef.current;
6558
if (!ws || !isConnected) return;
@@ -96,14 +89,14 @@ const App: React.FC = () => {
9689

9790
useEffect(() => {
9891
if (isConnected && !wasConnectedRef.current) {
99-
openFolder('.');
92+
fileTree.openFolder('.');
10093
terminals.reconnectTerminals();
10194
agents.reconnectToAcpAgents();
10295
git.fetchGitStatus();
10396
git.fetchBranches();
10497
}
10598
wasConnectedRef.current = isConnected;
106-
}, [isConnected, openFolder, terminals.reconnectTerminals,
99+
}, [isConnected, fileTree.openFolder, terminals.reconnectTerminals,
107100
agents.reconnectToAcpAgents, git.fetchGitStatus, git.fetchBranches]);
108101

109102
useEffect(() => {
@@ -326,7 +319,7 @@ const App: React.FC = () => {
326319
onToggle={fileTree.toggleNode}
327320
onSelect={fileTree.selectNode}
328321
onOpenFile={handleOpenFile}
329-
onLoadFolder={openFolder}
322+
onLoadFolder={fileTree.openFolder}
330323
onFocusEditor={() => editors.focusEditorInPane(editors.activeEditorPaneId)}
331324
onNavigateByKey={fileTree.navigateByKey}
332325
/>

anycode/hooks/useFileTree.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { useCallback, useState } from 'react';
2+
import type { Socket } from 'socket.io-client';
23
import type { TreeNode, WatcherCreate, WatcherRemove } from '../types';
34
import { getFileName, getParentPath, joinPath } from '../utils';
45

6+
type UseFileTreeParams = {
7+
wsRef: React.RefObject<Socket | null>;
8+
isConnected: boolean;
9+
};
10+
511
type TreeNavigationHandlers = {
612
onOpenFile: (path: string) => void;
713
onLoadFolder: (path: string) => void;
@@ -105,7 +111,7 @@ const walkVisibleForNode = (nodes: TreeNode[], targetNodeId: string | null): Vis
105111
return context;
106112
};
107113

108-
export const useFileTree = () => {
114+
export const useFileTree = ({ wsRef, isConnected }: UseFileTreeParams) => {
109115
const [fileTree, setFileTree] = useState<TreeNode[]>([]);
110116
const [activeNodeId, setActiveNodeId] = useState<string | null>(null);
111117

@@ -200,6 +206,11 @@ export const useFileTree = () => {
200206
});
201207
}, [convertToTree]);
202208

209+
const openFolder = useCallback((path: string) => {
210+
if (!wsRef.current || !isConnected) return;
211+
wsRef.current.emit('dir:list', { path }, handleOpenFolderResponse);
212+
}, [wsRef, isConnected, handleOpenFolderResponse]);
213+
203214
const toggleNode = useCallback((nodeId: string) => {
204215
setFileTree((prevTree) => {
205216
const updateNode = (nodes: TreeNode[]): TreeNode[] => {
@@ -441,6 +452,7 @@ export const useFileTree = () => {
441452
fileTree,
442453
activeNodeId,
443454
setActiveNode,
455+
openFolder,
444456
handleOpenFolderResponse,
445457
toggleNode,
446458
findNodeByPath,

0 commit comments

Comments
 (0)