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-03-04 15:40:14 +03:00
|
|
|
|
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 16:28:00 +03:00
|
|
|
import * as api 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-03-04 15:40:14 +03:00
|
|
|
import { withErrorHandlingAsync } from './util';
|
|
|
|
import { toast } from 'react-toastify';
|
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-16 16:31:26 +03:00
|
|
|
const [nickName, setNickName] = useState('');
|
2021-01-14 03:15:07 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-01-17 20:22:10 +03:00
|
|
|
createClient(relayNode)
|
|
|
|
.then((client) => setClient(client))
|
|
|
|
.catch((err) => console.log('Client initialization failed', err));
|
2021-01-14 03:15:07 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const joinRoom = async () => {
|
2021-01-14 12:17:36 +03:00
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:40:14 +03:00
|
|
|
await withErrorHandlingAsync(async () => {
|
|
|
|
await api.join(client, nickName);
|
|
|
|
setIsInRoom(true);
|
|
|
|
});
|
2021-01-14 03:15:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const leaveRoom = async () => {
|
2021-01-14 12:17:36 +03:00
|
|
|
if (!client) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-04 15:40:14 +03:00
|
|
|
await withErrorHandlingAsync(async () => {
|
|
|
|
await api.leave(client);
|
|
|
|
setIsInRoom(false);
|
|
|
|
});
|
2021-01-14 03:15:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FluenceClientContext.Provider value={client}>
|
2021-01-16 16:25:28 +03:00
|
|
|
<div className="header-wrapper">
|
|
|
|
<div className="header">
|
|
|
|
<div className="header-item">
|
|
|
|
{isInRoom && (
|
|
|
|
<button className="button" disabled={!isInRoom} onClick={leaveRoom}>
|
|
|
|
Leave
|
|
|
|
</button>
|
|
|
|
)}
|
2021-01-14 03:15:07 +03:00
|
|
|
</div>
|
2021-01-16 16:25:28 +03:00
|
|
|
|
|
|
|
<div className="header-item">
|
|
|
|
Connection status: {client ? <span className="accent">connected</span> : 'disconnected'}
|
2021-01-16 00:48:57 +03:00
|
|
|
</div>
|
2021-01-14 03:15:07 +03:00
|
|
|
</div>
|
2021-01-16 16:25:28 +03:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div className="content">
|
|
|
|
{!isInRoom && (
|
2021-01-16 20:37:40 +03:00
|
|
|
<form
|
|
|
|
className="welcome-form"
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
joinRoom();
|
|
|
|
}}
|
|
|
|
>
|
2021-01-16 16:25:28 +03:00
|
|
|
<h1 className="form-caption">Welcome to FluentPad</h1>
|
|
|
|
<input
|
|
|
|
className="text-input"
|
|
|
|
placeholder="Your name"
|
|
|
|
type="text"
|
|
|
|
value={nickName}
|
|
|
|
disabled={isInRoom}
|
|
|
|
onChange={(e) => {
|
|
|
|
const name = e.target.value;
|
|
|
|
setNickName(name);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2021-01-16 20:37:40 +03:00
|
|
|
<input
|
|
|
|
type="submit"
|
2021-01-16 16:31:26 +03:00
|
|
|
className="join-button"
|
|
|
|
disabled={isInRoom || !client || !nickName}
|
2021-01-16 20:37:40 +03:00
|
|
|
value="Join"
|
|
|
|
/>
|
|
|
|
</form>
|
2021-01-16 16:25:28 +03:00
|
|
|
)}
|
2021-01-14 03:15:07 +03:00
|
|
|
|
2021-01-16 16:25:28 +03:00
|
|
|
{isInRoom && (
|
|
|
|
<div className="room-wrapper">
|
|
|
|
<h1 className="fluent-pad">FluentPad</h1>
|
|
|
|
<UserList selfName={nickName} />
|
|
|
|
<CollaborativeEditor />
|
|
|
|
</div>
|
|
|
|
)}
|
2021-01-14 13:17:48 +03:00
|
|
|
</div>
|
2021-01-14 03:15:07 +03:00
|
|
|
</div>
|
|
|
|
</FluenceClientContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|