-
Notifications
You must be signed in to change notification settings - Fork 23
feat(command-center): autofill empty cells with recent active tasks #2212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||||||||
| 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; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis 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(); | ||
| }); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: Do not attempt to comment on incorrect alphabetica... (source) Prompt To Fix With AIThis 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! |
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workspacesFetchedbut 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 emptytasksarray, finds zero candidates, setshasRunRef.current = true, and exits. When the tasks response eventually arrives, the dependency change re-runs the effect — buthasRunRef.currentis alreadytrue, so it bails. The autofill silently never runs.Prompt To Fix With AI