Simplify Zustand selectors by removing memoized callbacks

- Replace `useCallback`-wrapped `useStore` selectors with inline selectors
- Apply across chat, diff, branch toolbar, and chat thread route components
This commit is contained in:
Julius Marminge committed 2026-03-02 10:20:53 -08:00
1 parent 5679c5fbea
commit ef85d8d1d6
4 files changed
+23 -51

No files matched your search

+5 -9
View File
@@ -1,6 +1,6 @@
import type { GitBranch, ThreadId } from "@t3tools/contracts";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback, useEffect, useState } from "react";
import { useEffect, useState } from "react";
import { newCommandId } from "../lib/utils";
import {
@@ -46,16 +46,12 @@ export default function BranchToolbar({
const setThreadBranchAction = useStore((state) => state.setThreadBranch);
const setThreadErrorAction = useStore((state) => state.setThreadError);
const draftThread = useComposerDraftStore((store) => store.getDraftThread(threadId));
const serverThread = useStore(
useCallback((state) => state.threads.find((thread) => thread.id === threadId), [threadId]),
const serverThread = useStore((state) =>
state.threads.find((thread) => thread.id === threadId),
);
const activeProjectId = serverThread?.projectId ?? draftThread?.projectId ?? null;
const activeProject = useStore(
useCallback(
(state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
[activeProjectId],
),
const activeProject = useStore((state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const queryClient = useQueryClient();
+12 -28
View File
@@ -490,18 +490,14 @@ export default function ChatView({ threadId }: ChatViewProps) {
const clearDraftThread = useComposerDraftStore((store) => store.clearDraftThread);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const draftThread = useComposerDraftStore((store) => store.draftThreadsByThreadId[threadId] ?? null);
const serverThread = useStore(
useCallback((state) => state.threads.find((thread) => thread.id === threadId), [threadId]),
const serverThread = useStore((state) =>
state.threads.find((thread) => thread.id === threadId),
);
const fallbackDraftProjectId = draftThread?.projectId ?? null;
const fallbackDraftProject = useStore(
useCallback(
(state) =>
fallbackDraftProjectId
? state.projects.find((project) => project.id === fallbackDraftProjectId)
: undefined,
[fallbackDraftProjectId],
),
const fallbackDraftProject = useStore((state) =>
fallbackDraftProjectId
? state.projects.find((project) => project.id === fallbackDraftProjectId)
: undefined,
);
const promptRef = useRef(prompt);
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
@@ -601,12 +597,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
const activeLatestTurn = activeThread?.latestTurn ?? null;
const latestTurnSettled = isLatestTurnSettled(activeLatestTurn, activeThread?.session ?? null);
const activeProjectId = activeThread?.projectId ?? null;
const activeProject = useStore(
useCallback(
(state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
[activeProjectId],
),
const activeProject = useStore((state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
);
useEffect(() => {
@@ -3541,20 +3533,12 @@ const OpenInPicker = memo(function OpenInPicker({
] satisfies { label: string; Icon: Icon; value: EditorId }[];
const primaryOption = options.find(({ value }) => value === lastEditor);
const activeThread = useStore(
useCallback(
(state) =>
activeThreadId ? state.threads.find((thread) => thread.id === activeThreadId) : undefined,
[activeThreadId],
),
const activeThread = useStore((state) =>
activeThreadId ? state.threads.find((thread) => thread.id === activeThreadId) : undefined,
);
const activeProjectId = activeThread?.projectId ?? null;
const activeProject = useStore(
useCallback(
(state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
[activeProjectId],
),
const activeProject = useStore((state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
);
const openInEditor = useCallback(
+4 -12
View File
@@ -167,20 +167,12 @@ export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
});
const diffSearch = useSearch({ strict: false, select: (search) => parseDiffRouteSearch(search) });
const activeThreadId = routeThreadId;
const activeThread = useStore(
useCallback(
(state) =>
activeThreadId ? state.threads.find((thread) => thread.id === activeThreadId) : undefined,
[activeThreadId],
),
const activeThread = useStore((state) =>
activeThreadId ? state.threads.find((thread) => thread.id === activeThreadId) : undefined,
);
const activeProjectId = activeThread?.projectId ?? null;
const activeProject = useStore(
useCallback(
(state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
[activeProjectId],
),
const activeProject = useStore((state) =>
activeProjectId ? state.projects.find((project) => project.id === activeProjectId) : undefined,
);
const activeCwd = activeThread?.worktreePath ?? activeProject?.cwd;
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
+2 -2
View File
@@ -139,8 +139,8 @@ function ChatThreadRouteView() {
select: (params) => ThreadId.makeUnsafe(params.threadId),
});
const search = Route.useSearch();
const threadExists = useStore(
useCallback((state) => state.threads.some((thread) => thread.id === threadId), [threadId]),
const threadExists = useStore((state) =>
state.threads.some((thread) => thread.id === threadId),
);
const draftThreadExists = useComposerDraftStore(
(store) => Object.hasOwn(store.draftThreadsByThreadId, threadId),