Skip to content

Commit 84ada41

Browse files
committed
Add auto-detection of language based on user input
1 parent f21e9c1 commit 84ada41

File tree

6 files changed

+150
-14
lines changed

6 files changed

+150
-14
lines changed

apps/frontend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"@ant-design/icons": "^5.5.1",
1313
"@ant-design/nextjs-registry": "^1.0.1",
1414
"@codemirror/lang-javascript": "^6.2.2",
15+
"@codemirror/lang-python": "^6.1.6",
16+
"@codemirror/language": "^6.10.3",
1517
"@codemirror/state": "^6.4.1",
1618
"antd": "^5.20.6",
1719
"codemirror": "^6.0.1",

apps/frontend/pnpm-lock.yaml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/frontend/src/app/collaboration/[id]/page.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import CollaborativeEditor from "@/components/CollaborativeEditor/CollaborativeE
3131
interface CollaborationProps {}
3232

3333
export default function CollaborationPage(props: CollaborationProps) {
34+
const router = useRouter();
35+
3436
const [isLoading, setIsLoading] = useState<boolean>(false);
3537

3638
// Code Editor States
@@ -115,6 +117,17 @@ export default function CollaborationPage(props: CollaborationProps) {
115117
},
116118
];
117119

120+
const handleCloseCollaboration = () => {
121+
// Remove localstorage variables for collaboration
122+
localStorage.removeItem("user");
123+
localStorage.removeItem("matchedUser");
124+
localStorage.removeItem("collaId");
125+
localStorage.removeItem("docRefId");
126+
127+
// Redirect back to matching page
128+
router.push("/matching");
129+
};
130+
118131
return (
119132
<Layout className="collaboration-layout">
120133
<Header selectedKey={undefined} />
@@ -180,19 +193,20 @@ export default function CollaborationPage(props: CollaborationProps) {
180193
Submit
181194
</Button>
182195
</div>
183-
<div className="code-second-container">
196+
{/* <div className="code-second-container">
184197
<div className="code-language">Select Language:</div>
185198
<Select
186199
className="language-select"
187200
defaultValue={selectedLanguage}
188201
options={ProgrammingLanguageOptions}
189202
onSelect={(val) => setSelectedLanguage(val)}
190203
/>
191-
</div>
192-
{collaborationId && currentUser && (
204+
</div> */}
205+
{collaborationId && currentUser && selectedLanguage && (
193206
<CollaborativeEditor
194207
user={currentUser}
195208
collaborationId={collaborationId}
209+
language={selectedLanguage}
196210
/>
197211
)}
198212
</div>
@@ -206,7 +220,10 @@ export default function CollaborationPage(props: CollaborationProps) {
206220
<ClockCircleOutlined className="title-icons" />
207221
Session Details
208222
</div>
209-
<Button danger>End</Button>
223+
{/* TODO: End the collaboration session, cleanup the localstorage variables */}
224+
<Button danger onClick={handleCloseCollaboration}>
225+
End
226+
</Button>
210227
</div>
211228

212229
<div className="session-duration">

apps/frontend/src/app/collaboration/[id]/styles.scss

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@
7474
padding: 1rem;
7575
}
7676

77-
.code-second-container {
78-
display: flex;
79-
margin: 4px 0px;
80-
}
77+
// .code-second-container {
78+
// display: flex;
79+
// margin: 4px 0px;
80+
// }
8181

8282
.code-language {
8383
margin: auto 8px auto 0px;
@@ -150,7 +150,6 @@
150150
}
151151

152152
.topic-label,
153-
.code-language,
154153
.session-duration,
155154
.session-matched-user-label {
156155
font-size: 14px;

apps/frontend/src/components/CollaborativeEditor/CollaborativeEditor.tsx

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
// Referenced from example in https://www.npmjs.com/package/y-codemirror.next
2-
import React, { useEffect, useRef } from "react";
2+
import React, { useEffect, useRef, useState } from "react";
33
import * as Y from "yjs";
44
import { yCollab } from "y-codemirror.next";
55
import { WebrtcProvider } from "y-webrtc";
66
import { EditorView, basicSetup } from "codemirror";
7-
import { EditorState } from "@codemirror/state";
7+
import { EditorState, Compartment } from "@codemirror/state";
88
import { javascript } from "@codemirror/lang-javascript";
9+
import { python, pythonLanguage } from "@codemirror/lang-python";
910
import "./styles.scss";
10-
import { message } from "antd";
11+
import { message, Select } from "antd";
12+
import { language } from "@codemirror/language";
13+
import { ProgrammingLanguageOptions } from "@/utils/SelectOptions";
1114

1215
interface CollaborativeEditorProps {
1316
user: string;
1417
collaborationId: string;
18+
language: string;
1519
}
1620

1721
export const usercolors = [
@@ -31,6 +35,29 @@ export const userColor =
3135

3236
const CollaborativeEditor = (props: CollaborativeEditorProps) => {
3337
const editorRef = useRef(null);
38+
// const viewRef = useRef<EditorView | null>(null);
39+
const [selectedLanguage, setSelectedLanguage] = useState("javascript");
40+
const [trigger, setTrigger] = useState(false);
41+
42+
const languageConf = new Compartment();
43+
44+
const autoLanguage = EditorState.transactionExtender.of((tr) => {
45+
if (!tr.docChanged) return null;
46+
const docIsPython = /^\s*def\s|\s*class\s/.test(
47+
tr.newDoc.sliceString(0, 100)
48+
);
49+
const stateIsPython = tr.startState.facet(language) === pythonLanguage;
50+
if (docIsPython === stateIsPython) return null;
51+
52+
const newLanguage = docIsPython ? "python" : "javascript";
53+
setSelectedLanguage(newLanguage);
54+
55+
return {
56+
effects: languageConf.reconfigure(
57+
newLanguage === "python" ? python() : javascript()
58+
),
59+
};
60+
});
3461

3562
const [messageApi, contextHolder] = message.useMessage();
3663

@@ -55,6 +82,29 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
5582
});
5683
};
5784

85+
// const handleLanguageChange = (val: any) => {
86+
// console.log("came in here");
87+
// console.log(val);
88+
// setSelectedLanguage(val);
89+
90+
// let languageExtension;
91+
// switch (val) {
92+
// case "python":
93+
// languageExtension = python();
94+
// break;
95+
// default:
96+
// languageExtension = javascript();
97+
// }
98+
99+
// // Update the language configuration
100+
// if (viewRef.current) {
101+
// console.log("insude here");
102+
// viewRef.current.dispatch({
103+
// effects: languageConf.reconfigure(languageExtension),
104+
// });
105+
// }
106+
// };
107+
58108
useEffect(() => {
59109
if (process.env.NEXT_PUBLIC_SIGNALLING_SERVICE_URL === undefined) {
60110
error("Missing Signalling Service Url");
@@ -78,7 +128,8 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
78128
doc: ytext.toString(),
79129
extensions: [
80130
basicSetup,
81-
javascript(),
131+
languageConf.of(javascript()),
132+
autoLanguage,
82133
yCollab(ytext, provider.awareness, { undoManager }),
83134
],
84135
});
@@ -88,9 +139,16 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
88139
parent: editorRef.current || undefined,
89140
});
90141

142+
// viewRef.current = new EditorView({
143+
// state: state,
144+
// parent: editorRef.current || undefined,
145+
// });
146+
91147
return () => {
92148
// Cleanup on component unmount
149+
console.log("unmounting collaboration editor"); // TODO: remove
93150
view.destroy();
151+
// viewRef.current?.destroy();
94152
provider.disconnect();
95153
ydoc.destroy();
96154
};
@@ -99,7 +157,27 @@ const CollaborativeEditor = (props: CollaborativeEditorProps) => {
99157
return (
100158
<>
101159
{contextHolder}
102-
<div ref={editorRef} id="editor" className="code-editor-yjs" />
160+
<div className="code-second-container">
161+
<div className="code-language">Select Language:</div>
162+
<Select
163+
className="language-select"
164+
defaultValue={selectedLanguage}
165+
options={ProgrammingLanguageOptions}
166+
onSelect={(val) => setSelectedLanguage(val)}
167+
/>
168+
</div>
169+
<div
170+
ref={editorRef}
171+
style={{ height: "400px", border: "1px solid #ddd" }}
172+
/>
173+
<div className="language-detected">
174+
<strong>Current Language Detected: </strong>{" "}
175+
{
176+
ProgrammingLanguageOptions.find(
177+
(language) => language.value === selectedLanguage
178+
)?.label
179+
}
180+
</div>
103181
</>
104182
);
105183
};

apps/frontend/src/components/CollaborativeEditor/styles.scss

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@
44
border: 1px solid #cccccc;
55
overflow-y: scroll;
66
}
7+
8+
.code-language {
9+
font-size: 14px;
10+
font-weight: bold;
11+
}
12+
13+
.code-second-container {
14+
display: flex;
15+
margin: 4px 0px;
16+
}
17+
18+
.language-detected {
19+
margin: 4px 0px;
20+
}

0 commit comments

Comments
 (0)