Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge 38f6a0b78a Add React Scan provider pill recordings
Co-authored-by: Julius Marminge <[email protected]>
2026-05-25 16:24:14 +00:00
Cursor AgentandJulius Marminge 8b2adcc9fd Reduce provider update pill rerenders
Co-authored-by: Julius Marminge <[email protected]>
2026-05-25 16:19:40 +00:00
8 changed files with 148 additions and 141 deletions

No files matched your search

+40 -12
View File
@@ -68,10 +68,7 @@ import { ComposerPrimaryActions } from "./ComposerPrimaryActions";
import { ComposerPendingApprovalPanel } from "./ComposerPendingApprovalPanel";
import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
import { ComposerPlanFollowUpBanner } from "./ComposerPlanFollowUpBanner";
import {
resolveComposerMenuActiveItemId,
resolveComposerMenuNudgedItemId,
} from "./composerMenuHighlight";
import { resolveComposerMenuActiveItemId } from "./composerMenuHighlight";
import { searchSlashCommandItems } from "./composerSlashCommandSearch";
import {
getComposerProviderState,
@@ -1137,6 +1134,35 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
composerTerminalContextsRef.current = composerTerminalContexts;
}, [composerTerminalContexts, composerTerminalContextsRef]);
// ------------------------------------------------------------------
// Composer menu highlight sync
// ------------------------------------------------------------------
useEffect(() => {
if (!composerMenuOpen) {
setComposerHighlightedItemId(null);
setComposerHighlightedSearchKey(null);
return;
}
const nextActiveItemId = resolveComposerMenuActiveItemId({
items: composerMenuItems,
highlightedItemId: composerHighlightedItemId,
currentSearchKey: composerMenuSearchKey,
highlightedSearchKey: composerHighlightedSearchKey,
});
setComposerHighlightedItemId((existing) =>
existing === nextActiveItemId ? existing : nextActiveItemId,
);
setComposerHighlightedSearchKey((existing) =>
existing === composerMenuSearchKey ? existing : composerMenuSearchKey,
);
}, [
composerHighlightedItemId,
composerHighlightedSearchKey,
composerMenuItems,
composerMenuOpen,
composerMenuSearchKey,
]);
const lastSyncedPendingInputRef = useRef<{
requestId: string | null;
questionId: string | null;
@@ -1550,16 +1576,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const nudgeComposerMenuHighlight = useCallback(
(key: "ArrowDown" | "ArrowUp") => {
if (composerMenuItems.length === 0) return;
setComposerHighlightedItemId(
resolveComposerMenuNudgedItemId({
items: composerMenuItems,
activeItemId: activeComposerMenuItem?.id ?? null,
direction: key === "ArrowDown" ? "next" : "previous",
}),
const highlightedIndex = composerMenuItems.findIndex(
(item) => item.id === composerHighlightedItemId,
);
setComposerHighlightedSearchKey(composerMenuSearchKey);
const normalizedIndex =
highlightedIndex >= 0 ? highlightedIndex : key === "ArrowDown" ? -1 : 0;
const offset = key === "ArrowDown" ? 1 : -1;
const nextIndex =
(normalizedIndex + offset + composerMenuItems.length) % composerMenuItems.length;
const nextItem = composerMenuItems[nextIndex];
setComposerHighlightedItemId(nextItem?.id ?? null);
},
[activeComposerMenuItem?.id, composerMenuItems, composerMenuSearchKey],
[composerHighlightedItemId, composerMenuItems],
);
const blurMobileComposerAfterSend = useCallback(() => {
@@ -1,9 +1,6 @@
import { describe, expect, it } from "vitest";
import {
resolveComposerMenuActiveItemId,
resolveComposerMenuNudgedItemId,
} from "./composerMenuHighlight";
import { resolveComposerMenuActiveItemId } from "./composerMenuHighlight";
describe("resolveComposerMenuActiveItemId", () => {
const items = [{ id: "top" }, { id: "second" }, { id: "third" }] as const;
@@ -52,61 +49,3 @@ describe("resolveComposerMenuActiveItemId", () => {
).toBe("top");
});
});
describe("resolveComposerMenuNudgedItemId", () => {
const items = [{ id: "top" }, { id: "second" }, { id: "third" }] as const;
it("moves from the active item", () => {
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "second",
direction: "next",
}),
).toBe("third");
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "second",
direction: "previous",
}),
).toBe("top");
});
it("wraps around at either edge", () => {
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "third",
direction: "next",
}),
).toBe("top");
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "top",
direction: "previous",
}),
).toBe("third");
});
it("starts from the first visible item when active state is stale", () => {
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "missing",
direction: "next",
}),
).toBe("top");
expect(
resolveComposerMenuNudgedItemId({
items,
activeItemId: "missing",
direction: "previous",
}),
).toBe("third");
});
});
@@ -18,20 +18,3 @@ export function resolveComposerMenuActiveItemId(input: {
return input.items[0]?.id ?? null;
}
export function resolveComposerMenuNudgedItemId(input: {
items: ReadonlyArray<{ id: string }>;
activeItemId: string | null;
direction: "next" | "previous";
}): string | null {
if (input.items.length === 0) {
return null;
}
const activeIndex = input.items.findIndex((item) => item.id === input.activeItemId);
const normalizedIndex = activeIndex >= 0 ? activeIndex : input.direction === "next" ? -1 : 0;
const offset = input.direction === "next" ? 1 : -1;
const nextIndex = (normalizedIndex + offset + input.items.length) % input.items.length;
return input.items[nextIndex]?.id ?? null;
}
@@ -1,7 +1,16 @@
import { useNavigate } from "@tanstack/react-router";
import type { ServerProvider } from "@t3tools/contracts";
import { CircleCheckIcon, DownloadIcon, LoaderIcon, TriangleAlertIcon, XIcon } from "lucide-react";
import { useCallback, useEffect, useState, type CSSProperties } from "react";
import {
useCallback,
useEffect,
useRef,
useState,
type CSSProperties,
type Dispatch,
type SetStateAction,
type TransitionEvent,
} from "react";
import { useServerProviders } from "../../rpc/serverState";
import {
@@ -37,48 +46,36 @@ function latestProviderCheckedAt(
);
}
export function SidebarProviderUpdatePill() {
const navigate = useNavigate();
const providers = useServerProviders();
const [dismissedKeys, setDismissedKeys] = useState<ReadonlySet<string>>(() => new Set());
const [renderedView, setRenderedView] = useState<ProviderUpdateSidebarPillView | null>(null);
const [pendingView, setPendingView] = useState<ProviderUpdateSidebarPillView | null>(null);
function useProviderUpdateVisibleAfterIso(
providers: ReadonlyArray<Pick<ServerProvider, "checkedAt">>,
): string | undefined {
const visibleAfterIsoRef = useRef<string | undefined>(undefined);
if (visibleAfterIsoRef.current === undefined) {
visibleAfterIsoRef.current = latestProviderCheckedAt(providers);
}
return visibleAfterIsoRef.current;
}
function useDisplayedProviderUpdatePill(input: {
view: ProviderUpdateSidebarPillView | null;
setDismissedKeys: Dispatch<SetStateAction<ReadonlySet<string>>>;
}) {
const { view, setDismissedKeys } = input;
const [renderedView, setRenderedView] = useState<ProviderUpdateSidebarPillView | null>(view);
const [exitingKey, setExitingKey] = useState<string | null>(null);
const [dismissAfterExitKey, setDismissAfterExitKey] = useState<string | null>(null);
const [visibleAfterIso, setVisibleAfterIso] = useState<string | undefined>();
const effectiveVisibleAfterIso = visibleAfterIso ?? latestProviderCheckedAt(providers);
const view = getProviderUpdateSidebarPillView(providers, {
...(effectiveVisibleAfterIso !== undefined
? { visibleAfterIso: effectiveVisibleAfterIso }
: {}),
dismissedKeys,
});
useEffect(() => {
if (visibleAfterIso === undefined && effectiveVisibleAfterIso !== undefined) {
setVisibleAfterIso(effectiveVisibleAfterIso);
}
}, [effectiveVisibleAfterIso, visibleAfterIso]);
const openProviderSettings = useCallback(() => {
void navigate({ to: "/settings/providers" });
}, [navigate]);
const displayedView = renderedView ?? view;
const dismissAfterVisibleMs = displayedView?.dismissAfterVisibleMs;
const viewKey = displayedView?.key ?? null;
const showDismissProgress =
dismissAfterVisibleMs !== undefined &&
displayedView?.tone !== "loading" &&
exitingKey !== viewKey;
const pendingViewRef = useRef<ProviderUpdateSidebarPillView | null>(null);
const dismissAfterExitKeyRef = useRef<string | null>(null);
const startExit = useCallback(
(key: string, nextView: ProviderUpdateSidebarPillView | null, dismissKey?: string) => {
if (exitingKey === key) {
return;
}
setPendingView(nextView);
pendingViewRef.current = nextView;
dismissAfterExitKeyRef.current = dismissKey ?? null;
setExitingKey(key);
setDismissAfterExitKey(dismissKey ?? null);
},
[exitingKey],
);
@@ -103,6 +100,46 @@ export function SidebarProviderUpdatePill() {
}
}, [exitingKey, renderedView, startExit, view]);
const displayedView = renderedView ?? view;
const onTransitionEnd = useCallback(
(event: TransitionEvent<HTMLDivElement>) => {
if (event.target !== event.currentTarget) {
return;
}
if (!displayedView || exitingKey !== displayedView.key) {
return;
}
if (dismissAfterExitKeyRef.current === displayedView.key) {
setDismissedKeys((previous) => new Set(previous).add(displayedView.key));
}
setRenderedView(pendingViewRef.current);
pendingViewRef.current = null;
dismissAfterExitKeyRef.current = null;
setExitingKey(null);
},
[displayedView, exitingKey, setDismissedKeys],
);
return {
displayedView,
exitingKey,
onTransitionEnd,
startExit,
};
}
function useProviderUpdateAutoDismiss(input: {
dismissAfterVisibleMs: number | undefined;
exitingKey: string | null;
startExit: (
key: string,
nextView: ProviderUpdateSidebarPillView | null,
dismissKey?: string,
) => void;
viewKey: string | null;
}) {
const { dismissAfterVisibleMs, exitingKey, startExit, viewKey } = input;
useEffect(() => {
if (!dismissAfterVisibleMs || !viewKey) {
return;
@@ -116,6 +153,40 @@ export function SidebarProviderUpdatePill() {
return () => window.clearTimeout(timeoutId);
}, [dismissAfterVisibleMs, exitingKey, startExit, viewKey]);
}
export function SidebarProviderUpdatePill() {
const navigate = useNavigate();
const providers = useServerProviders();
const [dismissedKeys, setDismissedKeys] = useState<ReadonlySet<string>>(() => new Set());
const effectiveVisibleAfterIso = useProviderUpdateVisibleAfterIso(providers);
const view = getProviderUpdateSidebarPillView(providers, {
...(effectiveVisibleAfterIso !== undefined
? { visibleAfterIso: effectiveVisibleAfterIso }
: {}),
dismissedKeys,
});
const openProviderSettings = useCallback(() => {
void navigate({ to: "/settings/providers" });
}, [navigate]);
const { displayedView, exitingKey, onTransitionEnd, startExit } = useDisplayedProviderUpdatePill({
view,
setDismissedKeys,
});
const dismissAfterVisibleMs = displayedView?.dismissAfterVisibleMs;
const viewKey = displayedView?.key ?? null;
const showDismissProgress =
dismissAfterVisibleMs !== undefined &&
displayedView?.tone !== "loading" &&
exitingKey !== viewKey;
useProviderUpdateAutoDismiss({
dismissAfterVisibleMs,
exitingKey,
startExit,
viewKey,
});
if (!displayedView) {
return null;
@@ -130,21 +201,7 @@ export function SidebarProviderUpdatePill() {
? "pointer-events-none translate-y-1.5 opacity-0"
: "translate-y-0 opacity-100"
}`}
onTransitionEnd={(event) => {
if (event.target !== event.currentTarget) {
return;
}
if (!displayedView || exitingKey !== displayedView.key) {
return;
}
if (dismissAfterExitKey === displayedView.key) {
setDismissedKeys((previous) => new Set(previous).add(displayedView.key));
}
setRenderedView(pendingView);
setPendingView(null);
setExitingKey(null);
setDismissAfterExitKey(null);
}}
onTransitionEnd={onTransitionEnd}
>
{showDismissProgress ? (
<div