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 } from "lucide-react";
import { useDiscussionBoards, usePosts, useCreatePost, usePinPost, useResolvePost } from "@/hooks/queries";
import { useToast } from "@/hooks/use-toast";
import type { DiscussionPost } from "@/types/communication";
function PostThread({ post, boardId, onPin, onResolve }: { post: DiscussionPost; boardId: number; onPin: (id: number, pinned: boolean) => void; onResolve: (id: number) => void }) {
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 post 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}
{!post.is_resolved && (
)}
{showReply && (
setReplyContent(e.target.value)} className="flex-1" />
)}
{post.replies && post.replies.length > 0 && (
{post.replies.map(reply => (
))}
)}
);
}
export default function TeacherDiscussionBoard() {
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 pinPost = usePinPost();
const resolvePost = useResolvePost();
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" }),
},
);
};
const handlePin = (id: number, pinned: boolean) => {
pinPost.mutate(
{ id, body: { pinned } },
{ onError: () => toast({ title: "Error", description: "Failed to pin post", variant: "destructive" }) },
);
};
const handleResolve = (id: number) => {
resolvePost.mutate(id, {
onError: () => toast({ title: "Error", description: "Failed to resolve post", variant: "destructive" }),
});
};
if (isLoading) return ;
return (
Discussion Boards
Manage and participate in course discussions.
{!selectedBoardId ? (
{boards.map(board => (
setSelectedBoardId(board.id)}>
{board.name}
{board.course_name}
{board.post_count} posts
))}
{boards.length === 0 && (
No discussion boards available.
)}
) : (
{loadingPosts ? (
) : (
{posts.map(post => (
))}
{posts.length === 0 && (
No posts yet. Start the conversation!
)}
)}
)}
);
}