Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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,63 @@
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 = [], isFetched: tasksFetched } = useTasks();
const { data: workspaces, isFetched: workspacesFetched } = useWorkspaces();
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 (!tasksFetched) return;

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,
tasksFetched,
archivedTaskIds,
autofillCells,
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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.each([
{
name: "fills empty cells from index 0",
input: ["t1", "t2"],
expectedCells: ["t1", "t2", null, null],
},
{
name: "ignores empty task list",
input: [],
expectedCells: [null, null, null, null],
},
{
name: "caps fill at the number of cells",
input: ["t1", "t2", "t3", "t4", "t5", "t6"],
expectedCells: ["t1", "t2", "t3", "t4"],
},
])("$name and leaves activeTaskId null", ({ input, expectedCells }) => {
useCommandCenterStore.getState().autofillCells(input);
expect(useCommandCenterStore.getState().cells).toEqual(expectedCells);
expect(useCommandCenterStore.getState().activeTaskId).toBeNull();
});

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,
]);
});
});
});
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