Skip to content

Commit 6482e5f

Browse files
committed
more cleanup
1 parent a5f24d5 commit 6482e5f

File tree

4 files changed

+50
-66
lines changed

4 files changed

+50
-66
lines changed

apps/array/src/main/services/fileWatcher.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ class FileService {
3939
return path.join(app.getPath("userData"), "snapshots", `${hash}.snapshot`);
4040
}
4141

42+
private flushPendingChanges(repoPath: string, state: RepoState): void {
43+
for (const dir of state.pendingDirs) {
44+
this.emit("fs:directory-changed", { repoPath, dirPath: dir });
45+
}
46+
for (const filePath of state.pendingFiles) {
47+
this.emit("fs:file-changed", { repoPath, filePath });
48+
}
49+
for (const filePath of state.pendingDeletes) {
50+
this.emit("fs:file-deleted", { repoPath, filePath });
51+
}
52+
state.pendingDirs.clear();
53+
state.pendingFiles.clear();
54+
state.pendingDeletes.clear();
55+
state.debounceTimer = null;
56+
}
57+
4258
async listDirectory(dirPath: string): Promise<DirectoryEntry[]> {
4359
try {
4460
const entries = await fs.readdir(dirPath, { withFileTypes: true });
@@ -95,23 +111,6 @@ class FileService {
95111
debounceTimer: null,
96112
};
97113

98-
const flushPendingChanges = () => {
99-
for (const dir of state.pendingDirs) {
100-
this.emit("fs:directory-changed", { repoPath, dirPath: dir });
101-
}
102-
for (const filePath of state.pendingFiles) {
103-
this.emit("fs:file-changed", { repoPath, filePath });
104-
}
105-
for (const filePath of state.pendingDeletes) {
106-
this.emit("fs:file-deleted", { repoPath, filePath });
107-
}
108-
state.pendingDirs.clear();
109-
state.pendingFiles.clear();
110-
state.pendingDeletes.clear();
111-
state.debounceTimer = null;
112-
};
113-
114-
// Watch repo (excluding .git and node_modules)
115114
const subscription = await watcher.subscribe(
116115
repoPath,
117116
(err, events) => {
@@ -129,15 +128,15 @@ class FileService {
129128
}
130129
}
131130

132-
if (state.debounceTimer) {
133-
clearTimeout(state.debounceTimer);
134-
}
135-
state.debounceTimer = setTimeout(flushPendingChanges, DEBOUNCE_MS);
131+
if (state.debounceTimer) clearTimeout(state.debounceTimer);
132+
state.debounceTimer = setTimeout(
133+
() => this.flushPendingChanges(repoPath, state),
134+
DEBOUNCE_MS,
135+
);
136136
},
137137
{ ignore: WATCHER_IGNORE_PATTERNS },
138138
);
139139

140-
// Watch .git directory for HEAD and index changes
141140
const gitDir = path.join(repoPath, ".git");
142141
let gitSubscription: watcher.AsyncSubscription | null = null;
143142
try {
@@ -147,16 +146,16 @@ class FileService {
147146
console.error("Git watcher error:", err);
148147
return;
149148
}
150-
151-
const gitStateChanged = events.some(
152-
(e) => e.path.endsWith("/HEAD") || e.path.endsWith("/index"),
153-
);
154-
if (gitStateChanged) {
149+
if (
150+
events.some(
151+
(e) => e.path.endsWith("/HEAD") || e.path.endsWith("/index"),
152+
)
153+
) {
155154
this.emit("git:state-changed", { repoPath });
156155
}
157156
});
158157
} catch {
159-
// .git directory doesn't exist (not a git repo)
158+
// .git directory doesn't exist
160159
}
161160

162161
state.subscription = subscription;

apps/array/src/renderer/features/panels/store/panelLayoutStore.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,20 @@ export const usePanelLayoutStore = createWithEqualityFn<PanelLayoutStore>()(
318318
},
319319

