103 lines
5.7 KiB
TypeScript
103 lines
5.7 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useRef, useState, useTransition } from 'react'
|
|
import { useRouter } from 'next/navigation'
|
|
import { Download } from 'lucide-react'
|
|
import type { SalaryRole } from '@/lib/performance-salary-roles'
|
|
import { isSalaryMonth, salaryDetailsHref, SALARY_PATH } from '@/lib/performance-salary-templates'
|
|
import { salaryDetailsExportFileName, SALARY_XLSX_MIME } from '@/lib/performance-salary-details-contract'
|
|
|
|
export default function PerformanceSalaryToolbar({ role, month, sourceLabel }: { role: SalaryRole; month: string; sourceLabel?: string }) {
|
|
const router = useRouter()
|
|
const [draft, setDraft] = useState(month)
|
|
const [pending, startTransition] = useTransition()
|
|
const [exporting, setExporting] = useState(false)
|
|
const exportRequest = useRef<AbortController | null>(null)
|
|
const [feedback, setFeedback] = useState<{ error: boolean; text: string } | null>(null)
|
|
useEffect(() => {
|
|
setDraft(month)
|
|
setFeedback(null)
|
|
setExporting(false)
|
|
return () => { exportRequest.current?.abort(); exportRequest.current = null }
|
|
}, [month, role])
|
|
|
|
async function exportDetails() {
|
|
if (exportRequest.current) return
|
|
const controller = new AbortController()
|
|
exportRequest.current = controller
|
|
setExporting(true)
|
|
setFeedback(null)
|
|
try {
|
|
const response = await fetch(`/api/organization/performance-salary/details/export?${new URLSearchParams({ month })}`, {
|
|
cache: 'no-store', signal: controller.signal,
|
|
})
|
|
if (!response.ok) {
|
|
const body = await response.json().catch(() => null)
|
|
throw new Error(typeof body?.error === 'string' ? body.error : '薪资明细导出失败,请稍后重试。')
|
|
}
|
|
if (response.headers.get('content-type')?.split(';')[0] !== SALARY_XLSX_MIME) throw new Error('下载响应异常,请重新导出。')
|
|
const blob = await response.blob()
|
|
controller.signal.throwIfAborted()
|
|
if (typeof URL.createObjectURL !== 'function') throw new Error('当前浏览器无法下载文件,请更换浏览器后重试。')
|
|
const url = URL.createObjectURL(blob)
|
|
const anchor = document.createElement('a')
|
|
anchor.href = url
|
|
anchor.download = salaryDetailsExportFileName(month)
|
|
anchor.style.display = 'none'
|
|
document.body.appendChild(anchor)
|
|
try { anchor.click() } finally {
|
|
anchor.remove()
|
|
window.setTimeout(() => URL.revokeObjectURL(url), 1000)
|
|
}
|
|
setFeedback({ error: false, text: '当月全部岗位薪资明细已生成,请在下载中查看。' })
|
|
} catch (error) {
|
|
if (!controller.signal.aborted) setFeedback({ error: true, text: error instanceof Error && !(error instanceof TypeError) ? error.message : '薪资明细导出失败,请稍后重试。' })
|
|
} finally {
|
|
if (exportRequest.current === controller) {
|
|
exportRequest.current = null
|
|
setExporting(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="border-b border-[#e2e8f0] px-5 py-4 sm:px-6">
|
|
<div className="flex flex-wrap items-end justify-between gap-3">
|
|
<form action={SALARY_PATH} method="get" className="flex min-w-0 items-end gap-2" onSubmit={event => {
|
|
event.preventDefault()
|
|
const value = new FormData(event.currentTarget).get('month')
|
|
if (isSalaryMonth(value) && value !== month) {
|
|
exportRequest.current?.abort()
|
|
setDraft(value)
|
|
startTransition(() => router.push(salaryDetailsHref(role, value), { scroll: false }))
|
|
}
|
|
}}>
|
|
<input type="hidden" name="role" value={role} />
|
|
<label className="grid min-w-0 gap-2 text-sm font-semibold text-[#344054]" htmlFor="salary-month">
|
|
薪资月份
|
|
<input
|
|
id="salary-month" name="month" type="month" min="0001-01" max="9999-12" required value={draft} disabled={pending}
|
|
className="h-11 w-44 min-w-0 rounded-lg border border-[#cfd8e3] bg-white px-3 text-sm font-normal text-[#101828] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] disabled:opacity-60"
|
|
onChange={event => setDraft(event.target.value)}
|
|
onBlur={() => { if (!isSalaryMonth(draft)) setDraft(month) }}
|
|
/>
|
|
</label>
|
|
<button type="submit" disabled={pending} className="min-h-11 shrink-0 rounded-lg border border-[#cfd8e3] bg-white px-4 py-2.5 text-sm font-semibold text-[#344054] hover:bg-[#f8fafc] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] disabled:opacity-60">查询</button>
|
|
</form>
|
|
<div className="flex flex-wrap items-center gap-x-3 gap-y-2">
|
|
<p id="salary-export-scope" className="text-sm text-[#66758b]">包含当月全部岗位</p>
|
|
<button
|
|
type="button" onClick={exportDetails} disabled={exporting || pending} aria-busy={exporting} aria-describedby="salary-export-scope"
|
|
className="inline-flex min-h-11 items-center justify-center gap-2 rounded-lg bg-[#0050cb] px-4 py-2.5 text-sm font-semibold text-white hover:bg-[#003da3] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#5a9ce6] focus-visible:ring-offset-2 disabled:cursor-wait disabled:opacity-60"
|
|
>
|
|
<Download className="h-4 w-4 shrink-0" aria-hidden="true" />
|
|
{exporting ? '正在生成…' : '导出薪资明细'}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<p aria-live="polite" className="mt-3 text-sm leading-6 text-[#66758b]">{pending ? '正在切换月份…' : sourceLabel || '正在核对数据来源…'}</p>
|
|
{feedback && <p role={feedback.error ? 'alert' : 'status'} className={`mt-2 text-sm leading-6 ${feedback.error ? 'text-[#b42318]' : 'text-[#344054]'}`}>{feedback.text}</p>}
|
|
</div>
|
|
)
|
|
}
|