fix: 优化留言和分享数据

This commit is contained in:
Daniel
2026-03-02 19:07:51 +08:00
parent 13ca470cad
commit 3c55485648
11 changed files with 76 additions and 36 deletions

View File

@@ -25,6 +25,8 @@ export function HeaderPanel() {
const [liked, setLiked] = useState(false)
const [viewers, setViewers] = useState(0)
const [cumulative, setCumulative] = useState(0)
const [feedbackCount, setFeedbackCount] = useState(0)
const [shareCount, setShareCount] = useState(0)
const [feedbackOpen, setFeedbackOpen] = useState(false)
const [feedbackText, setFeedbackText] = useState('')
const [feedbackSending, setFeedbackSending] = useState(false)
@@ -41,6 +43,8 @@ export function HeaderPanel() {
const data = await res.json()
if (data.viewers != null) setViewers(data.viewers)
if (data.cumulative != null) setCumulative(data.cumulative)
if (data.feedbackCount != null) setFeedbackCount(data.feedbackCount)
if (data.shareCount != null) setShareCount(data.shareCount)
} catch {
setViewers((v) => (v > 0 ? v : 0))
setCumulative((c) => (c > 0 ? c : 0))
@@ -56,16 +60,27 @@ export function HeaderPanel() {
const handleShare = async () => {
const url = window.location.href
const title = '美伊军事态势显示'
let shared = false
if (typeof navigator.share === 'function') {
try {
await navigator.share({ title, url })
shared = true
} catch (e) {
if ((e as Error).name !== 'AbortError') {
await copyToClipboard(url)
shared = true
}
}
} else {
await copyToClipboard(url)
shared = true
}
if (shared) {
try {
const res = await fetch('/api/share', { method: 'POST' })
const data = await res.json()
if (data.shareCount != null) setShareCount(data.shareCount)
} catch {}
}
}
@@ -87,6 +102,7 @@ export function HeaderPanel() {
if (data.ok) {
setFeedbackText('')
setFeedbackDone(true)
setFeedbackCount((c) => c + 1)
setTimeout(() => {
setFeedbackOpen(false)
setFeedbackDone(false)
@@ -163,7 +179,7 @@ export function HeaderPanel() {
className="flex shrink-0 items-center gap-1 rounded border border-military-border px-1.5 py-0.5 text-[9px] text-military-text-secondary hover:bg-military-border/30 hover:text-cyan-400 sm:px-2 sm:py-1 sm:text-[10px]"
>
<MessageSquare className="h-2.5 w-2.5 sm:h-3 sm:w-3" />
{feedbackCount > 0 && <span className="tabular-nums">{feedbackCount}</span>}
</button>
<button
type="button"
@@ -171,7 +187,7 @@ export function HeaderPanel() {
className="flex shrink-0 items-center gap-1 rounded border border-military-border px-1.5 py-0.5 text-[9px] text-military-text-secondary hover:bg-military-border/30 hover:text-cyan-400 sm:px-2 sm:py-1 sm:text-[10px]"
>
<Share2 className="h-2.5 w-2.5 sm:h-3 sm:w-3" />
{shareCount > 0 && <span className="tabular-nums">{shareCount}</span>}
</button>
<button
type="button"