336 lines
15 KiB
TypeScript
336 lines
15 KiB
TypeScript
'use client'
|
|
|
|
import Image from 'next/image'
|
|
import { useRouter } from 'next/navigation'
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import {
|
|
ArrowLeft,
|
|
BookOpen,
|
|
Download,
|
|
FileText,
|
|
Heart,
|
|
Share2,
|
|
ThumbsUp,
|
|
} from 'lucide-react'
|
|
import DepartmentShell from './DepartmentShell'
|
|
|
|
type BrandKnowledgeItem = {
|
|
id: string
|
|
title: string
|
|
knowledgeType: string
|
|
brand: string
|
|
productCategoryName: string
|
|
fileName: string
|
|
mimeType: string
|
|
fileSize: number
|
|
downloadCount: number
|
|
createdAt: string
|
|
uploadedBy: string
|
|
downloadUrl: string
|
|
previewUrl: string | null
|
|
previewUrls: string[]
|
|
previewImageName: string | null
|
|
likeCount: number
|
|
isLiked: boolean
|
|
isFavorited: boolean
|
|
}
|
|
|
|
function formatFileSize(size: number) {
|
|
if (size < 1024) return `${size} B`
|
|
if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`
|
|
if (size < 1024 * 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(2)} MB`
|
|
return `${(size / (1024 * 1024 * 1024)).toFixed(2)} GB`
|
|
}
|
|
|
|
function formatDateTime(value: string) {
|
|
const date = new Date(value)
|
|
if (Number.isNaN(date.getTime())) return value
|
|
return new Intl.DateTimeFormat('zh-CN', {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false,
|
|
}).format(date).replaceAll('/', '-')
|
|
}
|
|
|
|
async function copyText(value: string) {
|
|
try {
|
|
await navigator.clipboard.writeText(value)
|
|
return true
|
|
} catch {
|
|
const textarea = document.createElement('textarea')
|
|
textarea.value = value
|
|
textarea.style.position = 'fixed'
|
|
textarea.style.left = '-999999px'
|
|
document.body.appendChild(textarea)
|
|
textarea.select()
|
|
try {
|
|
return document.execCommand('copy')
|
|
} catch {
|
|
return false
|
|
} finally {
|
|
document.body.removeChild(textarea)
|
|
}
|
|
}
|
|
}
|
|
|
|
function DetailAction({
|
|
active,
|
|
disabled = false,
|
|
label,
|
|
onClick,
|
|
children,
|
|
}: {
|
|
active?: boolean
|
|
disabled?: boolean
|
|
label: string
|
|
onClick: () => void
|
|
children: React.ReactNode
|
|
}) {
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={onClick}
|
|
disabled={disabled}
|
|
aria-pressed={typeof active === 'boolean' ? active : undefined}
|
|
className={`inline-flex min-h-11 items-center justify-center gap-2 rounded-lg border px-3.5 text-sm font-semibold transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1677ff] focus-visible:ring-offset-2 disabled:cursor-wait disabled:opacity-60 ${active ? 'border-[#1a1c1d] bg-[#1a1c1d] text-white' : 'border-[#dfe4ea] bg-white text-[#1a1c1d] hover:border-[#1a1c1d]'}`}
|
|
>
|
|
{children}
|
|
{label}
|
|
</button>
|
|
)
|
|
}
|
|
|
|
export default function BrandKnowledgeDetail({ knowledgeId }: { knowledgeId: string }) {
|
|
const router = useRouter()
|
|
const [item, setItem] = useState<BrandKnowledgeItem | null>(null)
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [error, setError] = useState('')
|
|
const [notice, setNotice] = useState('')
|
|
const [busyAction, setBusyAction] = useState<'like' | 'favorite' | 'share' | null>(null)
|
|
|
|
const loadItem = useCallback(async () => {
|
|
setIsLoading(true)
|
|
setError('')
|
|
try {
|
|
const response = await fetch(`/api/department-dashboard/brand-knowledge/${encodeURIComponent(knowledgeId)}`, { cache: 'no-store' })
|
|
const payload = await response.json().catch(() => null)
|
|
if (!response.ok || !payload?.success) throw new Error(payload?.error || '品牌知识详情读取失败')
|
|
setItem(payload.data)
|
|
} catch (loadError) {
|
|
setError(loadError instanceof Error ? loadError.message : '品牌知识详情读取失败,请稍后重试')
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}, [knowledgeId])
|
|
|
|
useEffect(() => { void loadItem() }, [loadItem])
|
|
|
|
const goBack = () => {
|
|
if (window.history.length > 1) {
|
|
router.back()
|
|
return
|
|
}
|
|
router.replace('/workspaces/assets/brand-knowledge')
|
|
}
|
|
|
|
const share = async () => {
|
|
if (!item || busyAction) return
|
|
setBusyAction('share')
|
|
const succeeded = await copyText(window.location.href)
|
|
setNotice(succeeded ? '分享链接已复制' : '分享链接复制失败,请手动复制')
|
|
setBusyAction(null)
|
|
}
|
|
|
|
const setEngagement = async (action: 'like' | 'favorite') => {
|
|
if (!item || busyAction) return
|
|
const active = action === 'like' ? !item.isLiked : !item.isFavorited
|
|
setBusyAction(action)
|
|
try {
|
|
const response = await fetch(`/api/department-dashboard/brand-knowledge/${encodeURIComponent(item.id)}/${action}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ active }),
|
|
})
|
|
const payload = await response.json().catch(() => null)
|
|
if (!response.ok || !payload?.success) throw new Error(payload?.error || '操作失败,请稍后重试')
|
|
setItem((current) => current ? {
|
|
...current,
|
|
...(action === 'like'
|
|
? { isLiked: payload.liked, likeCount: payload.likeCount }
|
|
: { isFavorited: payload.isFavorited }),
|
|
} : current)
|
|
setNotice(action === 'like'
|
|
? (active ? '点赞成功' : '已取消点赞')
|
|
: (active ? '收藏成功' : '已取消收藏'))
|
|
} catch (actionError) {
|
|
setNotice(actionError instanceof Error ? actionError.message : '操作失败,请稍后重试')
|
|
} finally {
|
|
setBusyAction(null)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<DepartmentShell>
|
|
<main className="min-h-[calc(100dvh-72px)] bg-[#f2f4f7] text-[#101828]">
|
|
<div className="mx-auto max-w-[1360px] px-4 pb-14 pt-5 sm:px-6 sm:pt-7 lg:px-8">
|
|
{isLoading ? (
|
|
<div className="animate-pulse" aria-label="正在加载品牌知识详情" aria-busy="true">
|
|
<div className="h-20 border-b border-[#dfe4ea]" />
|
|
<div className="mt-7 grid gap-6 lg:grid-cols-[minmax(0,1fr)_21rem]">
|
|
<div className="aspect-[4/3] rounded-xl bg-[#e4e8ed]" />
|
|
<div className="h-[540px] rounded-xl bg-[#e4e8ed]" />
|
|
</div>
|
|
</div>
|
|
) : error || !item ? (
|
|
<section className="py-24 text-center" aria-live="polite">
|
|
<BookOpen className="mx-auto h-9 w-9 text-[#aab5c3]" aria-hidden="true" />
|
|
<h1 className="mt-4 text-xl font-bold text-[#101828]">无法打开品牌知识</h1>
|
|
<p className="mt-2 text-sm text-[#596579]">{error || '知识内容不存在'}</p>
|
|
<button
|
|
type="button"
|
|
onClick={goBack}
|
|
className="mt-6 inline-flex min-h-11 items-center gap-2 rounded-lg border border-[#dfe4ea] bg-white px-4 text-sm font-semibold text-[#101828] hover:border-[#101828] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1677ff] focus-visible:ring-offset-2"
|
|
>
|
|
<ArrowLeft className="h-4 w-4" aria-hidden="true" />
|
|
返回品牌知识库
|
|
</button>
|
|
</section>
|
|
) : (
|
|
<>
|
|
<header className="border-b border-[#dfe4ea] pb-5">
|
|
<div className="flex flex-col gap-4 xl:flex-row xl:items-center">
|
|
<div className="flex min-w-0 flex-1 items-center gap-3">
|
|
<button
|
|
type="button"
|
|
onClick={goBack}
|
|
aria-label="返回品牌知识库"
|
|
className="inline-flex h-11 w-11 shrink-0 items-center justify-center rounded-lg border border-[#dfe4ea] bg-white text-[#101828] transition-colors hover:border-[#101828] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1677ff] focus-visible:ring-offset-2"
|
|
>
|
|
<ArrowLeft className="h-5 w-5" aria-hidden="true" />
|
|
</button>
|
|
<div className="min-w-0">
|
|
<h1 className="truncate text-xl font-bold text-[#101828] sm:text-2xl" title={item.title}>{item.title}</h1>
|
|
<p className="mt-1 flex min-w-0 items-center gap-2 text-xs text-[#596579]">
|
|
<span className="shrink-0">{item.knowledgeType}</span>
|
|
<span aria-hidden="true">·</span>
|
|
<span className="truncate font-en tabular-nums" title={item.id}>ID {item.id}</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="grid grid-cols-3 gap-2 sm:flex sm:flex-wrap xl:justify-end">
|
|
<DetailAction
|
|
active={item.isFavorited}
|
|
disabled={busyAction !== null}
|
|
label={item.isFavorited ? '已收藏' : '收藏'}
|
|
onClick={() => void setEngagement('favorite')}
|
|
>
|
|
<Heart className="h-4 w-4" fill={item.isFavorited ? 'currentColor' : 'none'} aria-hidden="true" />
|
|
</DetailAction>
|
|
<DetailAction
|
|
active={item.isLiked}
|
|
disabled={busyAction !== null}
|
|
label={item.isLiked ? '已点赞' : '点赞'}
|
|
onClick={() => void setEngagement('like')}
|
|
>
|
|
<ThumbsUp className="h-4 w-4" fill={item.isLiked ? 'currentColor' : 'none'} aria-hidden="true" />
|
|
</DetailAction>
|
|
<DetailAction
|
|
disabled={busyAction !== null}
|
|
label={busyAction === 'share' ? '复制中' : '分享'}
|
|
onClick={() => void share()}
|
|
>
|
|
<Share2 className="h-4 w-4" aria-hidden="true" />
|
|
</DetailAction>
|
|
</div>
|
|
</div>
|
|
<p className="mt-3 min-h-5 text-sm text-[#087052]" role="status" aria-live="polite">{notice}</p>
|
|
</header>
|
|
|
|
<div className="mt-7 grid items-start gap-6 lg:grid-cols-[minmax(0,1fr)_21rem] lg:gap-8">
|
|
<section aria-label="知识预览" className="min-w-0 overflow-hidden rounded-xl border border-[#dfe4ea] bg-white">
|
|
<div className="w-full">
|
|
{(item.previewUrls?.length ? item.previewUrls : item.previewUrl ? [item.previewUrl] : []).length > 0 ? (
|
|
<div className="w-full bg-white" data-testid="brand-knowledge-preview-slices">
|
|
{(item.previewUrls?.length ? item.previewUrls : [item.previewUrl as string]).map((previewUrl, index, previewUrls) => (
|
|
<Image
|
|
key={previewUrl}
|
|
src={previewUrl}
|
|
alt={previewUrls.length > 1 ? `预览图:${item.title}(第 ${index + 1} 片)` : `预览图:${item.title}`}
|
|
width={1600}
|
|
height={1500}
|
|
priority={index === 0}
|
|
unoptimized
|
|
className="block h-auto w-full"
|
|
sizes="(max-width: 1023px) 100vw, 70vw"
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="flex h-full min-h-[320px] flex-col items-center justify-center gap-3 bg-[#f8fafc] px-6 text-center text-[#596579]">
|
|
<FileText className="h-10 w-10" aria-hidden="true" />
|
|
<p className="text-sm font-semibold">此知识内容暂无预览图</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</section>
|
|
|
|
<aside className="rounded-xl border border-[#dfe4ea] bg-white p-5 sm:p-6 lg:sticky lg:top-6" aria-label="知识文件信息">
|
|
<h2 className="break-words text-xl font-bold text-[#101828]">{item.title}</h2>
|
|
<div className="mt-3 flex flex-wrap gap-2">
|
|
{[item.brand, item.productCategoryName, item.knowledgeType].filter(Boolean).map((value) => (
|
|
<span key={value} className="rounded-md bg-[#eef2f6] px-2.5 py-1 text-xs font-semibold text-[#4f6077]">{value}</span>
|
|
))}
|
|
</div>
|
|
|
|
<dl className="mt-7 grid grid-cols-2 gap-x-5 gap-y-6">
|
|
<div>
|
|
<dt className="text-xs text-[#596579]">上传人</dt>
|
|
<dd className="mt-1 break-words text-sm font-semibold text-[#101828]">{item.uploadedBy}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-xs text-[#596579]">文件大小</dt>
|
|
<dd className="mt-1 text-sm font-semibold tabular-nums text-[#101828]">{formatFileSize(item.fileSize)}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-xs text-[#596579]">下载次数</dt>
|
|
<dd className="mt-1 text-sm font-semibold tabular-nums text-[#101828]">{item.downloadCount}</dd>
|
|
</div>
|
|
<div>
|
|
<dt className="text-xs text-[#596579]">点赞数</dt>
|
|
<dd className="mt-1 text-sm font-semibold tabular-nums text-[#101828]">{item.likeCount}</dd>
|
|
</div>
|
|
<div className="col-span-2">
|
|
<dt className="text-xs text-[#596579]">文件名</dt>
|
|
<dd className="mt-1 break-all text-sm font-semibold leading-6 text-[#101828]">{item.fileName}</dd>
|
|
</div>
|
|
<div className="col-span-2">
|
|
<dt className="text-xs text-[#596579]">上传时间</dt>
|
|
<dd className="mt-1 text-sm font-semibold tabular-nums text-[#101828]">{formatDateTime(item.createdAt)}</dd>
|
|
</div>
|
|
<div className="col-span-2">
|
|
<dt className="text-xs text-[#596579]">作品 ID</dt>
|
|
<dd className="mt-1 break-all font-en text-xs leading-5 text-[#4f6077]">{item.id}</dd>
|
|
</div>
|
|
</dl>
|
|
|
|
<a
|
|
href={item.downloadUrl}
|
|
className="mt-8 inline-flex min-h-12 w-full items-center justify-center gap-2 rounded-lg bg-[#1a1c1d] px-4 text-sm font-bold text-white transition-colors hover:bg-[#2a2d2f] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#1677ff] focus-visible:ring-offset-2"
|
|
aria-label={`立即下载${item.title}`}
|
|
>
|
|
<Download className="h-5 w-5" aria-hidden="true" />
|
|
立即下载
|
|
</a>
|
|
</aside>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
</main>
|
|
</DepartmentShell>
|
|
)
|
|
}
|