gongxiangp2/components/department-dashboard/OngoingProjectsInsights.tsx
2026-08-27 13:19:27 +08:00

575 lines
27 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import { useMemo, useState } from 'react'
import {
BriefcaseBusiness,
ChevronDown,
Layers3,
Search,
UserRound,
UsersRound,
} from 'lucide-react'
import type {
OngoingDingWorkflowActiveTask,
OngoingDingWorkflowInsights,
OngoingDingWorkflowListItem,
} from '@/lib/ding-workflow-sync'
import WorkflowDetailTable, {
workflowNodeNames,
} from './WorkflowDetailTable'
type Perspective = 'brand' | 'owner' | 'initiator'
type ProjectGroup = {
key: string
label: string
description: string
count: number
}
const BRAND_ORDER = ['亿家达', '卓禾', '欧意朗', '顾家', '索菲亚', '朴美', '其他品牌']
const PROJECT_TYPE_ORDER = ['美术设计流程', '内容视频和图片拍摄']
const PERSPECTIVES: Array<{
key: Perspective
label: string
description: string
icon: typeof Layers3
}> = [
{ key: 'brand', label: '品牌维度', description: '按品牌查看进行中数量、任务类型与当前节点', icon: Layers3 },
{ key: 'owner', label: '项目负责人', description: '项目负责人按当前节点处理人认定', icon: UsersRound },
{ key: 'initiator', label: '发起人维度', description: '按发起人及人员架构所在部门查看项目', icon: UserRound },
]
function ownerKey(task: OngoingDingWorkflowActiveTask) {
return task.assigneeUserId || `name:${task.assigneeName}`
}
function humanOwnerTasks(project: OngoingDingWorkflowListItem) {
return project.activeTasks.filter((task) => task.isBrandCenter && task.assigneeName !== '系统处理')
}
function uniqueProjects(projects: OngoingDingWorkflowListItem[]) {
return Array.from(new Map(projects.map((project) => [project.id, project])).values())
}
function projectMatchesQuery(project: OngoingDingWorkflowListItem, query: string) {
const normalized = query.trim().toLocaleLowerCase('zh-CN')
if (!normalized) return true
return [
project.title,
project.businessId,
project.id,
project.brand,
project.projectType,
project.requesterName,
project.requesterDepartment,
project.initiatorName,
project.initiatorDepartment,
project.formFields.platform,
project.formFields.brand,
project.formFields.category,
project.formFields.spu,
project.formFields.demandType,
project.formFields.standardOutputDeadline,
project.assignmentNodeLabel,
project.reviewNodeLabel,
...project.activeTasks.flatMap((task) => [task.assigneeName, task.nodeName, task.taskId]),
].some((value) => String(value || '').toLocaleLowerCase('zh-CN').includes(normalized))
}
function CurrentTaskSummary({
project,
tasks = project.activeTasks,
}: {
project: OngoingDingWorkflowListItem
tasks?: OngoingDingWorkflowActiveTask[]
}) {
if (tasks.length === 0) return <span className="text-slate-500">待进入处理节点</span>
return (
<div className="space-y-1.5">
{tasks.map((task) => (
<p key={`${project.id}:${task.taskId}`} className="leading-5 text-slate-700">
<span className="font-bold text-slate-950">{task.nodeName}</span>
<span className="mx-1.5 text-slate-300">·</span>
{task.assigneeName}
</p>
))}
</div>
)
}
export default function OngoingProjectsInsights({ insights }: { insights: OngoingDingWorkflowInsights }) {
const [perspective, setPerspective] = useState<Perspective>('brand')
const [selectedGroupKey, setSelectedGroupKey] = useState('亿家达')
const [expandedType, setExpandedType] = useState<string | null>(PROJECT_TYPE_ORDER[0])
const [brandFilter, setBrandFilter] = useState<string | null>(null)
const [typeFilter, setTypeFilter] = useState<string | null>(null)
const [nodeFilter, setNodeFilter] = useState<string | null>(null)
const [query, setQuery] = useState('')
const [visibleCount, setVisibleCount] = useState(30)
const groups = useMemo<ProjectGroup[]>(() => {
if (perspective === 'brand') {
return BRAND_ORDER.map((brand) => ({
key: brand,
label: brand,
description: brand === '其他品牌' ? '未归入六个固定品牌' : '进行中流程',
count: insights.items.filter((project) => project.brand === brand).length,
}))
}
if (perspective === 'owner') {
const owners = new Map<string, { label: string; department: string; projectIds: Set<string> }>()
insights.items.forEach((project) => {
humanOwnerTasks(project).forEach((task) => {
const key = ownerKey(task)
const current = owners.get(key) || {
label: task.assigneeName,
department: task.assigneeDepartment || '品牌中心',
projectIds: new Set<string>(),
}
current.projectIds.add(project.id)
owners.set(key, current)
})
})
return Array.from(owners.entries())
.map(([key, owner]) => ({
key,
label: owner.label,
description: owner.department,
count: owner.projectIds.size,
}))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN'))
}
const initiators = new Map<string, { label: string; department: string; projectIds: Set<string> }>()
insights.items.forEach((project) => {
const key = project.originatorUserId?.trim() || `name:${project.initiatorName}`
const current = initiators.get(key) || {
label: project.initiatorName || '未记录',
department: project.initiatorDepartment || '未记录部门',
projectIds: new Set<string>(),
}
current.projectIds.add(project.id)
initiators.set(key, current)
})
return Array.from(initiators.entries())
.map(([key, initiator]) => ({
key,
label: initiator.label,
description: initiator.department,
count: initiator.projectIds.size,
}))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN'))
}, [insights.items, perspective])
const selectedGroup = groups.find((group) => group.key === selectedGroupKey) || groups[0] || null
const selectedProjects = useMemo(() => {
if (!selectedGroup) return []
if (perspective === 'brand') {
return insights.items.filter((project) => project.brand === selectedGroup.key)
}
if (perspective === 'owner') {
return insights.items.filter((project) => (
humanOwnerTasks(project).some((task) => ownerKey(task) === selectedGroup.key)
))
}
return insights.items.filter((project) => (
(project.originatorUserId?.trim() || `name:${project.initiatorName}`) === selectedGroup.key
))
}, [insights.items, perspective, selectedGroup])
const tasksForSelectedOwner = (project: OngoingDingWorkflowListItem) => (
perspective === 'owner' && selectedGroup
? humanOwnerTasks(project).filter((task) => ownerKey(task) === selectedGroup.key)
: project.activeTasks
)
const ownerBrandSummaries = BRAND_ORDER.map((brand) => ({
brand,
count: selectedProjects.filter((project) => project.brand === brand).length,
}))
const projectsForBreakdown = brandFilter
? selectedProjects.filter((project) => project.brand === brandFilter)
: selectedProjects
const typeSummaries = PROJECT_TYPE_ORDER.map((projectType) => {
const projects = projectsForBreakdown.filter((project) => project.projectType === projectType)
const nodes = new Map<string, Set<string>>()
projects.forEach((project) => {
workflowNodeNames(project, tasksForSelectedOwner(project)).forEach((nodeName) => {
const projectIds = nodes.get(nodeName) || new Set<string>()
projectIds.add(project.id)
nodes.set(nodeName, projectIds)
})
})
return {
projectType,
count: uniqueProjects(projects).length,
nodes: Array.from(nodes.entries())
.map(([label, projectIds]) => ({ label, count: projectIds.size }))
.sort((a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN')),
}
})
const filteredProjects = selectedProjects.filter((project) => {
if (brandFilter && project.brand !== brandFilter) return false
if (typeFilter && project.projectType !== typeFilter) return false
if (nodeFilter && !workflowNodeNames(project, tasksForSelectedOwner(project)).includes(nodeFilter)) return false
return projectMatchesQuery(project, query)
})
const displayedProjects = filteredProjects.slice(0, visibleCount)
const selectPerspective = (nextPerspective: Perspective) => {
setPerspective(nextPerspective)
setSelectedGroupKey(nextPerspective === 'brand' ? '亿家达' : '')
setExpandedType(PROJECT_TYPE_ORDER[0])
setBrandFilter(null)
setTypeFilter(null)
setNodeFilter(null)
setVisibleCount(30)
}
const selectGroup = (key: string) => {
setSelectedGroupKey(key)
setExpandedType(PROJECT_TYPE_ORDER[0])
setBrandFilter(null)
setTypeFilter(null)
setNodeFilter(null)
setVisibleCount(30)
}
return (
<section aria-labelledby="project-insights-title" className="mt-10">
<div className="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
<div>
<h2 id="project-insights-title" className="text-xl font-bold tracking-[-0.02em] sm:text-2xl">进行中项目分析</h2>
<p className="mt-1 max-w-3xl text-sm leading-6 text-slate-600">
从品牌、当前节点处理人和发起人三个维度下钻;所有数字均来自每小时同步、且发起时间不早于 2026 年 4 月 1 日的未完结流程快照。
</p>
</div>
<p className="text-sm font-semibold tabular-nums text-slate-600">共 {insights.total} 个进行中项目</p>
</div>
<div className="mt-5 grid gap-2 rounded-xl border border-slate-200 bg-white p-2 md:grid-cols-3" role="tablist" aria-label="进行中项目分析维度">
{PERSPECTIVES.map((item) => {
const Icon = item.icon
const selected = perspective === item.key
return (
<button
key={item.key}
type="button"
role="tab"
aria-selected={selected}
className={`flex min-h-14 items-center gap-3 rounded-lg px-4 text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${selected ? 'bg-[#e6f4ff] text-[#0050cb]' : 'text-slate-600 hover:bg-[#eef2f6]'}`}
onClick={() => selectPerspective(item.key)}
>
<Icon className="h-5 w-5 shrink-0" aria-hidden="true" />
<span className="min-w-0">
<span className="block font-bold">{item.label}</span>
<span className={`mt-0.5 hidden truncate text-xs md:block ${selected ? 'text-[#315f91]' : 'text-slate-500'}`}>{item.description}</span>
</span>
</button>
)
})}
</div>
<div className="mt-6 overflow-hidden rounded-xl border border-slate-200 bg-white">
<div className="border-b border-slate-200 px-5 py-4 sm:px-6">
<h3 className="font-bold text-slate-950">
{perspective === 'brand' ? '品牌进行中数量' : perspective === 'owner' ? '项目负责人(当前处理人)' : '发起人进行中数量'}
</h3>
<p className="mt-1 text-sm text-slate-500">
{perspective === 'owner'
? '负责人按当前未完结节点的处理人统计;并行处理会分别计入对应人员。'
: perspective === 'initiator'
? '发起人按钉钉唯一标识匹配人员架构;所在部门以 HR 花名册为准。'
: '选择一项查看任务类型、当前节点与项目明细。'}
</p>
</div>
{groups.length === 0 ? (
<div className="px-5 py-12 text-center text-sm text-slate-500">当前没有可匹配的数据。</div>
) : perspective === 'initiator' ? (
<>
<div className="max-h-[520px] overflow-y-auto md:hidden">
<ol className="divide-y divide-slate-200" aria-label="发起人进行中项目排名">
{groups.map((group, index) => {
const selected = selectedGroup?.key === group.key
return (
<li key={group.key}>
<button
type="button"
aria-pressed={selected}
className={`grid min-h-20 w-full grid-cols-[2rem_1fr_auto] items-center gap-3 px-5 py-3 text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[#5a9ce6] ${selected ? 'bg-[#e6f4ff]' : 'hover:bg-[#eef2f6]'}`}
onClick={() => selectGroup(group.key)}
>
<span className="text-sm font-bold tabular-nums text-slate-500">{index + 1}</span>
<span className="min-w-0">
<span className={`block font-bold ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.label}</span>
<span className="mt-1 block text-xs leading-5 text-slate-500">{group.description}</span>
</span>
<span className={`text-2xl font-bold tabular-nums ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.count}</span>
</button>
</li>
)
})}
</ol>
</div>
<div className="hidden max-h-[520px] overflow-y-auto md:block">
<table className="w-full text-left text-sm">
<caption className="sr-only">发起人进行中项目排名</caption>
<thead className="sticky top-0 z-10 bg-slate-50 text-xs font-bold text-slate-500">
<tr>
<th scope="col" className="w-20 px-6 py-3.5">排名</th>
<th scope="col" className="w-60 px-4 py-3.5">发起人</th>
<th scope="col" className="px-4 py-3.5">所在部门</th>
<th scope="col" className="w-32 px-6 py-3.5 text-right">进行中数量</th>
</tr>
</thead>
<tbody className="divide-y divide-slate-200">
{groups.map((group, index) => {
const selected = selectedGroup?.key === group.key
return (
<tr key={group.key} className={selected ? 'bg-[#e6f4ff]' : 'hover:bg-[#eef2f6]'}>
<td className="px-6 py-3.5 font-bold tabular-nums text-slate-500">{index + 1}</td>
<td className="px-4 py-3.5">
<button
type="button"
aria-pressed={selected}
className={`min-h-11 rounded-lg px-3 text-left font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${selected ? 'text-[#0050cb]' : 'text-slate-950 hover:text-[#0050cb]'}`}
onClick={() => selectGroup(group.key)}
>
{group.label}
</button>
</td>
<td className="px-4 py-3.5 leading-6 text-slate-700">{group.description}</td>
<td className={`px-6 py-3.5 text-right text-xl font-bold tabular-nums ${selected ? 'text-[#0050cb]' : 'text-slate-950'}`}>{group.count}</td>
</tr>
)
})}
</tbody>
</table>
</div>
</>
) : (
<div className="max-h-[430px] overflow-y-auto p-3 sm:p-4">
<div className="grid gap-2 sm:grid-cols-2 xl:grid-cols-3">
{groups.map((group) => {
const selected = selectedGroup?.key === group.key
return (
<button
key={group.key}
type="button"
aria-pressed={selected}
className={`flex min-h-16 items-center justify-between gap-4 rounded-lg border px-4 py-3 text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${selected ? 'border-[#91caff] bg-[#e6f4ff]' : 'border-transparent bg-slate-50 hover:border-slate-200 hover:bg-[#eef2f6]'}`}
onClick={() => selectGroup(group.key)}
>
<span className="min-w-0">
<span className={`block font-bold ${selected ? 'text-[#0050cb]' : 'text-slate-900'}`}>{group.label}</span>
<span className={`mt-1 block truncate text-xs ${selected ? 'text-[#315f91]' : 'text-slate-500'}`}>{group.description}</span>
</span>
<span className={`shrink-0 text-2xl font-bold tabular-nums ${selected ? 'text-[#0050cb]' : 'text-slate-900'}`}>{group.count}</span>
</button>
)
})}
</div>
</div>
)}
</div>
{selectedGroup && (
<div className="mt-6 overflow-hidden rounded-xl border border-slate-200 bg-white">
<div className="flex flex-col gap-2 border-b border-slate-200 px-5 py-4 sm:flex-row sm:items-center sm:justify-between sm:px-6">
<div>
<h3 className="font-bold text-slate-950">{selectedGroup.label}</h3>
<p className="mt-1 text-sm text-slate-500">按任务类型展开当前节点及节点内流程数量</p>
</div>
<span className="text-sm font-bold tabular-nums text-slate-600">{selectedProjects.length} 个进行中项目</span>
</div>
<div className="divide-y divide-slate-200">
{perspective === 'owner' && (
<div className="bg-slate-50 px-5 py-4 sm:px-6">
<p className="text-xs font-bold text-slate-500">品牌分布</p>
<div className="mt-2 flex flex-wrap gap-2" aria-label={`${selectedGroup.label}负责项目的品牌分布`}>
<button
type="button"
aria-pressed={!brandFilter}
className={`min-h-11 rounded-lg px-3 text-sm font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${!brandFilter ? 'bg-[#1677ff] text-white' : 'border border-slate-200 bg-white text-slate-700 hover:bg-[#eef2f6]'}`}
onClick={() => {
setBrandFilter(null)
setTypeFilter(null)
setNodeFilter(null)
setVisibleCount(30)
}}
>
全部品牌 <span className="ml-1 tabular-nums">{selectedProjects.length}</span>
</button>
{ownerBrandSummaries.map((item) => (
<button
key={item.brand}
type="button"
aria-pressed={brandFilter === item.brand}
className={`min-h-11 rounded-lg px-3 text-sm font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${brandFilter === item.brand ? 'bg-[#1677ff] text-white' : 'border border-slate-200 bg-white text-slate-700 hover:bg-[#eef2f6]'}`}
onClick={() => {
setBrandFilter(item.brand)
setTypeFilter(null)
setNodeFilter(null)
setVisibleCount(30)
}}
>
{item.brand} <span className="ml-1 tabular-nums">{item.count}</span>
</button>
))}
</div>
</div>
)}
{typeSummaries.map((summary) => {
const expanded = expandedType === summary.projectType
return (
<div key={summary.projectType}>
<button
type="button"
aria-expanded={expanded}
className="flex min-h-14 w-full items-center justify-between gap-4 px-5 py-3 text-left hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[#5a9ce6] sm:px-6"
onClick={() => {
setExpandedType(expanded ? null : summary.projectType)
setTypeFilter(expanded ? null : summary.projectType)
setNodeFilter(null)
setVisibleCount(30)
}}
>
<span className="flex min-w-0 items-center gap-3">
<span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-blue-50 text-blue-700">
<BriefcaseBusiness className="h-4 w-4" aria-hidden="true" />
</span>
<span>
<span className="block font-bold text-slate-950">{summary.projectType}</span>
<span className="mt-0.5 block text-xs text-slate-500">点击展开当前流程节点</span>
</span>
</span>
<span className="flex shrink-0 items-center gap-3">
<span className="text-xl font-bold tabular-nums text-slate-950">{summary.count}</span>
<ChevronDown className={`h-4 w-4 text-slate-500 transition-transform ${expanded ? 'rotate-180' : ''}`} aria-hidden="true" />
</span>
</button>
{expanded && (
<div className="border-t border-slate-100 bg-slate-50 px-5 py-4 sm:px-6">
{summary.nodes.length === 0 ? (
<p className="text-sm text-slate-500">当前类型暂无进行中流程。</p>
) : (
<div className="flex flex-wrap gap-2" aria-label={`${summary.projectType}当前节点`}>
<button
type="button"
className={`min-h-11 rounded-lg px-3 text-sm font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${typeFilter === summary.projectType && !nodeFilter ? 'bg-[#1677ff] text-white' : 'border border-slate-200 bg-white text-slate-700 hover:bg-slate-100'}`}
onClick={() => {
setTypeFilter(summary.projectType)
setNodeFilter(null)
setVisibleCount(30)
}}
>
全部 {summary.count}
</button>
{summary.nodes.map((node) => (
<button
key={node.label}
type="button"
className={`min-h-11 rounded-lg px-3 text-sm font-bold focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${typeFilter === summary.projectType && nodeFilter === node.label ? 'bg-[#1677ff] text-white' : 'border border-slate-200 bg-white text-slate-700 hover:bg-slate-100'}`}
onClick={() => {
setTypeFilter(summary.projectType)
setNodeFilter(node.label)
setVisibleCount(30)
}}
>
{node.label} <span className="ml-1 tabular-nums">{node.count}</span>
</button>
))}
</div>
)}
</div>
)}
</div>
)
})}
</div>
</div>
)}
{selectedGroup && (
<div className="mt-6">
<div className="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
<div>
<h3 className="text-lg font-bold text-slate-950">项目明细</h3>
<p className="mt-1 text-sm text-slate-500">
当前筛选 {filteredProjects.length} 个项目;字段与业务流程明细表一致,需求部门按发起人人员架构匹配。
</p>
</div>
<div className="flex w-full max-w-xl gap-2 lg:w-auto lg:flex-1">
<label htmlFor="ongoing-project-search" className="sr-only">搜索进行中项目</label>
<div className="relative min-w-0 flex-1">
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400" aria-hidden="true" />
<input
id="ongoing-project-search"
type="search"
value={query}
placeholder="搜索标题、审批编号、平台、品牌、SPU、发起人、节点或处理人"
className="min-h-11 w-full rounded-lg border border-slate-300 bg-white py-2 pl-9 pr-3 text-sm text-slate-950 outline-none placeholder:text-slate-400 focus:border-[#1677ff] focus:ring-2 focus:ring-blue-100"
onChange={(event) => {
setQuery(event.target.value)
setVisibleCount(30)
}}
/>
</div>
{(brandFilter || typeFilter || nodeFilter || query) && (
<button
type="button"
className="min-h-11 shrink-0 rounded-lg border border-slate-300 bg-white px-3 text-sm font-bold text-slate-700 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6]"
onClick={() => {
setBrandFilter(null)
setTypeFilter(null)
setNodeFilter(null)
setQuery('')
setVisibleCount(30)
}}
>
清除筛选
</button>
)}
</div>
</div>
{filteredProjects.length === 0 ? (
<div role="status" className="mt-5 rounded-xl border border-slate-200 bg-white px-5 py-12 text-center">
<Search className="mx-auto h-7 w-7 text-slate-400" aria-hidden="true" />
<p className="mt-3 font-bold text-slate-900">当前条件下没有匹配项目</p>
<p className="mt-1 text-sm text-slate-500">请切换节点、任务类型或清除搜索条件。</p>
</div>
) : (
<>
<div className="mt-5">
<WorkflowDetailTable
items={displayedProjects}
tableLabel="进行中项目流程明细横向滚动表格"
mobileListLabel="进行中项目任务明细"
tasksForItem={tasksForSelectedOwner}
/>
</div>
{displayedProjects.length < filteredProjects.length && (
<div className="mt-5 text-center">
<button
type="button"
className="min-h-11 rounded-lg border border-slate-300 bg-white px-5 text-sm font-bold text-slate-700 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6]"
onClick={() => setVisibleCount((count) => count + 30)}
>
加载更多(剩余 {filteredProjects.length - displayedProjects.length})
</button>
</div>
)}
</>
)}
</div>
)}
</section>
)
}