@@ -6,17 +6,35 @@ import {
66 type ThreadId ,
77} from "@t3tools/contracts" ;
88import { 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" ;
1024import GitActionsControl from "../GitActionsControl" ;
1125import { type DraftId } from "~/composerDraftStore" ;
1226import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
27+ import { toastManager } from "../ui/toast" ;
1328import ProjectScriptsControl , {
1429 type NewProjectScriptInput ,
1530 type ProjectScriptActionResult ,
1631} from "../ProjectScriptsControl" ;
1732import { OpenInPicker } from "./OpenInPicker" ;
1833import { usePrimaryEnvironmentId } from "../../state/environments" ;
1934import { useT3ProjectFileScripts } from "~/hooks/useT3ProjectFileScripts" ;
35+ import { useThreadActionMenu } from "~/hooks/useThreadActionMenu" ;
36+ import { threadEnvironment } from "../../state/threads" ;
37+ import { useAtomCommand } from "../../state/use-atom-command" ;
2038import { ProjectFavicon } from "../ProjectFavicon" ;
2139import { 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+
4783export 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