Skip to content

Commit 7a7f3b2

Browse files
committed
Use selectors where possible for workspace, repo, file watcher, etc
1 parent e8dadf9 commit 7a7f3b2

6 files changed

Lines changed: 33 additions & 19 deletions

File tree

apps/twig/src/renderer/features/panels/hooks/usePanelLayoutHooks.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { FileIcon } from "@components/ui/FileIcon";
22
import { TabContentRenderer } from "@features/task-detail/components/TabContentRenderer";
3-
import { useTaskExecutionStore } from "@features/task-detail/stores/taskExecutionStore";
3+
import {
4+
selectTaskRepoPath,
5+
useTaskExecutionStore,
6+
} from "@features/task-detail/stores/taskExecutionStore";
47
import { ChatCenteredText, Terminal } from "@phosphor-icons/react";
58
import type { Task } from "@shared/types";
69
import { useCallback, useEffect, useMemo, useRef } from "react";
@@ -82,9 +85,7 @@ export function useTabInjection(
8285
closeTab: (taskId: string, panelId: string, tabId: string) => void,
8386
): Tab[] {
8487
const worktreePath = useWorkspaceStore(selectWorktreePath(taskId));
85-
const storedRepoPath = useTaskExecutionStore(
86-
(state) => state.taskStates[taskId]?.repoPath ?? null,
87-
);
88+
const storedRepoPath = useTaskExecutionStore(selectTaskRepoPath(taskId));
8889
const repoPath = worktreePath || storedRepoPath || "";
8990

9091
return useMemo(

apps/twig/src/renderer/features/right-sidebar/stores/fileTreeStore.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ export const useFileTreeStore = create<FileTreeStore>()((set) => ({
3838
},
3939
})),
4040
}));
41+
42+
// Selector factory for checking if a path is expanded
43+
export const selectIsPathExpanded =
44+
(taskId: string, path: string) => (state: FileTreeStore) =>
45+
state.expandedPaths[taskId]?.has(path) ?? false;

apps/twig/src/renderer/features/task-detail/components/FileTreePanel.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { FileIcon } from "@components/ui/FileIcon";
22
import { PanelMessage } from "@components/ui/PanelMessage";
33
import { isFileTabActiveInTree, usePanelLayoutStore } from "@features/panels";
4-
import { useFileTreeStore } from "@features/right-sidebar/stores/fileTreeStore";
4+
import {
5+
selectIsPathExpanded,
6+
useFileTreeStore,
7+
} from "@features/right-sidebar/stores/fileTreeStore";
58
import { useTaskData } from "@features/task-detail/hooks/useTaskData";
69
import { CaretRight, FolderIcon, FolderOpenIcon } from "@phosphor-icons/react";
710
import { Box, Flex, Text } from "@radix-ui/themes";
@@ -40,9 +43,7 @@ function LazyTreeItem({
4043
repoPath,
4144
isFileActive,
4245
}: LazyTreeItemProps) {
43-
const isExpanded = useFileTreeStore(
44-
(state) => state.expandedPaths[taskId]?.has(entry.path) ?? false,
45-
);
46+
const isExpanded = useFileTreeStore(selectIsPathExpanded(taskId, entry.path));
4647
const togglePath = useFileTreeStore((state) => state.togglePath);
4748
const collapseAll = useFileTreeStore((state) => state.collapseAll);
4849
const openFile = usePanelLayoutStore((state) => state.openFile);

apps/twig/src/renderer/features/task-detail/components/TaskDetail.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Box, Code, Flex, Text } from "@radix-ui/themes";
1212
import type { Task } from "@shared/types";
1313
import { useMemo } from "react";
1414
import {
15+
selectWorkspace,
1516
selectWorktreePath,
1617
useWorkspaceStore,
1718
} from "@/renderer/features/workspace/stores/workspaceStore";
@@ -56,10 +57,7 @@ export function TaskDetail({ task: initialTask }: TaskDetailProps) {
5657

5758
useWorkspaceEvents(taskId);
5859
const task = taskData.task;
59-
const workspace = useWorkspaceStore(
60-
(state) => state.workspaces[taskId] ?? null,
61-
);
62-
60+
const workspace = useWorkspaceStore(selectWorkspace(taskId));
6361
const branchName = workspace?.branchName;
6462

6563
const headerContent = useMemo(

apps/twig/src/renderer/features/task-detail/hooks/useTaskData.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { useTaskExecutionStore } from "@features/task-detail/stores/taskExecutionStore";
1+
import {
2+
selectTaskRepoExists,
3+
selectTaskRepoPath,
4+
useTaskExecutionStore,
5+
} from "@features/task-detail/stores/taskExecutionStore";
26
import { useTasks } from "@features/tasks/hooks/useTasks";
37
import type { Task } from "@shared/types";
48
import { cloneStore } from "@stores/cloneStore";
@@ -27,12 +31,8 @@ export function useTaskData({ taskId, initialTask }: UseTaskDataParams) {
2731
}, [initializeRepoPath, taskId, task]);
2832

2933
// Subscribe to specific fields reactively to avoid unnecessary rerenders
30-
const repoPath = useTaskExecutionStore(
31-
(state) => state.taskStates[taskId]?.repoPath ?? null,
32-
);
33-
const repoExists = useTaskExecutionStore(
34-
(state) => state.taskStates[taskId]?.repoExists ?? null,
35-
);
34+
const repoPath = useTaskExecutionStore(selectTaskRepoPath(taskId));
35+
const repoExists = useTaskExecutionStore(selectTaskRepoExists(taskId));
3636

3737
const repository = getTaskRepository(task);
3838

apps/twig/src/renderer/features/task-detail/stores/taskExecutionStore.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,12 @@ export const useTaskExecutionStore = create<TaskExecutionStore>()(
147147
},
148148
),
149149
);
150+
151+
// Selector factories for parameterized access (taskId-based)
152+
export const selectTaskRepoPath =
153+
(taskId: string) => (state: TaskExecutionStore) =>
154+
state.taskStates[taskId]?.repoPath ?? null;
155+
156+
export const selectTaskRepoExists =
157+
(taskId: string) => (state: TaskExecutionStore) =>
158+
state.taskStates[taskId]?.repoExists ?? null;

0 commit comments

Comments
 (0)