Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSetHeaderContent } from "@hooks/useSetHeaderContent";
import { Lightning } from "@phosphor-icons/react";
import { Box, Flex, Text } from "@radix-ui/themes";
import { useEffect, useMemo } from "react";
import { useAutofillCommandCenter } from "../hooks/useAutofillCommandCenter";
import { useCommandCenterData } from "../hooks/useCommandCenterData";
import { useCommandCenterStore } from "../stores/commandCenterStore";
import { CommandCenterGrid } from "./CommandCenterGrid";
Expand All @@ -13,6 +14,8 @@ export function CommandCenterView() {
const { cells, summary } = useCommandCenterData();
const { markAsViewed } = useTaskViewed();

useAutofillCommandCenter();

const visibleTaskIdsKey = cells
.map((c) => c.taskId)
.filter(Boolean)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useArchivedTaskIds } from "@features/archive/hooks/useArchivedTaskIds";
import { useTasks } from "@features/tasks/hooks/useTasks";
import { useWorkspaces } from "@features/workspace/hooks/useWorkspace";
import type { Task } from "@shared/types";
import { useEffect, useRef } from "react";
import { useCommandCenterStore } from "../stores/commandCenterStore";

const RECENT_WINDOW_MS = 2 * 60 * 60 * 1000;

function getLastActivity(task: Task): number {
const taskTime = new Date(task.updated_at).getTime();
const runTime = task.latest_run?.updated_at
? new Date(task.latest_run.updated_at).getTime()
: 0;
return Math.max(taskTime, runTime);
}

