Skip to content

Commit 7c793b1

Browse files
authored
Merge pull request #26 from oslabs-beta/Lauren
remote cursor removes upon join and updated occupancy
2 parents c31003c + af3a48b commit 7c793b1

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import store from '../../redux/store';
4848
import {
4949
initializeSocket,
5050
getSocket,
51+
emitEvent,
5152
disconnectSocket
5253
} from '../../helperFunctions/socket';
5354
import {
@@ -447,8 +448,7 @@ const RoomsContainer = () => {
447448
fontSize: 'smaller'
448449
}}
449450
>
450-
Note: For the best experience, limit Collab room occupancy to 3
451-
people. Exceeding this limit may lead to app performance issues.
451+
Note: Max Occupancy: 6 Users
452452
</Typography>
453453
</Stack>
454454
</div>

app/src/components/main/Canvas.tsx

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { combineStyles } from '../../helperFunctions/combineStyles';
1616
import renderChildren from '../../helperFunctions/renderChildren';
1717
import { emitEvent, getSocket } from '../../helperFunctions/socket';
1818
import { GiBoba } from 'react-icons/gi';
19-
import { Console } from 'console';
2019

2120
function Canvas(props: {}): JSX.Element {
2221
const state = useSelector((store: RootState) => store.appState);
@@ -28,20 +27,9 @@ function Canvas(props: {}): JSX.Element {
2827
//-------cursors tracking-------
2928
console.log('canvas is rendered');
3029

31-
//remote cursor data
32-
3330
const [remoteCursors, setRemoteCursors] = useState([]);
34-
35-
//toggle switch
3631
const [toggleSwitch, setToggleSwitch] = useState(true);
3732

38-
// toggle text
39-
const [toggleText, setToggleText] = useState('off');
40-
41-
const toggleButton = () => {
42-
setToggleText(toggleText === 'on' ? 'off' : 'on');
43-
};
44-
4533
const debounceSetPosition = debounce((newX, newY) => {
4634
//emit socket event every 300ms when cursor moves
4735
if (userList.length > 1)
@@ -93,6 +81,19 @@ function Canvas(props: {}): JSX.Element {
9381
});
9482
};
9583

84+
console.log('userList:', userList);
85+
//[{x,y,remoteUserName, isVisible}, {...}, {...}]
86+
console.log('remoteCursors:', remoteCursors);
87+
88+
const handleCursorDeleteFromServer = () => {
89+
setRemoteCursors((prevRemoteCursors) =>
90+
prevRemoteCursors.map((cursor) => ({
91+
...cursor,
92+
isVisible: userList.includes(cursor.remoteUserName)
93+
}))
94+
);
95+
};
96+
9697
const handleToggleSwitch = () => {
9798
setToggleSwitch(!toggleSwitch);
9899
//checks the state before it's updated so need to check the opposite condition
@@ -124,15 +125,8 @@ function Canvas(props: {}): JSX.Element {
124125

125126
console.log('Toggle Switch:', toggleSwitch);
126127

127-
//Function to handle the click events.
128-
const multipleClicks = () => {
129-
handleToggleSwitch();
130-
toggleButton();
131-
};
132-
133128
const socket = getSocket();
134-
/* wrap the socket event listener in useEffect with dependency array as [socket], so the effect will run only when:
135-
1. After the initial rendering of the component 2. Every time the socket instance changes(connect, disconnect) */
129+
//wrap the socket event listener in useEffect with dependency array as [socket], so the the effect will run only when: 1. After the initial rendering of the component 2. Every time the socket instance changes(connect, disconnect)
136130
useEffect(() => {
137131
console.log(
138132
'socket inside useEffect:',
@@ -152,6 +146,11 @@ function Canvas(props: {}): JSX.Element {
152146
};
153147
}, [socket]);
154148

149+
useEffect(() => {
150+
handleCursorDeleteFromServer();
151+
console.log('handle delete has been called');
152+
}, [userList]);
153+
155154
//-----------------
156155

157156
// find the current component based on the canvasFocus component ID in the state
@@ -170,7 +169,7 @@ function Canvas(props: {}): JSX.Element {
170169
childId?: number | null
171170
) => {
172171
dispatch(changeFocus({ componentId, childId }));
173-
//if room exists, send focus dispatch to all users
172+
//if room exists, send focus dispatcht to all users
174173
if (roomCode) {
175174
emitEvent('changeFocusAction', roomCode, {
176175
componentId: componentId,
@@ -350,29 +349,19 @@ function Canvas(props: {}): JSX.Element {
350349
</div>
351350
)
352351
)}
353-
<label className="switch">
354-
{userList.length > 1 && (
355-
<button
356-
className="btn-toggle"
357-
onClick={multipleClicks}
358-
style={{
359-
position: 'fixed',
360-
width: 'max-content',
361-
height: 'max-content',
362-
bottom: '100px',
363-
left: '51vw',
364-
textAlign: 'center',
365-
color: '#ffffff',
366-
backgroundColor: '#151515',
367-
zIndex: 0,
368-
padding: '5px',
369-
borderColor: '#46C0A5',
370-
borderRadius: '5px'
371-
}}
372-
>
373-
{toggleText === 'on' ? 'View Cursors' : 'Hide Cursors'}
374-
</button>
375-
)}
352+
353+
<label
354+
className="switch"
355+
style={{
356+
position: 'relative',
357+
display: 'inline-block',
358+
width: '60px',
359+
height: '34px'
360+
}}
361+
>
362+
<button className="btn-toggle" onClick={handleToggleSwitch}>
363+
On/Off
364+
</button>
376365
</label>
377366
</div>
378367
);

0 commit comments

Comments
 (0)