-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
147 lines (142 loc) · 6.17 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
/* @refresh reload */
import { useEffect, useRef, useState } from "react"
import { RuntimeStore } from "./core/store"
import { IAlgorithmManifest } from "./core/manifest"
import { useVisualizer } from "./core/react"
import "@fontsource-variable/arimo"
import styles from "./App.module.scss"
import classNames from "classnames"
import { VList, VListHandle } from "virtua"
type AppProps = {
manifest: IAlgorithmManifest,
store: RuntimeStore
}
enum Tab {
Starter, Render
}
const App = ({ manifest, store }: AppProps) => {
const vis = useVisualizer(store)
const {curState, curEvent, events, currentStep, start, next} = vis
const [eventOverride, setEventOverride] = useState<number | undefined>(undefined)
const curEventOverride = eventOverride !== undefined && events !== undefined ? events[eventOverride] : curEvent
const curStateOverride = eventOverride !== undefined && events !== undefined ? events[eventOverride].state : curState
const isRunning = curEventOverride !== undefined && curStateOverride !== undefined
const [curTab, setCurTab] = useState<Tab>(Tab.Starter)
const doNext = () => {
if(currentStep === undefined) {
return
}
if (eventOverride !== undefined && events !== undefined && eventOverride < events.length - 1) {
setEventOverride(eventOverride + 1)
} else {
next()
setEventOverride(undefined)
}
}
const doPrev = () => {
if(currentStep === undefined || events === undefined || !vis.config.storeEvents || currentStep === 0 || eventOverride === 0) {
return
}
if(eventOverride === undefined) {
setEventOverride(currentStep - 2)
} else if (eventOverride > 0) {
setEventOverride(eventOverride - 1)
}
}
const eventsHandle = useRef<VListHandle>(null)
const doStart: typeof start = (...args) => {
setCurTab(Tab.Render)
setEventOverride(undefined)
start(...args)
}
useEffect(() => {
if(eventsHandle.current !== null) {
eventsHandle.current.scrollToIndex(eventOverride ?? currentStep ?? 0, {align: "nearest"})
}
}, [eventOverride, currentStep])
useEffect(() => {
const keyListener = (e: KeyboardEvent) => {
if (e.key === "ArrowRight") {
e.preventDefault()
doNext()
}
if (e.key === "ArrowLeft") {
e.preventDefault()
doPrev()
}
}
window.addEventListener("keydown", keyListener)
return () => {
window.removeEventListener("keydown", keyListener)
}
}, [eventOverride, currentStep])
return <div className={styles.app}>
<div className={styles.header}>
<a onClick={(_) => history.back()}>← Go back</a>
<h1>{manifest.nameEn}</h1>
<article>{manifest.descriptionEn}</article>
<section className={styles.tabSwitcher}>
<button onClick={() => setCurTab(Tab.Starter)}>Starter</button>
<button onClick={() => setCurTab(Tab.Render)} disabled={!isRunning}>Render</button>
</section>
</div>
<section className={styles.main}>
<div className={classNames(styles.tab, styles.tabStarter, {
[styles.enabled]: curTab === Tab.Starter
})}>
<manifest.startComponent doStart={doStart}/>
</div>
<div className={classNames(styles.tab, styles.tabRender, {
[styles.enabled]: curTab === Tab.Render
})}>
{curStateOverride && curEventOverride &&
("customErrorHandling" in manifest && manifest.customErrorHandling === true ?
<manifest.renderComponent curState={curStateOverride} curEvent={curEventOverride}/> :
curEventOverride.name === "error" ?
<pre>
{curEventOverride.error.stack}
</pre>:
<manifest.renderComponent curState={curStateOverride} curEvent={curEventOverride}/>
)
}
</div>
</section>
<div className={styles.sidebar}>
<section className={styles.player}>
<div>
{currentStep === undefined ?
"Not started" :
"Step " + (eventOverride === undefined ? currentStep : eventOverride + 1)
}
</div>
<div className={styles.controls}>
<button onClick={doNext}>Next</button>
<button disabled={!vis.config.storeEvents} onClick={doPrev}>Prev</button>
<button disabled={!vis.config.storeEvents} onClick={() => setEventOverride(0)}>Beginning</button>
<button disabled={!vis.config.storeEvents} onClick={() => setEventOverride(events!.length - 1)}>End</button>
</div>
<small>Use arrow keys to navigate</small>
</section>
<section className={styles.events}>
Events:
<div className={styles.eventsContainer}>
{events ?
<VList ref={eventsHandle}>
{events.map((event, index) => {
const isSelected = index === eventOverride || (eventOverride === undefined && index === currentStep! - 1)
return <div key={index} className={classNames(styles.event, {
[styles.selected]: isSelected
})} onClick={() => setEventOverride(index)}>
<button onClick={() => setEventOverride(index)} disabled={isSelected}>→</button>
{index + 1} {event.name}
</div>
})}
</VList>
: "Nothing yet.."
}
</div>
</section>
</div>
</div>
}
export default App