feat: implement TagSelector component for improved tag management across dashboard features

- Added TagSelector component to streamline tag selection in various components.
- Refactored existing tag handling in DashboardConversationsPage, ConversationTagPicker, EditDialog, and TicketsPage to utilize the new TagSelector.
- Removed redundant tag handling functions and optimized state management for tags.
- Updated tests to ensure proper functionality of the new tag handling logic.
This commit is contained in:
mlogclub
2026-05-30 11:23:05 +08:00
parent d2da9de641
commit 8398cd6f91
9 changed files with 653 additions and 354 deletions
+8 -105
View File
@@ -1,6 +1,5 @@
"use client"
import { CheckIcon, TagIcon } from "lucide-react"
import { useEffect, useMemo, useState } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import type { Resolver } from "react-hook-form"
@@ -11,16 +10,8 @@ import { ContentEditor } from "@/components/content-editor"
import { OptionCombobox } from "@/components/option-combobox"
import { ProjectDialog } from "@/components/project-dialog"
import { isRichTextEmpty } from "@/components/safe-rich-html"
import { Badge } from "@/components/ui/badge"
import { TagSelector } from "@/components/tag-selector"
import { Button } from "@/components/ui/button"
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command"
import {
Field,
FieldContent,
@@ -29,7 +20,6 @@ import {
FieldLabel,
} from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import {
fetchAgentProfilesAll,
fetchTagsAll,
@@ -113,97 +103,6 @@ function buildPayload(form: EditForm): CreateTicketPayload {
}
}
type FlatTagNode = TagTree & {
depth: number
path: string
}
function flattenTagTree(nodes: TagTree[], depth = 0, parentPath = ""): FlatTagNode[] {
const result: FlatTagNode[] = []
nodes.forEach((item) => {
const path = parentPath ? `${parentPath} / ${item.name}` : item.name
result.push({ ...item, depth, path })
if (item.children.length > 0) {
result.push(...flattenTagTree(item.children, depth + 1, path))
}
})
return result
}
type TicketTagSelectorProps = {
value?: number[]
onChange: (value: number[]) => void
availableTags: TagTree[]
t: TFunction
}
function TicketTagSelector({ value, onChange, availableTags, t }: TicketTagSelectorProps) {
const selectedValues = useMemo(() => value ?? [], [value])
const flatTags = useMemo(() => flattenTagTree(availableTags), [availableTags])
const selectedTagIDs = useMemo(() => new Set(selectedValues), [selectedValues])
const selectedTags = useMemo(
() => flatTags.filter((tag) => selectedTagIDs.has(tag.id)),
[flatTags, selectedTagIDs],
)
function handleToggle(tagID: number) {
if (selectedTagIDs.has(tagID)) {
onChange(selectedValues.filter((item) => item !== tagID))
return
}
onChange(selectedValues.concat(tagID))
}
return (
<div className="space-y-2">
<Popover>
<PopoverTrigger
render={
<Button type="button" variant="outline" className="w-full justify-start" />
}
>
<TagIcon className="size-4" />
{selectedTags.length > 0 ? t("ticket.selectedTags", { count: selectedTags.length }) : t("ticket.selectTags")}
</PopoverTrigger>
<PopoverContent align="start" className="w-[320px] p-0">
<Command>
<CommandInput placeholder={t("ticket.searchTags")} />
<CommandList>
<CommandEmpty>{t("ticket.emptyTags")}</CommandEmpty>
<CommandGroup heading={t("ticket.tags")}>
{flatTags.map((tag) => {
const checked = selectedTagIDs.has(tag.id)
return (
<CommandItem
key={tag.id}
value={`${tag.id} ${tag.path} ${tag.remark}`}
onSelect={() => handleToggle(tag.id)}
>
<CheckIcon className={`mr-2 size-4 ${checked ? "opacity-100" : "opacity-0"}`} />
<span className="truncate" style={{ paddingLeft: `${tag.depth * 12}px` }}>
{tag.name}
</span>
</CommandItem>
)
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{selectedTags.length > 0 ? (
<div className="flex flex-wrap gap-1">
{selectedTags.map((tag) => (
<Badge key={tag.id} variant="outline">
{tag.path}
</Badge>
))}
</div>
) : null}
</div>
)
}
export function EditDialog({
open,
saving,
@@ -421,11 +320,15 @@ function TicketEditDialogBody({
control={control}
name="tagIds"
render={({ field }) => (
<TicketTagSelector
<TagSelector
mode="multiple"
value={field.value}
onChange={field.onChange}
availableTags={tags}
t={t}
tags={tags}
placeholder={t("ticket.selectTags")}
selectedCountText={(count) => t("ticket.selectedTags", { count })}
searchPlaceholder={t("ticket.searchTags")}
emptyText={t("ticket.emptyTags")}
/>
)}
/>
+8 -26
View File
@@ -60,10 +60,6 @@ function getAssigneeAllOption(t: TFunction): ComboboxOption {
return { value: "0", label: t("ticket.allAssignees") }
}
function getTagAllOption(t: TFunction): ComboboxOption {
return { value: "0", label: t("ticket.allTags") }
}
function getStaleHourOptions(t: TFunction): ComboboxOption[] {
return [
{ value: "24", label: t("ticket.hours", { hours: 24 }) },
@@ -72,21 +68,6 @@ function getStaleHourOptions(t: TFunction): ComboboxOption[] {
]
}
function buildTagOptions(nodes: TagTree[], parentPath = ""): ComboboxOption[] {
const result: ComboboxOption[] = []
nodes.forEach((item) => {
const currentPath = parentPath ? `${parentPath}/${item.name}` : item.name
result.push({
value: String(item.id),
label: currentPath,
})
if (item.children.length > 0) {
result.push(...buildTagOptions(item.children, currentPath))
}
})
return result
}
function sourceLabel(source: string, t: TFunction) {
switch (source) {
case "manual":
@@ -117,7 +98,7 @@ export default function TicketsPage() {
const searchParams = useSearchParams()
const [summary, setSummary] = useState<TicketSummary>(emptySummary)
const [assigneeOptions, setAssigneeOptions] = useState<ComboboxOption[]>([])
const [tagOptions, setTagOptions] = useState<ComboboxOption[]>([])
const [tags, setTags] = useState<TagTree[]>([])
const [selectedTicketId, setSelectedTicketId] = useState<number | null>(null)
const [detailOpen, setDetailOpen] = useState(false)
const [createOpen, setCreateOpen] = useState(false)
@@ -125,7 +106,6 @@ export default function TicketsPage() {
const listReloadRef = useRef<() => Promise<void>>(async () => undefined)
const assigneeAllOption = useMemo(() => getAssigneeAllOption(t), [t])
const tagAllOption = useMemo(() => getTagAllOption(t), [t])
const staleHourOptions = useMemo(() => getStaleHourOptions(t), [t])
const quickViews = useMemo(
@@ -201,11 +181,13 @@ export default function TicketsPage() {
{
name: "tagId",
label: t("ticket.allTags"),
type: "select",
type: "tag",
defaultValue: "0",
allValue: "0",
valueType: "number",
options: tagOptions,
tags,
searchPlaceholder: t("ticket.searchTags"),
emptyText: t("ticket.emptyTags"),
className: "w-full sm:w-44",
},
{
@@ -217,7 +199,7 @@ export default function TicketsPage() {
className: "w-full sm:w-40",
},
],
[assigneeOptions, quickViews, staleHourOptions, tagOptions, t],
[assigneeOptions, quickViews, staleHourOptions, tags, t],
)
const fetchList = useCallback(
@@ -365,7 +347,7 @@ export default function TicketsPage() {
t("ticket.agentFallback", { id: agent.userId }),
})),
])
setTagOptions([tagAllOption, ...buildTagOptions(Array.isArray(tags) ? tags : [])])
setTags(Array.isArray(tags) ? tags : [])
})
.catch((error) => {
toast.error(error instanceof Error ? error.message : t("ticket.loadFiltersFailed"))
@@ -373,7 +355,7 @@ export default function TicketsPage() {
return () => {
active = false
}
}, [assigneeAllOption, tagAllOption, t])
}, [assigneeAllOption, t])
async function handleCreateTicket(payload: CreateTicketPayload) {
setSavingCreate(true)