Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change termination logic #208

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/collaboration-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"test": "jest",
"lint": "eslint . --fix --no-error-on-unmatched-pattern && prettier --write --ignore-unknown .",
"start": "npm run build && node dist/server.js",
"dev": "nodemon src/server.ts"
"dev": "nodemon src/server.ts",
"format": "prettier --write ."
},
"license": "MIT",
"dependencies": {
Expand Down
13 changes: 13 additions & 0 deletions backend/collaboration-service/src/services/socketio.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ export class WebSocketConnection {
}
})

socket.on('end-session', async () => {
const socketsInRoom = this.io.sockets.adapter.rooms.get(roomId)
if (socketsInRoom) {
socketsInRoom.forEach((socketId) => {
const socket = this.io.sockets.sockets.get(socketId)
if (socket) {
socket.leave(roomId)
socket.disconnect(true)
}
})
}
})

socket.on('disconnect', async () => {
const room = this.io.sockets.adapter.rooms.get(roomId)
socket.leave(roomId)
Expand Down
36 changes: 32 additions & 4 deletions frontend/pages/code/[id]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use client'

import { EndIcon, PlayIcon } from '@/assets/icons'
import { ChatModel, ICollabDto, LanguageMode, getCodeMirrorLanguage } from '@repo/collaboration-types'
import { useEffect, useRef, useState } from 'react'
Expand Down Expand Up @@ -25,6 +27,7 @@ import { ISubmission } from '@repo/submission-types'
import { mapLanguageToJudge0 } from '@/util/language-mapper'
import TestResult from '../test-result'
import { Cross1Icon } from '@radix-ui/react-icons'
import ConfirmDialog from '@/components/customs/confirm-dialog'
import { getChatHistory, getCollabHistory } from '@/services/collaboration-service-api'
import ReadOnlyCodeMirrorEditor from '../read-only-editor'
import { ResultModel } from '@repo/collaboration-types'
Expand All @@ -49,11 +52,12 @@ export default function Code() {
const [isOtherUserOnline, setIsOtherUserOnline] = useState(true)
const [isCodeRunning, setIsCodeRunning] = useState(false)
const [activeTest, setActiveTest] = useState(0)
const [isViewOnly, setIsViewOnly] = useState(true)
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [retry, setRetry] = useState(0)
const [testResult, setTestResult] = useState<{ data: ResultModel | undefined; expectedOutput: string } | undefined>(
undefined
)
const [isViewOnly, setIsViewOnly] = useState(true)

const retrieveMatchDetails = async (matchId: string) => {
const response = await getMatchDetails(matchId).catch((err) => {
Expand Down Expand Up @@ -94,7 +98,6 @@ export default function Code() {
})

socketRef.current.on('connect', async () => {
console.log('hi')
if (socketRef.current) {
socketRef.current.emit('joinRoom', { roomId: id })
setChatData((await getChatHistory(id as string)) ?? [])
Expand Down Expand Up @@ -130,6 +133,12 @@ export default function Code() {
}
})

socketRef.current.on('disconnect', () => {
if (!isViewOnly) {
router.push('/')
}
})

socketRef.current.on('receive_message', (data: ChatModel) => {
setChatData((prev) => [...prev, data])
})
Expand Down Expand Up @@ -184,10 +193,18 @@ export default function Code() {
router.push('/sessions')
return
}

if (socketRef.current) {
setIsDialogOpen(true)
}
}

function handleEndSessionConfirmation() {
if (socketRef.current) {
socketRef.current.disconnect()
socketRef.current?.emit('end-session')
router.push('/')
}
router.push('/')
setIsDialogOpen(false)
}

const renderCloseButton = () => {
Expand Down Expand Up @@ -292,6 +309,17 @@ export default function Code() {
<Button className="bg-red hover:bg-red-dark" onClick={handleEndSession}>
{renderCloseButton()}
</Button>
<ConfirmDialog
showCancelButton
dialogData={{
title: 'Warning!',
content:
'Are you sure you want to end the session? This will permanently end the session for both you and the other participant.',
isOpen: isDialogOpen,
}}
closeHandler={() => setIsDialogOpen(false)}
confirmHandler={handleEndSessionConfirmation}
/>
</div>
</div>
<div id="editor-container" className="mt-4">
Expand Down
Loading