gongxiangp2/components/department-dashboard/PerformanceSalaryDashboard.tsx

134 lines
10 KiB
TypeScript

'use client'
import Link from 'next/link'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import { ChevronDown, ListChecks, Settings2 } from 'lucide-react'
import { SALARY_ROLES, type SalaryRole } from '@/lib/performance-salary-roles'
import { currentSalaryMonth, salaryDetailsHref, SALARY_PATH } from '@/lib/performance-salary-templates'
import DepartmentShell from './DepartmentShell'
import PersonnelSalaryPanel from './PersonnelSalaryPanel'
import PerformanceSalaryDetails from './PerformanceSalaryDetails'
import { SalaryConfigurationProvider, useSalaryConfiguration } from './SalaryConfigurationContext'
import SalaryPositionPanel from './SalaryPositionPanel'
import { positionConfigurationHref, personnelPositions, performancePositions, positionAtMonth } from '@/lib/salary-position-contract'
const navigationItemClass = (active: boolean) => `flex min-h-11 min-w-0 items-center gap-3 rounded-lg px-3 py-3 text-sm font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] ${active ? 'bg-[#e6f4ff] text-[#0050cb]' : 'text-[#4f6077] hover:bg-[#eef2f6] hover:text-[#101828]'}`
// Operate: URL-backed role navigation within the existing salary workbench.
type Props = { view?: 'details' | 'configuration'; role?: SalaryRole; month?: string; config?: 'personnel' | 'performance'; position?: string }
export default function PerformanceSalaryDashboard(props: Props) {
return <SalaryConfigurationProvider month={props.month}><SalaryWorkbench {...props} /></SalaryConfigurationProvider>
}
// Operate: preserve the workbench; job ownership and weighting live beside personnel configuration.
function SalaryWorkbench({ view = 'details', role = 'ai-planning', month = currentSalaryMonth(), config = 'personnel', position }: Props) {
const { payload, loading, error } = useSalaryConfiguration()
const router = useRouter()
const [expandedConfigurations, setExpandedConfigurations] = useState({
personnel: view === 'configuration' && config === 'personnel',
performance: view === 'configuration' && config === 'performance',
})
useEffect(() => {
if (view === 'configuration') {
setExpandedConfigurations(previous => previous[config] ? previous : { ...previous, [config]: true })
}
}, [view, config, position])
const toggleConfiguration = (section: 'personnel' | 'performance') => {
setExpandedConfigurations(previous => ({ ...previous, [section]: !previous[section] }))
}
const personnel = personnelPositions(payload?.positions ?? [])
const schemes = performancePositions(payload?.positions.map(item => positionAtMonth(item, month)) ?? [])
const wanted = payload?.positions.find(item => item.id === position)
const performancePosition = schemes.find(item => item.id === position)
|| (wanted && schemes.find(item => item.id === wanted.personnelPositionId || item.personnelPositionId === wanted.id))
|| schemes[0]
useEffect(() => {
if (view !== 'configuration' || config !== 'personnel' || !position || position === 'unassigned' || !payload || loading || error) return
const selected = payload.positions.find(item => item.id === position)
if (!selected || selected.personnelPositionId) router.replace(positionConfigurationHref('personnel', month, selected?.personnelPositionId ?? undefined), { scroll: false })
}, [view, config, position, payload, loading, error, month, router])
useEffect(() => {
if (view === 'configuration' && config === 'performance' && payload && !loading && !error && position !== performancePosition?.id) {
router.replace(positionConfigurationHref('performance', month, performancePosition?.id), { scroll: false })
}
}, [view, config, payload, loading, error, position, performancePosition?.id, month, router])
const activeRole = SALARY_ROLES.find((item) => item.key === role)!
const title = view === 'configuration' ? config === 'performance' ? '岗位绩效配置' : '人员薪资配置' : `${activeRole.title} · 薪资明细`
return (
<DepartmentShell>
<main className="flex min-h-[calc(100dvh-72px)] flex-col bg-white lg:grid lg:grid-cols-[232px_minmax(0,1fr)]" aria-label="绩效薪资">
<aside className="shrink-0 border-b border-[#e2e8f0] bg-[#f8fafc] px-4 py-4 lg:border-b-0 lg:border-r lg:py-6">
<nav aria-label="绩效薪资二级导航" className="space-y-5">
<div>
<p id="salary-role-nav-title" className="mb-2 flex items-center gap-3 px-3 py-2 text-sm font-semibold text-[#4f6077]">
<ListChecks className="h-4 w-4 shrink-0" aria-hidden="true" />
薪资明细
</p>
<ul aria-labelledby="salary-role-nav-title" className="grid grid-cols-2 gap-2 lg:ml-3 lg:grid-cols-1 lg:gap-1">
{SALARY_ROLES.map((item) => {
const active = view === 'details' && item.key === role
return (
<li key={item.key} className="min-w-0">
<Link
href={salaryDetailsHref(item.key, month)}
scroll={false}
aria-current={active ? 'page' : undefined}
className={navigationItemClass(active)}
>
{item.title}
</Link>
</li>
)
})}
</ul>
</div>
<div className="border-t border-[#e2e8f0] pt-4">
<Link
href={`${SALARY_PATH}?${new URLSearchParams({ view: 'configuration', month })}`}
scroll={false}
aria-current={view === 'configuration' ? 'page' : undefined}
className={navigationItemClass(view === 'configuration')}
>
<Settings2 className="h-4 w-4 shrink-0" aria-hidden="true" />
后台配置
</Link>
<ul className="mt-2 space-y-1 lg:ml-3">
<li>
<button type="button" aria-expanded={expandedConfigurations.personnel} aria-controls="salary-personnel-navigation" onClick={() => toggleConfiguration('personnel')} className={`${navigationItemClass(view === 'configuration' && config === 'personnel')} w-full justify-between text-left`}>
<span className="min-w-0 break-words">人员薪资配置</span>
<ChevronDown className={`h-4 w-4 shrink-0 ${expandedConfigurations.personnel ? 'rotate-180' : ''}`} aria-hidden="true" />
</button>
<ul id="salary-personnel-navigation" aria-label="人员薪资配置岗位" hidden={!expandedConfigurations.personnel} className={`${expandedConfigurations.personnel ? 'grid' : 'hidden'} grid-cols-2 gap-1 pl-3 lg:grid-cols-1`}>
<li><Link href={positionConfigurationHref('personnel', month)} scroll={false} aria-current={view === 'configuration' && config === 'personnel' && !position ? 'page' : undefined} className={navigationItemClass(view === 'configuration' && config === 'personnel' && !position)}>全部人员</Link></li>
{personnel.map(item => <li className="min-w-0" key={item.id}><Link href={positionConfigurationHref('personnel', month, item.id)} scroll={false} aria-current={view === 'configuration' && config === 'personnel' && position === item.id ? 'page' : undefined} className={`${navigationItemClass(view === 'configuration' && config === 'personnel' && position === item.id)} justify-between text-xs`}><span className="min-w-0 break-words">{item.name}</span><span className="shrink-0 tabular-nums">{item.personnelCount}</span></Link></li>)}
<li><Link href={positionConfigurationHref('personnel', month, 'unassigned')} scroll={false} aria-current={view === 'configuration' && config === 'personnel' && position === 'unassigned' ? 'page' : undefined} className={navigationItemClass(view === 'configuration' && config === 'personnel' && position === 'unassigned')}>未分配</Link></li>
</ul>
</li>
<li>
<button type="button" aria-expanded={expandedConfigurations.performance} aria-controls="salary-performance-navigation" onClick={() => toggleConfiguration('performance')} className={`${navigationItemClass(view === 'configuration' && config === 'performance')} w-full justify-between text-left`}>
<span className="min-w-0 break-words">岗位绩效配置</span>
<ChevronDown className={`h-4 w-4 shrink-0 ${expandedConfigurations.performance ? 'rotate-180' : ''}`} aria-hidden="true" />
</button>
<ul id="salary-performance-navigation" aria-label="岗位绩效配置岗位" hidden={!expandedConfigurations.performance} className={`${expandedConfigurations.performance ? 'grid' : 'hidden'} grid-cols-2 gap-1 pl-3 lg:grid-cols-1`}>
{schemes.map(item => {
const active = view === 'configuration' && config === 'performance' && performancePosition?.id === item.id
return <li className="min-w-0" key={item.id}><Link href={positionConfigurationHref('performance', month, item.id)} scroll={false} aria-current={active ? 'page' : undefined} className={`${navigationItemClass(active)} text-xs`}><span className="min-w-0 break-words">{item.name}</span></Link></li>
})}
</ul>
</li>
</ul>
</div>
</nav>
</aside>
<section aria-labelledby="salary-view-title" className="min-w-0">
<header className="border-b border-[#e2e8f0] px-5 py-5 sm:px-6">
<h1 id="salary-view-title" className="text-xl font-bold leading-8 text-[#101828]">{title}</h1>
</header>
{view === 'configuration' ? config === 'performance' ? <SalaryPositionPanel selectedId={performancePosition?.id} month={month} /> : <PersonnelSalaryPanel selectedPosition={position} month={month} /> : <PerformanceSalaryDetails role={role} month={month} />}
</section>
</main>
</DepartmentShell>
)
}