Skip to content

Commit 6eba964

Browse files
committed
Add provider awareness
1 parent 85464a9 commit 6eba964

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

frontend/src/domain/context/CollaborationContext.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MonacoBinding } from "y-monaco";
66
import { WebsocketProvider } from "y-websocket";
77
import { useAuth } from "./AuthContext";
88
import { toast } from "react-toastify";
9-
import { AWARENESS_KEYS } from "presentation/utils/constants";
9+
import { COLLABORATION_AWARENESS_KEYS, COLLABORATION_YMAP_KEYS } from "presentation/utils/constants";
1010

1111
interface CollaborationContextType {
1212
initialiseEditor: (roomId: string, editor: any, monaco: Monaco) => void;
@@ -18,9 +18,10 @@ const CollaborationContext = createContext<CollaborationContextType | undefined>
1818

1919
export const CollaborationProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
2020
const { user } = useAuth();
21-
const userId = user?._id;
21+
const username = user?.username;
2222

23-
const { USER_ID, SELECTED_LANGUAGE } = AWARENESS_KEYS;
23+
const { USERNAME } = COLLABORATION_AWARENESS_KEYS;
24+
const { SELECTED_LANGUAGE } = COLLABORATION_YMAP_KEYS;
2425

2526
const [selectedLanguage, setSelectedLanguage] = useState<string>("javascript");
2627
const [languages, setLanguages] = useState<{ label: string; value: string }[]>([]);
@@ -40,6 +41,7 @@ export const CollaborationProvider: React.FC<{ children: ReactNode }> = ({ child
4041
const { yDoc, provider, yMap } = initialiseYdoc(roomId);
4142
bindEditorToDoc(editor, yDoc, provider);
4243
setUpObserver(yMap);
44+
setUpConnectionAwareness(provider);
4345
};
4446

4547
const initialiseLanguages = (monaco: Monaco) => {
@@ -63,7 +65,9 @@ export const CollaborationProvider: React.FC<{ children: ReactNode }> = ({ child
6365
// Test locally across browers with 'HOST=localhost PORT 1234 npx y-websocket'
6466
const provider = new WebsocketProvider("ws://localhost:1234", roomId, yDoc);
6567
provider.on("status", (event: any) => {
66-
console.log(event.status);
68+
if (event.status === "disconnected") {
69+
toast.error("You have disconnected");
70+
}
6771
});
6872
providerRef.current = provider;
6973
return { yDoc, yMap, provider };
@@ -80,6 +84,7 @@ export const CollaborationProvider: React.FC<{ children: ReactNode }> = ({ child
8084
bindingRef.current = binding;
8185
};
8286

87+
// Observer to listen to any changes to shared state (e.g. language changes)
8388
const setUpObserver = (yMap: Y.Map<string>) => {
8489
yMap.observe((event) => {
8590
event.changes.keys.forEach((change, key) => {
@@ -93,6 +98,15 @@ export const CollaborationProvider: React.FC<{ children: ReactNode }> = ({ child
9398
});
9499
};
95100

101+
// Observer to listen to any changes to users' presence (e.g. connection status, is typing, cursor)
102+
const setUpConnectionAwareness = (provider: WebsocketProvider) => {
103+
provider.awareness.setLocalStateField(USERNAME, username);
104+
provider.awareness.on("change", (update: any) => {
105+
const users = provider.awareness.getStates();
106+
// TODO: Some UI feedback about connection status of the other user
107+
});
108+
};
109+
96110
useEffect(() => {
97111
return () => {
98112
bindingRef.current?.destroy();

frontend/src/presentation/utils/constants.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ export const QUESTIONS_FILTER_TEXT = {
5555
SELECT_TITLE: "Search by title"
5656
};
5757

58-
5958
export const FILTER_DIFFICULTY_TEXT = {
6059
ALL: "All" as QuestionDifficulty | "All",
6160
EASY: "Easy" as QuestionDifficulty,
6261
MEDIUM: "Medium" as QuestionDifficulty,
6362
HARD: "Hard" as QuestionDifficulty
6463
};
6564

66-
6765
export const LANDING_CARD_TEXT = {
6866
ADMIN_WELCOME: "Welcome, Admin",
6967
ADMIN_INSTRUCTIONS: "You can add or select a question to get started.",
@@ -72,7 +70,6 @@ export const LANDING_CARD_TEXT = {
7270
PEER_INSTRUCTIONS: "Please select a question from the list to begin with your peer."
7371
};
7472

75-
7673
export const QUESTION_FORM_FIELDS = {
7774
FIELD_TITLE: { label: "Title", name: "title" },
7875
FIELD_DESCRIPTION: { label: "Description", name: "description" },
@@ -95,7 +92,10 @@ export const SIGN_UP_FORM_FIELDS = {
9592
FIELD_CONFIRM_PASSWORD: { label: "Confirm password", name: "confirmPassword" }
9693
};
9794

98-
export const AWARENESS_KEYS = {
99-
USER_ID: "userId",
95+
export const COLLABORATION_AWARENESS_KEYS = {
96+
USERNAME: "username"
97+
};
98+
99+
export const COLLABORATION_YMAP_KEYS = {
100100
SELECTED_LANGUAGE: "selectedLanguage"
101-
}
101+
};

0 commit comments

Comments
 (0)