2021-01-16 00:48:57 +03:00
|
|
|
import { createClient, FluenceClient } from '@fluencelabs/fluence';
|
2021-01-14 03:15:07 +03:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import './App.scss';
|
2021-01-16 13:24:26 +03:00
|
|
|
import { FluenceClientContext } from '../app/FluenceClientContext';
|
2021-01-14 03:15:07 +03:00
|
|
|
import { UserList } from './UserList';
|
2021-01-16 13:24:26 +03:00
|
|
|
import * as calls from 'src/app/api';
|
2021-01-14 13:17:48 +03:00
|
|
|
import { CollaborativeEditor } from './CollaborativeEditor';
|
2021-01-16 13:24:26 +03:00
|
|
|
import { relayNode } from 'src/app/constants';
|
2021-01-14 03:15:07 +03:00
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
const [client, setClient] = useState<FluenceClient | null>(null);
|
2021-01-14 12:17:36 +03:00
|
|
|
const [isInRoom, setIsInRoom] = useState<boolean>(false);
|
2021-01-14 03:15:07 +03:00
|
|
|
const [nickName, setNickName] = useState('myNickName');
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fn = async () => {
|
2021-01-16 13:24:26 +03:00
|
|
|
const c = await createClient(relayNode);
|
2021-01-16 00:48:57 +03:00
|
|
|
|
2021-01-14 03:15:07 +03:00
|
|
|
setClient(c);
|
|
|
|
};
|
2021-01-14 12:17:36 +03:00
|
|
|
fn();
|
2021-01-14 03:15:07 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const joinRoom = async () => {
|
2021-01-14 12:17:36 +03:00
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-14 19:38:45 +03:00
|
|
|
await calls.join(client, nickName);
|
2021-01-14 03:15:07 +03:00
|
|
|
setIsInRoom(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const leaveRoom = async () => {
|
2021-01-14 12:17:36 +03:00
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:02:42 +03:00
|
|
|
await calls.leave(client);
|
2021-01-14 03:15:07 +03:00
|
|
|
setIsInRoom(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FluenceClientContext.Provider value={client}>
|
|
|
|
<div className="App">
|
|
|
|
<div>
|
2021-01-14 12:17:36 +03:00
|
|
|
<div>Connection status: {client ? 'connected' : 'disconnected'}</div>
|
2021-01-14 03:15:07 +03:00
|
|
|
<div>
|
|
|
|
<label>Nickname: </label>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={nickName}
|
|
|
|
disabled={isInRoom}
|
|
|
|
onChange={(e) => {
|
|
|
|
const name = e.target.value;
|
|
|
|
setNickName(name);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
2021-01-14 12:17:36 +03:00
|
|
|
<button disabled={isInRoom || !client} onClick={joinRoom}>
|
2021-01-14 03:15:07 +03:00
|
|
|
Join Room
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<button disabled={!isInRoom} onClick={leaveRoom}>
|
|
|
|
Leave Room
|
|
|
|
</button>
|
|
|
|
</div>
|
2021-01-16 00:48:57 +03:00
|
|
|
<div>
|
|
|
|
<button onClick={() => calls.clean(client!)}>Clean</button>
|
|
|
|
</div>
|
2021-01-14 03:15:07 +03:00
|
|
|
</div>
|
|
|
|
|
2021-01-14 13:17:48 +03:00
|
|
|
<div className="wrapper">
|
2021-01-14 23:57:24 +03:00
|
|
|
<div>{isInRoom && client && <CollaborativeEditor />}</div>
|
2021-01-16 00:48:57 +03:00
|
|
|
<div>{isInRoom && client && <UserList selfName={nickName} />}</div>
|
2021-01-14 13:17:48 +03:00
|
|
|
</div>
|
2021-01-14 03:15:07 +03:00
|
|
|
</div>
|
|
|
|
</FluenceClientContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|