tz / kkndme /frontend /src /components /forum /ThreadView.tsx
Ethscriptions's picture
Add kkndme 论房价 project source (in kkndme/)
95ab2cb verified
Raw History Blame Contribute Delete
9.25 kB
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 (
<div style={{ color: C.text, fontSize: size, lineHeight: 1.9 }}>
{t.map((x, i) => (
<p key={i} className="mb-1.5 break-words last:mb-0">
<Highlight text={sani(x)} kw={kw} />
</p>
))}
</div>
)
}
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 (
<PostFrame
id={`fl-${f.n}`}
user="kkndme"
op
time={`发表于 ${f.dd}`}
floorLabel={label}
hl={hl}
right={
IS_MINI ? undefined : (
<button
className="hidden sm:inline"
style={{ color: C.link }}
onClick={() => {
const url = `${location.origin}${location.pathname}${location.search}#bbs/thread/f${f.n}`
navigator.clipboard?.writeText(url).catch(() => {})
}}
title="复制本楼链接"
>
分享
</button>
)
}
>
{shown.length > 0 && (
<div className="mb-3 overflow-hidden rounded-[3px]" style={{ border: `1px solid ${C.quoteBorder}`, background: C.quote }}>
{shown.map((x, i) => {
const op = x.u === 'kkndme'
return (
<div key={i} className="px-3 py-2" style={{ borderTop: i ? `1px dashed ${C.quoteBorder}` : undefined }}>
<div className="mb-1 flex flex-wrap items-center gap-x-1.5 gap-y-0.5 text-[12px]" style={{ color: C.sub }}>
<Avatar name={x.u || '网友'} op={op} size={16} />
{x.u ? (
<>
<span>作者:</span>
<span className="font-bold" style={{ color: C.link }}>
{sani(x.u)}
</span>
</>
) : (
<span className="font-bold" style={{ color: C.link }}>
网友提问
</span>
)}
{op ? (
<span className="rounded-[2px] px-1 text-[10px] font-bold leading-[15px] text-white" style={{ background: C.op }}>
楼主
</span>
) : (
<span className="rounded-[2px] px-1 text-[10px] leading-[15px]" style={{ border: `1px solid ${C.quoteBorder}`, color: C.sub }}>
{x.u ? '网友' : '原帖未署名'}
</span>
)}
{x.d && <span>回复日期:{x.d}</span>}
</div>
{x.t.length > 0 ? (
<SegText t={x.t} kw={kw} size={13.5} />
) : (
<div className="text-[12px] italic" style={{ color: '#a59d8c' }}>
(原文此处未保留回复内容)
</div>
)}
</div>
)
})}
</div>
)}
{shown.length > 0 && (
<div className="mb-2 text-[12px] tracking-[-1px]" style={{ color: '#b9ae97' }}>
————————————————
</div>
)}
{main.map((x, i) => (
<div key={i}>
{i > 0 && (
<div className="my-2 text-[12px] tracking-[-1px]" style={{ color: '#b9ae97' }}>
————————————————
</div>
)}
<SegText t={x.t} kw={kw} size={15} />
</div>
))}
<RefChips refs={refsByFloor.get(f.n)} />
<div className="mt-2 flex justify-between gap-2 text-[11px]" style={{ color: '#a59d8c' }}>
<span>{others > 0 ? `本楼含 ${others} 条网友回复` : ''}</span>
<span>原帖 PDF 第 {f.p} 页</span>
</div>
</PostFrame>
)
}
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<number | null>(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 <Loading err={err} />
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 (
<div id="thread-top" data-page={page} className="scroll-mt-20">
<div {...barAttr} className={`${BAR_CLS} flex flex-col gap-2 border-b px-3 py-2 md:px-4`} style={{ background: '#f8f5ee', borderColor: C.line }}>
<div className="flex gap-2">
<SearchBox value={kwIn} onChange={setKwIn} placeholder="搜本帖:户口、昆明、房产税、通胀……" />
<form
className="flex shrink-0 gap-1"
onSubmit={(e) => {
e.preventDefault()
const n = parseInt(jump, 10)
if (!isNaN(n)) go('thread', `f${n}`)
}}
>
<input
value={jump}
onChange={(e) => setJump(e.target.value.replace(/\D/g, ''))}
inputMode="numeric"
placeholder="楼层"
className="h-[32px] w-[58px] rounded-[3px] px-2 text-[13px] outline-none"
style={{ border: `1px solid ${C.barBorder}`, background: '#fff', color: C.text }}
/>
<button className="h-[32px] rounded-[3px] px-2 text-[13px] font-semibold text-white" style={{ background: C.head }}>
跳楼
</button>
</form>
</div>
<div className="flex flex-wrap items-center gap-2">
<Toggle on={onlyOp} onClick={() => { setOnlyOp((v) => !v); go('thread', 'p1') }}>
只看楼主
</Toggle>
<Toggle on={onlyRef} onClick={() => { setOnlyRef((v) => !v); go('thread', 'p1') }}>
只看被核对的楼
</Toggle>
</div>
<div className="text-[12px] leading-[18px]" style={{ color: C.sub }}>
共 {list.length} 楼{!onlyOp && netizen > 0 ? ` · 含 ${netizen} 条网友回复` : ''} · 第 {page}/{pages} 页
</div>
</div>
<Pager page={page} total={pages} onGo={gotoPage} />
<div className="border-t" style={{ borderColor: C.line }}>
{slice.map((f) => (
<FloorPost key={f.n} f={f} kw={kw} onlyOp={onlyOp} hl={hlFloor === f.n} />
))}
{slice.length === 0 && (
<div className="py-12 text-center text-sm" style={{ color: C.sub }}>
没有找到相关楼层,换个关键词试试
</div>
)}
</div>
<Pager page={page} total={pages} onGo={gotoPage} />
</div>
)
}