@@ -12,9 +12,7 @@ import {
1212import { ChevronDownIcon } from "lucide-react" ;
1313import {
1414 memo ,
15- useCallback ,
1615 useEffect ,
17- useMemo ,
1816 useRef ,
1917 useState ,
2018 type KeyboardEvent as ReactKeyboardEvent ,
@@ -26,7 +24,6 @@ import { toastManager } from "../ui/toast";
2624import { useThreadActionMenu } from "~/hooks/useThreadActionMenu" ;
2725import { readLocalApi } from "~/localApi" ;
2826import { threadEnvironment } from "../../state/threads" ;
29- import { useAtomCommand } from "../../state/use-atom-command" ;
3027import { useOrchestrationCommand } from "../../state/use-orchestration-command" ;
3128import { readEnvironmentScope , useEnvironmentScope } from "../../state/session" ;
3229import { ProjectFavicon } from "../ProjectFavicon" ;
@@ -85,10 +82,7 @@ export const ChatHeader = memo(function ChatHeader({
8582 const interfaceFont = useClientSettings ( ( settings ) => settings . fontFamilySans ) ;
8683 const breadcrumbContainerRef = useRef < HTMLDivElement > ( null ) ;
8784 const [ collapseParentTitle , setCollapseParentTitle ] = useState ( false ) ;
88- const activeThreadRef = useMemo (
89- ( ) => scopeThreadRef ( activeThreadEnvironmentId , activeThreadId ) ,
90- [ activeThreadEnvironmentId , activeThreadId ] ,
91- ) ;
85+ const activeThreadRef = scopeThreadRef ( activeThreadEnvironmentId , activeThreadId ) ;
9286 const canOperateThread = useEnvironmentScope (
9387 activeThreadEnvironmentId ,
9488 AuthOrchestrationOperateScope ,
@@ -147,9 +141,16 @@ export const ChatHeader = memo(function ChatHeader({
147141 document . fonts . removeEventListener ( "loadingdone" , measure ) ;
148142 stopObserving ( ) ;
149143 } ;
150- } , [ activeProjectName , activeThreadTitle , parentThreadLink , interfaceFont , isRenamingTitle ] ) ;
144+ } , [
145+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Title, font and rename changes resize the breadcrumb text without resizing the list, so it re-measures.
146+ activeProjectName ,
147+ activeThreadTitle ,
148+ parentThreadLink ,
149+ interfaceFont ,
150+ isRenamingTitle ,
151+ ] ) ;
151152 const renameCommittedRef = useRef ( false ) ;
152- const startRename = useCallback ( ( ) => {
153+ const startRename = ( ) => {
153154 if (
154155 ! isServerThread ||
155156 ! readEnvironmentScope ( activeThreadEnvironmentId , AuthOrchestrationOperateScope )
@@ -161,120 +162,108 @@ export const ChatHeader = memo(function ChatHeader({
161162 threadId : activeThreadId ,
162163 title : activeThreadTitle ,
163164 } ) ;
164- } , [ activeThreadEnvironmentId , activeThreadId , activeThreadTitle , isServerThread ] ) ;
165- const commitRename = useCallback (
166- ( title : string ) => {
167- setRenaming ( null ) ;
168- if ( ! readEnvironmentScope ( activeThreadEnvironmentId , AuthOrchestrationOperateScope ) ) return ;
169- const resolution = resolveRenameCommit ( { title, originalTitle : activeThreadTitle } ) ;
170- if ( resolution . action === "reject-empty" ) {
171- toastManager . add ( { type : "warning" , title : "Thread title cannot be empty" } ) ;
172- return ;
165+ } ;
166+ const commitRename = ( title : string ) => {
167+ setRenaming ( null ) ;
168+ if ( ! readEnvironmentScope ( activeThreadEnvironmentId , AuthOrchestrationOperateScope ) ) return ;
169+ const resolution = resolveRenameCommit ( { title, originalTitle : activeThreadTitle } ) ;
170+ if ( resolution . action === "reject-empty" ) {
171+ toastManager . add ( { type : "warning" , title : "Thread title cannot be empty" } ) ;
172+ return ;
173+ }
174+ if ( resolution . action === "noop" ) return ;
175+ void updateThreadMetadata ( {
176+ environmentId : activeThreadEnvironmentId ,
177+ input : { threadId : activeThreadId , title : resolution . title } ,
178+ } ) . then ( ( result ) => {
179+ if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
180+ const error = squashAtomCommandFailure ( result ) ;
181+ toastManager . add ( {
182+ type : "error" ,
183+ title : "Failed to rename thread" ,
184+ description : error instanceof Error ? error . message : "An error occurred." ,
185+ } ) ;
173186 }
174- if ( resolution . action === "noop" ) return ;
175- void updateThreadMetadata ( {
176- environmentId : activeThreadEnvironmentId ,
177- input : { threadId : activeThreadId , title : resolution . title } ,
178- } ) . then ( ( result ) => {
179- if ( result . _tag === "Failure" && ! isAtomCommandInterrupted ( result ) ) {
180- const error = squashAtomCommandFailure ( result ) ;
181- toastManager . add ( {
182- type : "error" ,
183- title : "Failed to rename thread" ,
184- description : error instanceof Error ? error . message : "An error occurred." ,
185- } ) ;
186- }
187- } ) ;
188- } ,
189- [ activeThreadEnvironmentId , activeThreadId , activeThreadTitle , updateThreadMetadata ] ,
190- ) ;
187+ } ) ;
188+ } ;
191189 const { openMenu, closeMenu } = useThreadActionMenu ( {
192190 threadRef : isServerThread ? activeThreadRef : null ,
193191 projectCwd : activeProjectCwd ,
194192 onStartRename : startRename ,
195193 } ) ;
196194 const titleButtonRef = useRef < HTMLButtonElement | null > ( null ) ;
197195 const titleMenuTimerRef = useRef < number | null > ( null ) ;
198- const cancelPendingTitleMenu = useCallback ( ( ) => {
196+ const cancelPendingTitleMenu = ( ) => {
199197 if ( titleMenuTimerRef . current === null ) return ;
200198 clearTimeout ( titleMenuTimerRef . current ) ;
201199 titleMenuTimerRef . current = null ;
202- } , [ ] ) ;
200+ } ;
203201 useEffect (
204202 ( ) => ( ) => {
205- cancelPendingTitleMenu ( ) ;
203+ if ( titleMenuTimerRef . current === null ) return ;
204+ clearTimeout ( titleMenuTimerRef . current ) ;
205+ titleMenuTimerRef . current = null ;
206206 } ,
207- [ activeThreadEnvironmentId , activeThreadId , cancelPendingTitleMenu ] ,
207+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A pending title menu belongs to the thread that was clicked, so switching threads drops it.
208+ [ activeThreadEnvironmentId , activeThreadId ] ,
208209 ) ;
209- const openTitleMenuNow = useCallback ( ( ) => {
210+ const openTitleMenuNow = ( ) => {
210211 cancelPendingTitleMenu ( ) ;
211212 const rect = titleButtonRef . current ?. getBoundingClientRect ( ) ;
212213 if ( ! rect ) return ;
213214 openMenu ( { x : rect . left , y : rect . bottom + 4 } ) ;
214- } , [ cancelPendingTitleMenu , openMenu ] ) ;
215- const openMenuFromTitle = useCallback (
216- ( event : ReactMouseEvent < HTMLButtonElement > ) => {
217- if ( isTrailingDoubleClick ( event . detail ) ) return ;
218- const clickedChevron =
219- ( event . target as HTMLElement ) . closest ( "[data-thread-title-chevron]" ) !== null ;
220- if ( event . detail === 0 || clickedChevron || window . desktopBridge === undefined ) {
221- openTitleMenuNow ( ) ;
222- return ;
223- }
224- cancelPendingTitleMenu ( ) ;
225- titleMenuTimerRef . current = window . setTimeout ( ( ) => {
226- titleMenuTimerRef . current = null ;
227- openTitleMenuNow ( ) ;
228- } , TITLE_MENU_OPEN_DELAY_MS ) ;
229- } ,
230- [ cancelPendingTitleMenu , openTitleMenuNow ] ,
231- ) ;
232- const handleTitleDoubleClick = useCallback (
233- ( event : ReactMouseEvent ) => {
234- if ( event . metaKey || event . ctrlKey || event . shiftKey || event . altKey ) return ;
235- if ( ( event . target as HTMLElement ) . closest ( "[data-thread-title-chevron]" ) !== null ) return ;
236- cancelPendingTitleMenu ( ) ;
237- closeMenu ( ) ;
238- startRename ( ) ;
239- } ,
240- [ cancelPendingTitleMenu , closeMenu , startRename ] ,
241- ) ;
242- const handleHeaderContextMenu = useCallback (
243- ( event : ReactMouseEvent ) => {
244- if ( renamingTitle !== null ) return ;
245- if ( ! isServerThread && onOpenProjectSettings === undefined ) return ;
246- cancelPendingTitleMenu ( ) ;
247- event . preventDefault ( ) ;
248- if ( ! isServerThread ) {
249- const api = readLocalApi ( ) ;
250- if ( ! api ) return ;
251- void api . contextMenu
252- . show ( [ { id : "project-settings" , label : "Project settings" , icon : "settings" } ] , {
253- x : event . clientX ,
254- y : event . clientY ,
255- } )
256- . then ( ( action ) => {
257- if ( action === "project-settings" ) onOpenProjectSettings ?.( ) ;
258- } ) ;
259- return ;
260- }
261- openMenu ( { x : event . clientX , y : event . clientY } ) ;
262- } ,
263- [ cancelPendingTitleMenu , isServerThread , onOpenProjectSettings , openMenu , renamingTitle ] ,
264- ) ;
265- const handleRenameKeyDown = useCallback (
266- ( event : ReactKeyboardEvent < HTMLInputElement > ) => {
267- if ( event . nativeEvent . isComposing || event . keyCode === 229 ) return ;
268- if ( event . key === "Enter" ) {
269- renameCommittedRef . current = true ;
270- commitRename ( event . currentTarget . value ) ;
271- } else if ( event . key === "Escape" ) {
272- renameCommittedRef . current = true ;
273- setRenaming ( null ) ;
274- }
275- } ,
276- [ commitRename ] ,
277- ) ;
215+ } ;
216+ const openMenuFromTitle = ( event : ReactMouseEvent < HTMLButtonElement > ) => {
217+ if ( isTrailingDoubleClick ( event . detail ) ) return ;
218+ const clickedChevron =
219+ ( event . target as HTMLElement ) . closest ( "[data-thread-title-chevron]" ) !== null ;
220+ if ( event . detail === 0 || clickedChevron || window . desktopBridge === undefined ) {
221+ openTitleMenuNow ( ) ;
222+ return ;
223+ }
224+ cancelPendingTitleMenu ( ) ;
225+ titleMenuTimerRef . current = window . setTimeout ( ( ) => {
226+ titleMenuTimerRef . current = null ;
227+ openTitleMenuNow ( ) ;
228+ } , TITLE_MENU_OPEN_DELAY_MS ) ;
229+ } ;
230+ const handleTitleDoubleClick = ( event : ReactMouseEvent ) => {
231+ if ( event . metaKey || event . ctrlKey || event . shiftKey || event . altKey ) return ;
232+ if ( ( event . target as HTMLElement ) . closest ( "[data-thread-title-chevron]" ) !== null ) return ;
233+ cancelPendingTitleMenu ( ) ;
234+ closeMenu ( ) ;
235+ startRename ( ) ;
236+ } ;
237+ const handleHeaderContextMenu = ( event : ReactMouseEvent ) => {
238+ if ( renamingTitle !== null ) return ;
239+ if ( ! isServerThread && onOpenProjectSettings === undefined ) return ;
240+ cancelPendingTitleMenu ( ) ;
241+ event . preventDefault ( ) ;
242+ if ( ! isServerThread ) {
243+ const api = readLocalApi ( ) ;
244+ if ( ! api ) return ;
245+ void api . contextMenu
246+ . show ( [ { id : "project-settings" , label : "Project settings" , icon : "settings" } ] , {
247+ x : event . clientX ,
248+ y : event . clientY ,
249+ } )
250+ . then ( ( action ) => {
251+ if ( action === "project-settings" ) onOpenProjectSettings ?.( ) ;
252+ } ) ;
253+ return ;
254+ }
255+ openMenu ( { x : event . clientX , y : event . clientY } ) ;
256+ } ;
257+ const handleRenameKeyDown = ( event : ReactKeyboardEvent < HTMLInputElement > ) => {
258+ if ( event . nativeEvent . isComposing || event . keyCode === 229 ) return ;
259+ if ( event . key === "Enter" ) {
260+ renameCommittedRef . current = true ;
261+ commitRename ( event . currentTarget . value ) ;
262+ } else if ( event . key === "Escape" ) {
263+ renameCommittedRef . current = true ;
264+ setRenaming ( null ) ;
265+ }
266+ } ;
278267 return (
279268 < div
280269 ref = { breadcrumbContainerRef }
0 commit comments