|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect } from "react"; |
| 4 | +import { useRouter, useParams } from "next/navigation"; |
| 5 | +import dynamic from "next/dynamic"; |
| 6 | +import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"; |
| 7 | +import Header from "@/components/ui/header"; |
| 8 | +import QuestionPanel from "@/components/practice/question-panel"; |
| 9 | +import CommunicationPanel from "@/components/practice/communication-panel"; |
| 10 | +import CodeOutputPanel from "@/components/practice/code-output-panel"; |
| 11 | +import { Button } from "@/components/ui/button"; |
| 12 | +import { Spinner } from "@/components/ui/spinner"; |
| 13 | +import { useCollaborationState, useCollaborationActions } from "@/stores/collaboration-store"; |
| 14 | + |
| 15 | +const CodeEditorPanel = dynamic(() => import("@/components/practice/code-editor-panel"), { |
| 16 | + ssr: false, |
| 17 | + loading: () => <div className="h-full flex items-center justify-center">Loading editor...</div>, |
| 18 | +}); |
| 19 | + |
| 20 | +export default function PracticePage() { |
| 21 | + const router = useRouter(); |
| 22 | + const params = useParams(); |
| 23 | + const roomId = params?.id as string; |
| 24 | + |
| 25 | + const { roomDetails, isLoading, error } = useCollaborationState(); |
| 26 | + const { fetchRoomDetails, reset } = useCollaborationActions(); |
| 27 | + |
| 28 | + // Fetch room details on mount |
| 29 | + useEffect(() => { |
| 30 | + if (roomId) { |
| 31 | + fetchRoomDetails(roomId).catch((err) => { |
| 32 | + console.error("Failed to fetch room details:", err); |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + // Cleanup on unmount |
| 37 | + return () => { |
| 38 | + reset(); |
| 39 | + }; |
| 40 | + }, [fetchRoomDetails, reset, roomId]); |
| 41 | + |
| 42 | + // Handle leave session |
| 43 | + const handleLeaveSession = () => { |
| 44 | + // Simply redirect to home page |
| 45 | + router.push("/"); |
| 46 | + }; |
| 47 | + |
| 48 | + // Loading state |
| 49 | + if (isLoading) { |
| 50 | + return ( |
| 51 | + <div className="h-screen w-full flex items-center justify-center"> |
| 52 | + <div className="flex flex-col items-center gap-4"> |
| 53 | + <Spinner /> |
| 54 | + <p className="text-muted-foreground">Loading collaboration room...</p> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // Error state |
| 61 | + if (error || !roomDetails) { |
| 62 | + return ( |
| 63 | + <div className="h-screen w-full flex items-center justify-center"> |
| 64 | + <div className="flex flex-col items-center gap-4 text-center"> |
| 65 | + <p className="text-destructive font-semibold">Failed to load room</p> |
| 66 | + <p className="text-muted-foreground">{error || "Room not found"}</p> |
| 67 | + <Button onClick={() => router.push("/")}>Go Back Home</Button> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Read-only mode (room is closed) |
| 74 | + const isReadOnly = !roomDetails.isActive; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="h-screen w-full flex flex-col"> |
| 78 | + <Header> |
| 79 | + <div className="flex items-center gap-4"> |
| 80 | + <Button variant={"destructive"} onClick={handleLeaveSession}> |
| 81 | + Leave Room |
| 82 | + </Button> |
| 83 | + </div> |
| 84 | + </Header> |
| 85 | + |
| 86 | + <div className="flex-1"> |
| 87 | + {isReadOnly ? ( |
| 88 | + // Read-only layout: Only question and read-only code editor |
| 89 | + <ResizablePanelGroup direction="horizontal" className="h-full w-full"> |
| 90 | + {/* Left Panel */} |
| 91 | + <ResizablePanel> |
| 92 | + <div className="h-full overflow-y-auto"> |
| 93 | + <QuestionPanel /> |
| 94 | + </div> |
| 95 | + </ResizablePanel> |
| 96 | + |
| 97 | + <ResizableHandle className="bg-gray-400 hover:bg-gray-600 w-1 cursor-col-resize" /> |
| 98 | + |
| 99 | + {/* Right Panel */} |
| 100 | + <ResizablePanel> |
| 101 | + <div className="h-full overflow-y-auto"> |
| 102 | + <CodeEditorPanel readOnly={true} /> |
| 103 | + </div> |
| 104 | + </ResizablePanel> |
| 105 | + </ResizablePanelGroup> |
| 106 | + ) : ( |
| 107 | + // Full layout with all panels |
| 108 | + <ResizablePanelGroup direction="horizontal" className="h-full w-full"> |
| 109 | + {/* Left Panel */} |
| 110 | + <ResizablePanel> |
| 111 | + <ResizablePanelGroup direction="vertical"> |
| 112 | + <ResizablePanel> |
| 113 | + <div className="h-full overflow-y-auto"> |
| 114 | + <QuestionPanel /> |
| 115 | + </div> |
| 116 | + </ResizablePanel> |
| 117 | + <ResizableHandle className="bg-gray-400 hover:bg-gray-600 w-1 cursor-col-resize" /> |
| 118 | + <ResizablePanel> |
| 119 | + <div className="h-full overflow-y-auto"> |
| 120 | + <CommunicationPanel /> |
| 121 | + </div> |
| 122 | + </ResizablePanel> |
| 123 | + </ResizablePanelGroup> |
| 124 | + </ResizablePanel> |
| 125 | + |
| 126 | + <ResizableHandle className="bg-gray-400 hover:bg-gray-600 w-1 cursor-col-resize" /> |
| 127 | + |
| 128 | + {/* Right Panel */} |
| 129 | + <ResizablePanel> |
| 130 | + <ResizablePanelGroup direction="vertical"> |
| 131 | + <ResizablePanel> |
| 132 | + <div className="h-full overflow-y-auto"> |
| 133 | + <CodeEditorPanel readOnly={false} /> |
| 134 | + </div> |
| 135 | + </ResizablePanel> |
| 136 | + <ResizableHandle className="bg-gray-400 hover:bg-gray-600 w-1 cursor-col-resize" /> |
| 137 | + <ResizablePanel> |
| 138 | + <div className="h-full overflow-y-auto"> |
| 139 | + <CodeOutputPanel /> |
| 140 | + </div> |
| 141 | + </ResizablePanel> |
| 142 | + </ResizablePanelGroup> |
| 143 | + </ResizablePanel> |
| 144 | + </ResizablePanelGroup> |
| 145 | + )} |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + ); |
| 149 | +} |
0 commit comments