fix: preserve session-backed task navigation (#2601)
This commit is contained in:
@@ -198,14 +198,17 @@ describe("syncActiveTaskSession", () => {
|
||||
const setActiveSessionAuto = vi.fn();
|
||||
const setActiveTask = vi.fn();
|
||||
|
||||
syncActiveTaskSession({
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: "task-1",
|
||||
fallbackTaskId: null,
|
||||
initialSessionId: "session-1",
|
||||
activeTaskId: null,
|
||||
previousRouteTaskId: undefined,
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
|
||||
expect(applied).toBe(true);
|
||||
expect(setActiveSessionAuto).toHaveBeenCalledWith("task-1", "session-1");
|
||||
expect(setActiveTask).not.toHaveBeenCalled();
|
||||
});
|
||||
@@ -214,17 +217,76 @@ describe("syncActiveTaskSession", () => {
|
||||
const setActiveSessionAuto = vi.fn();
|
||||
const setActiveTask = vi.fn();
|
||||
|
||||
syncActiveTaskSession({
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: "task-1",
|
||||
fallbackTaskId: null,
|
||||
initialSessionId: null,
|
||||
activeTaskId: null,
|
||||
previousRouteTaskId: undefined,
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
|
||||
expect(applied).toBe(true);
|
||||
expect(setActiveTask).toHaveBeenCalledWith("task-1");
|
||||
expect(setActiveSessionAuto).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("applies a changed route over the previous active task", () => {
|
||||
const setActiveSessionAuto = vi.fn();
|
||||
const setActiveTask = vi.fn();
|
||||
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: "task-2",
|
||||
fallbackTaskId: null,
|
||||
initialSessionId: "session-2",
|
||||
activeTaskId: "task-1",
|
||||
previousRouteTaskId: "task-1",
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
|
||||
expect(applied).toBe(true);
|
||||
expect(setActiveSessionAuto).toHaveBeenCalledWith("task-2", "session-2");
|
||||
expect(setActiveTask).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("adopts a session that arrives for the current route", () => {
|
||||
const setActiveSessionAuto = vi.fn();
|
||||
const setActiveTask = vi.fn();
|
||||
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: "task-1",
|
||||
fallbackTaskId: null,
|
||||
initialSessionId: "session-1",
|
||||
activeTaskId: "task-1",
|
||||
previousRouteTaskId: "task-1",
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
|
||||
expect(applied).toBe(true);
|
||||
expect(setActiveSessionAuto).toHaveBeenCalledWith("task-1", "session-1");
|
||||
expect(setActiveTask).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not restore an unchanged route over an in-place sibling selection", () => {
|
||||
const setActiveSessionAuto = vi.fn();
|
||||
const setActiveTask = vi.fn();
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: "missing-task",
|
||||
fallbackTaskId: null,
|
||||
initialSessionId: "sibling-session",
|
||||
activeTaskId: "sibling-task",
|
||||
previousRouteTaskId: "missing-task",
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
|
||||
expect(applied).toBe(false);
|
||||
expect(setActiveSessionAuto).not.toHaveBeenCalled();
|
||||
expect(setActiveTask).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveEffectiveTask archived state", () => {
|
||||
|
||||
@@ -201,13 +201,18 @@ export function syncActiveTaskSession(params: {
|
||||
initialTaskId: string | undefined;
|
||||
fallbackTaskId: string | null | undefined;
|
||||
initialSessionId: string | null;
|
||||
activeTaskId: string | null;
|
||||
previousRouteTaskId: string | null | undefined;
|
||||
setActiveSessionAuto: (taskId: string, sessionId: string) => void;
|
||||
setActiveTask: (taskId: string) => void;
|
||||
}) {
|
||||
}): boolean {
|
||||
const taskId = params.initialTaskId ?? params.fallbackTaskId;
|
||||
if (!taskId) return;
|
||||
if (!taskId) return false;
|
||||
const routeChanged = params.previousRouteTaskId !== taskId;
|
||||
if (!routeChanged && params.activeTaskId !== taskId) return false;
|
||||
if (params.initialSessionId) params.setActiveSessionAuto(taskId, params.initialSessionId);
|
||||
else params.setActiveTask(taskId);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function resolveTaskIds(task: Task | null) {
|
||||
|
||||
@@ -260,6 +260,12 @@ function useTaskPageData(
|
||||
const activeTaskId = useAppStore((state) => state.tasks.activeTaskId);
|
||||
const setActiveSessionAuto = useAppStore((state) => state.setActiveSessionAuto);
|
||||
const setActiveTask = useAppStore((state) => state.setActiveTask);
|
||||
const previousRouteTaskId = useRef<string | null | undefined>(undefined);
|
||||
// Route synchronization must see the current selection when route data or
|
||||
// delayed session hydration changes, but a sidebar selection must not
|
||||
// retrigger it and overwrite the session chosen by task selection.
|
||||
const activeTaskIdForSyncRef = useRef(activeTaskId);
|
||||
activeTaskIdForSyncRef.current = activeTaskId;
|
||||
|
||||
// Validate that activeSessionId belongs to activeTaskId to prevent showing
|
||||
// messages from an unrelated session when navigating to a task without sessions.
|
||||
@@ -282,13 +288,16 @@ function useTaskPageData(
|
||||
const effectiveSessionId = validatedActiveSessionId ?? initialSessionId;
|
||||
|
||||
useEffect(() => {
|
||||
syncActiveTaskSession({
|
||||
const applied = syncActiveTaskSession({
|
||||
initialTaskId: initialTask?.id,
|
||||
fallbackTaskId,
|
||||
initialSessionId,
|
||||
activeTaskId: activeTaskIdForSyncRef.current,
|
||||
previousRouteTaskId: previousRouteTaskId.current,
|
||||
setActiveSessionAuto,
|
||||
setActiveTask,
|
||||
});
|
||||
if (applied) previousRouteTaskId.current = initialTask?.id ?? fallbackTaskId;
|
||||
}, [initialTask?.id, fallbackTaskId, initialSessionId, setActiveSessionAuto, setActiveTask]);
|
||||
|
||||
const { repositories } = useRepositories(task?.workspace_id ?? null, Boolean(task?.workspace_id));
|
||||
|
||||
Reference in New Issue
Block a user