import { useEffect, useMemo, useRef, useState } from 'react'
import {
Avatar,
C,
type Floor,
Highlight,
Loading,
PAGE_SIZE,
Pager,
PostFrame,
RefChips,
SearchBox,
Toggle,
go,
refsByFloor,
sani,
scrollToId,
useKK, BAR_CLS, barAttr } from './shared'
import { IS_MINI } from './platform'
function SegText({ t, kw, size }: { t: string[]; kw: string; size: number }) {
return (
)
}
function FloorPost({ f, kw, onlyOp, hl }: { f: Floor; kw: string; onlyOp: boolean; hl: boolean }) {
const label = f.n === 0 ? '主帖' : `${f.n}楼`
const segs = f.s
const main = segs.filter((x) => x.main)
const chain = segs.filter((x) => !x.main)
const shown = onlyOp ? chain.filter((x) => x.u === 'kkndme') : chain
const others = chain.filter((x) => x.u !== 'kkndme').length
return (
{
const url = `${location.origin}${location.pathname}${location.search}#bbs/thread/f${f.n}`
navigator.clipboard?.writeText(url).catch(() => {})
}}
title="复制本楼链接"
>
分享
)
}
>
{shown.length > 0 && (
{shown.map((x, i) => {
const op = x.u === 'kkndme'
return (
{x.u ? (
<>
作者:
{sani(x.u)}
>
) : (
网友提问
)}
{op ? (
楼主
) : (
{x.u ? '网友' : '原帖未署名'}
)}
{x.d &&
回复日期:{x.d}}
{x.t.length > 0 ? (
) : (
(原文此处未保留回复内容)
)}
)
})}
)}
{shown.length > 0 && (
————————————————
)}
{main.map((x, i) => (
{i > 0 && (
————————————————
)}
))}
{others > 0 ? `本楼含 ${others} 条网友回复` : ''}
原帖 PDF 第 {f.p} 页
)
}
export default function ThreadView({ arg }: { arg: string }) {
const { data, err } = useKK<{ floors: Floor[] }>('thread.json')
const [onlyOp, setOnlyOp] = useState(false)
const [onlyRef, setOnlyRef] = useState(false)
const [kwIn, setKwIn] = useState('')
const [kw, setKw] = useState('')
const [jump, setJump] = useState('')
const [hlFloor, setHlFloor] = useState(null)
useEffect(() => {
const t = setTimeout(() => setKw(kwIn.trim()), 250)
return () => clearTimeout(t)
}, [kwIn])
const list = useMemo(() => {
if (!data) return []
const q = kw.toLowerCase()
return data.floors.filter((f) => {
if (onlyRef && !refsByFloor.has(f.n)) return false
if (q) {
const s = f.s
.filter((x) => !onlyOp || x.u === 'kkndme')
.map((x) => x.t.join(''))
.join('')
.toLowerCase()
if (!s.includes(q)) return false
}
return true
})
}, [data, onlyOp, onlyRef, kw])
const pages = Math.max(1, Math.ceil(list.length / PAGE_SIZE))
const netizen = useMemo(() => list.reduce((a, f) => a + f.s.filter((x) => x.u !== 'kkndme').length, 0), [list])
// 路由参数:p3 = 第3页;f256 = 跳到256楼
const m = /^([pf])(\d+)$/.exec(arg)
let page = 1
let target: number | null = null
if (m?.[1] === 'p') page = Math.min(pages, Math.max(1, +m[2]))
if (m?.[1] === 'f') {
target = +m[2]
const idx = list.findIndex((f) => f.n >= target!)
page = idx >= 0 ? Math.floor(idx / PAGE_SIZE) + 1 : 1
}
useEffect(() => {
if (target != null && data) {
const idx = list.findIndex((f) => f.n >= target!)
if (idx < 0 && (onlyOp || onlyRef || kw)) {
setOnlyOp(false)
setOnlyRef(false)
setKwIn('')
setKw('')
return
}
const real = list[idx]?.n
if (real != null) {
setHlFloor(real)
scrollToId(`fl-${real}`)
}
}
}, [target, data, list, onlyOp, onlyRef, kw])
// 搜索词变化时回第一页(首次渲染不处理)
const first = useRef(true)
useEffect(() => {
if (first.current) {
first.current = false
return
}
if (kw) go('thread', 'p1')
}, [kw])
if (!data) return
const slice = list.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE)
const gotoPage = (p: number) => {
setHlFloor(null)
go('thread', `p${p}`)
scrollToId('thread-top')
}
return (
{ setOnlyOp((v) => !v); go('thread', 'p1') }}>
只看楼主
{ setOnlyRef((v) => !v); go('thread', 'p1') }}>
只看被核对的楼
共 {list.length} 楼{!onlyOp && netizen > 0 ? ` · 含 ${netizen} 条网友回复` : ''} · 第 {page}/{pages} 页
{slice.map((f) => (
))}
{slice.length === 0 && (
没有找到相关楼层,换个关键词试试
)}
)
}