Skip to content

Commit 4621461

Browse files
committed
Add lang / execution sync
1 parent 2666839 commit 4621461

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

frontend/src/app/components/code-editor/CodeEditor.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import {
33
codeLangAtom,
44
codeMirrorValueAtom,
5+
innkeeperWriteAtom,
56
isMatchedAtom,
67
} from "@/libs/room-jotai";
78
import { ExclamationCircleFilled } from "@ant-design/icons";
89
import { Button } from "antd";
9-
import { useAtom, useAtomValue, useSetAtom } from "jotai";
10+
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
1011
import dynamic from "next/dynamic";
1112
import { useEffect, useState } from "react";
1213
import Skeleton from "react-loading-skeleton";
@@ -45,6 +46,17 @@ const UiElementOnClose = () => {
4546
);
4647
};
4748

49+
const codeLangAtomWrapper = atom(
50+
(get) => get(codeLangAtom),
51+
(_get, set, lang: string) => {
52+
set(codeLangAtom, lang);
53+
set(innkeeperWriteAtom, {
54+
eventName: "sendUpdate",
55+
eventArgs: [{ language: lang }],
56+
});
57+
},
58+
);
59+
4860
const CodeMirrorEditor = ({
4961
userId,
5062
authToken,
@@ -58,7 +70,7 @@ const CodeMirrorEditor = ({
5870
const setCodeMirrorValue = useSetAtom(codeMirrorValueAtom);
5971
const isMatched = useAtomValue(isMatchedAtom);
6072

61-
const [selectedLanguage, setSelectedLanguage] = useAtom(codeLangAtom);
73+
const [selectedLanguage, setSelectedLanguage] = useAtom(codeLangAtomWrapper);
6274
const [languageExtension, setLanguageExtension] = useState<any>(null);
6375
const [dragging, setDragging] = useState<boolean>(false);
6476
const [startY, setStartY] = useState<number>(0); // To track the Y position where drag started

frontend/src/app/components/status-bar/StatusBar.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const triggerExecutionRequestAtom = atom(null, async (get, set) => {
2121
const codeLang = get(codeLangAtom);
2222
const result = await executeCode(code, codeLang);
2323
set(resultAtom, result);
24+
set(innkeeperWriteAtom, {
25+
eventName: "sendUpdate",
26+
eventArgs: [{ executionResult: code }],
27+
});
2428
});
2529

2630
const triggerExitRoomRequestAtom = atom(null, (get, set) => {

frontend/src/app/hooks/useInnKeeper.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {
22
JotaiInnkeeperListenAdapter,
33
chatHistoryAtom,
4+
codeLangAtom,
45
isConnectedAtom,
56
isMatchedAtom,
67
questionIdAtom,
8+
resultAtom,
79
roomStateAtom,
810
socketAtom,
911
userStatesAtom,
@@ -22,6 +24,8 @@ function _useInnkeeperSocket(authToken: string | null) {
2224
const setUserStates = useSetAtom(userStatesAtom);
2325
const setQuestionId = useSetAtom(questionIdAtom);
2426
const setChatHistory = useSetAtom(chatHistoryAtom);
27+
const setLanguage = useSetAtom(codeLangAtom);
28+
const setResult = useSetAtom(resultAtom);
2529

2630
const jotaiAdapter: JotaiInnkeeperListenAdapter = {
2731
connect() {
@@ -69,6 +73,9 @@ function _useInnkeeperSocket(authToken: string | null) {
6973
if (partialUpdate.chatHistory) setChatHistory(partialUpdate.chatHistory);
7074
if (partialUpdate.userStates) setUserStates(partialUpdate.userStates);
7175
if (partialUpdate.questionId) setQuestionId(partialUpdate.questionId);
76+
if (partialUpdate.language) setLanguage(partialUpdate.language);
77+
if (partialUpdate.executionResult)
78+
setResult(partialUpdate.executionResult);
7279
},
7380

7481
closeRoom(finalUpdate) {

frontend/src/libs/innkeeper-api-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export type PartialRoomState = {
4949
questionId?: string;
5050
userStates?: [UserState, UserState];
5151
chatHistory?: ChatMessage[];
52+
executionResult?: string;
53+
language?: string;
5254
questionDifficulty?: "EASY" | "MEDIUM" | "HARD";
5355
};
5456

innkeeper/src/types/innkeeper-api-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export type PartialRoomState = {
4949
questionId?: string;
5050
userStates?: [UserState, UserState];
5151
chatHistory?: ChatMessage[];
52+
executionResult?: string;
53+
language?: string;
5254
questionDifficulty?: 'EASY' | 'MEDIUM' | 'HARD';
5355
};
5456

0 commit comments

Comments
 (0)