-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
87 lines (73 loc) · 2.31 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { useState, useEffect, useRef, useContext } from 'preact/hooks'
import { render, html, Ref } from 'htm/preact'
import Client from './client'
import Host from './host'
export const HOST = 'https://poom.live:9091'
type Tab = 'client' | 'host' | '' | 'old_session'
const NavBar = ({ setTab, refs }: { setTab: (tab: Tab) => void; refs: Ref<HTMLButtonElement>[] }) => {
useEffect(() => {
chrome.storage.local.get(({ mode }) => {
if (mode === 'host') {
refs[1].current?.setAttribute('disabled', 'disabled')
} else if (mode === 'client') {
refs[0].current?.setAttribute('disabled', 'disabled')
}
})
}, [])
chrome.storage.onChanged.addListener((mode) => {
if (mode?.mode?.newValue === 'client') {
refs[0].current?.setAttribute('disabled', 'disabled')
} else if (mode?.mode?.newValue === 'host') {
refs[1].current?.setAttribute('disabled', 'disabled')
}
})
return html` <nav>
<button onclick=${() => setTab('host')} ref=${refs[0]}>Host</button>
<button onclick=${() => setTab('client')} ref=${refs[1]}>Client</button>
</nav>`
}
const Wrapper = ({
children,
setTab,
refs,
}: {
children: any
setTab: (tab: Tab) => void
tab: Tab
refs: Ref<HTMLButtonElement>[]
}) => {
return html`
<${NavBar} setTab=${setTab} refs=${refs} />
${children}
`
}
const App = () => {
const [tab, setTab] = useState<Tab>('')
const clientRef = useRef<HTMLButtonElement>()
const hostRef = useRef<HTMLButtonElement>()
useEffect(() => {
chrome.storage.local.get(({ mode }) => {
if (mode === 'old_session') {
chrome.storage.local.remove(['id', 'sendLinks'])
}
setTab(mode)
})
}, [])
switch (tab) {
case 'old_session':
return html`<${Wrapper} refs=${[hostRef, clientRef]} setTab=${setTab}>
<p>Сессия устарела, создайте новую</p>
</Wrapper>`
case 'client':
return html`<${Wrapper} refs=${[hostRef, clientRef]} setTab=${setTab}>
<${Client} />
</Wrapper>`
case 'host':
return html`<${Wrapper} refs=${[hostRef, clientRef]} setTab=${setTab} >
<${Host} />
</Wrapper>`
default:
return html`<${Wrapper} refs=${[hostRef, clientRef]} setTab=${setTab} />`
}
}
render(html`<${App} />`, document.getElementById('app') as HTMLElement)