Extract ChatComposer to fix composer keystroke re-renders (#1857)
Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
This commit is contained in:
9 files changed
+2359
-1730
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime";
|
||||
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
|
||||
import { useMemo } from "react";
|
||||
import { memo, useMemo } from "react";
|
||||
|
||||
import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
|
||||
import { useStore } from "../store";
|
||||
@@ -27,7 +27,7 @@ interface BranchToolbarProps {
|
||||
onEnvironmentChange?: (environmentId: EnvironmentId) => void;
|
||||
}
|
||||
|
||||
export function BranchToolbar({
|
||||
export const BranchToolbar = memo(function BranchToolbar({
|
||||
environmentId,
|
||||
threadId,
|
||||
draftId,
|
||||
@@ -101,4 +101,4 @@ export function BranchToolbar({
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
type EnvironmentId,
|
||||
ProjectId,
|
||||
type ModelSelection,
|
||||
type ProviderKind,
|
||||
type ScopedThreadRef,
|
||||
type ThreadId,
|
||||
type TurnId,
|
||||
@@ -225,6 +226,17 @@ export function threadHasStarted(thread: Thread | null | undefined): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
export function deriveLockedProvider(input: {
|
||||
thread: Thread | null | undefined;
|
||||
selectedProvider: ProviderKind | null;
|
||||
threadProvider: ProviderKind | null;
|
||||
}): ProviderKind | null {
|
||||
if (!threadHasStarted(input.thread)) {
|
||||
return null;
|
||||
}
|
||||
return input.thread?.session?.provider ?? input.threadProvider ?? input.selectedProvider ?? null;
|
||||
}
|
||||
|
||||
export async function waitForStartedServerThread(
|
||||
threadRef: ScopedThreadRef,
|
||||
timeoutMs = 1_000,
|
||||
|
||||
+200
-1675
File diff suppressed because it is too large.
Load diff
@@ -79,7 +79,7 @@ import {
|
||||
import { useGitStatus } from "../lib/gitStatusState";
|
||||
import { readLocalApi } from "../localApi";
|
||||
import { useComposerDraftStore } from "../composerDraftStore";
|
||||
import { useHandleNewThread } from "../hooks/useHandleNewThread";
|
||||
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
|
||||
|
||||
import { useThreadActions } from "../hooks/useThreadActions";
|
||||
import {
|
||||
@@ -932,7 +932,7 @@ interface SidebarProjectItemProps {
|
||||
isThreadListExpanded: boolean;
|
||||
activeRouteThreadKey: string | null;
|
||||
newThreadShortcutLabel: string | null;
|
||||
handleNewThread: ReturnType<typeof useHandleNewThread>["handleNewThread"];
|
||||
handleNewThread: ReturnType<typeof useNewThreadHandler>["handleNewThread"];
|
||||
archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];
|
||||
deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
|
||||
threadJumpLabelByKey: ReadonlyMap<string, string>;
|
||||
@@ -2033,7 +2033,7 @@ interface SidebarProjectsContentProps {
|
||||
handleProjectDragStart: (event: DragStartEvent) => void;
|
||||
handleProjectDragEnd: (event: DragEndEvent) => void;
|
||||
handleProjectDragCancel: (event: DragCancelEvent) => void;
|
||||
handleNewThread: ReturnType<typeof useHandleNewThread>["handleNewThread"];
|
||||
handleNewThread: ReturnType<typeof useNewThreadHandler>["handleNewThread"];
|
||||
archiveThread: ReturnType<typeof useThreadActions>["archiveThread"];
|
||||
deleteThread: ReturnType<typeof useThreadActions>["deleteThread"];
|
||||
sortedProjects: readonly SidebarProjectSnapshot[];
|
||||
@@ -2338,7 +2338,7 @@ export default function Sidebar() {
|
||||
const sidebarProjectSortOrder = useSettings((s) => s.sidebarProjectSortOrder);
|
||||
const defaultThreadEnvMode = useSettings((s) => s.defaultThreadEnvMode);
|
||||
const { updateSettings } = useUpdateSettings();
|
||||
const { handleNewThread } = useHandleNewThread();
|
||||
const { handleNewThread } = useNewThreadHandler();
|
||||
const { archiveThread, deleteThread } = useThreadActions();
|
||||
const routeThreadRef = useParams({
|
||||
strict: false,
|
||||
@@ -2827,31 +2827,62 @@ export default function Sidebar() {
|
||||
);
|
||||
const [threadJumpLabelByKey, setThreadJumpLabelByKey] =
|
||||
useState<ReadonlyMap<string, string>>(EMPTY_THREAD_JUMP_LABELS);
|
||||
const threadJumpLabelsRef = useRef<ReadonlyMap<string, string>>(EMPTY_THREAD_JUMP_LABELS);
|
||||
threadJumpLabelsRef.current = threadJumpLabelByKey;
|
||||
const showThreadJumpHintsRef = useRef(showThreadJumpHints);
|
||||
showThreadJumpHintsRef.current = showThreadJumpHints;
|
||||
const visibleThreadJumpLabelByKey = showThreadJumpHints
|
||||
? threadJumpLabelByKey
|
||||
: EMPTY_THREAD_JUMP_LABELS;
|
||||
const orderedSidebarThreadKeys = visibleSidebarThreadKeys;
|
||||
|
||||
useEffect(() => {
|
||||
const clearThreadJumpHints = () => {
|
||||
setThreadJumpLabelByKey((current) =>
|
||||
current === EMPTY_THREAD_JUMP_LABELS ? current : EMPTY_THREAD_JUMP_LABELS,
|
||||
);
|
||||
updateThreadJumpHintsVisibility(false);
|
||||
};
|
||||
const shouldIgnoreThreadJumpHintUpdate = (event: globalThis.KeyboardEvent) =>
|
||||
!event.metaKey &&
|
||||
!event.ctrlKey &&
|
||||
!event.altKey &&
|
||||
!event.shiftKey &&
|
||||
event.key !== "Meta" &&
|
||||
event.key !== "Control" &&
|
||||
event.key !== "Alt" &&
|
||||
event.key !== "Shift" &&
|
||||
!showThreadJumpHintsRef.current &&
|
||||
threadJumpLabelsRef.current === EMPTY_THREAD_JUMP_LABELS;
|
||||
|
||||
const onWindowKeyDown = (event: globalThis.KeyboardEvent) => {
|
||||
if (shouldIgnoreThreadJumpHintUpdate(event)) {
|
||||
return;
|
||||
}
|
||||
const shortcutContext = getCurrentSidebarShortcutContext();
|
||||
const shouldShowHints = shouldShowThreadJumpHints(event, keybindings, {
|
||||
platform,
|
||||
context: shortcutContext,
|
||||
});
|
||||
setThreadJumpLabelByKey((current) => {
|
||||
if (!shouldShowHints) {
|
||||
return current === EMPTY_THREAD_JUMP_LABELS ? current : EMPTY_THREAD_JUMP_LABELS;
|
||||
if (!shouldShowHints) {
|
||||
if (
|
||||
showThreadJumpHintsRef.current ||
|
||||
threadJumpLabelsRef.current !== EMPTY_THREAD_JUMP_LABELS
|
||||
) {
|
||||
clearThreadJumpHints();
|
||||
}
|
||||
const nextLabelMap = buildThreadJumpLabelMap({
|
||||
keybindings,
|
||||
platform,
|
||||
terminalOpen: shortcutContext.terminalOpen,
|
||||
threadJumpCommandByKey,
|
||||
} else {
|
||||
setThreadJumpLabelByKey((current) => {
|
||||
const nextLabelMap = buildThreadJumpLabelMap({
|
||||
keybindings,
|
||||
platform,
|
||||
terminalOpen: shortcutContext.terminalOpen,
|
||||
threadJumpCommandByKey,
|
||||
});
|
||||
return threadJumpLabelMapsEqual(current, nextLabelMap) ? current : nextLabelMap;
|
||||
});
|
||||
return threadJumpLabelMapsEqual(current, nextLabelMap) ? current : nextLabelMap;
|
||||
});
|
||||
updateThreadJumpHintsVisibility(shouldShowHints);
|
||||
updateThreadJumpHintsVisibility(true);
|
||||
}
|
||||
|
||||
if (event.defaultPrevented || event.repeat) {
|
||||
return;
|
||||
@@ -2902,15 +2933,19 @@ export default function Sidebar() {
|
||||
};
|
||||
|
||||
const onWindowKeyUp = (event: globalThis.KeyboardEvent) => {
|
||||
if (shouldIgnoreThreadJumpHintUpdate(event)) {
|
||||
return;
|
||||
}
|
||||
const shortcutContext = getCurrentSidebarShortcutContext();
|
||||
const shouldShowHints = shouldShowThreadJumpHints(event, keybindings, {
|
||||
platform,
|
||||
context: shortcutContext,
|
||||
});
|
||||
if (!shouldShowHints) {
|
||||
clearThreadJumpHints();
|
||||
return;
|
||||
}
|
||||
setThreadJumpLabelByKey((current) => {
|
||||
if (!shouldShowHints) {
|
||||
return current === EMPTY_THREAD_JUMP_LABELS ? current : EMPTY_THREAD_JUMP_LABELS;
|
||||
}
|
||||
const nextLabelMap = buildThreadJumpLabelMap({
|
||||
keybindings,
|
||||
platform,
|
||||
@@ -2919,14 +2954,11 @@ export default function Sidebar() {
|
||||
});
|
||||
return threadJumpLabelMapsEqual(current, nextLabelMap) ? current : nextLabelMap;
|
||||
});
|
||||
updateThreadJumpHintsVisibility(shouldShowHints);
|
||||
updateThreadJumpHintsVisibility(true);
|
||||
};
|
||||
|
||||
const onWindowBlur = () => {
|
||||
setThreadJumpLabelByKey((current) =>
|
||||
current === EMPTY_THREAD_JUMP_LABELS ? current : EMPTY_THREAD_JUMP_LABELS,
|
||||
);
|
||||
updateThreadJumpHintsVisibility(false);
|
||||
clearThreadJumpHints();
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", onWindowKeyDown);
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,120 @@
|
||||
import { memo, useCallback, useEffect, useState } from "react";
|
||||
import { ChevronLeftIcon, ChevronRightIcon, XIcon } from "lucide-react";
|
||||
import { Button } from "../ui/button";
|
||||
import type { ExpandedImagePreview } from "./ExpandedImagePreview";
|
||||
|
||||
interface ExpandedImageDialogProps {
|
||||
preview: ExpandedImagePreview;
|
||||
onClose: () => void;
|
||||
}
|
||||
|
||||
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
|
||||
preview: initialPreview,
|
||||
onClose,
|
||||
}: ExpandedImageDialogProps) {
|
||||
const [preview, setPreview] = useState(initialPreview);
|
||||
|
||||
// Sync when the parent hands us a new preview reference.
|
||||
useEffect(() => {
|
||||
setPreview(initialPreview);
|
||||
}, [initialPreview]);
|
||||
|
||||
const navigateImage = useCallback((direction: -1 | 1) => {
|
||||
setPreview((existing) => {
|
||||
if (existing.images.length <= 1) return existing;
|
||||
const nextIndex =
|
||||
(existing.index + direction + existing.images.length) % existing.images.length;
|
||||
if (nextIndex === existing.index) return existing;
|
||||
return { ...existing, index: nextIndex };
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const onKeyDown = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
if (preview.images.length <= 1) return;
|
||||
if (event.key === "ArrowLeft") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
navigateImage(-1);
|
||||
return;
|
||||
}
|
||||
if (event.key !== "ArrowRight") return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
navigateImage(1);
|
||||
};
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [navigateImage, onClose, preview.images.length]);
|
||||
|
||||
const item = preview.images[preview.index];
|
||||
if (!item) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 px-4 py-6 [-webkit-app-region:no-drag]"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label="Expanded image preview"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
className="absolute inset-0 z-0 cursor-zoom-out"
|
||||
aria-label="Close image preview"
|
||||
onClick={onClose}
|
||||
/>
|
||||
{preview.images.length > 1 && (
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="absolute left-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:left-6"
|
||||
aria-label="Previous image"
|
||||
onClick={() => navigateImage(-1)}
|
||||
>
|
||||
<ChevronLeftIcon className="size-5" />
|
||||
</Button>
|
||||
)}
|
||||
<div className="relative isolate z-10 max-h-[92vh] max-w-[92vw]">
|
||||
<Button
|
||||
type="button"
|
||||
size="icon-xs"
|
||||
variant="ghost"
|
||||
className="absolute right-2 top-2"
|
||||
onClick={onClose}
|
||||
aria-label="Close image preview"
|
||||
>
|
||||
<XIcon />
|
||||
</Button>
|
||||
<img
|
||||
src={item.src}
|
||||
alt={item.name}
|
||||
className="max-h-[86vh] max-w-[92vw] select-none rounded-lg border border-border/70 bg-background object-contain shadow-2xl"
|
||||
draggable={false}
|
||||
/>
|
||||
<p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
|
||||
{item.name}
|
||||
{preview.images.length > 1 ? ` (${preview.index + 1}/${preview.images.length})` : ""}
|
||||
</p>
|
||||
</div>
|
||||
{preview.images.length > 1 && (
|
||||
<Button
|
||||
type="button"
|
||||
size="icon"
|
||||
variant="ghost"
|
||||
className="absolute right-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:right-6"
|
||||
aria-label="Next image"
|
||||
onClick={() => navigateImage(1)}
|
||||
>
|
||||
<ChevronRightIcon className="size-5" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -38,6 +38,7 @@ import {
|
||||
} from "./lib/terminalContext";
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import { useShallow } from "zustand/react/shallow";
|
||||
import { createDebouncedStorage, createMemoryStorage } from "./lib/storage";
|
||||
import { getDefaultServerModel } from "./providerModels";
|
||||
import { UnifiedSettings } from "@t3tools/contracts/settings";
|
||||
@@ -378,6 +379,11 @@ export interface EffectiveComposerModelState {
|
||||
modelOptions: ProviderModelOptions | null;
|
||||
}
|
||||
|
||||
interface ComposerDraftModelState {
|
||||
activeProvider: ProviderKind | null;
|
||||
modelSelectionByProvider: Partial<Record<ProviderKind, ModelSelection>>;
|
||||
}
|
||||
|
||||
function providerModelOptionsFromSelection(
|
||||
modelSelection: ModelSelection | null | undefined,
|
||||
): ProviderModelOptions | null {
|
||||
@@ -420,6 +426,10 @@ Object.freeze(EMPTY_IDS);
|
||||
Object.freeze(EMPTY_PERSISTED_ATTACHMENTS);
|
||||
const EMPTY_MODEL_SELECTION_BY_PROVIDER: Partial<Record<ProviderKind, ModelSelection>> =
|
||||
Object.freeze({});
|
||||
const EMPTY_COMPOSER_DRAFT_MODEL_STATE = Object.freeze<ComposerDraftModelState>({
|
||||
activeProvider: null,
|
||||
modelSelectionByProvider: EMPTY_MODEL_SELECTION_BY_PROVIDER,
|
||||
});
|
||||
|
||||
const EMPTY_THREAD_DRAFT = Object.freeze<ComposerThreadDraftState>({
|
||||
prompt: "",
|
||||
@@ -2764,6 +2774,22 @@ export function useComposerThreadDraft(threadRef: ComposerThreadTarget): Compose
|
||||
});
|
||||
}
|
||||
|
||||
export function useComposerDraftModelState(
|
||||
threadRef: ComposerThreadTarget,
|
||||
): ComposerDraftModelState {
|
||||
return useComposerDraftStore(
|
||||
useShallow((state) => {
|
||||
const draft = getComposerDraftState(state, threadRef);
|
||||
return draft
|
||||
? {
|
||||
activeProvider: draft.activeProvider,
|
||||
modelSelectionByProvider: draft.modelSelectionByProvider,
|
||||
}
|
||||
: EMPTY_COMPOSER_DRAFT_MODEL_STATE;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function useEffectiveComposerModelState(input: {
|
||||
threadRef?: ComposerThreadTarget;
|
||||
draftId?: DraftId;
|
||||
@@ -2773,7 +2799,9 @@ export function useEffectiveComposerModelState(input: {
|
||||
projectModelSelection: ModelSelection | null | undefined;
|
||||
settings: UnifiedSettings;
|
||||
}): EffectiveComposerModelState {
|
||||
const draft = useComposerThreadDraft(input.threadRef ?? input.draftId ?? DraftId.makeUnsafe(""));
|
||||
const draft = useComposerDraftModelState(
|
||||
input.threadRef ?? input.draftId ?? DraftId.makeUnsafe(""),
|
||||
);
|
||||
|
||||
return useMemo(
|
||||
() =>
|
||||
|
||||
@@ -16,39 +16,15 @@ import { createThreadSelectorByRef } from "../storeSelectors";
|
||||
import { resolveThreadRouteTarget } from "../threadRoutes";
|
||||
import { useUiStateStore } from "../uiStateStore";
|
||||
|
||||
export function useHandleNewThread() {
|
||||
function useNewThreadState() {
|
||||
const projects = useStore(useShallow((store) => selectProjectsAcrossEnvironments(store)));
|
||||
const projectOrder = useUiStateStore((store) => store.projectOrder);
|
||||
const router = useRouter();
|
||||
const routeTarget = useParams({
|
||||
strict: false,
|
||||
select: (params) => resolveThreadRouteTarget(params),
|
||||
});
|
||||
const routeThreadRef = routeTarget?.kind === "server" ? routeTarget.threadRef : null;
|
||||
const activeThread = useStore(
|
||||
useMemo(() => createThreadSelectorByRef(routeThreadRef), [routeThreadRef]),
|
||||
);
|
||||
const getDraftThread = useComposerDraftStore((store) => store.getDraftThread);
|
||||
const activeDraftThread = useComposerDraftStore(() =>
|
||||
routeTarget
|
||||
? routeTarget.kind === "server"
|
||||
? getDraftThread(routeTarget.threadRef)
|
||||
: useComposerDraftStore.getState().getDraftSession(routeTarget.draftId)
|
||||
: null,
|
||||
);
|
||||
const orderedProjects = useMemo(() => {
|
||||
return orderItemsByPreferredIds({
|
||||
items: projects,
|
||||
preferredIds: projectOrder,
|
||||
getId: (project) => scopedProjectKey(scopeProjectRef(project.environmentId, project.id)),
|
||||
});
|
||||
}, [projectOrder, projects]);
|
||||
const getCurrentRouteTarget = useCallback(() => {
|
||||
const currentRouteParams = router.state.matches[router.state.matches.length - 1]?.params ?? {};
|
||||
return resolveThreadRouteTarget(currentRouteParams);
|
||||
}, [router]);
|
||||
|
||||
const handleNewThread = useCallback(
|
||||
return useCallback(
|
||||
(
|
||||
projectRef: ScopedProjectRef,
|
||||
options?: {
|
||||
@@ -154,6 +130,43 @@ export function useHandleNewThread() {
|
||||
},
|
||||
[getCurrentRouteTarget, router, projects],
|
||||
);
|
||||
}
|
||||
|
||||
export function useNewThreadHandler() {
|
||||
const handleNewThread = useNewThreadState();
|
||||
|
||||
return {
|
||||
handleNewThread,
|
||||
};
|
||||
}
|
||||
|
||||
export function useHandleNewThread() {
|
||||
const projectOrder = useUiStateStore((store) => store.projectOrder);
|
||||
const routeTarget = useParams({
|
||||
strict: false,
|
||||
select: (params) => resolveThreadRouteTarget(params),
|
||||
});
|
||||
const routeThreadRef = routeTarget?.kind === "server" ? routeTarget.threadRef : null;
|
||||
const activeThread = useStore(
|
||||
useMemo(() => createThreadSelectorByRef(routeThreadRef), [routeThreadRef]),
|
||||
);
|
||||
const getDraftThread = useComposerDraftStore((store) => store.getDraftThread);
|
||||
const activeDraftThread = useComposerDraftStore(() =>
|
||||
routeTarget
|
||||
? routeTarget.kind === "server"
|
||||
? getDraftThread(routeTarget.threadRef)
|
||||
: useComposerDraftStore.getState().getDraftSession(routeTarget.draftId)
|
||||
: null,
|
||||
);
|
||||
const projects = useStore(useShallow((store) => selectProjectsAcrossEnvironments(store)));
|
||||
const orderedProjects = useMemo(() => {
|
||||
return orderItemsByPreferredIds({
|
||||
items: projects,
|
||||
preferredIds: projectOrder,
|
||||
getId: (project) => scopedProjectKey(scopeProjectRef(project.environmentId, project.id)),
|
||||
});
|
||||
}, [projectOrder, projects]);
|
||||
const handleNewThread = useNewThreadState();
|
||||
|
||||
return {
|
||||
activeDraftThread,
|
||||
|
||||
@@ -6,7 +6,7 @@ import { useCallback } from "react";
|
||||
|
||||
import { getFallbackThreadIdAfterDelete } from "../components/Sidebar.logic";
|
||||
import { useComposerDraftStore } from "../composerDraftStore";
|
||||
import { useHandleNewThread } from "./useHandleNewThread";
|
||||
import { useNewThreadHandler } from "./useHandleNewThread";
|
||||
import { ensureEnvironmentApi, readEnvironmentApi } from "../environmentApi";
|
||||
import { invalidateGitQueries } from "../lib/gitReactQuery";
|
||||
import { newCommandId } from "../lib/utils";
|
||||
@@ -32,7 +32,7 @@ export function useThreadActions() {
|
||||
);
|
||||
const clearTerminalState = useTerminalStateStore((state) => state.clearTerminalState);
|
||||
const router = useRouter();
|
||||
const { handleNewThread } = useHandleNewThread();
|
||||
const { handleNewThread } = useNewThreadHandler();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const resolveThreadTarget = useCallback((target: ScopedThreadRef) => {
|
||||
|
||||
Reference in new issue
Block a user