-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBeta.tsx
65 lines (57 loc) · 1.81 KB
/
Beta.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
import { FC, useState } from 'react'
import { useAppSelector, useObserverAncestor } from '@/hooks'
import { selectOptions } from '../global/optionsSlice'
import Timer from './Timer'
import { findElement } from '@/utils'
import { getRoot } from './utils'
import { Portal } from '@/components/Portal'
import Random from './Random'
const Beta: FC<{ beta?: boolean }> = () => {
const options = useAppSelector(selectOptions)
const [timerRoot, setTimerRoot] = useState<HTMLElement>()
const [randomRoot, setRandomRoot] = useState<HTMLElement>()
const showTimer = !!options?.problemsPage.timer
const showRandomQuestion = false
useObserverAncestor(
async state => {
if (!showRandomQuestion) return
// 创建「随机一题」按钮根元素
const nav = await findElement(
'#__next > div > div > div > nav > div > div > div:nth-child(2)'
)
if (!state.isMount) return
const randomRoot = document.createElement('div')
randomRoot.style.lineHeight = '0'
setRandomRoot(randomRoot)
nav.append(randomRoot)
state.unmount.push(() => randomRoot && randomRoot.remove())
return randomRoot
},
[showRandomQuestion]
)
useObserverAncestor(
async state => {
if (!showTimer) return
// 创建「计时器」按钮根元素
const parent = await getRoot()
if (!state.isMount) return
const root = document.createElement('div')
parent.prepend(root)
setTimerRoot(root)
state.unmount.push(() => root && root.remove())
return root!
},
[showTimer]
)
return (
<>
{showTimer && timerRoot && <Timer beta={true} root={timerRoot} />}
{randomRoot && showRandomQuestion && (
<Portal container={randomRoot}>
<Random />
</Portal>
)}
</>
)
}
export default Beta