Skip to content

Commit 74d12a2

Browse files
authored
fix: Cleanup file watch and navigate users before delete (#220)
1 parent 607a0d7 commit 74d12a2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fsSync from "node:fs";
12
import fs from "node:fs/promises";
23
import path from "node:path";
34
import * as watcher from "@parcel/watcher";
@@ -118,6 +119,17 @@ class FileService {
118119
repoPath,
119120
(err, events) => {
120121
if (err) {
122+
if (!fsSync.existsSync(repoPath)) {
123+
log.info(
124+
`Watched directory no longer exists, stopping watcher: ${repoPath}`,
125+
);
126+
this.stopWatching(repoPath).catch((stopErr) => {
127+
log.warn(
128+
`Failed to stop watcher for deleted directory: ${stopErr}`,
129+
);
130+
});
131+
return;
132+
}
121133
log.error("Watcher error:", err);
122134
return;
123135
}

apps/array/src/main/services/workspace/workspaceService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
WorktreeInfo,
1515
} from "../../../shared/types";
1616
import { logger } from "../../lib/logger";
17+
import { fileService } from "../fileWatcher";
1718
import { getWorktreeLocation } from "../settingsStore";
1819
import { foldersStore } from "../store";
1920
import { deleteWorktreeIfExists } from "../worktreeUtils";
@@ -618,6 +619,15 @@ export class WorkspaceService {
618619
mainRepoPath: string,
619620
worktreePath: string,
620621
): Promise<void> {
622+
try {
623+
await fileService.stopWatching(worktreePath);
624+
} catch (error) {
625+
log.warn(
626+
`Failed to stop file watcher for worktree ${worktreePath}:`,
627+
error,
628+
);
629+
}
630+
621631
try {
622632
await deleteWorktreeIfExists(mainRepoPath, worktreePath);
623633
} catch (error) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
useDuplicateTask,
44
} from "@features/tasks/hooks/useTasks";
55
import { logger } from "@renderer/lib/logger";
6+
import { useNavigationStore } from "@renderer/stores/navigationStore";
67
import type { Task } from "@shared/types";
78
import { handleExternalAppAction } from "@utils/handleExternalAppAction";
89
import { useCallback, useState } from "react";
@@ -15,6 +16,7 @@ export function useTaskContextMenu() {
1516
const [renameDialogOpen, setRenameDialogOpen] = useState(false);
1617
const duplicateTask = useDuplicateTask();
1718
const deleteTask = useDeleteTask();
19+
const { view, navigateToTaskInput } = useNavigationStore();
1820

1921
const showContextMenu = useCallback(
2022
async (task: Task, event: React.MouseEvent, worktreePath?: string) => {
@@ -48,6 +50,10 @@ export function useTaskContextMenu() {
4850
await duplicateTask.mutateAsync(task.id);
4951
break;
5052
case "delete":
53+
// navigate away first if we are viewing this task
54+
if (view.type === "task-detail" && view.data?.id === task.id) {
55+
navigateToTaskInput();
56+
}
5157
await deleteTask.mutateAsync(task.id);
5258
break;
5359
}
@@ -68,7 +74,7 @@ export function useTaskContextMenu() {
6874
log.error("Failed to show context menu", error);
6975
}
7076
},
71-
[duplicateTask, deleteTask],
77+
[duplicateTask, deleteTask, view, navigateToTaskInput],
7278
);
7379

7480
return {

0 commit comments

Comments
 (0)