-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
App.tsx
63 lines (53 loc) · 1.82 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
import * as React from 'react'
import { useErrorBoundary } from 'use-error-boundary'
import { Route, useRoute, Redirect } from 'wouter'
import { Global, Loading, Page, DemoPanel, Dot, Error } from './styles'
import * as demos from './demos'
const DEFAULT_COMPONENT_NAME = 'Portals'
const visibleComponents: any = Object.entries(demos).reduce((acc, [name, item]) => ({ ...acc, [name]: item }), {})
function ErrorBoundary({ children, fallback, name }: any) {
const { ErrorBoundary, didCatch, error } = useErrorBoundary()
return didCatch ? fallback(error) : <ErrorBoundary key={name}>{children}</ErrorBoundary>
}
function Demo() {
const [match, params] = useRoute('/demo/:name')
const compName = match ? params.name : DEFAULT_COMPONENT_NAME
const { Component } = visibleComponents[compName]
return (
<ErrorBoundary key={compName} fallback={(e: any) => <Error>{e.message}</Error>}>
<Component />
</ErrorBoundary>
)
}
function Dots() {
const [match, params] = useRoute('/demo/:name')
if (!match) return null
return (
<>
<DemoPanel>
{Object.entries(visibleComponents).map(function mapper([name, item]) {
const background = params.name === name ? 'salmon' : '#fff'
return <Dot key={name} to={`/demo/${name}`} style={{ background }} />
})}
</DemoPanel>
<span style={{ color: 'white' }}>{params.name}</span>
</>
)
}
export default function App() {
const dev = new URLSearchParams(location.search).get('dev')
return (
<>
<Global />
<Page>
<React.Suspense fallback={<Loading />}>
<Route path="/" children={<Redirect to={`/demo/${DEFAULT_COMPONENT_NAME}`} />} />
<Route path="/demo/:name">
<Demo />
</Route>
</React.Suspense>
{dev === null && <Dots />}
</Page>
</>
)
}