-
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
Open
richardsolomou
wants to merge
2
commits into
main
Choose a base branch
from
posthog-code/command-center-autofill-recent-tasks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apps/code/src/renderer/features/command-center/hooks/useAutofillCommandCenter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| 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, | ||
| ]); | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
apps/code/src/renderer/features/command-center/stores/commandCenterStore.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ]); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
workspacesFetchedguard needs a paralleltasksFetchedguard so the effect waits for both data sources before proceeding.Prompt To Fix With AI