[codex] debounce thread jump hint pills (#1526)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
6358444900
commit
32dca3b76b
3 files changed
+195
-21
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
createThreadJumpHintVisibilityController,
|
||||
getVisibleSidebarThreadIds,
|
||||
resolveAdjacentThreadId,
|
||||
getFallbackThreadIdAfterDelete,
|
||||
@@ -15,6 +16,7 @@ import {
|
||||
shouldClearThreadSelectionOnMouseDown,
|
||||
sortProjectsForSidebar,
|
||||
sortThreadsForSidebar,
|
||||
THREAD_JUMP_HINT_SHOW_DELAY_MS,
|
||||
} from "./Sidebar.logic";
|
||||
import { ProjectId, ThreadId } from "@t3tools/contracts";
|
||||
import {
|
||||
@@ -52,6 +54,68 @@ describe("hasUnseenCompletion", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createThreadJumpHintVisibilityController", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("delays showing jump hints until the configured delay elapses", () => {
|
||||
const visibilityChanges: boolean[] = [];
|
||||
const controller = createThreadJumpHintVisibilityController({
|
||||
delayMs: THREAD_JUMP_HINT_SHOW_DELAY_MS,
|
||||
onVisibilityChange: (visible) => {
|
||||
visibilityChanges.push(visible);
|
||||
},
|
||||
});
|
||||
|
||||
controller.sync(true);
|
||||
vi.advanceTimersByTime(THREAD_JUMP_HINT_SHOW_DELAY_MS - 1);
|
||||
|
||||
expect(visibilityChanges).toEqual([]);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
|
||||
expect(visibilityChanges).toEqual([true]);
|
||||
});
|
||||
|
||||
it("hides immediately when the modifiers are released", () => {
|
||||
const visibilityChanges: boolean[] = [];
|
||||
const controller = createThreadJumpHintVisibilityController({
|
||||
delayMs: THREAD_JUMP_HINT_SHOW_DELAY_MS,
|
||||
onVisibilityChange: (visible) => {
|
||||
visibilityChanges.push(visible);
|
||||
},
|
||||
});
|
||||
|
||||
controller.sync(true);
|
||||
vi.advanceTimersByTime(THREAD_JUMP_HINT_SHOW_DELAY_MS);
|
||||
controller.sync(false);
|
||||
|
||||
expect(visibilityChanges).toEqual([true, false]);
|
||||
});
|
||||
|
||||
it("cancels a pending reveal when the modifier is released early", () => {
|
||||
const visibilityChanges: boolean[] = [];
|
||||
const controller = createThreadJumpHintVisibilityController({
|
||||
delayMs: THREAD_JUMP_HINT_SHOW_DELAY_MS,
|
||||
onVisibilityChange: (visible) => {
|
||||
visibilityChanges.push(visible);
|
||||
},
|
||||
});
|
||||
|
||||
controller.sync(true);
|
||||
vi.advanceTimersByTime(Math.floor(THREAD_JUMP_HINT_SHOW_DELAY_MS / 2));
|
||||
controller.sync(false);
|
||||
vi.advanceTimersByTime(THREAD_JUMP_HINT_SHOW_DELAY_MS);
|
||||
|
||||
expect(visibilityChanges).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shouldClearThreadSelectionOnMouseDown", () => {
|
||||
it("preserves selection for thread items", () => {
|
||||
const child = {
|
||||
@@ -171,6 +235,27 @@ describe("getVisibleSidebarThreadIds", () => {
|
||||
ThreadId.makeUnsafe("thread-6"),
|
||||
]);
|
||||
});
|
||||
|
||||
it("skips threads from collapsed projects whose thread panels are not shown", () => {
|
||||
expect(
|
||||
getVisibleSidebarThreadIds([
|
||||
{
|
||||
shouldShowThreadPanel: false,
|
||||
renderedThreads: [
|
||||
{ id: ThreadId.makeUnsafe("thread-hidden-2") },
|
||||
{ id: ThreadId.makeUnsafe("thread-hidden-1") },
|
||||
],
|
||||
},
|
||||
{
|
||||
shouldShowThreadPanel: true,
|
||||
renderedThreads: [
|
||||
{ id: ThreadId.makeUnsafe("thread-12") },
|
||||
{ id: ThreadId.makeUnsafe("thread-11") },
|
||||
],
|
||||
},
|
||||
]),
|
||||
).toEqual([ThreadId.makeUnsafe("thread-12"), ThreadId.makeUnsafe("thread-11")]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isContextMenuPointerDown", () => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as React from "react";
|
||||
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
|
||||
import type { Thread } from "../types";
|
||||
import { cn } from "../lib/utils";
|
||||
@@ -8,6 +9,7 @@ import {
|
||||
} from "../session-logic";
|
||||
|
||||
export const THREAD_SELECTION_SAFE_SELECTOR = "[data-thread-item], [data-thread-selection-safe]";
|
||||
export const THREAD_JUMP_HINT_SHOW_DELAY_MS = 100;
|
||||
export type SidebarNewThreadEnvMode = "local" | "worktree";
|
||||
type SidebarProject = {
|
||||
id: string;
|
||||
@@ -46,6 +48,91 @@ type ThreadStatusInput = Pick<
|
||||
"interactionMode" | "latestTurn" | "lastVisitedAt" | "proposedPlans" | "session"
|
||||
>;
|
||||
|
||||
export interface ThreadJumpHintVisibilityController {
|
||||
sync: (shouldShow: boolean) => void;
|
||||
dispose: () => void;
|
||||
}
|
||||
|
||||
export function createThreadJumpHintVisibilityController(input: {
|
||||
delayMs: number;
|
||||
onVisibilityChange: (visible: boolean) => void;
|
||||
setTimeoutFn?: typeof globalThis.setTimeout;
|
||||
clearTimeoutFn?: typeof globalThis.clearTimeout;
|
||||
}): ThreadJumpHintVisibilityController {
|
||||
const setTimeoutFn = input.setTimeoutFn ?? globalThis.setTimeout;
|
||||
const clearTimeoutFn = input.clearTimeoutFn ?? globalThis.clearTimeout;
|
||||
let isVisible = false;
|
||||
let timeoutId: NodeJS.Timeout | null = null;
|
||||
|
||||
const clearPendingShow = () => {
|
||||
if (timeoutId === null) {
|
||||
return;
|
||||
}
|
||||
clearTimeoutFn(timeoutId);
|
||||
timeoutId = null;
|
||||
};
|
||||
|
||||
return {
|
||||
sync: (shouldShow) => {
|
||||
if (!shouldShow) {
|
||||
clearPendingShow();
|
||||
if (isVisible) {
|
||||
isVisible = false;
|
||||
input.onVisibilityChange(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (isVisible || timeoutId !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
timeoutId = setTimeoutFn(() => {
|
||||
timeoutId = null;
|
||||
isVisible = true;
|
||||
input.onVisibilityChange(true);
|
||||
}, input.delayMs);
|
||||
},
|
||||
dispose: () => {
|
||||
clearPendingShow();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function useThreadJumpHintVisibility(): {
|
||||
showThreadJumpHints: boolean;
|
||||
updateThreadJumpHintsVisibility: (shouldShow: boolean) => void;
|
||||
} {
|
||||
const [showThreadJumpHints, setShowThreadJumpHints] = React.useState(false);
|
||||
const controllerRef = React.useRef<ThreadJumpHintVisibilityController | null>(null);
|
||||
|
||||
React.useEffect(() => {
|
||||
const controller = createThreadJumpHintVisibilityController({
|
||||
delayMs: THREAD_JUMP_HINT_SHOW_DELAY_MS,
|
||||
onVisibilityChange: (visible) => {
|
||||
setShowThreadJumpHints(visible);
|
||||
},
|
||||
setTimeoutFn: window.setTimeout.bind(window),
|
||||
clearTimeoutFn: window.clearTimeout.bind(window),
|
||||
});
|
||||
controllerRef.current = controller;
|
||||
|
||||
return () => {
|
||||
controller.dispose();
|
||||
controllerRef.current = null;
|
||||
};
|
||||
}, []);
|
||||
|
||||
const updateThreadJumpHintsVisibility = React.useCallback((shouldShow: boolean) => {
|
||||
controllerRef.current?.sync(shouldShow);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
showThreadJumpHints,
|
||||
updateThreadJumpHintsVisibility,
|
||||
};
|
||||
}
|
||||
|
||||
export function hasUnseenCompletion(thread: ThreadStatusInput): boolean {
|
||||
if (!thread.latestTurn?.completedAt) return false;
|
||||
const completedAt = Date.parse(thread.latestTurn.completedAt);
|
||||
@@ -71,13 +158,16 @@ export function resolveSidebarNewThreadEnvMode(input: {
|
||||
|
||||
export function getVisibleSidebarThreadIds<TThreadId>(
|
||||
renderedProjects: readonly {
|
||||
shouldShowThreadPanel?: boolean;
|
||||
renderedThreads: readonly {
|
||||
id: TThreadId;
|
||||
}[];
|
||||
}[],
|
||||
): TThreadId[] {
|
||||
return renderedProjects.flatMap((renderedProject) =>
|
||||
renderedProject.renderedThreads.map((thread) => thread.id),
|
||||
renderedProject.shouldShowThreadPanel === false
|
||||
? []
|
||||
: renderedProject.renderedThreads.map((thread) => thread.id),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ import { serverConfigQueryOptions } from "../lib/serverReactQuery";
|
||||
import { readNativeApi } from "../nativeApi";
|
||||
import { useComposerDraftStore } from "../composerDraftStore";
|
||||
import { useHandleNewThread } from "../hooks/useHandleNewThread";
|
||||
|
||||
import { useThreadActions } from "../hooks/useThreadActions";
|
||||
import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
|
||||
import { toastManager } from "./ui/toast";
|
||||
@@ -116,6 +117,7 @@ import {
|
||||
shouldClearThreadSelectionOnMouseDown,
|
||||
sortProjectsForSidebar,
|
||||
sortThreadsForSidebar,
|
||||
useThreadJumpHintVisibility,
|
||||
} from "./Sidebar.logic";
|
||||
import { SidebarUpdatePill } from "./sidebar/SidebarUpdatePill";
|
||||
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
|
||||
@@ -399,7 +401,7 @@ export default function Sidebar() {
|
||||
const [expandedThreadListsByProject, setExpandedThreadListsByProject] = useState<
|
||||
ReadonlySet<ProjectId>
|
||||
>(() => new Set());
|
||||
const [showThreadJumpHints, setShowThreadJumpHints] = useState(false);
|
||||
const { showThreadJumpHints, updateThreadJumpHintsVisibility } = useThreadJumpHintVisibility();
|
||||
const renamingCommittedRef = useRef(false);
|
||||
const renamingInputRef = useRef<HTMLInputElement | null>(null);
|
||||
const dragInProgressRef = useRef(false);
|
||||
@@ -1122,23 +1124,22 @@ export default function Sidebar() {
|
||||
visibleThreads,
|
||||
],
|
||||
);
|
||||
const visibleSidebarThreadIds = useMemo(
|
||||
() => getVisibleSidebarThreadIds(renderedProjects),
|
||||
[renderedProjects],
|
||||
);
|
||||
const threadJumpCommandById = useMemo(() => {
|
||||
const mapping = new Map<ThreadId, NonNullable<ReturnType<typeof threadJumpCommandForIndex>>>();
|
||||
let visibleThreadIndex = 0;
|
||||
|
||||
for (const renderedProject of renderedProjects) {
|
||||
for (const thread of renderedProject.renderedThreads) {
|
||||
const jumpCommand = threadJumpCommandForIndex(visibleThreadIndex);
|
||||
if (!jumpCommand) {
|
||||
return mapping;
|
||||
}
|
||||
mapping.set(thread.id, jumpCommand);
|
||||
visibleThreadIndex += 1;
|
||||
for (const [visibleThreadIndex, threadId] of visibleSidebarThreadIds.entries()) {
|
||||
const jumpCommand = threadJumpCommandForIndex(visibleThreadIndex);
|
||||
if (!jumpCommand) {
|
||||
return mapping;
|
||||
}
|
||||
mapping.set(threadId, jumpCommand);
|
||||
}
|
||||
|
||||
return mapping;
|
||||
}, [renderedProjects]);
|
||||
}, [visibleSidebarThreadIds]);
|
||||
const threadJumpThreadIds = useMemo(
|
||||
() => [...threadJumpCommandById.keys()],
|
||||
[threadJumpCommandById],
|
||||
@@ -1153,10 +1154,7 @@ export default function Sidebar() {
|
||||
}
|
||||
return mapping;
|
||||
}, [keybindings, sidebarShortcutLabelOptions, threadJumpCommandById]);
|
||||
const orderedSidebarThreadIds = useMemo(
|
||||
() => getVisibleSidebarThreadIds(renderedProjects),
|
||||
[renderedProjects],
|
||||
);
|
||||
const orderedSidebarThreadIds = visibleSidebarThreadIds;
|
||||
|
||||
useEffect(() => {
|
||||
const getShortcutContext = () => ({
|
||||
@@ -1165,7 +1163,7 @@ export default function Sidebar() {
|
||||
});
|
||||
|
||||
const onWindowKeyDown = (event: KeyboardEvent) => {
|
||||
setShowThreadJumpHints(
|
||||
updateThreadJumpHintsVisibility(
|
||||
shouldShowThreadJumpHints(event, keybindings, {
|
||||
platform,
|
||||
context: getShortcutContext(),
|
||||
@@ -1213,7 +1211,7 @@ export default function Sidebar() {
|
||||
};
|
||||
|
||||
const onWindowKeyUp = (event: KeyboardEvent) => {
|
||||
setShowThreadJumpHints(
|
||||
updateThreadJumpHintsVisibility(
|
||||
shouldShowThreadJumpHints(event, keybindings, {
|
||||
platform,
|
||||
context: getShortcutContext(),
|
||||
@@ -1222,7 +1220,7 @@ export default function Sidebar() {
|
||||
};
|
||||
|
||||
const onWindowBlur = () => {
|
||||
setShowThreadJumpHints(false);
|
||||
updateThreadJumpHintsVisibility(false);
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", onWindowKeyDown);
|
||||
@@ -1242,6 +1240,7 @@ export default function Sidebar() {
|
||||
routeTerminalOpen,
|
||||
routeThreadId,
|
||||
threadJumpThreadIds,
|
||||
updateThreadJumpHintsVisibility,
|
||||
]);
|
||||
|
||||
function renderProjectItem(
|
||||
|
||||
Reference in new issue
Block a user