@@ -161,9 +161,9 @@ export interface ConversationFocus {
161161 * beside `ensureConversation`, and only while it has a user to create it
162162 * for): the id that create mints takes the focus the conversation had at
163163 * this moment. Only that id: one the host resolves or is handed instead (a
164- * found thread the send joins, a history row) restores its own. A create
165- * that fails spends the mark (see the hook's `createError`), and so does
166- * another target while unsent .
164+ * found thread the send joins, a history row), even one an earlier send of
165+ * this host created, restores its own. A create that fails spends the mark
166+ * (see the hook's `createError`) .
167167 */
168168 markSending : ( ) => void ;
169169}
@@ -177,47 +177,45 @@ interface FocusState {
177177 label ?: string ;
178178 /** What the host was asked to show when the state last moved (see `origin`). */
179179 origin : string | undefined ;
180- /**
181- * How many times another target started another unsent conversation. A
182- * first-send mark is good only for the unsent conversation it was made in.
183- */
184- epoch : number ;
185180}
186181
187- /** A first send's mark: the entry it fixes, for the unsent conversation it was made in. */
182+ /**
183+ * A first send's mark: the entry it fixes, and the id the host's create had
184+ * minted last when the send was made. Only an id minted after it, by this
185+ * send's own create, takes the entry.
186+ */
188187interface SendingMark {
189188 entry : ChatFocusEntry ;
190- epoch : number ;
189+ createdBefore : string | undefined ;
191190}
192191
193192/** The entry a conversation the host arrives at restores: this tab's copy, else its metadata's. */
194193function restoredState (
195194 key : string | undefined ,
196195 origin : string | undefined ,
197196 fromMetadata : ChatFocusFromMetadata | null ,
198- epoch : number ,
199197) : FocusState {
200198 const stored = readStoredEntry ( key ) ;
201- if ( stored ) return { key, entry : stored , origin, epoch } ;
199+ if ( stored ) return { key, entry : stored , origin } ;
202200 if ( key !== undefined && fromMetadata ) {
203201 return {
204202 key,
205203 entry : { focus : { object : fromMetadata . object , id : fromMetadata . id } , dismissed : false } ,
206204 label : fromMetadata . label ,
207205 origin,
208- epoch,
209206 } ;
210207 }
211- return { key, entry : EMPTY_ENTRY , origin, epoch } ;
208+ return { key, entry : EMPTY_ENTRY , origin } ;
212209}
213210
214211/**
215212 * The state the host's props move `state` to. `undefined` → the id the first
216- * send's own create minted (`createdId`) takes the mark `sending` holds, when
217- * that mark was made in this unsent conversation. Every other arrival of an
218- * id restores that conversation's own entry: a thread the resolve found, which
219- * a send made during the resolve joins, and a history row opened while the
220- * create was on the wire, keep the focus they were fixed on.
213+ * send's own create minted takes the mark `sending` holds: `createdId`, the
214+ * host's latest create, when it is newer than the one the mark saw at the
215+ * send. Every other arrival of an id restores that conversation's own entry:
216+ * a thread the resolve found, which a send made during the resolve joins, and
217+ * a history row opened while the create was on the wire, keep the focus they
218+ * were fixed on, a thread this host created at an earlier send included.
221219 */
222220function nextFocusState (
223221 state : FocusState ,
@@ -228,21 +226,15 @@ function nextFocusState(
228226 sending : { readonly current : SendingMark | null } ,
229227) : FocusState {
230228 if ( state . key !== conversationId ) {
231- const mark =
232- state . key === undefined && conversationId !== undefined && conversationId === createdId
233- ? sending . current
234- : null ;
235- return mark && mark . epoch === state . epoch
236- ? { key : conversationId , entry : mark . entry , origin, epoch : state . epoch }
237- : restoredState ( conversationId , origin , fromMetadata , state . epoch ) ;
229+ const mark = state . key === undefined && conversationId !== undefined ? sending . current : null ;
230+ return mark && conversationId === createdId && createdId !== mark . createdBefore
231+ ? { key : conversationId , entry : mark . entry , origin }
232+ : restoredState ( conversationId , origin , fromMetadata ) ;
238233 }
239234 if ( state . origin !== origin ) {
240235 // Another target while unsent is another conversation: × on the last one
241- // does not carry over, nor does a first send's mark. A sent conversation
242- // keeps its own entry.
243- return state . key === undefined
244- ? { key : undefined , entry : EMPTY_ENTRY , origin, epoch : state . epoch + 1 }
245- : { ...state , origin } ;
236+ // does not carry over. A sent conversation keeps its own entry.
237+ return state . key === undefined ? { key : undefined , entry : EMPTY_ENTRY , origin } : { ...state , origin } ;
246238 }
247239 if (
248240 state . key !== undefined &&
@@ -253,7 +245,7 @@ function nextFocusState(
253245 ) {
254246 // The conversation's row arrived after its id: its metadata restores the
255247 // chip this tab never held.
256- return restoredState ( state . key , origin , fromMetadata , state . epoch ) ;
248+ return restoredState ( state . key , origin , fromMetadata ) ;
257249 }
258250 return state ;
259251}
@@ -285,13 +277,14 @@ export function useConversationFocus(
285277 origin ?: string ,
286278 /**
287279 * The id of the row the host's own create minted last
288- * (`useChatConversation().createdConversationId`): the one id a first
289- * send's mark goes to.
280+ * (`useChatConversation().createdConversationId`). A first send's mark
281+ * goes only to an id it holds after that send, never to the one it held
282+ * when the send was made.
290283 */
291284 createdId ?: string ,
292285) : ConversationFocus {
293286 const fromMetadata = React . useMemo ( ( ) => chatFocusFromMetadata ( conversationMetadata ) , [ conversationMetadata ] ) ;
294- const [ state , setState ] = React . useState < FocusState > ( ( ) => restoredState ( conversationId , origin , fromMetadata , 0 ) ) ;
287+ const [ state , setState ] = React . useState < FocusState > ( ( ) => restoredState ( conversationId , origin , fromMetadata ) ) ;
295288 // The focus a first send in flight fixes: a ref, not state, so marking it
296289 // re-renders nothing while the pane holds the message it is creating the
297290 // row for.
@@ -300,7 +293,7 @@ export function useConversationFocus(
300293 // Derived state set during render: React re-renders before committing, so
301294 // no child of this commit sees the previous conversation's focus.
302295 if ( current !== state ) setState ( current ) ;
303- const { key, entry, label, epoch } = current ;
296+ const { key, entry, label } = current ;
304297
305298 React . useEffect ( ( ) => {
306299 if ( key !== undefined ) writeStoredChatFocus ( key , entry ) ;
@@ -330,17 +323,17 @@ export function useConversationFocus(
330323 // What the conversation has as of this commit, for `markSending`, which runs
331324 // in an event or a child's effect after it. An insertion effect runs before
332325 // every layout and passive effect of the commit, the children's included.
333- const effectiveRef = React . useRef < SendingMark > ( { entry : EMPTY_ENTRY , epoch : 0 } ) ;
326+ const effectiveRef = React . useRef < SendingMark > ( { entry : EMPTY_ENTRY , createdBefore : undefined } ) ;
334327 React . useInsertionEffect ( ( ) => {
335- effectiveRef . current = { entry : { focus : effective , dismissed : entry . dismissed } , epoch } ;
336- } , [ effective , entry . dismissed , epoch ] ) ;
328+ effectiveRef . current = { entry : { focus : effective , dismissed : entry . dismissed } , createdBefore : createdId } ;
329+ } , [ effective , entry . dismissed , createdId ] ) ;
337330
338331 const dismiss = React . useCallback ( ( ) => {
339332 setState ( ( s ) => ( s . entry . dismissed && ! s . entry . focus ? s : { ...s , entry : { focus : null , dismissed : true } , label : undefined } ) ) ;
340333 } , [ ] ) ;
341334 const markSending = React . useCallback ( ( ) => {
342- const { entry : now , epoch : madeIn } = effectiveRef . current ;
343- sendingRef . current = { entry : { ...now } , epoch : madeIn } ;
335+ const { entry : now , createdBefore } = effectiveRef . current ;
336+ sendingRef . current = { entry : { ...now } , createdBefore } ;
344337 } , [ ] ) ;
345338
346339 return React . useMemo (
0 commit comments