|
1 | | -import React from 'react'; |
| 1 | +'use client'; |
2 | 2 |
|
3 | | -type Props = {}; |
| 3 | +import ProblemDescriptionPanel from '@/components/problems/ProblemDescriptionPanel'; |
| 4 | +import ProblemTable from '@/components/problems/ProblemTable'; |
| 5 | +import { Button } from '@/components/ui/button'; |
| 6 | +import { |
| 7 | + Select, |
| 8 | + SelectContent, |
| 9 | + SelectItem, |
| 10 | + SelectTrigger, |
| 11 | + SelectValue, |
| 12 | +} from '@/components/ui/select'; |
| 13 | +import { Textarea } from '@/components/ui/textarea'; |
| 14 | +import { useFilteredProblems } from '@/hooks/useFilteredProblems'; |
| 15 | +import { DEFAULT_CODE, SUPPORTED_PROGRAMMING_LANGUAGES } from '@/lib/constants'; |
| 16 | +import { Problem } from '@/types/types'; |
| 17 | +import { UserCircle } from 'lucide-react'; |
| 18 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
4 | 19 |
|
5 | | -const page = (props: Props) => { |
6 | | - return <div>This is the collaboration page</div>; |
| 20 | +const CollaborationPage = () => { |
| 21 | + const [selectionProblem, setSelectionProblem] = useState<Problem | null>( |
| 22 | + null, |
| 23 | + ); |
| 24 | + const [code, setCode] = useState(DEFAULT_CODE); |
| 25 | + const [language, setLanguage] = useState(SUPPORTED_PROGRAMMING_LANGUAGES[0]); |
| 26 | + const { problems, isLoading } = useFilteredProblems(); |
| 27 | + |
| 28 | + // Layout states |
| 29 | + const [leftWidth, setLeftWidth] = useState(50); |
| 30 | + const isDragging = useRef(false); |
| 31 | + const containerRef = useRef<HTMLDivElement>(null); |
| 32 | + |
| 33 | + // Handle dragging of the divider |
| 34 | + const handleMouseDown = useCallback(() => { |
| 35 | + isDragging.current = true; |
| 36 | + }, []); |
| 37 | + |
| 38 | + const handleMouseUp = useCallback(() => { |
| 39 | + isDragging.current = false; |
| 40 | + }, []); |
| 41 | + |
| 42 | + const handleMouseMove = useCallback((e: MouseEvent) => { |
| 43 | + if (!isDragging.current || !containerRef.current) return; |
| 44 | + const containerRect = containerRef.current.getBoundingClientRect(); |
| 45 | + const newLeftWidth = |
| 46 | + ((e.clientX - containerRect.left) / containerRect.width) * 100; |
| 47 | + setLeftWidth(Math.max(20, Math.min(80, newLeftWidth))); |
| 48 | + }, []); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + document.addEventListener('mousemove', handleMouseMove); |
| 52 | + document.addEventListener('mouseup', handleMouseUp); |
| 53 | + return () => { |
| 54 | + document.removeEventListener('mousemove', handleMouseMove); |
| 55 | + document.removeEventListener('mouseup', handleMouseUp); |
| 56 | + }; |
| 57 | + }, [handleMouseMove, handleMouseUp]); |
| 58 | + |
| 59 | + // Handle selection of a problem |
| 60 | + const handleCallback = (id: number) => { |
| 61 | + const problem = problems.find((p) => p._id === id); |
| 62 | + if (problem) { |
| 63 | + setSelectionProblem(problem); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div |
| 69 | + className="flex h-screen overflow-hidden bg-gray-900 p-6 pt-24 text-gray-100" |
| 70 | + ref={containerRef} |
| 71 | + > |
| 72 | + <div |
| 73 | + className="h-full overflow-y-auto p-6" |
| 74 | + style={{ width: `${leftWidth}%` }} |
| 75 | + > |
| 76 | + {selectionProblem ? ( |
| 77 | + <ProblemDescriptionPanel |
| 78 | + problem={selectionProblem} |
| 79 | + resetQuestion={() => setSelectionProblem(null)} |
| 80 | + /> |
| 81 | + ) : ( |
| 82 | + <> |
| 83 | + <h2 className="mb-4 text-2xl font-bold">Choose a question</h2> |
| 84 | + <ProblemTable |
| 85 | + problems={problems} |
| 86 | + isLoading={isLoading} |
| 87 | + rowCallback={handleCallback} |
| 88 | + /> |
| 89 | + </> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + |
| 93 | + <div |
| 94 | + className="flex w-2 cursor-col-resize items-center justify-center bg-gray-600 transition-colors duration-200 hover:bg-gray-500" |
| 95 | + onMouseDown={handleMouseDown} |
| 96 | + > |
| 97 | + <div className="h-8 w-1 rounded-full bg-gray-400" /> |
| 98 | + </div> |
| 99 | + |
| 100 | + <div |
| 101 | + className="flex h-full flex-col overflow-y-auto bg-gray-800 p-6" |
| 102 | + style={{ width: `${100 - leftWidth}%` }} |
| 103 | + > |
| 104 | + <div className="mb-4 flex justify-between"> |
| 105 | + <Select value={language} onValueChange={setLanguage}> |
| 106 | + <SelectTrigger className="w-32"> |
| 107 | + <SelectValue placeholder="Select Language" /> |
| 108 | + </SelectTrigger> |
| 109 | + <SelectContent> |
| 110 | + {SUPPORTED_PROGRAMMING_LANGUAGES.map((lang) => ( |
| 111 | + <SelectItem key={lang} value={lang}> |
| 112 | + {lang} |
| 113 | + </SelectItem> |
| 114 | + ))} |
| 115 | + </SelectContent> |
| 116 | + </Select> |
| 117 | + <div className="flex"> |
| 118 | + <Button |
| 119 | + variant="ghost" |
| 120 | + size="icon" |
| 121 | + className="relative h-8 w-8 rounded-full" |
| 122 | + onClick={() => { |
| 123 | + console.log('Clicked user'); |
| 124 | + }} |
| 125 | + > |
| 126 | + <UserCircle className="h-6 w-6 text-gray-300" /> |
| 127 | + </Button> |
| 128 | + <Button |
| 129 | + variant="ghost" |
| 130 | + size="icon" |
| 131 | + className="relative h-8 w-8 rounded-full" |
| 132 | + onClick={() => { |
| 133 | + console.log('Clicked user'); |
| 134 | + }} |
| 135 | + > |
| 136 | + <UserCircle className="h-6 w-6 text-gray-300" /> |
| 137 | + </Button> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + |
| 141 | + <Textarea |
| 142 | + value={code} |
| 143 | + onChange={(e) => setCode(e.target.value)} |
| 144 | + className="flex-grow overflow-y-auto bg-gray-900 font-mono text-gray-100" |
| 145 | + style={{ resize: 'none', height: 'calc(100% - 3rem)' }} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + ); |
7 | 150 | }; |
8 | 151 |
|
9 | | -export default page; |
| 152 | +export default CollaborationPage; |
0 commit comments