fix(ticket): ignore stale assign completions

This commit is contained in:
mlogclub
2026-05-02 20:36:34 +08:00
parent 1c65bec11b
commit 3eea8f29b7
@@ -1,6 +1,6 @@
"use client" "use client"
import { useEffect, useState } from "react" import { useEffect, useRef, useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod" import { zodResolver } from "@hookform/resolvers/zod"
import { Controller, Resolver, useForm } from "react-hook-form" import { Controller, Resolver, useForm } from "react-hook-form"
import { toast } from "sonner" import { toast } from "sonner"
@@ -84,6 +84,7 @@ function TicketAssignDialogBody({
const [saving, setSaving] = useState(false) const [saving, setSaving] = useState(false)
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [agents, setAgents] = useState<AdminAgentProfile[]>([]) const [agents, setAgents] = useState<AdminAgentProfile[]>([])
const activeRef = useRef(false)
const form = useForm< const form = useForm<
z.input<typeof schema>, z.input<typeof schema>,
@@ -102,6 +103,13 @@ function TicketAssignDialogBody({
formState: { errors }, formState: { errors },
} = form } = form
useEffect(() => {
activeRef.current = true
return () => {
activeRef.current = false
}
}, [])
useEffect(() => { useEffect(() => {
reset({ reset({
toUserId: currentAssigneeId ? String(currentAssigneeId) : "", toUserId: currentAssigneeId ? String(currentAssigneeId) : "",
@@ -113,12 +121,22 @@ function TicketAssignDialogBody({
setLoading(true) setLoading(true)
fetchAgentProfilesAll() fetchAgentProfilesAll()
.then((agentData) => { .then((agentData) => {
if (!activeRef.current) {
return
}
setAgents(Array.isArray(agentData) ? agentData : []) setAgents(Array.isArray(agentData) ? agentData : [])
}) })
.catch((error) => { .catch((error) => {
if (!activeRef.current) {
return
}
toast.error(error instanceof Error ? error.message : "加载处理人失败") toast.error(error instanceof Error ? error.message : "加载处理人失败")
}) })
.finally(() => setLoading(false)) .finally(() => {
if (activeRef.current) {
setLoading(false)
}
})
}, []) }, [])
async function onFormSubmit(values: FormValues) { async function onFormSubmit(values: FormValues) {
@@ -135,6 +153,9 @@ function TicketAssignDialogBody({
toUserId: Number(values.toUserId), toUserId: Number(values.toUserId),
reason: values.reason.trim() || undefined, reason: values.reason.trim() || undefined,
}) })
if (!activeRef.current) {
return
}
toast.success(`已批量指派 ${validTicketIds.length} 张工单`) toast.success(`已批量指派 ${validTicketIds.length} 张工单`)
} else { } else {
await assignTicket({ await assignTicket({
@@ -142,16 +163,27 @@ function TicketAssignDialogBody({
toUserId: Number(values.toUserId), toUserId: Number(values.toUserId),
reason: values.reason.trim() || undefined, reason: values.reason.trim() || undefined,
}) })
if (!activeRef.current) {
return
}
toast.success("处理人已更新") toast.success("处理人已更新")
} }
if (!activeRef.current) {
return
}
onOpenChange(false) onOpenChange(false)
await onSuccess?.() await onSuccess?.()
} catch (error) { } catch (error) {
if (!activeRef.current) {
return
}
toast.error(error instanceof Error ? error.message : "指派工单失败") toast.error(error instanceof Error ? error.message : "指派工单失败")
} finally { } finally {
if (activeRef.current) {
setSaving(false) setSaving(false)
} }
} }
}
return ( return (
<DialogContent className="max-w-lg gap-0 p-0 sm:max-w-lg"> <DialogContent className="max-w-lg gap-0 p-0 sm:max-w-lg">