调整目录
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { Resolver, useForm } from "react-hook-form"
|
||||
import { z } from "zod/v4"
|
||||
import { CircleXIcon } from "lucide-react"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { closeConversation } from "@/lib/api/admin"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog"
|
||||
import {
|
||||
Field,
|
||||
FieldContent,
|
||||
FieldError,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
|
||||
type ConversationCloseDialogProps = {
|
||||
open: boolean
|
||||
conversationId: number | null
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
const closeSchema = z.object({
|
||||
closeReason: z.string().trim().min(1, "请输入关闭原因"),
|
||||
})
|
||||
|
||||
type CloseForm = z.infer<typeof closeSchema>
|
||||
|
||||
const closeResolver = zodResolver(closeSchema as never) as Resolver<
|
||||
z.input<typeof closeSchema>,
|
||||
undefined,
|
||||
z.output<typeof closeSchema>
|
||||
>
|
||||
|
||||
const emptyForm: CloseForm = {
|
||||
closeReason: "",
|
||||
}
|
||||
|
||||
export function ConversationCloseDialog({
|
||||
open,
|
||||
conversationId,
|
||||
onOpenChange,
|
||||
onSuccess,
|
||||
}: ConversationCloseDialogProps) {
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
{open ? (
|
||||
<ConversationCloseDialogBody
|
||||
key={conversationId ? `close-${conversationId}` : "close"}
|
||||
conversationId={conversationId}
|
||||
onOpenChange={onOpenChange}
|
||||
onSuccess={onSuccess}
|
||||
/>
|
||||
) : null}
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
type ConversationCloseDialogBodyProps = {
|
||||
conversationId: number | null
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
function ConversationCloseDialogBody({
|
||||
conversationId,
|
||||
onOpenChange,
|
||||
onSuccess,
|
||||
}: ConversationCloseDialogBodyProps) {
|
||||
const [saving, setSaving] = useState(false)
|
||||
const form = useForm<
|
||||
z.input<typeof closeSchema>,
|
||||
undefined,
|
||||
z.output<typeof closeSchema>
|
||||
>({
|
||||
resolver: closeResolver,
|
||||
defaultValues: emptyForm,
|
||||
})
|
||||
const {
|
||||
handleSubmit,
|
||||
reset,
|
||||
register,
|
||||
formState: { errors },
|
||||
} = form
|
||||
|
||||
useEffect(() => {
|
||||
reset(emptyForm)
|
||||
}, [conversationId, reset])
|
||||
|
||||
async function onFormSubmit(values: CloseForm) {
|
||||
if (!conversationId) {
|
||||
toast.error("会话不存在")
|
||||
return
|
||||
}
|
||||
|
||||
setSaving(true)
|
||||
try {
|
||||
await closeConversation(conversationId, values.closeReason.trim())
|
||||
toast.success(`已关闭会话:#${conversationId}`)
|
||||
reset(emptyForm)
|
||||
onOpenChange(false)
|
||||
await onSuccess?.()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : "关闭会话失败")
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DialogContent className="max-w-lg gap-0 p-0 sm:max-w-lg">
|
||||
<DialogHeader className="px-6 pt-6">
|
||||
<DialogTitle>关闭会话</DialogTitle>
|
||||
{/* <DialogDescription>
|
||||
当前会话:{conversationId ? `#${conversationId}` : "-"}
|
||||
</DialogDescription> */}
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit(onFormSubmit)}>
|
||||
<div className="space-y-4 p-6">
|
||||
<Field data-invalid={!!errors.closeReason}>
|
||||
<FieldLabel htmlFor="conversation-close-reason">关闭原因</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="conversation-close-reason"
|
||||
rows={4}
|
||||
placeholder="填写关闭原因,关闭后会写入操作记录"
|
||||
aria-invalid={!!errors.closeReason}
|
||||
{...register("closeReason")}
|
||||
/>
|
||||
<FieldError errors={[errors.closeReason]} />
|
||||
</FieldContent>
|
||||
</Field>
|
||||
</div>
|
||||
<DialogFooter className="mx-0 mb-0 px-6 py-4">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={saving}
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button type="submit" disabled={saving}>
|
||||
<CircleXIcon />
|
||||
{saving ? "关闭中..." : "确认关闭"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
"use client"
|
||||
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { ArrowRightLeftIcon } from "lucide-react"
|
||||
import { useEffect, useState } from "react"
|
||||
import { Controller, Resolver, useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod/v4"
|
||||
|
||||
import { OptionCombobox } from "@/components/option-combobox"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle
|
||||
} from "@/components/ui/dialog"
|
||||
import {
|
||||
Field,
|
||||
FieldContent,
|
||||
FieldError,
|
||||
FieldLabel,
|
||||
} from "@/components/ui/field"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import {
|
||||
assignConversation,
|
||||
transferConversation,
|
||||
fetchAgentProfilesAll,
|
||||
type AdminAgentProfile,
|
||||
} from "@/lib/api/admin"
|
||||
|
||||
type ConversationTransferDialogProps = {
|
||||
open: boolean
|
||||
mode: "assign" | "transfer"
|
||||
conversationId: number | null
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
const transferSchema = z.object({
|
||||
toUserId: z.string().trim().min(1, "请选择目标客服"),
|
||||
reason: z.string().trim(),
|
||||
})
|
||||
|
||||
type TransferForm = z.infer<typeof transferSchema>
|
||||
|
||||
const emptyForm: TransferForm = {
|
||||
toUserId: "",
|
||||
reason: "",
|
||||
}
|
||||
|
||||
const transferResolver = zodResolver(transferSchema as never) as Resolver<
|
||||
z.input<typeof transferSchema>,
|
||||
undefined,
|
||||
z.output<typeof transferSchema>
|
||||
>
|
||||
|
||||
export function ConversationTransferDialog({
|
||||
open,
|
||||
mode,
|
||||
conversationId,
|
||||
onOpenChange,
|
||||
onSuccess,
|
||||
}: ConversationTransferDialogProps) {
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
{open ? (
|
||||
<ConversationTransferDialogBody
|
||||
key={conversationId ? `transfer-${conversationId}` : "transfer"}
|
||||
mode={mode}
|
||||
conversationId={conversationId}
|
||||
onOpenChange={onOpenChange}
|
||||
onSuccess={onSuccess}
|
||||
/>
|
||||
) : null}
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
type ConversationTransferDialogBodyProps = {
|
||||
mode: "assign" | "transfer"
|
||||
conversationId: number | null
|
||||
onOpenChange: (open: boolean) => void
|
||||
onSuccess?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
function ConversationTransferDialogBody({
|
||||
mode,
|
||||
conversationId,
|
||||
onOpenChange,
|
||||
onSuccess,
|
||||
}: ConversationTransferDialogBodyProps) {
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [loadingAgents, setLoadingAgents] = useState(false)
|
||||
const [agents, setAgents] = useState<AdminAgentProfile[]>([])
|
||||
const userOptions = agents.map((agent) => ({
|
||||
value: String(agent.userId),
|
||||
label: agent.displayName || agent.nickname || agent.username || `客服 #${agent.userId}`,
|
||||
}))
|
||||
|
||||
const form = useForm<
|
||||
z.input<typeof transferSchema>,
|
||||
undefined,
|
||||
z.output<typeof transferSchema>
|
||||
>({
|
||||
resolver: transferResolver,
|
||||
defaultValues: emptyForm,
|
||||
})
|
||||
const {
|
||||
control,
|
||||
handleSubmit,
|
||||
reset,
|
||||
register,
|
||||
formState: { errors },
|
||||
} = form
|
||||
|
||||
useEffect(() => {
|
||||
reset(emptyForm)
|
||||
}, [conversationId, reset])
|
||||
|
||||
useEffect(() => {
|
||||
setLoadingAgents(true)
|
||||
fetchAgentProfilesAll()
|
||||
.then((data) => {
|
||||
setAgents(data.filter((item) => item.serviceStatus === 0))
|
||||
})
|
||||
.catch((error) => {
|
||||
toast.error(error instanceof Error ? error.message : "加载客服列表失败")
|
||||
})
|
||||
.finally(() => {
|
||||
setLoadingAgents(false)
|
||||
})
|
||||
}, [])
|
||||
|
||||
async function onFormSubmit(values: TransferForm) {
|
||||
if (!conversationId) {
|
||||
toast.error("会话不存在")
|
||||
return
|
||||
}
|
||||
|
||||
const toUserId = Number(values.toUserId)
|
||||
const reason = values.reason.trim()
|
||||
|
||||
setSaving(true)
|
||||
try {
|
||||
if (mode === "assign") {
|
||||
await assignConversation(conversationId, toUserId, reason)
|
||||
toast.success(`已分配会话:#${conversationId}`)
|
||||
} else {
|
||||
await transferConversation(conversationId, toUserId, reason)
|
||||
toast.success(`已转接会话:#${conversationId}`)
|
||||
}
|
||||
reset(emptyForm)
|
||||
onOpenChange(false)
|
||||
await onSuccess?.()
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : mode === "assign" ? "分配会话失败" : "转接会话失败")
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const isAssign = mode === "assign"
|
||||
|
||||
return (
|
||||
<DialogContent className="max-w-lg gap-0 p-0 sm:max-w-lg">
|
||||
<DialogHeader className="px-6 pt-6">
|
||||
<DialogTitle>{isAssign ? "分配会话" : "转接会话"}</DialogTitle>
|
||||
{/* <DialogDescription>
|
||||
当前会话:{conversationId ? `#${conversationId}` : "-"}
|
||||
</DialogDescription> */}
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit(onFormSubmit)}>
|
||||
<div className="space-y-4 p-6">
|
||||
<Field data-invalid={!!errors.toUserId}>
|
||||
<FieldLabel htmlFor="conversation-transfer-user">目标客服</FieldLabel>
|
||||
<FieldContent>
|
||||
<Controller
|
||||
control={control}
|
||||
name="toUserId"
|
||||
render={({ field }) => (
|
||||
<OptionCombobox
|
||||
value={field.value}
|
||||
options={userOptions}
|
||||
placeholder={loadingAgents ? "加载中..." : "选择目标客服"}
|
||||
searchPlaceholder="搜索客服"
|
||||
emptyText="暂无可选客服"
|
||||
disabled={saving || loadingAgents}
|
||||
onChange={field.onChange}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<FieldError errors={[errors.toUserId]} />
|
||||
</FieldContent>
|
||||
</Field>
|
||||
<Field data-invalid={!!errors.reason}>
|
||||
<FieldLabel htmlFor="conversation-transfer-reason">
|
||||
{isAssign ? "分配说明" : "转接原因"}
|
||||
</FieldLabel>
|
||||
<FieldContent>
|
||||
<Textarea
|
||||
id="conversation-transfer-reason"
|
||||
rows={4}
|
||||
placeholder={isAssign ? "填写分配说明,便于后续追踪" : "填写转接原因,便于后续追踪"}
|
||||
aria-invalid={!!errors.reason}
|
||||
{...register("reason")}
|
||||
/>
|
||||
<FieldError errors={[errors.reason]} />
|
||||
</FieldContent>
|
||||
</Field>
|
||||
</div>
|
||||
<DialogFooter className="mx-0 mb-0 px-6 py-4">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={saving}
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button type="submit" disabled={saving}>
|
||||
<ArrowRightLeftIcon />
|
||||
{saving ? (isAssign ? "分配中..." : "转接中...") : isAssign ? "确认分配" : "确认转接"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user