85 lines
2.6 KiB
TypeScript
Raw Normal View History

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