fix: restore Changes focus on task return (#2091)

This commit is contained in:
Carlos Florêncio
2026-07-31 13:26:20 +01:00
committed by GitHub
parent d46ec16770
commit a8af0787b1
6 changed files with 261 additions and 29 deletions
+11 -20
View File
@@ -7,7 +7,6 @@ import { useAppStore } from "@/components/state-provider";
import { useDockviewStore } from "@/lib/state/dockview-store";
import type { AppState } from "@/lib/state/store";
import type { FileInfo, GitStatusEntry } from "@/lib/state/slices/session-runtime/types";
import { getEnvLayout } from "@/lib/local-storage";
import { djb2Hash } from "@/lib/utils/hash";
type DockviewPanel = NonNullable<ReturnType<DockviewApi["getPanel"]>>;
@@ -251,9 +250,10 @@ export function applyChangesPanelAutoFocusState(args: {
environmentIdBySessionId: Record<string, string>;
previousMarkers: Record<string, ChangesMarker>;
pendingEnvKeys: Set<string>;
hasSavedLayout?: boolean;
isRestoringLayout: boolean;
getIsRestoringLayout?: () => boolean;
isMaximized: boolean;
getIsMaximized?: () => boolean;
activate: () => ActivateChangesPanelResult;
}): string | null {
const {
@@ -263,9 +263,10 @@ export function applyChangesPanelAutoFocusState(args: {
environmentIdBySessionId,
previousMarkers,
pendingEnvKeys,
hasSavedLayout = false,
isRestoringLayout,
getIsRestoringLayout,
isMaximized,
getIsMaximized,
activate,
} = args;
@@ -288,17 +289,13 @@ export function applyChangesPanelAutoFocusState(args: {
});
const isCurrentlyRestoringLayout = () => isRestoringLayout || getIsRestoringLayout?.() === true;
const isCurrentlyMaximized = () => isMaximized || getIsMaximized?.() === true;
// A saved env layout records the user's active panel. Pending Changes
// attention may choose the first panel for an unseen env, but must not
// override that explicit focus when the user returns to a task.
if (activeEnvKey && hasSavedLayout) {
pendingEnvKeys.delete(activeEnvKey);
} else if (activeEnvKey && !isCurrentlyRestoringLayout() && pendingEnvKeys.has(activeEnvKey)) {
if (activeEnvKey && !isCurrentlyRestoringLayout() && pendingEnvKeys.has(activeEnvKey)) {
const result = activate();
if (
shouldClearPendingChangesFocus(result) &&
!(result === "no-panel" && isCurrentlyRestoringLayout())
!(result === "no-panel" && (isCurrentlyRestoringLayout() || isCurrentlyMaximized()))
) {
pendingEnvKeys.delete(activeEnvKey);
}
@@ -310,12 +307,12 @@ export function applyChangesPanelAutoFocusState(args: {
export function useChangesPanelAutoFocus(activeEnvKey: string | null) {
const api = useDockviewStore((s) => s.api);
const isRestoringLayout = useDockviewStore((s) => s.isRestoringLayout);
const isMaximized = useDockviewStore((s) => s.preMaximizeLayout !== null);
const signalsByEnv = useAppStore(useShallow(selectChangesSignalByEnvironment));
const environmentIdBySessionId = useAppStore((state) => state.environmentIdBySessionId);
const previousMarkersRef = useRef<Record<string, ChangesMarker>>({});
const pendingEnvKeysRef = useRef<Set<string>>(new Set());
const previousActiveEnvKeyRef = useRef<string | null>(null);
const hasSavedLayout = activeEnvKey !== null && getEnvLayout(activeEnvKey) !== null;
useEffect(() => {
previousActiveEnvKeyRef.current = applyChangesPanelAutoFocusState({
@@ -325,17 +322,11 @@ export function useChangesPanelAutoFocus(activeEnvKey: string | null) {
environmentIdBySessionId,
previousMarkers: previousMarkersRef.current,
pendingEnvKeys: pendingEnvKeysRef.current,
hasSavedLayout,
isRestoringLayout,
getIsRestoringLayout: () => useDockviewStore.getState().isRestoringLayout,
isMaximized,
getIsMaximized: () => useDockviewStore.getState().preMaximizeLayout !== null,
activate: () => activateChangesPanel(api),
});
}, [
signalsByEnv,
activeEnvKey,
api,
isRestoringLayout,
environmentIdBySessionId,
hasSavedLayout,
]);
}, [signalsByEnv, activeEnvKey, api, isRestoringLayout, isMaximized, environmentIdBySessionId]);
}