export function useAutofillCommandCenter(): void {
const { data: tasks = [] } = useTasks();
const { data: workspaces, isFetched: workspacesFetched } = useWorkspaces();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 The hook guards on workspacesFetched but has no equivalent guard for the tasks query. useTasks() returns its data defaulted to [], so if workspaces resolve first (common when they are cached from a previous mount), the effect fires immediately with an empty tasks array, finds zero candidates, sets hasRunRef.current = true, and exits. When the tasks response eventually arrives, the dependency change re-runs the effect — but hasRunRef.current is already true, so it bails. The autofill silently never runs.

Suggested change
const { data: tasks = [] } = useTasks();
const { data: workspaces, isFetched: workspacesFetched } = useWorkspaces();
const { data: tasks = [], isFetched: tasksFetched } = useTasks();
const { data: workspaces, isFetched: workspacesFetched } = useWorkspaces();
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/renderer/features/command-center/hooks/useAutofillCommandCenter.ts
Line: 19-20

Comment:
The hook guards on `workspacesFetched` but has no equivalent guard for the tasks query. `useTasks()` returns its data defaulted to `[]`, so if workspaces resolve first (common when they are cached from a previous mount), the effect fires immediately with an empty `tasks` array, finds zero candidates, sets `hasRunRef.current = true`, and exits. When the tasks response eventually arrives, the dependency change re-runs the effect — but `hasRunRef.current` is already `true`, so it bails. The autofill silently never runs.

```suggestion
  const { data: tasks = [], isFetched: tasksFetched } = useTasks();
  const { data: workspaces, isFetched: workspacesFetched } = useWorkspaces();
```

How can I resolve this? If you propose a fix, please make it concise.

const archivedTaskIds = useArchivedTaskIds();

const cells = useCommandCenterStore((s) => s.cells);
const autofillCells = useCommandCenterStore((s) => s.autofillCells);

const hasRunRef = useRef(false);

useEffect(() => {
if (hasRunRef.current) return;
if (!workspacesFetched || !workspaces) return;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 The workspacesFetched guard needs a parallel tasksFetched guard so the effect waits for both data sources before proceeding.

Suggested change
if (!workspacesFetched || !workspaces) return;
if (!workspacesFetched || !workspaces) return;
if (!tasksFetched) return;
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/renderer/features/command-center/hooks/useAutofillCommandCenter.ts
Line: 30

Comment:
The `workspacesFetched` guard needs a parallel `tasksFetched` guard so the effect waits for both data sources before proceeding.

```suggestion
    if (!workspacesFetched || !workspaces) return;
    if (!tasksFetched) return;
```

How can I resolve this? If you propose a fix, please make it concise.


if (!cells.every((id) => id == null)) {
hasRunRef.current = true;
return;
}

const cutoff = Date.now() - RECENT_WINDOW_MS;
const candidates = tasks
.filter(
(task) =>
!archivedTaskIds.has(task.id) &&
!!workspaces[task.id] &&
getLastActivity(task) >= cutoff,
)
.sort((a, b) => getLastActivity(b) - getLastActivity(a))
.slice(0, cells.length)
.map((task) => task.id);

if (candidates.length > 0) {
autofillCells(candidates);
}
hasRunRef.current = true;
}, [
cells,
workspaces,
workspacesFetched,
tasks,
archivedTaskIds,
autofillCells,
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

vi.mock("@utils/electronStorage", () => ({
electronStorage: {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
},
}));

import { useCommandCenterStore } from "./commandCenterStore";

describe("commandCenterStore", () => {
beforeEach(() => {
useCommandCenterStore.setState({
layout: "2x2",
cells: [null, null, null, null],
activeTaskId: null,
activeCellIndex: null,
zoom: 1,
creatingCells: [],
});
});

describe("autofillCells", () => {
it("fills empty cells from index 0", () => {
useCommandCenterStore.getState().autofillCells(["t1", "t2"]);
expect(useCommandCenterStore.getState().cells).toEqual([
"t1",
"t2",
null,
null,
]);
});

it("does nothing when any cell is already populated", () => {
useCommandCenterStore.setState({ cells: [null, "existing", null, null] });
useCommandCenterStore.getState().autofillCells(["t1", "t2"]);
expect(useCommandCenterStore.getState().cells).toEqual([
null,
"existing",
null,
null,
]);
});

it("ignores empty task list", () => {
useCommandCenterStore.getState().autofillCells([]);
expect(useCommandCenterStore.getState().cells).toEqual([
null,
null,
null,
null,
]);
});

it("caps fill at the number of cells", () => {
useCommandCenterStore
.getState()
.autofillCells(["t1", "t2", "t3", "t4", "t5", "t6"]);
expect(useCommandCenterStore.getState().cells).toEqual([
"t1",
"t2",
"t3",
"t4",
]);
});

it("does not set activeTaskId", () => {
useCommandCenterStore.getState().autofillCells(["t1"]);
expect(useCommandCenterStore.getState().activeTaskId).toBeNull();
});
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The five autofillCells tests share the same pattern — setState, call autofillCells(input), assert cells. The team's preference is parameterised tests. The first, third, fourth, and fifth cases could be collapsed into a single it.each table covering (input, expectedCells, expectedActiveTaskId), reducing duplication and making it easy to add edge cases in future.

Context Used: Do not attempt to comment on incorrect alphabetica... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/renderer/features/command-center/stores/commandCenterStore.test.ts
Line: 26-73

Comment:
The five `autofillCells` tests share the same pattern — `setState`, call `autofillCells(input)`, assert `cells`. The team's preference is parameterised tests. The first, third, fourth, and fifth cases could be collapsed into a single `it.each` table covering `(input, expectedCells, expectedActiveTaskId)`, reducing duplication and making it easy to add edge cases in future.

**Context Used:** Do not attempt to comment on incorrect alphabetica... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface CommandCenterStoreActions {
setActiveTask: (taskId: string | null) => void;
setActiveCell: (cellIndex: number | null) => void;
assignTask: (cellIndex: number, taskId: string) => void;
autofillCells: (taskIds: string[]) => void;
removeTask: (cellIndex: number) => void;
removeTaskById: (taskId: string) => void;
clearAll: () => void;
Expand Down Expand Up @@ -115,6 +116,18 @@ export const useCommandCenterStore = create<CommandCenterStore>()(
};
}),

autofillCells: (taskIds) =>
set((state) => {
if (!state.cells.every((id) => id == null)) return state;
if (taskIds.length === 0) return state;
const cells: (string | null)[] = [...state.cells];
const limit = Math.min(cells.length, taskIds.length);
for (let i = 0; i < limit; i++) {
cells[i] = taskIds[i];
}
return { cells };
}),

removeTask: (cellIndex) =>
set((state) => {
const cells = [...state.cells];
Expand Down
Loading