's'
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "@/i18n/navigation";
|
||||
import { Link, useTranslation } from "@/i18n/navigation";
|
||||
import Footer from "@/app/components/Footer";
|
||||
import { apiFetch } from "@/app/lib/api-client";
|
||||
import { apiFetch, fetchAuthMe } from "@/app/lib/api-client";
|
||||
import { toggleDiscussionLike } from "@/app/lib/social";
|
||||
import { mediaUrl } from "@/app/lib/media";
|
||||
import { avatarForIndex, avatarForName } from "@/app/data/avatars";
|
||||
|
||||
@@ -19,6 +20,7 @@ interface Topic {
|
||||
lastActive: string;
|
||||
pinned: boolean;
|
||||
excerpt?: string;
|
||||
likedByMe?: boolean;
|
||||
}
|
||||
|
||||
interface DiscussionRecord {
|
||||
@@ -30,6 +32,9 @@ interface DiscussionRecord {
|
||||
excerpt?: string;
|
||||
replies?: number;
|
||||
views?: number;
|
||||
likes?: number;
|
||||
pinned?: boolean;
|
||||
likedByMe?: boolean;
|
||||
created?: string;
|
||||
}
|
||||
|
||||
@@ -143,7 +148,7 @@ const CATEGORIES = [
|
||||
{ key: "目的地", zh: "目的地", en: "Destinations" },
|
||||
];
|
||||
|
||||
function mapDiscussion(topic: DiscussionRecord, index: number): Topic {
|
||||
function mapDiscussion(topic: DiscussionRecord): Topic {
|
||||
const author = topic.author || "NomadCNA";
|
||||
const tags = Array.isArray(topic.tags) ? topic.tags : [];
|
||||
return {
|
||||
@@ -153,11 +158,12 @@ function mapDiscussion(topic: DiscussionRecord, index: number): Topic {
|
||||
avatar: avatarForName(author),
|
||||
views: Number(topic.views || 0),
|
||||
replies: Number(topic.replies || 0),
|
||||
likes: Math.max(3, Math.round(Number(topic.views || 0) / 30) + Number(topic.replies || 0)),
|
||||
likes: Number(topic.likes || 0),
|
||||
category: tags[0] || topic.city || "资源分享",
|
||||
lastActive: topic.created ? new Date(topic.created).toLocaleDateString("zh-CN") : "刚刚",
|
||||
pinned: index < 2,
|
||||
pinned: Boolean(topic.pinned),
|
||||
excerpt: topic.excerpt,
|
||||
likedByMe: Boolean(topic.likedByMe),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -169,6 +175,12 @@ export default function DiscussPage() {
|
||||
const [showComposer, setShowComposer] = useState(false);
|
||||
const [posting, setPosting] = useState(false);
|
||||
const [draft, setDraft] = useState({ title: "", city: "", excerpt: "" });
|
||||
const [isLoggedIn, setIsLoggedIn] = useState(false);
|
||||
const [likingId, setLikingId] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetchAuthMe().then((data) => setIsLoggedIn(Boolean(data?.user?.id)));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
apiFetch<{ items: DiscussionRecord[] }>("/api/discussions")
|
||||
@@ -180,6 +192,25 @@ export default function DiscussPage() {
|
||||
.catch(() => setTopics(FALLBACK_TOPICS));
|
||||
}, []);
|
||||
|
||||
const handleLike = async (topicId: string, event: React.MouseEvent) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (!isLoggedIn) return;
|
||||
setLikingId(topicId);
|
||||
try {
|
||||
const result = await toggleDiscussionLike(topicId);
|
||||
setTopics((current) =>
|
||||
current.map((topic) =>
|
||||
topic.id === topicId
|
||||
? { ...topic, likes: result.likes, likedByMe: result.liked }
|
||||
: topic
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
setLikingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
const filteredTopics = useMemo(() => topics.filter((topic) => {
|
||||
const matchesCategory = activeCategory === "all" || topic.category === activeCategory;
|
||||
const matchesSearch = topic.title.toLowerCase().includes(searchQuery.toLowerCase());
|
||||
@@ -201,7 +232,7 @@ export default function DiscussPage() {
|
||||
excerpt: draft.excerpt.trim(),
|
||||
}),
|
||||
});
|
||||
setTopics((current) => [mapDiscussion(data.record, 0), ...current]);
|
||||
setTopics((current) => [mapDiscussion(data.record), ...current]);
|
||||
setDraft({ title: "", city: "", excerpt: "" });
|
||||
setShowComposer(false);
|
||||
} finally {
|
||||
@@ -292,9 +323,10 @@ export default function DiscussPage() {
|
||||
|
||||
<div className="space-y-3 sm:space-y-4">
|
||||
{filteredTopics.map((topic) => (
|
||||
<div
|
||||
<Link
|
||||
key={topic.id}
|
||||
className="bg-white dark:bg-gray-900 rounded-xl p-4 sm:p-5 shadow-sm border border-gray-100 dark:border-gray-800 hover:shadow-md transition-shadow cursor-pointer"
|
||||
href={`/discuss/${topic.id}`}
|
||||
className="block bg-white dark:bg-gray-900 rounded-xl p-4 sm:p-5 shadow-sm border border-gray-100 dark:border-gray-800 hover:shadow-md transition-shadow"
|
||||
>
|
||||
<div className="flex items-start gap-3 sm:gap-4">
|
||||
<img
|
||||
@@ -324,11 +356,20 @@ export default function DiscussPage() {
|
||||
<span className="hidden sm:inline">·</span>
|
||||
<span>👁 {topic.views}</span>
|
||||
<span>💬 {topic.replies}</span>
|
||||
<span>❤️ {topic.likes}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={(event) => handleLike(topic.id, event)}
|
||||
disabled={likingId === topic.id}
|
||||
className={`inline-flex items-center gap-1 rounded-full px-2 py-0.5 transition ${
|
||||
topic.likedByMe ? "bg-red-50 text-[#ff4d4f] dark:bg-red-950/30" : "hover:bg-gray-100 dark:hover:bg-gray-800"
|
||||
}`}
|
||||
>
|
||||
{topic.likedByMe ? "❤️" : "🤍"} {topic.likes}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user