Skip to content

Commit b2ee17d

Browse files
t3dotggclaude
andauthored
feat(web): thread actions from the chat header title (pingdotgg#5592)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 7963cc7 commit b2ee17d

8 files changed

Lines changed: 692 additions & 70 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5979,6 +5979,8 @@ function ChatViewContent(props: ChatViewProps) {
59795979
activeThreadId={activeThread.id}
59805980
{...(routeKind === "draft" && draftId ? { draftId } : {})}
59815981
activeThreadTitle={activeThread.title}
5982+
isServerThread={isServerThread}
5983+
changeRequestState={activeThreadPr?.state ?? null}
59825984
activeProjectName={activeProject?.title}
59835985
activeProjectCwd={activeProject?.workspaceRoot ?? null}
59845986
openInCwd={gitCwd}

‎apps/web/src/components/SidebarV2.tsx‎

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import {
108108
import { formatRelativeTimeLabel, parseTimestampDate } from "../timestampFormat";
109109
import type { SidebarThreadSummary } from "../types";
110110
import { cn } from "~/lib/utils";
111+
import { buildThreadActionMenuItems } from "./threadActionMenu.logic";
111112
import {
112113
buildBulkTitleRegenerationContextMenuItem,
113114
formatWorkingDurationLabel,
@@ -2539,62 +2540,21 @@ export default function SidebarV2() {
25392540
const snoozePresets = resolveSnoozePresets(new Date(), timestampFormat);
25402541
const clicked = await settlePromise(() =>
25412542
api.contextMenu.show(
2542-
[
2543-
...(thread.branch
2544-
? [
2545-
{
2546-
id: "new-thread-on-branch",
2547-
label: `New thread on ${thread.branch}`,
2548-
},
2549-
]
2550-
: []),
2551-
...(supportsPinning
2552-
? [
2553-
isPinned
2554-
? { id: "unpin", label: "Unpin thread" }
2555-
: { id: "pin", label: "Pin thread" },
2556-
]
2557-
: []),
2558-
// Both lifecycle actions stay available on pinned threads:
2559-
// settling clears the pin ("done" beats "keep on top"), and
2560-
// snoozing hides the card until wake with the pin intact.
2561-
...(supportsSettlement
2562-
? [
2563-
isSettled
2564-
? { id: "unsettle", label: "Un-settle thread" }
2565-
: { id: "settle", label: "Settle thread" },
2566-
]
2567-
: []),
2568-
...(supportsSnooze
2569-
? [
2570-
isSnoozed
2571-
? { id: "unsnooze", label: "Wake thread" }
2572-
: {
2573-
id: "snooze",
2574-
label: "Snooze",
2575-
disabled: !canSnooze(thread, { now: new Date().toISOString() }),
2576-
children: snoozePresets.map((preset) => ({
2577-
id: `snooze:${preset.id}`,
2578-
label: `${preset.label} (${preset.whenLabel})`,
2579-
})),
2580-
},
2581-
]
2582-
: []),
2583-
{ id: "rename", label: "Rename thread" },
2584-
...(supportsTitleRegeneration
2585-
? [
2586-
{
2587-
id: "regenerate-title",
2588-
label: isRegeneratingTitle ? "Regenerating…" : "Regenerate title",
2589-
disabled: isRegeneratingTitle,
2590-
},
2591-
]
2592-
: []),
2593-
{ id: "mark-unread", label: "Mark unread" },
2594-
{ id: "copy-path", label: "Copy path", icon: "copy" },
2595-
...(thread.branch ? [{ id: "copy-branch", label: "Copy branch", icon: "copy" }] : []),
2596-
{ id: "delete", label: "Delete", destructive: true, icon: "trash" },
2597-
],
2543+
buildThreadActionMenuItems({
2544+
branch: thread.branch ?? null,
2545+
isPinned,
2546+
isSettled,
2547+
isSnoozed,
2548+
canSnoozeNow: canSnooze(thread, { now: new Date().toISOString() }),
2549+
isRegeneratingTitle,
2550+
supports: {
2551+
settlement: supportsSettlement,
2552+
snooze: supportsSnooze,
2553+
pinning: supportsPinning,
2554+
titleRegeneration: supportsTitleRegeneration,
2555+
},
2556+
snoozePresets,
2557+
}),
25982558
position,
25992559
),
26002560
);

‎apps/web/src/components/chat/ChatHeader.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EnvironmentId } from "@t3tools/contracts";
22
import { describe, expect, it } from "vite-plus/test";
33

4-
import { shouldShowOpenInPicker } from "./ChatHeader";
4+
import { resolveRenameCommit, shouldShowOpenInPicker } from "./ChatHeader";
55

66
describe("shouldShowOpenInPicker", () => {
77
const primaryEnvironmentId = EnvironmentId.make("environment-primary");
@@ -46,3 +46,24 @@ describe("shouldShowOpenInPicker", () => {
4646
).toBe(false);
4747
});
4848
});
49+
50+
describe("resolveRenameCommit", () => {
51+
it("commits a trimmed changed title", () => {
52+
expect(resolveRenameCommit({ title: " New title ", originalTitle: "Old" })).toEqual({
53+
action: "commit",
54+
title: "New title",
55+
});
56+
});
57+
58+
it("rejects empty and whitespace-only titles", () => {
59+
expect(resolveRenameCommit({ title: " ", originalTitle: "Old" })).toEqual({
60+
action: "reject-empty",
61+
});
62+
});
63+
64+
it("no-ops when the trimmed title is unchanged", () => {
65+
expect(resolveRenameCommit({ title: " Old ", originalTitle: "Old" })).toEqual({
66+
action: "noop",
67+
});
68+
});
69+
});

‎apps/web/src/components/chat/ChatHeader.tsx‎

Lines changed: 173 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,35 @@ import {
66
type ThreadId,
77
} from "@t3tools/contracts";
88
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9-
import { memo } from "react";
9+
import {
10+
isAtomCommandInterrupted,
11+
squashAtomCommandFailure,
12+
} from "@t3tools/client-runtime/state/runtime";
13+
import type { ChangeRequestStateLike } from "@t3tools/client-runtime/state/thread-settled";
14+
import { ChevronDownIcon } from "lucide-react";
15+
import {
16+
memo,
17+
useCallback,
18+
useMemo,
19+
useRef,
20+
useState,
21+
type KeyboardEvent as ReactKeyboardEvent,
22+
type MouseEvent as ReactMouseEvent,
23+
} from "react";
1024
import GitActionsControl from "../GitActionsControl";
1125
import { type DraftId } from "~/composerDraftStore";
1226
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
27+
import { toastManager } from "../ui/toast";
1328
import ProjectScriptsControl, {
1429
type NewProjectScriptInput,
1530
type ProjectScriptActionResult,
1631
} from "../ProjectScriptsControl";
1732
import { OpenInPicker } from "./OpenInPicker";
1833
import { usePrimaryEnvironmentId } from "../../state/environments";
1934
import { useT3ProjectFileScripts } from "~/hooks/useT3ProjectFileScripts";
35+
import { useThreadActionMenu } from "~/hooks/useThreadActionMenu";
36+
import { threadEnvironment } from "../../state/threads";
37+
import { useAtomCommand } from "../../state/use-atom-command";
2038
import { ProjectFavicon } from "../ProjectFavicon";
2139
import { cn } from "~/lib/utils";
2240

@@ -25,6 +43,10 @@ interface ChatHeaderProps {
2543
activeThreadId: ThreadId;
2644
draftId?: DraftId;
2745
activeThreadTitle: string;
46+
/** Drafts have no server thread yet, so the title carries no action menu. */
47+
isServerThread: boolean;
48+
/** PR state feeding the settled classification, resolved by ChatView. */
49+
changeRequestState: ChangeRequestStateLike | null;
2850
activeProjectName: string | undefined;
2951
activeProjectCwd: string | null;
3052
openInCwd: string | null;
@@ -44,6 +66,20 @@ interface ChatHeaderProps {
4466
onDeleteProjectScript: (scriptId: string) => Promise<ProjectScriptActionResult>;
4567
}
4668

69+
/**
70+
* Rename commit rule shared with the sidebar's inline rename: trim, reject
71+
* empty (the caller toasts), and skip the mutation when nothing changed.
72+
*/
73+
export function resolveRenameCommit(input: {
74+
readonly title: string;
75+
readonly originalTitle: string;
76+
}): { action: "commit"; title: string } | { action: "reject-empty" } | { action: "noop" } {
77+
const trimmed = input.title.trim();
78+
if (trimmed.length === 0) return { action: "reject-empty" };
79+
if (trimmed === input.originalTitle) return { action: "noop" };
80+
return { action: "commit", title: trimmed };
81+
}
82+
4783
export function shouldShowOpenInPicker(input: {
4884
readonly activeProjectName: string | undefined;
4985
readonly activeThreadEnvironmentId: EnvironmentId;
@@ -61,6 +97,8 @@ export const ChatHeader = memo(function ChatHeader({
6197
activeThreadId,
6298
draftId,
6399
activeThreadTitle,
100+
isServerThread,
101+
changeRequestState,
64102
activeProjectName,
65103
activeProjectCwd,
66104
openInCwd,
@@ -86,8 +124,91 @@ export const ChatHeader = memo(function ChatHeader({
86124
activeThreadEnvironmentId,
87125
primaryEnvironmentId,
88126
});
127+
const activeThreadRef = useMemo(
128+
() => scopeThreadRef(activeThreadEnvironmentId, activeThreadId),
129+
[activeThreadEnvironmentId, activeThreadId],
130+
);
131+
const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
132+
reportFailure: false,
133+
});
134+
// Inline rename, keyed by thread: navigating away drops an in-progress
135+
// rename instead of committing stale text. Cleared on thread change (not
136+
// just hidden) so returning to the thread doesn't revive the old draft.
137+
const [renaming, setRenaming] = useState<{ threadId: ThreadId; title: string } | null>(null);
138+
if (renaming !== null && renaming.threadId !== activeThreadId) {
139+
setRenaming(null);
140+
}
141+
const renamingTitle = renaming?.threadId === activeThreadId ? renaming.title : null;
142+
const renameCommittedRef = useRef(false);
143+
const startRename = useCallback(() => {
144+
renameCommittedRef.current = false;
145+
setRenaming({ threadId: activeThreadId, title: activeThreadTitle });
146+
}, [activeThreadId, activeThreadTitle]);
147+
const commitRename = useCallback(
148+
(title: string) => {
149+
setRenaming(null);
150+
const resolution = resolveRenameCommit({ title, originalTitle: activeThreadTitle });
151+
if (resolution.action === "reject-empty") {
152+
toastManager.add({ type: "warning", title: "Thread title cannot be empty" });
153+
return;
154+
}
155+
if (resolution.action === "noop") return;
156+
void updateThreadMetadata({
157+
environmentId: activeThreadEnvironmentId,
158+
input: { threadId: activeThreadId, title: resolution.title },
159+
}).then((result) => {
160+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
161+
const error = squashAtomCommandFailure(result);
162+
toastManager.add({
163+
type: "error",
164+
title: "Failed to rename thread",
165+
description: error instanceof Error ? error.message : "An error occurred.",
166+
});
167+
}
168+
});
169+
},
170+
[activeThreadEnvironmentId, activeThreadId, activeThreadTitle, updateThreadMetadata],
171+
);
172+
const { openMenu } = useThreadActionMenu({
173+
threadRef: isServerThread ? activeThreadRef : null,
174+
projectCwd: activeProjectCwd,
175+
changeRequestState,
176+
onStartRename: startRename,
177+
});
178+
const titleButtonRef = useRef<HTMLButtonElement | null>(null);
179+
const openMenuFromTitle = useCallback(() => {
180+
const rect = titleButtonRef.current?.getBoundingClientRect();
181+
if (!rect) return;
182+
openMenu({ x: rect.left, y: rect.bottom + 4 });
183+
}, [openMenu]);
184+
const handleHeaderContextMenu = useCallback(
185+
(event: ReactMouseEvent) => {
186+
if (!isServerThread || renamingTitle !== null) return;
187+
// The right-side controls (git, scripts, open-in) keep their own
188+
// behavior; only the breadcrumb area opens the thread menu.
189+
if ((event.target as HTMLElement).closest("[data-chat-header-actions]")) return;
190+
event.preventDefault();
191+
openMenu({ x: event.clientX, y: event.clientY });
192+
},
193+
[isServerThread, openMenu, renamingTitle],
194+
);
195+
const handleRenameKeyDown = useCallback(
196+
(event: ReactKeyboardEvent<HTMLInputElement>) => {
197+
if (event.key === "Enter") {
198+
renameCommittedRef.current = true;
199+
commitRename(event.currentTarget.value);
200+
} else if (event.key === "Escape") {
201+
renameCommittedRef.current = true;
202+
setRenaming(null);
203+
}
204+
},
205+
[commitRename],
206+
);
89207
return (
90-
<div className="@container/header-actions flex min-w-0 flex-1 items-center gap-2 sm:gap-3">
208+
<div
209+
className="@container/header-actions flex min-w-0 flex-1 items-center gap-2 sm:gap-3"
210+
onContextMenu={handleHeaderContextMenu}
211+
>
91212
<div className="flex min-w-0 flex-1 items-center gap-2 overflow-hidden sm:gap-3">
92213
{/* The project always leads the header: knowing which project a
93214
thread lives in is priority zero, and the thread title alone
@@ -119,19 +240,58 @@ export const ChatHeader = memo(function ChatHeader({
119240
</span>
120241
</span>
121242
) : null}
122-
<Tooltip>
123-
<TooltipTrigger
124-
render={
125-
<h2
126-
aria-label={activeThreadTitle}
127-
className="min-w-0 flex-1 truncate text-sm font-medium text-foreground"
128-
>
243+
{renamingTitle !== null ? (
244+
<input
245+
autoFocus
246+
aria-label="Thread title"
247+
className="min-w-0 flex-1 rounded-sm bg-transparent text-sm font-medium text-foreground outline-none ring-1 ring-ring/50 focus:ring-ring"
248+
defaultValue={renamingTitle}
249+
onBlur={(event) => {
250+
if (renameCommittedRef.current) return;
251+
commitRename(event.currentTarget.value);
252+
}}
253+
onFocus={(event) => event.currentTarget.select()}
254+
onKeyDown={handleRenameKeyDown}
255+
/>
256+
) : isServerThread ? (
257+
<Tooltip>
258+
<TooltipTrigger
259+
render={
260+
<button
261+
ref={titleButtonRef}
262+
type="button"
263+
aria-label={`Thread actions for ${activeThreadTitle}`}
264+
aria-haspopup="menu"
265+
onClick={openMenuFromTitle}
266+
className="group/thread-title inline-flex min-w-0 flex-1 cursor-pointer items-center gap-1 rounded-sm text-left focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring"
267+
/>
268+
}
269+
>
270+
<h2 className="min-w-0 truncate text-sm font-medium text-foreground">
129271
{activeThreadTitle}
130272
</h2>
131-
}
132-
/>
133-
<TooltipPopup side="top">{activeThreadTitle}</TooltipPopup>
134-
</Tooltip>
273+
<ChevronDownIcon
274+
aria-hidden
275+
className="size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/thread-title:opacity-100 group-focus-visible/thread-title:opacity-100"
276+
/>
277+
</TooltipTrigger>
278+
<TooltipPopup side="top">{activeThreadTitle}</TooltipPopup>
279+
</Tooltip>
280+
) : (
281+
<Tooltip>
282+
<TooltipTrigger
283+
render={
284+
<h2
285+
aria-label={activeThreadTitle}
286+
className="min-w-0 flex-1 truncate text-sm font-medium text-foreground"
287+
>
288+
{activeThreadTitle}
289+
</h2>
290+
}
291+
/>
292+
<TooltipPopup side="top">{activeThreadTitle}</TooltipPopup>
293+
</Tooltip>
294+
)}
135295
</div>
136296
<div
137297
data-chat-header-actions

0 commit comments

Comments
 (0)