fix: guard sidebar dialog async state (#3566)

This commit is contained in:
Neil
2026-05-30 02:06:34 -07:00
committed by GitHub
parent 4c1e17bce5
commit bb6ef72348
3 changed files with 49 additions and 11 deletions
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import {
Dialog,
DialogContent,
@@ -24,6 +24,14 @@ export function ProjectGroupDeleteDialog({
}: ProjectGroupDeleteDialogProps): React.JSX.Element {
const [deleting, setDeleting] = useState(false)
const [wasOpen, setWasOpen] = useState(open)
const mountedRef = useRef(true)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
// Why: opening the dialog must clear a stale in-flight state before the
// destructive button renders; an Effect would leave one disabled frame.
@@ -41,11 +49,15 @@ export function ProjectGroupDeleteDialog({
setDeleting(true)
try {
await onConfirm()
setDeleting(false)
onOpenChange(false)
if (mountedRef.current) {
setDeleting(false)
onOpenChange(false)
}
} catch (error) {
console.error('Failed to delete project group:', error)
setDeleting(false)
if (mountedRef.current) {
setDeleting(false)
}
}
}, [deleting, onConfirm, onOpenChange])
@@ -1,4 +1,4 @@
import React, { useCallback, useId, useRef, useState } from 'react'
import React, { useCallback, useEffect, useId, useRef, useState } from 'react'
import {
Dialog,
DialogContent,
@@ -35,8 +35,16 @@ export function ProjectGroupNameDialog({
const [name, setName] = useState(initialName)
const [submitting, setSubmitting] = useState(false)
const [previousOpenState, setPreviousOpenState] = useState({ open, initialName })
const mountedRef = useRef(true)
const trimmedName = name.trim()
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
// Why: the input should mount already seeded and selectable for the active
// group; Effect-based hydration shows one frame with the prior draft.
if (open !== previousOpenState.open || initialName !== previousOpenState.initialName) {
@@ -56,10 +64,14 @@ export function ProjectGroupNameDialog({
setSubmitting(true)
try {
await onSubmit(trimmedName)
onOpenChange(false)
if (mountedRef.current) {
onOpenChange(false)
}
} catch (error) {
console.error('Failed to save project group name:', error)
setSubmitting(false)
if (mountedRef.current) {
setSubmitting(false)
}
}
},
[onOpenChange, onSubmit, submitting, trimmedName]
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState } from 'react'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import { LoaderCircle } from 'lucide-react'
import { toast } from 'sonner'
import { Input } from '@/components/ui/input'
@@ -41,10 +41,18 @@ export function WorktreeTitleInlineRename({
}: WorktreeTitleInlineRenameProps): React.JSX.Element {
const editingRef = useRef(false)
const savingRef = useRef(false)
const mountedRef = useRef(true)
const [editing, setEditing] = useState(false)
const [value, setValue] = useState(displayName)
const [saving, setSaving] = useState(false)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
const setEditingMode = useCallback(
(nextEditing: boolean) => {
if (editingRef.current === nextEditing) {
@@ -104,12 +112,18 @@ export function WorktreeTitleInlineRename({
setSaving(true)
try {
await onRename(commit.displayName)
setEditingMode(false)
if (mountedRef.current) {
setEditingMode(false)
}
} catch (err) {
toast.error(err instanceof Error ? err.message : 'Failed to rename workspace.')
if (mountedRef.current) {
toast.error(err instanceof Error ? err.message : 'Failed to rename workspace.')
}
} finally {
savingRef.current = false
setSaving(false)
if (mountedRef.current) {
setSaving(false)
}
}
}, [cancelRename, displayName, onRename, setEditingMode, value])