Skip to content

Commit 411089f

Browse files
authored
feat: Automatically close diff tabs when stale (#303)
1 parent 9df116a commit 411089f

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

apps/array/src/renderer/features/code-editor/components/DiffEditorPanel.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { PanelMessage } from "@components/ui/PanelMessage";
22
import { CodeMirrorDiffEditor } from "@features/code-editor/components/CodeMirrorDiffEditor";
33
import { CodeMirrorEditor } from "@features/code-editor/components/CodeMirrorEditor";
44
import { getRelativePath } from "@features/code-editor/utils/pathUtils";
5+
import { usePanelLayoutStore } from "@features/panels/store/panelLayoutStore";
56
import { useTaskData } from "@features/task-detail/hooks/useTaskData";
67
import { Box } from "@radix-ui/themes";
78
import { trpcVanilla } from "@renderer/trpc/client";
89
import type { Task } from "@shared/types";
910
import { useQuery, useQueryClient } from "@tanstack/react-query";
10-
import { useCallback } from "react";
11+
import { useCallback, useEffect } from "react";
1112
import {
1213
selectWorktreePath,
1314
useWorkspaceStore,
@@ -29,6 +30,9 @@ export function DiffEditorPanel({
2930
const repoPath = worktreePath ?? taskData.repoPath;
3031
const filePath = getRelativePath(absolutePath, repoPath);
3132
const queryClient = useQueryClient();
33+
const closeDiffTabsForFile = usePanelLayoutStore(
34+
(s) => s.closeDiffTabsForFile,
35+
);
3236

3337
const { data: changedFiles = [] } = useQuery({
3438
queryKey: ["changed-files-head", repoPath],
@@ -41,6 +45,7 @@ export function DiffEditorPanel({
4145
});
4246

4347
const fileInfo = changedFiles.find((f) => f.path === filePath);
48+
const isFileStillChanged = !!fileInfo;
4449
const status = fileInfo?.status ?? "modified";
4550
const originalPath = fileInfo?.originalPath ?? filePath;
4651
const isDeleted = status === "deleted";
@@ -90,16 +95,33 @@ export function DiffEditorPanel({
9095
[repoPath, filePath, queryClient],
9196
);
9297

98+
const isLoading =
99+
(!isDeleted && loadingModified) || (!isNew && loadingOriginal);
100+
101+
const hasNoChanges =
102+
!!repoPath &&
103+
!isLoading &&
104+
(!isFileStillChanged ||
105+
(!isDeleted && !isNew && originalContent === modifiedContent));
106+
107+
useEffect(() => {
108+
if (hasNoChanges) {
109+
closeDiffTabsForFile(taskId, filePath);
110+
}
111+
}, [hasNoChanges, closeDiffTabsForFile, taskId, filePath]);
112+
93113
if (!repoPath) {
94114
return <PanelMessage>No repository path available</PanelMessage>;
95115
}
96116

97-
const isLoading =
98-
(!isDeleted && loadingModified) || (!isNew && loadingOriginal);
99117
if (isLoading) {
100118
return <PanelMessage>Loading diff...</PanelMessage>;
101119
}
102120

121+
if (hasNoChanges) {
122+
return null;
123+
}
124+
103125
const showDiff = !isDeleted && !isNew;
104126
const content = isDeleted ? originalContent : modifiedContent;
105127

0 commit comments

Comments
 (0)