Ship portal module + /api/social/v1 SQLite backend separate from /community; update README for MiroTalk CSP and test5 as the next development branch. Co-authored-by: Cursor <cursoragent@cursor.com>
191 lines
5.9 KiB
TypeScript
191 lines
5.9 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useState } from "react";
|
||
import SiteShell from "./SiteShell";
|
||
import { socialApi } from "@social/lib/api";
|
||
import { SEED_CAFES } from "@social/lib/seed";
|
||
import { loadJson, saveJson } from "@social/lib/storage";
|
||
import type { SocialCafeReview } from "@social/lib/types";
|
||
|
||
export default function CoffeechatClient() {
|
||
const [cafeId, setCafeId] = useState(SEED_CAFES[0]?.id || "");
|
||
const [reviews, setReviews] = useState<SocialCafeReview[]>([]);
|
||
const [author, setAuthor] = useState("游民");
|
||
const [score, setScore] = useState(5);
|
||
const [text, setText] = useState("");
|
||
const [photoNote, setPhotoNote] = useState("");
|
||
const [online, setOnline] = useState(true);
|
||
const [busy, setBusy] = useState(false);
|
||
|
||
const cafe = SEED_CAFES.find((c) => c.id === cafeId) || SEED_CAFES[0];
|
||
const cacheKey = `cafe-reviews:${cafeId}`;
|
||
|
||
const refresh = useCallback(async () => {
|
||
if (!cafeId) return;
|
||
try {
|
||
const res = await socialApi.listReviews(cafeId);
|
||
const items = (res.items || []).map((r) => ({
|
||
id: r.id,
|
||
cafeId: r.cafeId,
|
||
author: r.author,
|
||
score: r.score,
|
||
text: r.text,
|
||
photoNote: r.photoNote,
|
||
at: r.at,
|
||
}));
|
||
setReviews(items);
|
||
saveJson(cacheKey, items);
|
||
setOnline(true);
|
||
} catch {
|
||
setOnline(false);
|
||
setReviews(loadJson<SocialCafeReview[]>(cacheKey, []));
|
||
}
|
||
}, [cafeId, cacheKey]);
|
||
|
||
useEffect(() => {
|
||
setAuthor(loadJson("nick", "游民"));
|
||
void refresh();
|
||
}, [refresh]);
|
||
|
||
const submit = async () => {
|
||
if (!cafe || !text.trim() || busy) return;
|
||
setBusy(true);
|
||
const payload = {
|
||
author: author.trim() || "游民",
|
||
score,
|
||
text: text.trim(),
|
||
photoNote: photoNote.trim(),
|
||
};
|
||
try {
|
||
await socialApi.postReview(cafe.id, payload);
|
||
saveJson("nick", payload.author);
|
||
setText("");
|
||
setPhotoNote("");
|
||
setOnline(true);
|
||
await refresh();
|
||
} catch {
|
||
setOnline(false);
|
||
const item: SocialCafeReview = {
|
||
id: `local_${Date.now()}`,
|
||
cafeId: cafe.id,
|
||
...payload,
|
||
at: new Date().toISOString(),
|
||
};
|
||
const next = [item, ...reviews];
|
||
setReviews(next);
|
||
saveJson(cacheKey, next);
|
||
saveJson("nick", payload.author);
|
||
setText("");
|
||
setPhotoNote("");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<SiteShell title="Coffeechat">
|
||
<section className="social-hero">
|
||
<div className="social-hero-tag">线下 · 咖啡馆</div>
|
||
<h1>
|
||
Coffeechat
|
||
<br />
|
||
<span>合作店 · 评价 · 合照备注</span>
|
||
</h1>
|
||
<p>
|
||
评价写入独立 social 库(与旅居社群数据隔离)。
|
||
{online ? " 当前已连后端。" : " 后端不可用时先存本机。"}
|
||
</p>
|
||
</section>
|
||
|
||
<div className="social-grid">
|
||
{SEED_CAFES.map((c) => (
|
||
<button
|
||
key={c.id}
|
||
type="button"
|
||
className={`social-card social-card-btn${c.id === cafeId ? " active" : ""}`}
|
||
onClick={() => setCafeId(c.id)}
|
||
>
|
||
<div className="social-card-top">
|
||
<span className="social-card-ico">☕</span>
|
||
{c.partners ? (
|
||
<span className="social-badge live">合作</span>
|
||
) : (
|
||
<span className="social-badge">收录</span>
|
||
)}
|
||
</div>
|
||
<strong>{c.name}</strong>
|
||
<p>
|
||
{c.city} · WiFi ~{c.wifiMbps}Mbps · ★{c.rating}
|
||
</p>
|
||
</button>
|
||
))}
|
||
</div>
|
||
|
||
{cafe ? (
|
||
<section className="social-panel">
|
||
<h2>{cafe.name}</h2>
|
||
<p className="social-muted">
|
||
{cafe.vibe} · 电源:{cafe.power} · 评价 {reviews.length} 条
|
||
</p>
|
||
|
||
<div className="social-form">
|
||
<label>
|
||
你的名字
|
||
<input value={author} onChange={(e) => setAuthor(e.target.value)} />
|
||
</label>
|
||
<label>
|
||
评分 {score}
|
||
<input
|
||
type="range"
|
||
min={1}
|
||
max={5}
|
||
value={score}
|
||
onChange={(e) => setScore(Number(e.target.value))}
|
||
/>
|
||
</label>
|
||
<label>
|
||
评价
|
||
<textarea
|
||
rows={3}
|
||
value={text}
|
||
onChange={(e) => setText(e.target.value)}
|
||
placeholder="网络、插座、适不适合深聊…"
|
||
/>
|
||
</label>
|
||
<label>
|
||
合照备注
|
||
<input
|
||
value={photoNote}
|
||
onChange={(e) => setPhotoNote(e.target.value)}
|
||
placeholder="例如:门口合照 / 工位视角 jpg"
|
||
/>
|
||
</label>
|
||
<button type="button" className="social-btn primary" onClick={() => void submit()} disabled={busy}>
|
||
{busy ? "提交中…" : "提交评价"}
|
||
</button>
|
||
</div>
|
||
|
||
<div className="social-list" style={{ marginTop: 20 }}>
|
||
{reviews.length === 0 ? (
|
||
<p className="social-muted">还没有评价,来写第一条吧。</p>
|
||
) : (
|
||
reviews.map((r) => (
|
||
<article key={r.id} className="social-list-item social-list-stack">
|
||
<div>
|
||
<strong>
|
||
{r.author} · {"★".repeat(r.score)}
|
||
</strong>
|
||
<p>{r.text}</p>
|
||
{r.photoNote ? <p className="social-muted">合照:{r.photoNote}</p> : null}
|
||
</div>
|
||
<time className="social-muted">{new Date(r.at).toLocaleString()}</time>
|
||
</article>
|
||
))
|
||
)}
|
||
</div>
|
||
</section>
|
||
) : null}
|
||
</SiteShell>
|
||
);
|
||
}
|