Skip to content

Commit aa73757

Browse files
committed
fix(mothership): hand a withdrawn send's pick back only while its new-chat surface is open
1 parent 544a855 commit aa73757

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/home/hooks

‎apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@ export function useChat(
926926
new Set())
927927
const streamReaderRef = useRef<ReadableStreamDefaultReader<Uint8Array> | null>(null)
928928
const chatIdRef = useRef<string | undefined>(initialChatId)
929+
/** Cleared on unmount, so a late rollback cannot hand a pick to a surface the user left. */
930+
const surfaceMountedRef = useRef(true)
931+
useEffect(() => {
932+
surfaceMountedRef.current = true
933+
return () => {
934+
surfaceMountedRef.current = false
935+
}
936+
}, [])
929937
const tableViewContextsRef = useRef({
930938
scopeId: desktopScopeId,
931939
views: new Map<string, MothershipTableViewContext>(),
@@ -3542,8 +3550,15 @@ export function useChat(
35423550
}
35433551

35443552
const rollbackOptimisticSend = () => {
3545-
// A withdrawn first send hands its pick back to the new-chat composer for the retry.
3546-
if (!requestChatId && effortChoice)
3553+
// A withdrawn first send hands its pick back to the new-chat composer for the retry,
3554+
// only while that surface is still open on the new chat.
3555+
if (
3556+
!requestChatId &&
3557+
effortChoice &&
3558+
surfaceMountedRef.current &&
3559+
!chatIdRef.current &&
3560+
!selectedChatIdRef.current
3561+
)
35473562
useMothershipEffortStore.getState().setNewChatEffort(effortChoice)
35483563
if (requestChatId) {
35493564
upsertChatHistory(requestChatId, (current) => ({

0 commit comments

Comments
 (0)