-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.tsx
165 lines (148 loc) · 4.09 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import './index.css'
import Editor from '@monaco-editor/react'
import { useSocketIOProviderState } from '@textea/y-socket.io/hooks'
import {
createSocketIOProvider,
SocketIOProvider
} from '@textea/y-socket.io/provider'
import React, { useDeferredValue, useEffect, useState } from 'react'
import { MonacoBinding } from 'y-monaco'
import { Awareness } from 'y-protocols/awareness'
import * as Y from 'yjs'
import { ClientData } from './types'
const yDoc = new Y.Doc()
const type = yDoc.getText('javascript')
const roomId = 'test-id'
const awareness = new Awareness(yDoc)
type User = {
id: Awareness['clientID']
name: string
}
const DEFAULT_USER: Readonly<User> = {
id: awareness.clientID,
name: `ID_${awareness.clientID.toString(16).toUpperCase()}`
}
awareness.setLocalState(DEFAULT_USER)
let _monacoBinding: MonacoBinding
export const App: React.FC = () => {
const [text, setText] = useState('')
const [userName, setUserName] = useState(DEFAULT_USER.name)
const [otherUsers, setOtherUsers] = useState<User[]>([])
const [provider, setProvider] = useState<SocketIOProvider<ClientData>>()
const isConnecting = useSocketIOProviderState(provider,
(state) => state.connecting)
const isConnected = useSocketIOProviderState(provider,
(state) => state.connected)
const isSynced = useSocketIOProviderState(provider, (state) => state.synced)
const deferredIsSynced = useDeferredValue(isSynced)
const status = isConnecting
? 'Connecting'
: isConnected
? deferredIsSynced
? 'Synced'
: 'Syncing'
: 'Disconnected'
const clientData = useSocketIOProviderState(provider, (state) => state.data)
const role = isConnected
? clientData
? clientData.isOwner
? 'Admin'
: 'User'
: 'Loading...'
: 'Not Available'
useEffect(() => {
const handleAwarenessUpdate = () => {
const localUser = awareness.getLocalState() as User | null
if (localUser) {
setUserName(localUser.name)
}
setOtherUsers(
[...awareness.getStates().entries()].filter(
([clientId]) => clientId !== yDoc.clientID)
.map(([, state]) => state as User)
)
}
awareness.on('update', handleAwarenessUpdate)
const provider = createSocketIOProvider<ClientData>('ws://localhost:1234',
roomId, yDoc, {
awareness,
autoConnect: false
})
setProvider(provider)
return () => {
awareness.off('update', handleAwarenessUpdate)
provider.destroy()
}
}, [])
if (!provider) {
return <p>Loading...</p>
}
return (
<>
<p>
<button
onClick={() => {
if (isConnected) {
provider.disconnect()
} else {
provider.connect()
}
}}
>
{isConnected ? 'Disconnect' : 'Connect'}
</button>
{role === 'Admin' && (
<>
<span>{' '}</span>
<button onClick={provider.closeRoom}>
Close Room
</button>
</>
)}
</p>
<p>Status: {status}</p>
<p>Role: {role}</p>
<Editor
height='400px'
onMount={(editor) => {
const model = editor.getModel()
if (model == null) {
throw new Error('model is null')
}
_monacoBinding = new MonacoBinding(type, model, new Set([editor]),
awareness)
}}
value={text}
onChange={
(value) => {
if (value) {
setText(value)
}
}
}
defaultLanguage='javascript'
/>
<p>
<label htmlFor='name'>Name: </label>
<input
id='name'
value={userName}
onChange={(event) => {
awareness.setLocalStateField('name', event.target.value)
}}
/>
</p>
{otherUsers.length > 0 && (
<div>
<div>Users:</div>
<ul>
{otherUsers.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
</div>
)}
</>
)
}
export default App