import { useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { MessageSquare, Pin, CheckCircle2, Loader2, ArrowLeft, Send, Users } from "lucide-react";
import { useDiscussionBoards, usePosts, useCreatePost } from "@/hooks/queries";
import { useToast } from "@/hooks/use-toast";
import type { DiscussionPost } from "@/types/communication";
function PostItem({ post, boardId }: { post: DiscussionPost; boardId: number }) {
const [showReply, setShowReply] = useState(false);
const [replyContent, setReplyContent] = useState("");
const createPost = useCreatePost();
const { toast } = useToast();
const handleReply = () => {
createPost.mutate(
{ boardId, data: { board_id: boardId, parent_id: post.id, content: replyContent } },
{
onSuccess: () => { setShowReply(false); setReplyContent(""); },
onError: () => toast({ title: "Error", description: "Failed to reply", variant: "destructive" }),
},
);
};
return (
{post.title &&
{post.title}
}
{post.content}
{post.author_name}
·
{post.author_role}
·
{new Date(post.created_at).toLocaleDateString()}
{post.is_pinned &&
Pinned}
{post.is_resolved &&
Resolved}
{showReply && (
setReplyContent(e.target.value)} className="flex-1" />
)}
{post.replies && post.replies.length > 0 && (
{post.replies.map(reply => (
))}
)}
);
}
export default function StudentDiscussionBoard() {
const { toast } = useToast();
const { data: boards = [], isLoading } = useDiscussionBoards();
const [selectedBoardId, setSelectedBoardId] = useState(null);
const { data: postsData, isLoading: loadingPosts } = usePosts(selectedBoardId ?? 0);
const createPost = useCreatePost();
const [showNewPost, setShowNewPost] = useState(false);
const [newTitle, setNewTitle] = useState("");
const [newContent, setNewContent] = useState("");
const posts = postsData?.items ?? [];
const handleCreatePost = () => {
if (!selectedBoardId) return;
createPost.mutate(
{ boardId: selectedBoardId, data: { board_id: selectedBoardId, title: newTitle, content: newContent } },
{
onSuccess: () => { toast({ title: "Post Created" }); setShowNewPost(false); setNewTitle(""); setNewContent(""); },
onError: () => toast({ title: "Error", description: "Failed to create post", variant: "destructive" }),
},
);
};
if (isLoading) return ;
return (
Discussion Boards
Participate in course discussions with your classmates.
{!selectedBoardId ? (
{boards.map(board => (
setSelectedBoardId(board.id)}>
{board.name}
{board.course_name}
{board.post_count} posts
))}
{boards.length === 0 && (
No discussion boards available for your courses.
)}
) : (
{loadingPosts ? (
) : (
{posts.map(post => (
))}
{posts.length === 0 && (
No posts yet. Start the conversation!
)}
)}
)}
);
}