-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.ts
71 lines (62 loc) · 1.63 KB
/
client.ts
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
import { HOST } from './app'
import { useState, useEffect, useContext } from 'preact/hooks'
import { JSX } from 'preact'
import { html } from 'htm/preact'
export const connectToSession = (id: string) => {
return fetch(`${HOST}/api/v1/meeting`, {
method: 'OPTIONS',
body: JSON.stringify({
id,
}),
})
.then((res) => {
if (res.status === 404 || res.status === 400) {
return false
} else {
return true
}
})
.catch((e) => {
document.write(e.message)
})
}
const Client = () => {
const [id, setId] = useState('')
const [connected, setConnected] = useState(false)
useEffect(() => {
chrome.storage.local.get(['id', 'mode'], (res) => {
if (res.mode == 'client') {
setId(res.id)
setConnected(true)
}
})
}, [])
const handleButton = () => {
if (!connected) {
connectToSession(id).then((ok) => {
if (ok) {
setConnected(true)
chrome.storage.local.set({ id })
chrome.storage.local.set({ mode: 'client' })
} else {
document.write('Connection error')
chrome.storage.local.remove(['id', 'mode'])
}
})
} else {
setConnected(false)
chrome.storage.local.remove(['mode', 'id', 'client_last_get_link'])
}
}
return html`<h1>Client</h1>
<input
disabled=${connected}
value=${id}
oninput=${(e: JSX.TargetedEvent<HTMLInputElement, Event>) => setId(e.currentTarget.value)}
placeholder="ID Session"
/>
<button onclick=${handleButton}>
${connected ? 'Disconnect' : 'Connect'}
</button>`
}
export default Client