- Step1 消息列表头部:深链 taskId 模式取到目的树后,左侧标题换成「策划流程」 彩色层级小图(任务→目的→…→叙事,每级配色对齐流程图节点 + 类型小图标), 点击打开任务目的流程图弹窗;仅第一步、有数据时出现。 - TaskPurposeDialog 流程图:横向(LR)布局 + 右下角缩略图(MiniMap)。 - FlowGraphPanel 深蓝主题适配(随 ThemeContext):容器/头部/画布点阵/连线/图例/ 小地图/节点详情弹窗 + 节点配色(FLOW_NODE_TYPE_META_DARK),浅色红主题不受影响。 - 圆桌侧栏深色兜底:neutral 家族 + 米色 hex(#FAF6F5/#ECE7E4/#E8D0CC) 映射到深蓝 token(关系图背板、第N步 tag 等不再泛白)。 附带提交工作区其余暂存改动(AI 写作等),不含 config.yaml。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
480 lines
17 KiB
TypeScript
480 lines
17 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
CheckIcon,
|
|
Loader2,
|
|
PencilIcon,
|
|
PlusIcon,
|
|
RefreshCwIcon,
|
|
Trash2Icon,
|
|
UserPlusIcon,
|
|
UsersIcon,
|
|
} from "lucide-react";
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { toast } from "sonner";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { resolveTagColor } from "@/components/workspace/tags/tag-colors";
|
|
import {
|
|
POSITION_RESOURCE_HINT,
|
|
POSITION_RESOURCE_LABELS,
|
|
POSITION_RESOURCE_TYPES,
|
|
resourceTypeToTagType,
|
|
useAddMembers,
|
|
useCreatePosition,
|
|
useDeletePosition,
|
|
usePositionMembers,
|
|
usePositionTagBindings,
|
|
usePositionUsers,
|
|
usePositions,
|
|
useRemoveMembers,
|
|
useResyncPosition,
|
|
useSetTagBindings,
|
|
useUpdatePosition,
|
|
type Position,
|
|
type PositionResourceType,
|
|
type TagBindings,
|
|
} from "@/core/positions";
|
|
import { useTags } from "@/core/tags";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
function errMsg(e: unknown): string {
|
|
return String(e instanceof Error ? e.message : e);
|
|
}
|
|
|
|
/** Inline multi-select chip row for the tags of one resource type. */
|
|
function TagMultiSelect({
|
|
resourceType,
|
|
value,
|
|
onChange,
|
|
}: {
|
|
resourceType: PositionResourceType;
|
|
value: string[];
|
|
onChange: (next: string[]) => void;
|
|
}) {
|
|
const { data: tags, isLoading } = useTags(resourceTypeToTagType(resourceType));
|
|
const toggle = (id: string) =>
|
|
onChange(value.includes(id) ? value.filter((x) => x !== id) : [...value, id]);
|
|
|
|
if (isLoading) {
|
|
return <Loader2 className="text-muted-foreground size-4 animate-spin" />;
|
|
}
|
|
if (!tags || tags.length === 0) {
|
|
return (
|
|
<span className="text-muted-foreground text-xs">
|
|
暂无{POSITION_RESOURCE_LABELS[resourceType]}标签,请先到「标签管理」创建。
|
|
</span>
|
|
);
|
|
}
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-1.5">
|
|
{tags.map((tag) => {
|
|
const checked = value.includes(tag.id);
|
|
const color = resolveTagColor(tag.color);
|
|
return (
|
|
<button
|
|
key={tag.id}
|
|
type="button"
|
|
onClick={() => toggle(tag.id)}
|
|
className={cn(
|
|
"inline-flex items-center gap-1 rounded-full border px-3 py-1 text-xs font-medium transition-all",
|
|
color.chip,
|
|
checked ? "ring-foreground/40 ring-2 ring-offset-1" : "opacity-45 hover:opacity-90",
|
|
)}
|
|
>
|
|
{checked && <CheckIcon className="size-3" />}
|
|
{tag.name}
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function PositionFormDialog({
|
|
open,
|
|
onOpenChange,
|
|
editing,
|
|
}: {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
editing: Position | null;
|
|
}) {
|
|
const createPosition = useCreatePosition();
|
|
const updatePosition = useUpdatePosition();
|
|
const [name, setName] = useState("");
|
|
const [description, setDescription] = useState("");
|
|
const [isDefault, setIsDefault] = useState(false);
|
|
|
|
const [syncedFor, setSyncedFor] = useState<string | null>(null);
|
|
const syncKey = `${open}:${editing?.id ?? "new"}`;
|
|
if (open && syncedFor !== syncKey) {
|
|
setName(editing?.name ?? "");
|
|
setDescription(editing?.description ?? "");
|
|
setIsDefault(editing?.is_default ?? false);
|
|
setSyncedFor(syncKey);
|
|
}
|
|
if (!open && syncedFor !== null) setSyncedFor(null);
|
|
|
|
const pending = createPosition.isPending || updatePosition.isPending;
|
|
const handleSave = async () => {
|
|
const trimmed = name.trim();
|
|
if (!trimmed) return;
|
|
try {
|
|
if (editing) {
|
|
await updatePosition.mutateAsync({ id: editing.id, input: { name: trimmed, description, is_default: isDefault } });
|
|
} else {
|
|
await createPosition.mutateAsync({ name: trimmed, description, is_default: isDefault });
|
|
}
|
|
onOpenChange(false);
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{editing ? "编辑岗位" : "新增岗位"}</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="flex flex-col gap-4">
|
|
<div className="flex flex-col gap-1.5">
|
|
<label className="text-sm font-medium">岗位名称</label>
|
|
<Input value={name} onChange={(e) => setName(e.target.value)} placeholder="如:理赔专员" maxLength={191} />
|
|
</div>
|
|
<div className="flex flex-col gap-1.5">
|
|
<label className="text-sm font-medium">描述</label>
|
|
<Textarea value={description} onChange={(e) => setDescription(e.target.value)} rows={2} maxLength={1024} />
|
|
</div>
|
|
<label className="flex cursor-pointer items-center gap-2 text-sm">
|
|
<input type="checkbox" checked={isDefault} onChange={(e) => setIsDefault(e.target.checked)} />
|
|
设为默认岗位(未分配岗位的用户继承此岗位的配置)
|
|
</label>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>取消</Button>
|
|
<Button onClick={handleSave} disabled={pending || !name.trim()}>
|
|
{pending && <Loader2 className="size-4 animate-spin" />}保存
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function AddMembersDialog({
|
|
open,
|
|
onOpenChange,
|
|
positionId,
|
|
}: {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
positionId: string;
|
|
}) {
|
|
const { data: users, isLoading } = usePositionUsers();
|
|
const addMembers = useAddMembers();
|
|
const [selected, setSelected] = useState<Set<string>>(new Set());
|
|
const [search, setSearch] = useState("");
|
|
|
|
useEffect(() => {
|
|
if (open) setSelected(new Set());
|
|
}, [open]);
|
|
|
|
const filtered = useMemo(() => {
|
|
const list = users ?? [];
|
|
const q = search.trim().toLowerCase();
|
|
return q ? list.filter((u) => u.email.toLowerCase().includes(q)) : list;
|
|
}, [users, search]);
|
|
|
|
const toggle = (id: string) => {
|
|
setSelected((prev) => {
|
|
const next = new Set(prev);
|
|
if (next.has(id)) next.delete(id);
|
|
else next.add(id);
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const handleSave = async () => {
|
|
if (selected.size === 0) return;
|
|
try {
|
|
await addMembers.mutateAsync({ positionId, userIds: [...selected] });
|
|
toast.success(`已为 ${selected.size} 名用户配置岗位`);
|
|
onOpenChange(false);
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="max-w-lg">
|
|
<DialogHeader>
|
|
<DialogTitle>批量添加成员</DialogTitle>
|
|
</DialogHeader>
|
|
<Input value={search} onChange={(e) => setSearch(e.target.value)} placeholder="搜索邮箱…" />
|
|
<div className="max-h-72 overflow-y-auto rounded-md border">
|
|
{isLoading && (
|
|
<div className="text-muted-foreground flex items-center justify-center gap-2 py-6">
|
|
<Loader2 className="size-4 animate-spin" />加载用户…
|
|
</div>
|
|
)}
|
|
{!isLoading && filtered.length === 0 && (
|
|
<div className="text-muted-foreground py-6 text-center text-sm">无匹配用户</div>
|
|
)}
|
|
{filtered.map((u) => {
|
|
const checked = selected.has(u.id);
|
|
return (
|
|
<button
|
|
key={u.id}
|
|
type="button"
|
|
onClick={() => toggle(u.id)}
|
|
className={cn(
|
|
"flex w-full items-center justify-between gap-2 border-b px-3 py-2 text-left text-sm last:border-b-0 hover:bg-muted/50",
|
|
checked && "bg-muted/40",
|
|
)}
|
|
>
|
|
<span className="truncate">
|
|
{u.email}
|
|
{u.system_role === "admin" && <span className="text-muted-foreground ml-2 text-xs">(管理员)</span>}
|
|
</span>
|
|
<span className="flex items-center gap-2">
|
|
{u.position_id && u.position_id !== positionId && (
|
|
<span className="text-muted-foreground text-xs">已属其他岗位</span>
|
|
)}
|
|
<span className={cn("flex size-4 items-center justify-center rounded border", checked ? "bg-foreground text-background border-foreground" : "border-muted-foreground/40")}>
|
|
{checked && <CheckIcon className="size-3" />}
|
|
</span>
|
|
</span>
|
|
</button>
|
|
);
|
|
})}
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>取消</Button>
|
|
<Button onClick={handleSave} disabled={addMembers.isPending || selected.size === 0}>
|
|
{addMembers.isPending && <Loader2 className="size-4 animate-spin" />}添加 {selected.size > 0 ? `(${selected.size})` : ""}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function PositionDetail({ position }: { position: Position }) {
|
|
const { data: serverBindings } = usePositionTagBindings(position.id);
|
|
const { data: members } = usePositionMembers(position.id);
|
|
const setBindings = useSetTagBindings();
|
|
const removeMembers = useRemoveMembers();
|
|
const resync = useResyncPosition();
|
|
const [draft, setDraft] = useState<TagBindings>({});
|
|
const [addOpen, setAddOpen] = useState(false);
|
|
|
|
// Reload the draft whenever the selected position or its server bindings change.
|
|
const [loadedFor, setLoadedFor] = useState<string | null>(null);
|
|
const loadKey = `${position.id}:${serverBindings ? "ready" : "loading"}`;
|
|
if (serverBindings && loadedFor !== loadKey) {
|
|
setDraft({ ...serverBindings });
|
|
setLoadedFor(loadKey);
|
|
}
|
|
|
|
const setType = (rt: PositionResourceType, ids: string[]) =>
|
|
setDraft((prev) => ({ ...prev, [rt]: ids }));
|
|
|
|
const handleSaveBindings = async () => {
|
|
try {
|
|
await setBindings.mutateAsync({ positionId: position.id, bindings: draft });
|
|
toast.success("标签配置已保存,已同步岗位成员");
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
const handleResync = async () => {
|
|
try {
|
|
const n = await resync.mutateAsync(position.id);
|
|
toast.success(`已重新同步 ${n} 名成员`);
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
const handleRemove = async (userId: string) => {
|
|
try {
|
|
await removeMembers.mutateAsync({ positionId: position.id, userIds: [userId] });
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-6">
|
|
<div className="rounded-lg border p-4">
|
|
<div className="mb-3 flex items-center justify-between">
|
|
<h3 className="text-sm font-semibold">默认资源标签配置</h3>
|
|
<Button size="sm" onClick={handleSaveBindings} disabled={setBindings.isPending}>
|
|
{setBindings.isPending && <Loader2 className="size-4 animate-spin" />}保存标签配置
|
|
</Button>
|
|
</div>
|
|
<div className="flex flex-col divide-y">
|
|
{POSITION_RESOURCE_TYPES.map((rt) => (
|
|
<div key={rt} className="flex flex-col gap-2 py-3 first:pt-0 last:pb-0">
|
|
<div className="flex items-baseline gap-2">
|
|
<span className="text-sm font-medium">{POSITION_RESOURCE_LABELS[rt]}</span>
|
|
<span className="text-muted-foreground text-xs">{POSITION_RESOURCE_HINT[rt]}</span>
|
|
</div>
|
|
<TagMultiSelect resourceType={rt} value={draft[rt] ?? []} onChange={(ids) => setType(rt, ids)} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="rounded-lg border p-4">
|
|
<div className="mb-3 flex items-center justify-between">
|
|
<h3 className="flex items-center gap-1.5 text-sm font-semibold">
|
|
<UsersIcon className="size-4" />岗位成员 ({members?.length ?? 0})
|
|
</h3>
|
|
<div className="flex gap-2">
|
|
<Button size="sm" variant="outline" onClick={handleResync} disabled={resync.isPending}>
|
|
{resync.isPending ? <Loader2 className="size-4 animate-spin" /> : <RefreshCwIcon className="size-4" />}重新同步
|
|
</Button>
|
|
<Button size="sm" onClick={() => setAddOpen(true)}>
|
|
<UserPlusIcon className="size-4" />添加成员
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
{members && members.length > 0 ? (
|
|
<div className="flex flex-col divide-y">
|
|
{members.map((m) => (
|
|
<div key={m.id} className="flex items-center justify-between py-2 text-sm">
|
|
<span className="truncate">{m.email}</span>
|
|
<Button variant="ghost" size="icon" onClick={() => handleRemove(m.id)} aria-label="移除">
|
|
<Trash2Icon className="text-destructive size-4" />
|
|
</Button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="text-muted-foreground py-6 text-center text-sm">暂无成员</div>
|
|
)}
|
|
</div>
|
|
|
|
<AddMembersDialog open={addOpen} onOpenChange={setAddOpen} positionId={position.id} />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function PositionManagementPage() {
|
|
const { data: positions, isLoading, error } = usePositions();
|
|
const deletePosition = useDeletePosition();
|
|
const [selectedId, setSelectedId] = useState<string | null>(null);
|
|
const [dialogOpen, setDialogOpen] = useState(false);
|
|
const [editing, setEditing] = useState<Position | null>(null);
|
|
|
|
const selected = useMemo(
|
|
() => positions?.find((p) => p.id === selectedId) ?? null,
|
|
[positions, selectedId],
|
|
);
|
|
// Auto-select the first position once loaded.
|
|
useEffect(() => {
|
|
if (!selectedId && positions && positions.length > 0) setSelectedId(positions[0].id);
|
|
}, [positions, selectedId]);
|
|
|
|
const handleDelete = async (p: Position) => {
|
|
if (!window.confirm(`确认删除岗位「${p.name}」?其成员将回退到默认岗位。`)) return;
|
|
try {
|
|
await deletePosition.mutateAsync(p.id);
|
|
if (selectedId === p.id) setSelectedId(null);
|
|
} catch (e) {
|
|
toast.error(errMsg(e));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="flex size-full min-h-0 flex-col gap-4 overflow-auto p-6">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h1 className="text-lg font-semibold">岗位管理</h1>
|
|
<p className="text-muted-foreground text-sm">
|
|
为岗位配置默认智能体/技能/定时任务/推荐问题的标签,并批量分配给用户。
|
|
</p>
|
|
</div>
|
|
<Button size="sm" onClick={() => { setEditing(null); setDialogOpen(true); }}>
|
|
<PlusIcon className="size-4" />新增岗位
|
|
</Button>
|
|
</div>
|
|
|
|
{isLoading && (
|
|
<div className="text-muted-foreground flex items-center justify-center gap-2 py-12">
|
|
<Loader2 className="size-4 animate-spin" />加载中…
|
|
</div>
|
|
)}
|
|
{error && <div className="text-destructive text-sm">{errMsg(error)}</div>}
|
|
|
|
{!isLoading && !error && (
|
|
<div className="grid grid-cols-1 gap-6 lg:grid-cols-[280px_1fr]">
|
|
<div className="flex flex-col gap-2">
|
|
{positions && positions.length === 0 && (
|
|
<div className="text-muted-foreground rounded-lg border border-dashed py-8 text-center text-sm">
|
|
还没有岗位,点击「新增岗位」创建。
|
|
</div>
|
|
)}
|
|
{positions?.map((p) => (
|
|
<div
|
|
key={p.id}
|
|
onClick={() => setSelectedId(p.id)}
|
|
className={cn(
|
|
"group cursor-pointer rounded-lg border p-3 transition-colors",
|
|
selectedId === p.id ? "border-foreground bg-muted/40" : "hover:bg-muted/30",
|
|
)}
|
|
>
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2">
|
|
<span className="text-sm font-medium">{p.name}</span>
|
|
{p.is_default && (
|
|
<span className="rounded bg-blue-100 px-1.5 py-0.5 text-[10px] text-blue-700 dark:bg-blue-950 dark:text-blue-300">默认</span>
|
|
)}
|
|
</div>
|
|
<div className="flex opacity-0 transition-opacity group-hover:opacity-100">
|
|
<Button variant="ghost" size="icon" onClick={(e) => { e.stopPropagation(); setEditing(p); setDialogOpen(true); }} aria-label="编辑">
|
|
<PencilIcon className="size-3.5" />
|
|
</Button>
|
|
<Button variant="ghost" size="icon" onClick={(e) => { e.stopPropagation(); void handleDelete(p); }} aria-label="删除">
|
|
<Trash2Icon className="text-destructive size-3.5" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
<div className="text-muted-foreground mt-1 flex items-center gap-2 text-xs">
|
|
<UsersIcon className="size-3" />{p.member_count} 名成员
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
<div>
|
|
{selected ? (
|
|
<PositionDetail key={selected.id} position={selected} />
|
|
) : (
|
|
<div className="text-muted-foreground rounded-lg border border-dashed py-16 text-center text-sm">
|
|
选择左侧一个岗位以配置标签与成员
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<PositionFormDialog open={dialogOpen} onOpenChange={setDialogOpen} editing={editing} />
|
|
</div>
|
|
);
|
|
}
|