110 lines
3.7 KiB
TypeScript
Raw Normal View History

import { createClient, FluenceClient } from '@fluencelabs/fluence';
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';
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';
const App = () => {
const [client, setClient] = useState<FluenceClient | null>(null);
const [isInRoom, setIsInRoom] = useState<boolean>(false);
2021-01-16 16:31:26 +03:00
const [nickName, setNickName] = useState('');
useEffect(() => {
2021-01-17 20:22:10 +03:00
createClient(relayNode)
.then((client) => setClient(client))
.catch((err) => console.log('Client initialization failed', err));
}, []);
const joinRoom = async () => {
if (!client) {
return;
}
2021-03-04 15:40:14 +03:00
await withErrorHandlingAsync(async () => {
await api.join(client, nickName);
setIsInRoom(true);
});
};
const leaveRoom = async () => {
if (!client) {
return;
}
2021-03-04 15:40:14 +03:00
await withErrorHandlingAsync(async () => {
await api.leave(client);
setIsInRoom(false);
});
};
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>
)}
</div>
2021-01-16 16:25:28 +03:00
<div className="header-item">
Connection status: {client ? <span className="accent">connected</span> : 'disconnected'}
</div>
</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-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>
</div>
</FluenceClientContext.Provider>
);
};
export default App;