feat: add forum seed categories, improve page layouts and navigation

- Seed default forum categories (announcements, learning, feedback) on startup
- Update forum pages with improved category/post/reply UI
- Refine navbar, mobile nav, and home page content layout
- Improve analytics, course-content, knowledge, profile, settings, spatial pages

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 10:02:30 +08:00
parent 45e3da82e1
commit 50faf7ad4e
13 changed files with 592 additions and 389 deletions
+36
View File
@@ -56,6 +56,36 @@ def startup_knowledge_base():
except Exception as e: except Exception as e:
print(f"启动知识库文件监控服务失败: {str(e)}") print(f"启动知识库文件监控服务失败: {str(e)}")
def seed_forum_categories():
"""初始化论坛默认分类"""
from src.core.database import get_db
from src.models.forum import ForumCategory
DEFAULT_CATEGORIES = [
{"slug": "announcements", "name": "课程公告", "description": "课程通知、作业安排与重要信息"},
{"slug": "course-learning", "name": "学习讨论", "description": "交流学习心得,讨论课程内容与难点"},
{"slug": "system-feedback", "name": "使用反馈", "description": "分享系统使用经验,提出改进建议"},
]
db = next(get_db())
try:
existing_count = db.query(ForumCategory).count()
if existing_count > 0:
print(f"论坛分类已存在({existing_count} 个),跳过初始化")
return
for cat_data in DEFAULT_CATEGORIES:
category = ForumCategory(**cat_data)
db.add(category)
db.commit()
print(f"已创建 {len(DEFAULT_CATEGORIES)} 个论坛分类")
except Exception as e:
db.rollback()
print(f"初始化论坛分类失败: {e}")
finally:
db.close()
# 应用启动事件 # 应用启动事件
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
@@ -97,6 +127,12 @@ async def startup_event():
startup_knowledge_base() startup_knowledge_base()
except Exception as e: except Exception as e:
print(f"启动知识库服务失败: {e}", file=sys.stderr) print(f"启动知识库服务失败: {e}", file=sys.stderr)
# 初始化论坛分类
try:
seed_forum_categories()
except Exception as e:
print(f"初始化论坛分类失败: {e}", file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()
print("应用启动事件完成", file=sys.stderr) print("应用启动事件完成", file=sys.stderr)
-6
View File
@@ -161,12 +161,6 @@ export default function AnalyticsPage() {
return ( return (
<div className="min-h-screen bg-background"> <div className="min-h-screen bg-background">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<h1 className="text-3xl font-bold text-foreground mb-2"></h1>
<p className="text-muted-foreground"></p>
</div>
{/* 标签页 */} {/* 标签页 */}
<div className="flex space-x-1 mb-8"> <div className="flex space-x-1 mb-8">
{[ {[
+1 -13
View File
@@ -93,19 +93,8 @@ export default function CourseContentPage() {
return ( return (
<div className="min-h-screen bg-app pb-16 lg:pb-0"> <div className="min-h-screen bg-app pb-16 lg:pb-0">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center space-x-3">
<div className="w-12 h-12 bg-gradient-to-r from-blue-600 to-purple-600 rounded-xl flex items-center justify-center shadow-lg">
<GraduationCap className="w-6 h-6 text-white" />
</div>
<div>
<h1 className="text-3xl font-bold"></h1>
<p className="text-muted-foreground"></p>
</div>
</div>
{/* 视图切换 */} {/* 视图切换 */}
<div className="flex justify-end mb-4">
<Tabs value={viewMode} onValueChange={(v) => setViewMode(v as 'graph' | 'list')}> <Tabs value={viewMode} onValueChange={(v) => setViewMode(v as 'graph' | 'list')}>
<TabsList> <TabsList>
<TabsTrigger value="graph" className="flex items-center space-x-2"> <TabsTrigger value="graph" className="flex items-center space-x-2">
@@ -119,7 +108,6 @@ export default function CourseContentPage() {
</TabsList> </TabsList>
</Tabs> </Tabs>
</div> </div>
</div>
{/* 错误提示 */} {/* 错误提示 */}
{error && ( {error && (
+151 -60
View File
@@ -2,17 +2,76 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import Link from "next/link"; import Link from "next/link";
import { MessageSquare, Loader2, MessageCircle } from "lucide-react"; import {
MessageSquare,
Loader2,
MessageCircle,
BookOpen,
Lightbulb,
Megaphone,
ChevronRight,
Clock,
User,
} from "lucide-react";
import { forumAPI } from "@/lib/api"; import { forumAPI } from "@/lib/api";
import type { ForumCategory, ForumPostSummary } from "@/types"; import type { ForumCategory, ForumPostSummary } from "@/types";
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button"; const CATEGORY_ICONS: Record<string, React.ReactNode> = {
import { Alert, AlertDescription } from "@/components/ui/alert"; default: <MessageSquare className="w-5 h-5" />,
};
const CATEGORY_ORDER = ["公告", "通知", "学习", "讨论", "课程", "反馈", "使用"];
function getCategoryIcon(name: string) {
if (name.includes("公告") || name.includes("通知"))
return <Megaphone className="w-4 h-4" />;
if (name.includes("学习") || name.includes("讨论") || name.includes("课程"))
return <BookOpen className="w-4 h-4" />;
if (name.includes("反馈") || name.includes("使用"))
return <Lightbulb className="w-4 h-4" />;
return CATEGORY_ICONS.default;
}
function getCategoryGradient(name: string) {
if (name.includes("公告") || name.includes("通知"))
return "from-rose-500/10 to-rose-50";
if (name.includes("学习") || name.includes("讨论") || name.includes("课程"))
return "from-blue-500/10 to-blue-50";
if (name.includes("反馈") || name.includes("使用"))
return "from-amber-500/10 to-amber-50";
return "from-slate-500/10 to-slate-50";
}
function getCategoryAccent(name: string) {
if (name.includes("公告") || name.includes("通知")) return "text-rose-600";
if (name.includes("学习") || name.includes("讨论") || name.includes("课程")) return "text-blue-600";
if (name.includes("反馈") || name.includes("使用")) return "text-amber-600";
return "text-slate-600";
}
function formatRelativeTime(dateStr: string) {
const date = new Date(
new Date(dateStr).getTime() + 8 * 60 * 60 * 1000
);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMinutes = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffMinutes < 1) return "刚刚";
if (diffMinutes < 60) return `${diffMinutes} 分钟前`;
if (diffHours < 24) return `${diffHours} 小时前`;
if (diffDays < 7) return `${diffDays} 天前`;
return date.toLocaleDateString("zh-CN", { month: "short", day: "numeric" });
}
export default function ForumHomePage() { export default function ForumHomePage() {
const [categories, setCategories] = useState<ForumCategory[]>([]); const [categories, setCategories] = useState<ForumCategory[]>([]);
const [categoryPosts, setCategoryPosts] = useState<Record<number, ForumPostSummary[]>>({}); const [categoryPosts, setCategoryPosts] = useState<
Record<number, ForumPostSummary[]>
>({});
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
@@ -25,10 +84,9 @@ export default function ForumHomePage() {
const postsEntries = await Promise.all( const postsEntries = await Promise.all(
data.map(async (category) => { data.map(async (category) => {
try { try {
const posts = await forumAPI.getPosts(category.id, 10); const posts = await forumAPI.getPosts(category.id, 3);
return [category.id, posts] as const; return [category.id, posts] as const;
} catch (err) { } catch {
console.warn(`加载分类 ${category.name} 的帖子失败:`, err);
return [category.id, []] as const; return [category.id, []] as const;
} }
}) })
@@ -36,7 +94,8 @@ export default function ForumHomePage() {
setCategoryPosts(Object.fromEntries(postsEntries)); setCategoryPosts(Object.fromEntries(postsEntries));
setError(null); setError(null);
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : "加载论坛数据失败"; const message =
err instanceof Error ? err.message : "加载论坛数据失败";
setError(message); setError(message);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
@@ -47,78 +106,110 @@ export default function ForumHomePage() {
}, []); }, []);
return ( return (
<div className="mit-container py-10 space-y-6"> <div className="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-6 space-y-5">
<div className="space-y-2">
<h1 className="text-3xl font-bold flex items-center gap-2">
<MessageSquare className="h-7 w-7 text-primary" />
</h1>
<p className="text-muted-foreground">
使
</p>
</div>
{isLoading ? ( {isLoading ? (
<div className="flex items-center gap-2 text-muted-foreground"> <div className="flex items-center justify-center gap-2 text-muted-foreground py-20">
<Loader2 className="h-4 w-4 animate-spin" /> <Loader2 className="h-4 w-4 animate-spin" />
... ...
</div> </div>
) : error ? ( ) : error ? (
<Alert variant="destructive"> <div className="text-center py-16">
<AlertDescription>{error}</AlertDescription> <p className="text-muted-foreground">{error}</p>
</Alert>
) : (
<div className="grid gap-4 md:grid-cols-2">
{categories.map((category) => (
<Card key={category.id} className="flex flex-col">
<CardHeader>
<CardTitle>{category.name}</CardTitle>
{category.description && (
<CardDescription>{category.description}</CardDescription>
)}
</CardHeader>
<CardContent className="flex flex-col gap-4 h-full">
<div className="flex items-center justify-between text-sm text-muted-foreground">
<span>{category.post_count} </span>
<Button asChild size="sm" variant="ghost" className="px-2">
<Link href={`/forum/${category.id}`}></Link>
</Button>
</div> </div>
<div className="flex-1 space-y-3 overflow-y-auto pr-1"> ) : (
{categoryPosts[category.id] && categoryPosts[category.id].length > 0 ? ( <div className="space-y-3">
categoryPosts[category.id].map((post) => ( {[...categories]
.sort((a, b) => {
const getOrder = (name: string) => {
for (let i = 0; i < CATEGORY_ORDER.length; i++) {
if (name.includes(CATEGORY_ORDER[i])) return i;
}
return CATEGORY_ORDER.length;
};
return getOrder(a.name) - getOrder(b.name);
})
.map((category) => {
const posts = categoryPosts[category.id] || [];
return (
<div
key={category.id}
className={`bg-gradient-to-r ${getCategoryGradient(
category.name
)} rounded-xl border border-border/40 overflow-hidden`}
>
{/* Category header */}
<div className="px-4 py-3 flex items-center justify-between border-b border-border/20">
<div className="flex items-center gap-2.5">
<span className={getCategoryAccent(category.name)}>
{getCategoryIcon(category.name)}
</span>
<div>
<h2 className="text-sm font-semibold text-foreground">
{category.name}
</h2>
{category.description && (
<p className="text-[11px] text-muted-foreground mt-0">
{category.description}
</p>
)}
</div>
</div>
<div className="flex items-center gap-2.5">
<span className="text-[11px] text-muted-foreground tabular-nums">
{category.post_count}
</span>
<Link
href={`/forum/${category.id}`}
className="text-[11px] font-medium text-primary hover:underline flex items-center gap-0.5"
>
<ChevronRight className="w-3 h-3" />
</Link>
</div>
</div>
{/* Posts list */}
<div className="divide-y divide-border/20">
{posts.length > 0 ? (
posts.map((post) => (
<Link <Link
key={post.id} key={post.id}
href={`/forum/post/${post.id}`} href={`/forum/post/${post.id}`}
className="block rounded-lg border border-border/60 hover:border-primary/50 hover:bg-muted/40 transition-colors" className="flex items-start gap-2.5 px-4 py-2.5 hover:bg-white/40 transition-colors group"
> >
<div className="px-3 py-2"> <div className="mt-0.5 w-6 h-6 rounded-full bg-muted/60 flex items-center justify-center flex-shrink-0">
<div className="font-medium text-foreground line-clamp-1"> <User className="w-3 h-3 text-muted-foreground" />
</div>
<div className="flex-1 min-w-0">
<p className="text-[13px] font-medium text-foreground group-hover:text-primary transition-colors line-clamp-1">
{post.title} {post.title}
</div> </p>
<div className="mt-1 flex items-center text-xs text-muted-foreground gap-2"> <div className="flex items-center gap-2 mt-0.5 text-[11px] text-muted-foreground">
<span>{post.author_name}</span> <span>{post.author_name}</span>
<span>·</span> <span className="inline-flex items-center gap-0.5">
<span>{new Date(new Date(post.created_at).getTime() + 8 * 60 * 60 * 1000).toLocaleString()}</span> <Clock className="w-2.5 h-2.5" />
<span>·</span> {formatRelativeTime(post.created_at)}
<span>{post.reply_count} </span> </span>
<span className="inline-flex items-center gap-0.5">
<MessageCircle className="w-2.5 h-2.5" />
{post.reply_count}
</span>
</div> </div>
</div> </div>
<ChevronRight className="w-3.5 h-3.5 text-muted-foreground/0 group-hover:text-muted-foreground transition-colors mt-1 flex-shrink-0" />
</Link> </Link>
)) ))
) : ( ) : (
<div className="flex items-center gap-2 text-sm text-muted-foreground"> <div className="px-4 py-5 text-center text-xs text-muted-foreground">
<MessageCircle className="h-4 w-4" />
</div> </div>
)} )}
</div> </div>
</CardContent> </div>
</Card> );
))} })}
</div> </div>
)} )}
</div> </div>
); );
} }
-6
View File
@@ -129,12 +129,6 @@ export default function KnowledgePage() {
return ( return (
<div className="min-h-screen bg-app pb-16 lg:pb-0"> <div className="min-h-screen bg-app pb-16 lg:pb-0">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<h1 className="text-3xl font-bold mb-2"></h1>
<p className="text-muted-foreground"></p>
</div>
{/* 错误提示 */} {/* 错误提示 */}
{error && ( {error && (
<Alert variant="destructive" className="mb-6"> <Alert variant="destructive" className="mb-6">
-6
View File
@@ -164,12 +164,6 @@ export default function ProfilePage() {
return ( return (
<div className="min-h-screen bg-app pb-16 lg:pb-0"> <div className="min-h-screen bg-app pb-16 lg:pb-0">
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<h1 className="text-3xl font-bold mb-2"></h1>
<p className="text-muted-foreground"></p>
</div>
{/* 错误提示 */} {/* 错误提示 */}
{error && ( {error && (
<Alert variant="destructive" className="mb-6"> <Alert variant="destructive" className="mb-6">
-6
View File
@@ -130,12 +130,6 @@ export default function SettingsPage() {
return ( return (
<div className="min-h-screen bg-app pb-16 lg:pb-0"> <div className="min-h-screen bg-app pb-16 lg:pb-0">
<div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<h1 className="text-3xl font-bold mb-2"></h1>
<p className="text-muted-foreground"></p>
</div>
{/* 错误提示 */} {/* 错误提示 */}
{error && ( {error && (
<Alert variant="destructive" className="mb-6"> <Alert variant="destructive" className="mb-6">
-6
View File
@@ -375,12 +375,6 @@ export default function SpatialPage() {
return ( return (
<div className="min-h-screen bg-background"> <div className="min-h-screen bg-background">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8"> <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* 页面头部 */}
<div className="mb-8">
<h1 className="text-3xl font-bold text-foreground mb-2"></h1>
<p className="text-muted-foreground">使AI生成国土空间规划相关的设计图和示意图</p>
</div>
{/* Tab导航 */} {/* Tab导航 */}
<Tabs defaultValue="text-to-image" className="w-full"> <Tabs defaultValue="text-to-image" className="w-full">
<TabsList className="grid w-full grid-cols-2"> <TabsList className="grid w-full grid-cols-2">
+143 -90
View File
@@ -5,14 +5,37 @@ import Link from "next/link";
import { useParams, useRouter } from "next/navigation"; import { useParams, useRouter } from "next/navigation";
import { forumAPI } from "@/lib/api"; import { forumAPI } from "@/lib/api";
import type { ForumCategory, ForumPostSummary } from "@/types"; import type { ForumCategory, ForumPostSummary } from "@/types";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { Alert, AlertDescription } from "@/components/ui/alert"; import {
import { Loader2, MessageCircle } from "lucide-react"; Loader2,
MessageCircle,
Clock,
User,
PenLine,
Send,
ArrowLeft,
} from "lucide-react";
import { useAuthStore } from "@/store/auth"; import { useAuthStore } from "@/store/auth";
function formatRelativeTime(dateStr: string) {
const date = new Date(
new Date(dateStr).getTime() + 8 * 60 * 60 * 1000
);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMinutes = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffMinutes < 1) return "刚刚";
if (diffMinutes < 60) return `${diffMinutes} 分钟前`;
if (diffHours < 24) return `${diffHours} 小时前`;
if (diffDays < 7) return `${diffDays} 天前`;
return date.toLocaleDateString("zh-CN", { month: "short", day: "numeric" });
}
export default function ForumCategoryPage() { export default function ForumCategoryPage() {
const params = useParams(); const params = useParams();
const router = useRouter(); const router = useRouter();
@@ -27,6 +50,7 @@ export default function ForumCategoryPage() {
const [content, setContent] = useState(""); const [content, setContent] = useState("");
const [submitError, setSubmitError] = useState<string | null>(null); const [submitError, setSubmitError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false);
const [showForm, setShowForm] = useState(false);
const { isAuthenticated } = useAuthStore(); const { isAuthenticated } = useAuthStore();
@@ -53,7 +77,8 @@ export default function ForumCategoryPage() {
setPosts(postList); setPosts(postList);
setError(null); setError(null);
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : "加载帖子失败"; const message =
err instanceof Error ? err.message : "加载帖子失败";
setError(message); setError(message);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
@@ -79,10 +104,12 @@ export default function ForumCategoryPage() {
}); });
setTitle(""); setTitle("");
setContent(""); setContent("");
setShowForm(false);
const updatedPosts = await forumAPI.getPosts(categoryId); const updatedPosts = await forumAPI.getPosts(categoryId);
setPosts(updatedPosts); setPosts(updatedPosts);
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : "发布帖子失败"; const message =
err instanceof Error ? err.message : "发布帖子失败";
setSubmitError(message); setSubmitError(message);
} finally { } finally {
setIsSubmitting(false); setIsSubmitting(false);
@@ -90,129 +117,155 @@ export default function ForumCategoryPage() {
}; };
return ( return (
<div className="min-h-screen bg-app py-10"> <div className="min-h-screen bg-background">
<div className="max-w-5xl mx-auto px-4 space-y-6"> <div className="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<Button variant="ghost" onClick={() => router.push("/forum")} className="px-0"> {/* Back nav */}
<button
</Button> onClick={() => router.push("/forum")}
className="inline-flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors mb-6"
>
<ArrowLeft className="w-4 h-4" />
</button>
<div className="space-y-2"> {/* Category header */}
<h1 className="text-2xl font-semibold flex items-center gap-2"> <div className="flex items-center justify-between mb-6">
<MessageCircle className="h-6 w-6 text-primary" /> <div>
<h1
className="text-xl font-bold tracking-tight"
style={{ fontFamily: "var(--font-serif), Georgia, serif" }}
>
{currentCategory ? currentCategory.name : "论坛分类"} {currentCategory ? currentCategory.name : "论坛分类"}
</h1> </h1>
{currentCategory?.description && ( {currentCategory?.description && (
<p className="text-muted-foreground">{currentCategory.description}</p> <p className="text-sm text-muted-foreground mt-1">
{currentCategory.description}
</p>
)}
</div>
{isAuthenticated && (
<Button
onClick={() => setShowForm(!showForm)}
size="sm"
className="gap-1.5"
>
<PenLine className="w-3.5 h-3.5" />
{showForm ? "收起" : "发帖"}
</Button>
)} )}
</div> </div>
{isLoading ? ( {/* New post form (collapsible) */}
<div className="flex items-center justify-center gap-2 text-muted-foreground py-20"> {showForm && (
<Loader2 className="h-4 w-4 animate-spin" /> <div className="bg-muted/30 rounded-xl border border-border/40 p-5 mb-6 space-y-4">
... <form onSubmit={handleSubmit} className="space-y-3">
</div>
) : error ? (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
) : (
<div className="grid gap-6 lg:grid-cols-[2fr,3fr]">
<div className="space-y-4">
{isAuthenticated ? (
<Card className="shadow-md">
<CardHeader>
<CardTitle></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
{submitError && ( {submitError && (
<Alert variant="destructive"> <p className="text-sm text-destructive">{submitError}</p>
<AlertDescription>{submitError}</AlertDescription>
</Alert>
)} )}
<div className="space-y-2">
<label className="text-sm font-medium text-foreground/80"></label>
<Input <Input
value={title} value={title}
onChange={(event) => setTitle(event.target.value)} onChange={(e) => setTitle(e.target.value)}
placeholder="写一个简洁的标题" placeholder="帖子标题"
maxLength={200} maxLength={200}
disabled={isSubmitting} disabled={isSubmitting}
className="bg-background"
/> />
</div>
<div className="space-y-2">
<label className="text-sm font-medium text-foreground/80"></label>
<Textarea <Textarea
value={content} value={content}
onChange={(event) => setContent(event.target.value)} onChange={(e) => setContent(e.target.value)}
placeholder="详细描述你的想法或问题..." placeholder="分享你的想法或问题..."
rows={8} rows={5}
className="min-h-[220px] resize-none" className="resize-none bg-background"
disabled={isSubmitting} disabled={isSubmitting}
/> />
</div> <div className="flex justify-end">
<div className="text-right"> <Button
<Button type="submit" disabled={isSubmitting}> type="submit"
size="sm"
disabled={isSubmitting}
className="gap-1.5"
>
{isSubmitting ? ( {isSubmitting ? (
<> <>
<Loader2 className="h-4 w-4 animate-spin mr-2" /> <Loader2 className="w-3.5 h-3.5 animate-spin" />
... ...
</> </>
) : ( ) : (
"发布帖子" <>
<Send className="w-3.5 h-3.5" />
</>
)} )}
</Button> </Button>
</div> </div>
</form> </form>
</CardContent> </div>
</Card> )}
) : (
<Alert> {/* Login prompt */}
<AlertDescription> {!isAuthenticated && (
<div className="text-center py-4 mb-6 bg-muted/20 rounded-lg text-sm text-muted-foreground">
<Link href="/login" className="underline ml-1">
<Link href="/login" className="text-primary hover:underline mx-1">
</Link> </Link>
<Link href="/register" className="underline ml-1"> <Link
href="/register"
className="text-primary hover:underline mx-1"
>
</Link> </Link>
</AlertDescription>
</Alert>
)}
</div> </div>
)}
<div className="space-y-3"> {/* Posts list */}
{posts.length === 0 ? ( {isLoading ? (
<Card className="shadow-sm"> <div className="flex items-center justify-center gap-2 text-muted-foreground py-20">
<CardContent className="py-6 text-sm text-muted-foreground text-center"> <Loader2 className="h-4 w-4 animate-spin" />
...
</CardContent>
</Card>
) : (
posts.map((post) => (
<Card key={post.id} className="shadow-sm transition hover:shadow-lg">
<CardHeader>
<CardTitle className="text-lg">
<Link href={`/forum/post/${post.id}`} className="hover:underline">
{post.title}
</Link>
</CardTitle>
<CardDescription>
{post.author_name} · {post.reply_count} ·{" "}
{new Date(new Date(post.created_at).getTime() + 8 * 60 * 60 * 1000).toLocaleString()}
</CardDescription>
</CardHeader>
</Card>
))
)}
</div> </div>
) : error ? (
<div className="text-center py-16">
<p className="text-muted-foreground">{error}</p>
</div>
) : posts.length === 0 ? (
<div className="text-center py-16">
<MessageCircle className="w-10 h-10 text-muted-foreground/40 mx-auto mb-3" />
<p className="text-muted-foreground"></p>
</div>
) : (
<div className="divide-y divide-border/40 rounded-xl border border-border/40 overflow-hidden">
{posts.map((post) => (
<Link
key={post.id}
href={`/forum/post/${post.id}`}
className="flex items-start gap-3 px-5 py-4 hover:bg-muted/30 transition-colors group"
>
<div className="mt-0.5 w-9 h-9 rounded-full bg-muted/60 flex items-center justify-center flex-shrink-0">
<User className="w-4 h-4 text-muted-foreground" />
</div>
<div className="flex-1 min-w-0">
<p className="font-medium text-foreground group-hover:text-primary transition-colors line-clamp-2">
{post.title}
</p>
<div className="flex items-center gap-3 mt-1.5 text-xs text-muted-foreground">
<span>{post.author_name}</span>
<span className="inline-flex items-center gap-0.5">
<Clock className="w-3 h-3" />
{formatRelativeTime(post.created_at)}
</span>
<span className="inline-flex items-center gap-0.5">
<MessageCircle className="w-3 h-3" />
{post.reply_count}
</span>
</div>
</div>
</Link>
))}
</div> </div>
)} )}
</div> </div>
</div> </div>
); );
} }
+122 -69
View File
@@ -5,13 +5,27 @@ import { useParams, useRouter } from "next/navigation";
import Link from "next/link"; import Link from "next/link";
import { forumAPI } from "@/lib/api"; import { forumAPI } from "@/lib/api";
import type { ForumPostDetail } from "@/types"; import type { ForumPostDetail } from "@/types";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { Alert, AlertDescription } from "@/components/ui/alert"; import { Loader2, MessageCircle, Clock, User, Send, ArrowLeft } from "lucide-react";
import { Loader2, MessageCircleReply } from "lucide-react";
import { useAuthStore } from "@/store/auth"; import { useAuthStore } from "@/store/auth";
function formatRelativeTime(dateStr: string) {
const date = new Date(
new Date(dateStr).getTime() + 8 * 60 * 60 * 1000
);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMinutes = Math.floor(diffMs / (1000 * 60));
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffMinutes < 1) return "刚刚";
if (diffMinutes < 60) return `${diffMinutes} 分钟前`;
if (diffHours < 24) return `${diffHours} 小时前`;
if (diffDays < 7) return `${diffDays} 天前`;
return date.toLocaleDateString("zh-CN", { month: "short", day: "numeric" });
}
export default function ForumPostPage() { export default function ForumPostPage() {
const params = useParams(); const params = useParams();
const router = useRouter(); const router = useRouter();
@@ -41,7 +55,8 @@ export default function ForumPostPage() {
setPost(detail); setPost(detail);
setError(null); setError(null);
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : "加载帖子详情失败"; const message =
err instanceof Error ? err.message : "加载帖子详情失败";
setError(message); setError(message);
} finally { } finally {
setIsLoading(false); setIsLoading(false);
@@ -74,116 +89,154 @@ export default function ForumPostPage() {
}; };
return ( return (
<div className="min-h-screen bg-app py-10"> <div className="min-h-screen bg-background">
<div className="max-w-4xl mx-auto px-4 space-y-6"> <div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<Button variant="ghost" onClick={() => router.back()} className="px-0"> {/* Back nav */}
<button
</Button> onClick={() => router.back()}
className="inline-flex items-center gap-1.5 text-sm text-muted-foreground hover:text-foreground transition-colors mb-6"
>
<ArrowLeft className="w-4 h-4" />
</button>
{isLoading ? ( {isLoading ? (
<div className="flex items-center justify-center gap-2 text-muted-foreground py-20"> <div className="flex items-center justify-center gap-2 text-muted-foreground py-20">
<Loader2 className="h-4 w-4 animate-spin" /> <Loader2 className="h-4 w-4 animate-spin" />
... ...
</div> </div>
) : error ? ( ) : error ? (
<Alert variant="destructive"> <div className="text-center py-16">
<AlertDescription>{error}</AlertDescription> <p className="text-muted-foreground">{error}</p>
</Alert> </div>
) : post ? ( ) : post ? (
<div className="space-y-6"> <div className="space-y-6">
<Card className="shadow-md"> {/* Post content */}
<CardHeader> <article className="rounded-xl border border-border/40 overflow-hidden">
<CardTitle className="text-2xl font-semibold">{post.title}</CardTitle> <div className="px-6 py-5 border-b border-border/20">
<p className="text-sm text-muted-foreground"> <h1
{post.author_name} ·{" "} className="text-xl font-bold tracking-tight leading-snug"
{new Date(new Date(post.created_at).getTime() + 8 * 60 * 60 * 1000).toLocaleString()} style={{ fontFamily: "var(--font-serif), Georgia, serif" }}
</p> >
</CardHeader> {post.title}
<CardContent className="whitespace-pre-wrap leading-relaxed text-foreground/90"> </h1>
<div className="flex items-center gap-3 mt-3 text-sm text-muted-foreground">
<div className="flex items-center gap-1.5">
<div className="w-6 h-6 rounded-full bg-muted/60 flex items-center justify-center">
<User className="w-3 h-3 text-muted-foreground" />
</div>
<span>{post.author_name}</span>
</div>
<span className="inline-flex items-center gap-0.5">
<Clock className="w-3 h-3" />
{formatRelativeTime(post.created_at)}
</span>
</div>
</div>
<div className="px-6 py-5 whitespace-pre-wrap leading-relaxed text-foreground/90 text-sm">
{post.content} {post.content}
</CardContent> </div>
</Card> </article>
<section className="space-y-4"> {/* Replies section */}
<h2 className="text-lg font-medium flex items-center gap-2"> <section>
<MessageCircleReply className="h-5 w-5 text-primary" /> <div className="flex items-center gap-2 mb-4">
<MessageCircle className="w-4 h-4 text-primary" />
<h2 className="font-medium text-sm">
{post.replies.length} {post.replies.length}
</h2> </h2>
</div>
{post.replies.length === 0 ? ( {post.replies.length === 0 ? (
<Card className="text-sm text-muted-foreground"> <div className="text-center py-8 text-sm text-muted-foreground bg-muted/20 rounded-xl">
<CardContent className="py-4">
</CardContent>
</Card>
) : (
post.replies.map((reply) => (
<Card key={reply.id} className="shadow-sm">
<CardHeader className="pb-2">
<div className="text-sm font-medium text-foreground">{reply.author_name}</div>
<div className="text-xs text-muted-foreground">
{new Date(new Date(reply.created_at).getTime() + 8 * 60 * 60 * 1000).toLocaleString()}
</div> </div>
</CardHeader> ) : (
<CardContent className="text-sm whitespace-pre-wrap leading-relaxed"> <div className="space-y-3">
{post.replies.map((reply) => (
<div
key={reply.id}
className="flex gap-3 px-5 py-4 rounded-xl border border-border/30 hover:border-border/60 transition-colors"
>
<div className="w-8 h-8 rounded-full bg-muted/60 flex items-center justify-center flex-shrink-0 mt-0.5">
<User className="w-3.5 h-3.5 text-muted-foreground" />
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1.5">
<span className="text-sm font-medium text-foreground">
{reply.author_name}
</span>
<span className="text-xs text-muted-foreground">
{formatRelativeTime(reply.created_at)}
</span>
</div>
<p className="text-sm whitespace-pre-wrap leading-relaxed text-foreground/90">
{reply.content} {reply.content}
</CardContent> </p>
</Card> </div>
)) </div>
))}
</div>
)} )}
</section> </section>
<Card className="shadow-sm"> {/* Reply form */}
<CardContent className="py-6"> <div className="rounded-xl border border-border/40 p-5">
{isAuthenticated ? ( {isAuthenticated ? (
<form onSubmit={handleReply} className="space-y-4"> <form onSubmit={handleReply} className="space-y-3">
{submitError && ( {submitError && (
<Alert variant="destructive"> <p className="text-sm text-destructive">{submitError}</p>
<AlertDescription>{submitError}</AlertDescription>
</Alert>
)} )}
<Textarea <Textarea
value={replyContent} value={replyContent}
onChange={(event) => setReplyContent(event.target.value)} onChange={(e) => setReplyContent(e.target.value)}
placeholder="写下你的想法..." placeholder="写下你的想法..."
rows={5} rows={4}
className="resize-none" className="resize-none"
disabled={isSubmitting} disabled={isSubmitting}
/> />
<div className="text-right"> <div className="flex justify-end">
<Button type="submit" disabled={isSubmitting}> <button
type="submit"
disabled={isSubmitting || !replyContent.trim()}
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium rounded-lg bg-[#2563eb] text-white hover:bg-[#2563eb]/90 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
>
{isSubmitting ? ( {isSubmitting ? (
<> <>
<Loader2 className="h-4 w-4 animate-spin mr-2" /> <Loader2 className="w-3.5 h-3.5 animate-spin" />
... ...
</> </>
) : ( ) : (
"提交回复" <>
<Send className="w-3.5 h-3.5" />
</>
)} )}
</Button> </button>
</div> </div>
</form> </form>
) : ( ) : (
<Alert> <div className="text-center py-3 text-sm text-muted-foreground">
<AlertDescription>
<Link
<Link href="/login" className="underline mx-1"> href="/login"
className="text-primary hover:underline mx-1"
>
</Link> </Link>
<Link href="/register" className="underline ml-1"> <Link
href="/register"
className="text-primary hover:underline ml-1"
>
</Link> </Link>
</div>
</AlertDescription>
</Alert>
)} )}
</CardContent> </div>
</Card>
</div> </div>
) : null} ) : null}
</div> </div>
</div> </div>
); );
} }
+1 -19
View File
@@ -38,7 +38,7 @@ const modules = [
visual: "spatial" as const, visual: "spatial" as const,
}, },
{ {
title: "论坛社区", title: "课程社区",
description: "课程讨论与学习经验交流平台,促进师生互动与同伴学习", description: "课程讨论与学习经验交流平台,促进师生互动与同伴学习",
href: "/forum", href: "/forum",
tags: ["协作"], tags: ["协作"],
@@ -436,13 +436,6 @@ export default function HomePageContent() {
<p className="text-sm text-muted-foreground leading-relaxed mb-5"> <p className="text-sm text-muted-foreground leading-relaxed mb-5">
AI技术的国土空间规划智能学习平台RAG检索增强生成 AI技术的国土空间规划智能学习平台RAG检索增强生成
</p> </p>
<div className="flex gap-3">
<Button asChild variant="outline" size="sm">
<a href="https://tsp.spacekg.com/#/" target="_blank" rel="noopener noreferrer">
<BookOpen className="mr-2 h-4 w-4" />
</a>
</Button>
<Button asChild variant="outline" size="sm"> <Button asChild variant="outline" size="sm">
<a href="https://homepage.hit.edu.cn/wusongtao" target="_blank" rel="noopener noreferrer"> <a href="https://homepage.hit.edu.cn/wusongtao" target="_blank" rel="noopener noreferrer">
<Users className="mr-2 h-4 w-4" /> <Users className="mr-2 h-4 w-4" />
@@ -450,7 +443,6 @@ export default function HomePageContent() {
</a> </a>
</Button> </Button>
</div> </div>
</div>
{/* 快速链接 */} {/* 快速链接 */}
<div> <div>
@@ -463,16 +455,6 @@ export default function HomePageContent() {
</Link> </Link>
</li> </li>
))} ))}
<li>
<Link href="/analytics" className="text-sm text-muted-foreground hover:text-foreground transition-colors">
</Link>
</li>
<li>
<Link href="/settings" className="text-sm text-muted-foreground hover:text-foreground transition-colors">
</Link>
</li>
</ul> </ul>
</div> </div>
+61 -33
View File
@@ -1,12 +1,13 @@
"use client"; "use client";
import Link from "next/link"; import Link from "next/link";
import { usePathname } from "next/navigation";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { ThemeToggle } from "@/components/ui/theme-toggle"; import { ThemeToggle } from "@/components/ui/theme-toggle";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { useAuthStore } from "@/store/auth"; import { useAuthStore } from "@/store/auth";
import { BookOpen, LogOut, User as UserIcon, Settings, ChevronDown, MessageSquare, Database, Image, TrendingUp, GraduationCap } from "lucide-react"; import { BookOpen, LogOut, User as UserIcon, Settings, ChevronDown, MessageSquare, Database, Image, TrendingUp, GraduationCap, Users } from "lucide-react";
import { User } from "@/types"; import { User } from "@/types";
interface NavbarProps { interface NavbarProps {
@@ -14,14 +15,28 @@ interface NavbarProps {
user: User | null; user: User | null;
} }
const NAV_LINKS = [
{ href: "/course-content", label: "课程内容", icon: GraduationCap },
{ href: "/chat", label: "智能问答", icon: MessageSquare },
{ href: "/knowledge", label: "知识库", icon: Database },
{ href: "/spatial", label: "空间设计", icon: Image },
{ href: "/forum", label: "课程社区", icon: Users },
];
export default function Navbar({ isAuthenticated, user }: NavbarProps) { export default function Navbar({ isAuthenticated, user }: NavbarProps) {
const { logout } = useAuthStore(); const { logout } = useAuthStore();
const pathname = usePathname();
const isActive = (href: string) => {
if (href === "/forum") return pathname === "/forum" || pathname.startsWith("/forum/");
return pathname === href || pathname.startsWith(href + "/");
};
return ( return (
<nav className="border-b bg-background/95 backdrop-blur-sm sticky top-0 z-50"> <nav className="border-b bg-background/95 backdrop-blur-sm sticky top-0 z-50">
<div className="mit-container"> <div className="mit-container">
<div className="flex justify-between items-center h-16"> <div className="flex justify-between items-center h-16">
{/* Logo 和标题 */} {/* Logo */}
<Link href="/" className="flex items-center space-x-3 hover:opacity-80 transition-opacity cursor-pointer"> <Link href="/" className="flex items-center space-x-3 hover:opacity-80 transition-opacity cursor-pointer">
<div className="w-10 h-10 bg-primary rounded-lg flex items-center justify-center shadow-sm"> <div className="w-10 h-10 bg-primary rounded-lg flex items-center justify-center shadow-sm">
<BookOpen className="w-6 h-6 text-white" /> <BookOpen className="w-6 h-6 text-white" />
@@ -36,46 +51,60 @@ export default function Navbar({ isAuthenticated, user }: NavbarProps) {
</div> </div>
</Link> </Link>
{/* 导航链接 */} {/* 导航链接 — 带底部下划线指示 */}
{isAuthenticated && ( {isAuthenticated && (
<div className="hidden md:flex items-center space-x-1"> <div className="hidden md:flex items-center h-full -mb-px">
<Link href="/course-content"> {NAV_LINKS.map((item) => {
<Button variant="ghost" className="flex items-center space-x-2 px-3 py-2 hover:bg-muted/50"> const active = isActive(item.href);
<GraduationCap className="w-4 h-4" /> return (
<span className="text-sm"></span> <Link
</Button> key={item.href}
href={item.href}
className={`
relative flex items-center gap-1.5 px-3 h-full text-sm transition-colors
${active
? "text-primary font-medium"
: "text-muted-foreground hover:text-foreground"
}
`}
>
<item.icon className="w-4 h-4" />
<span>{item.label}</span>
{active && (
<span className="absolute bottom-0 left-3 right-3 h-0.5 bg-primary rounded-full" />
)}
</Link> </Link>
<Link href="/chat"> );
<Button variant="ghost" className="flex items-center space-x-2 px-3 py-2 hover:bg-muted/50"> })}
<MessageSquare className="w-4 h-4" /> </div>
<span className="text-sm"></span> )}
</Button>
</Link> {/* 未登录时也显示课程社区链接 */}
<Link href="/knowledge"> {!isAuthenticated && (
<Button variant="ghost" className="flex items-center space-x-2 px-3 py-2 hover:bg-muted/50"> <div className="hidden md:flex items-center h-full -mb-px">
<Database className="w-4 h-4" /> <Link
<span className="text-sm"></span> href="/forum"
</Button> className={`
</Link> relative flex items-center gap-1.5 px-3 h-full text-sm transition-colors
<Link href="/spatial"> ${isActive("/forum")
<Button variant="ghost" className="flex items-center space-x-2 px-3 py-2 hover:bg-muted/50"> ? "text-primary font-medium"
<Image className="w-4 h-4" /> : "text-muted-foreground hover:text-foreground"
<span className="text-sm"></span> }
</Button> `}
>
<Users className="w-4 h-4" />
<span></span>
{isActive("/forum") && (
<span className="absolute bottom-0 left-3 right-3 h-0.5 bg-primary rounded-full" />
)}
</Link> </Link>
</div> </div>
)} )}
<div className="flex items-center space-x-3"> <div className="flex items-center space-x-3">
<ThemeToggle /> <ThemeToggle />
<Link href="/forum">
<Button variant="outline" size="sm">
</Button>
</Link>
{isAuthenticated && user ? ( {isAuthenticated && user ? (
// 登录后状态 - 用户下拉菜单
<DropdownMenu> <DropdownMenu>
<DropdownMenuTrigger asChild> <DropdownMenuTrigger asChild>
<Button variant="ghost" className="flex items-center space-x-2 px-3 hover:bg-muted/50"> <Button variant="ghost" className="flex items-center space-x-2 px-3 hover:bg-muted/50">
@@ -127,7 +156,6 @@ export default function Navbar({ isAuthenticated, user }: NavbarProps) {
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
) : ( ) : (
// 未登录状态
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<Button variant="ghost" asChild> <Button variant="ghost" asChild>
<Link href="/login"></Link> <Link href="/login"></Link>
+3 -1
View File
@@ -14,7 +14,8 @@ import {
User, User,
LogOut, LogOut,
GraduationCap, GraduationCap,
TrendingUp TrendingUp,
Users
} from "lucide-react"; } from "lucide-react";
const navItems = [ const navItems = [
@@ -22,6 +23,7 @@ const navItems = [
{ id: "chat", name: "对话", icon: MessageSquare, href: "/chat" }, { id: "chat", name: "对话", icon: MessageSquare, href: "/chat" },
{ id: "knowledge", name: "知识库", icon: BookOpen, href: "/knowledge" }, { id: "knowledge", name: "知识库", icon: BookOpen, href: "/knowledge" },
{ id: "spatial", name: "空间出图", icon: Image, href: "/spatial" }, { id: "spatial", name: "空间出图", icon: Image, href: "/spatial" },
{ id: "forum", name: "社区", icon: Users, href: "/forum" },
]; ];
export default function MobileNav() { export default function MobileNav() {