1- import { memo , useEffect , useRef , useState , type ReactNode , type KeyboardEvent } from "react" ;
1+ import { memo , useRef , useState , type ReactNode , type KeyboardEvent } from "react" ;
2+ import { flushSync } from "react-dom" ;
23import { ChevronLeftIcon , ChevronRightIcon , XIcon } from "lucide-react" ;
34import { Image as ImageGlyph , Text as TextGlyph } from "lucide" ;
45import { Button } from "../ui/button" ;
@@ -23,6 +24,7 @@ import { useAtomValue } from "@effect/atom-react";
2324import { Atom } from "effect/reactivity" ;
2425import { appAtomRegistry } from "~/rpc/atomRegistry" ;
2526import { expandedImageKey } from "./ExpandedImagePreview" ;
27+ import { registerThreadOverlay } from "./threadOverlayRetention" ;
2628
2729interface ExpandedImageDialogProps {
2830 preview : ExpandedImagePreview ;
@@ -76,6 +78,13 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
7678 const [ returnFocusTarget ] = useState ( ( ) =>
7779 document . activeElement instanceof HTMLElement ? document . activeElement : null ,
7880 ) ;
81+ // The host unmounts the dialog outright, so Base UI never runs finalFocus. Without
82+ // this, focus stays on the removed dialog, and the composer that owned the opener
83+ // reads that as a blur and rests.
84+ const close = ( ) => {
85+ flushSync ( onClose ) ;
86+ if ( returnFocusTarget ?. isConnected ) returnFocusTarget . focus ( { preventScroll : true } ) ;
87+ } ;
7988 const closeButtonRef = useRef < HTMLButtonElement > ( null ) ;
8089 const imageCount = preview . images . length ;
8190 const index =
@@ -92,7 +101,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
92101 ...source ,
93102 onOpenFile : ( ) => {
94103 openFile ( ) ;
95- onClose ( ) ;
104+ close ( ) ;
96105 } ,
97106 }
98107 : source ;
@@ -101,18 +110,6 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
101110 setImageOffset ( ( current ) => current + direction ) ;
102111 } ;
103112
104- // Restore opener focus to prevent composer collapse.
105- const openerRef = useRef < Element | null > ( null ) ;
106- useEffect ( ( ) => {
107- openerRef . current = document . activeElement ;
108- return ( ) => {
109- const opener = openerRef . current ;
110- if ( opener instanceof HTMLElement && opener . isConnected ) {
111- opener . focus ( { preventScroll : true } ) ;
112- }
113- } ;
114- } , [ ] ) ;
115-
116113 const onKeyDown = ( event : KeyboardEvent < HTMLDivElement > ) => {
117114 if ( event . defaultPrevented || isContextMenuOpen ( ) || event . target instanceof HTMLVideoElement )
118115 return ;
@@ -134,17 +131,6 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
134131 navigateImage ( 1 ) ;
135132 } ;
136133
137- useEffect ( ( ) => {
138- const onEscape = ( event : globalThis . KeyboardEvent ) => {
139- if ( event . key !== "Escape" || isContextMenuOpen ( ) ) return ;
140- event . preventDefault ( ) ;
141- event . stopPropagation ( ) ;
142- onClose ( ) ;
143- } ;
144- window . addEventListener ( "keydown" , onEscape , { capture : true } ) ;
145- return ( ) => window . removeEventListener ( "keydown" , onEscape , { capture : true } ) ;
146- } , [ onClose ] ) ;
147-
148134 if ( ! item ) return null ;
149135 const mediaLabel = item . type === "video" ? "video" : "image" ;
150136 const openOriginalLink =
@@ -163,8 +149,14 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
163149 return (
164150 < Dialog
165151 open
166- onOpenChange = { ( open ) => {
167- if ( ! open ) onClose ( ) ;
152+ onOpenChange = { ( open , eventDetails ) => {
153+ if ( open ) return ;
154+ // Escape belongs to an open context menu first.
155+ if ( eventDetails . reason === "escape-key" && isContextMenuOpen ( ) ) {
156+ eventDetails . cancel ( ) ;
157+ return ;
158+ }
159+ close ( ) ;
168160 } }
169161 >
170162 < DialogPopup
@@ -177,7 +169,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
177169 initialFocus = { closeButtonRef }
178170 finalFocus = { ( ) => returnFocusTarget }
179171 onClick = { ( event ) => {
180- if ( event . target === event . currentTarget ) onClose ( ) ;
172+ if ( event . target === event . currentTarget ) close ( ) ;
181173 } }
182174 >
183175 < DialogTitle className = "sr-only" > Expanded { mediaLabel } preview</ DialogTitle >
@@ -187,7 +179,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
187179 size = "icon-sm"
188180 variant = "media-close"
189181 className = "absolute right-0 top-0 z-20"
190- onClick = { onClose }
182+ onClick = { close }
191183 aria-label = { `Close ${ mediaLabel } preview` }
192184 >
193185 < XIcon />
@@ -286,35 +278,45 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
286278} ) ;
287279
288280// Keep preview state outside ChatView to isolate dialog renders.
289- const expandedImagePreviewAtom = Atom . make < {
281+ type ExpandedImagePreviewState = {
290282 readonly threadKey : string ;
291283 readonly preview : ExpandedImagePreview ;
292- } | null > ( null ) . pipe ( Atom . keepAlive , Atom . withLabel ( "chat:expanded-image-preview" ) ) ;
284+ } | null ;
285+
286+ const expandedImagePreviewAtom = Atom . make < ExpandedImagePreviewState > ( null ) . pipe (
287+ Atom . keepAlive ,
288+ Atom . withLabel ( "chat:expanded-image-preview" ) ,
289+ ) ;
290+
291+ function ownedSrc ( state : ExpandedImagePreviewState ) : string | null {
292+ const item = state ?. preview . images [ state . preview . index ] ;
293+ return item ?. ownsSrc === true ? item . src : null ;
294+ }
295+
296+ // The preview owns a blob URL made for it, so whatever replaces it releases that URL.
297+ function setExpandedImagePreview ( next : ExpandedImagePreviewState ) : void {
298+ const previous = ownedSrc ( appAtomRegistry . get ( expandedImagePreviewAtom ) ) ;
299+ appAtomRegistry . set ( expandedImagePreviewAtom , next ) ;
300+ if ( previous !== null && previous !== ownedSrc ( next ) ) URL . revokeObjectURL ( previous ) ;
301+ }
293302
294303export function openExpandedImagePreview ( threadKey : string , preview : ExpandedImagePreview ) : void {
295- appAtomRegistry . set ( expandedImagePreviewAtom , { threadKey, preview } ) ;
304+ setExpandedImagePreview ( { threadKey, preview } ) ;
296305}
297306
298307function closeExpandedImagePreview ( ) : void {
299- appAtomRegistry . set ( expandedImagePreviewAtom , null ) ;
308+ setExpandedImagePreview ( null ) ;
300309}
301310
302- function closeExpandedImagePreviewFor ( threadKey : string ) : void {
303- if ( appAtomRegistry . get ( expandedImagePreviewAtom ) ?. threadKey === threadKey ) {
304- closeExpandedImagePreview ( ) ;
305- }
306- }
311+ // Leaving the thread's route (or the chat view) drops its preview.
312+ registerThreadOverlay ( ( threadKey ) => {
313+ const current = appAtomRegistry . get ( expandedImagePreviewAtom ) ;
314+ if ( current !== null && current . threadKey !== threadKey ) closeExpandedImagePreview ( ) ;
315+ } ) ;
307316
308317export function ExpandedImageDialogHost ( { threadKey } : { readonly threadKey : string } ) {
309318 const state = useAtomValue ( expandedImagePreviewAtom ) ;
310319 const preview = state ?. threadKey === threadKey ? state . preview : null ;
311- const item = preview ?. images [ preview . index ] ;
312- const ownedSrc = item ?. ownsSrc === true ? item . src : null ;
313- useEffect ( ( ) => {
314- if ( ownedSrc === null ) return ;
315- return ( ) => URL . revokeObjectURL ( ownedSrc ) ;
316- } , [ ownedSrc ] ) ;
317- useEffect ( ( ) => ( ) => closeExpandedImagePreviewFor ( threadKey ) , [ threadKey ] ) ;
318320 if ( preview === null ) return null ;
319321 return (
320322 < ExpandedImageDialog
0 commit comments