320320
closeTabsForFile: (taskId, filePath) => {
321-
// Try all possible diff tab statuses
322-
const statuses = [
323-
"modified",
324-
"deleted",
325-
"added",
326-
"untracked",
327-
"renamed",
328-
undefined,
329-
];
321+
const layout = get().taskLayouts[taskId];
322+
if (!layout) return;
323+
330324
const tabIds = [
331325
createFileTabId(filePath),
332-
...statuses.map((status) => createDiffTabId(filePath, status)),
326+
createDiffTabId(filePath),
327+
createDiffTabId(filePath, "modified"),
328+
createDiffTabId(filePath, "deleted"),
329+
createDiffTabId(filePath, "added"),
330+
createDiffTabId(filePath, "untracked"),
331+
createDiffTabId(filePath, "renamed"),
333332
];
334333

335334
for (const tabId of tabIds) {
336-
const layout = get().taskLayouts[taskId];
337-
if (!layout) continue;
338-
339335
const tabLocation = findTabInTree(layout.panelTree, tabId);
340336
if (tabLocation) {
341337
get().closeTab(taskId, tabLocation.panelId, tabId);

apps/array/src/renderer/features/task-detail/components/ChangesPanel.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,9 @@ export function ChangesPanel({ taskId, task }: ChangesPanelProps) {
8181

8282
const { data: changedFiles = [], isLoading } = useQuery({
8383
queryKey: ["changed-files-head", repoPath],
84+
queryFn: () => window.electronAPI.getChangedFilesHead(repoPath!),
8485
enabled: !!repoPath,
8586
staleTime: Infinity,
86-
queryFn: async () => {
87-
if (!window.electronAPI || !repoPath) {
88-
return [];
89-
}
90-
return window.electronAPI.getChangedFilesHead(repoPath);
91-
},
9287
});
9388

9489
if (!repoPath) {

apps/array/src/renderer/hooks/useFileWatcher.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,43 @@ export function useFileWatcher(repoPath: string | null, taskId?: string) {
77
const closeTabsForFile = usePanelLayoutStore((s) => s.closeTabsForFile);
88

99
useEffect(() => {
10-
if (!repoPath || !window.electronAPI) return;
10+
if (!repoPath) return;
1111

12-
const currentRepoPath = repoPath;
13-
14-
window.electronAPI.watcherStart(currentRepoPath).catch((error) => {
12+
window.electronAPI.watcherStart(repoPath).catch((error) => {
1513
console.error("Failed to start file watcher:", error);
1614
});
1715

1816
const unsubFile = window.electronAPI.onFileChanged(
1917
({ repoPath: rp, filePath }) => {
20-
if (rp !== currentRepoPath) return;
21-
const relativePath = filePath.replace(`${currentRepoPath}/`, "");
18+
if (rp !== repoPath) return;
19+
const relativePath = filePath.replace(`${repoPath}/`, "");
2220
queryClient.invalidateQueries({
23-
queryKey: ["repo-file", currentRepoPath, relativePath],
21+
queryKey: ["repo-file", repoPath, relativePath],
2422
});
25-
// Also refresh the changed files list since git status output changed
2623
queryClient.invalidateQueries({
27-
queryKey: ["changed-files-head", currentRepoPath],
24+
queryKey: ["changed-files-head", repoPath],
2825
});
2926
},
3027
);
3128

3229
const unsubDelete = window.electronAPI.onFileDeleted(
3330
({ repoPath: rp, filePath }) => {
34-
if (rp !== currentRepoPath) return;
35-
// Refresh the changed files list
31+
if (rp !== repoPath) return;
3632
queryClient.invalidateQueries({
37-
queryKey: ["changed-files-head", currentRepoPath],
33+
queryKey: ["changed-files-head", repoPath],
3834
});
3935
if (!taskId) return;
40-
const relativePath = filePath.replace(`${currentRepoPath}/`, "");
36+
const relativePath = filePath.replace(`${repoPath}/`, "");
4137
closeTabsForFile(taskId, relativePath);
4238
},
4339
);
4440

4541
const unsubGit = window.electronAPI.onGitStateChanged(
4642
({ repoPath: rp }) => {
47-
if (rp !== currentRepoPath) return;
48-
queryClient.invalidateQueries({
49-
queryKey: ["file-at-head", currentRepoPath],
50-
});
43+
if (rp !== repoPath) return;
44+
queryClient.invalidateQueries({ queryKey: ["file-at-head", repoPath] });
5145
queryClient.invalidateQueries({
52-
queryKey: ["changed-files-head", currentRepoPath],
46+
queryKey: ["changed-files-head", repoPath],
5347
});
5448
},
5549
);
@@ -58,7 +52,7 @@ export function useFileWatcher(repoPath: string | null, taskId?: string) {
5852
unsubFile();
5953
unsubDelete();
6054
unsubGit();
61-
window.electronAPI.watcherStop(currentRepoPath);
55+
window.electronAPI.watcherStop(repoPath);
6256
};
6357
}, [repoPath, taskId, queryClient, closeTabsForFile]);
6458
}

0 commit comments

Comments
 (0)