-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemos.tsx
48 lines (38 loc) · 1.28 KB
/
Demos.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
import React, { useState } from 'react';
import { BrowserRouter, useLocation } from 'react-router-dom';
import { ConfigProvider, DemosConfig } from './configContext';
import { DemosGroup } from './DemoGroup';
import { DemoRenderer } from './DemoRenderer';
import { DemosList } from './DemosList';
import { Sidebar } from './Sidebar';
type AppProps = DemosConfig<unknown> & {
demos: DemosGroup<any>[];
};
export const App: React.FC<AppProps> = ({ demos, ...config }) => (
<BrowserRouter basename={config.basename}>
<ConfigProvider value={config}>
<Demos groups={demos} />
</ConfigProvider>
</BrowserRouter>
);
type DemosProps = {
groups: DemosGroup<unknown>[];
};
const Demos: React.FC<DemosProps> = ({ groups }) => {
const [search, setSearch] = useState('');
const demoPath = new URLSearchParams(useLocation().search).get('demo-path');
if (demoPath) {
const path = demoPath.replace(/^\//, '').split('/');
const [demo] = groups.map((group) => group.findDemo(path)).filter(Boolean);
if (!demo) {
throw new Error(`demo not found, path=${demoPath}`);
}
return <DemoRenderer demo={demo} />;
}
return (
<div className="demos__wrapper">
<Sidebar groups={groups} setSearch={setSearch} />
<DemosList groups={groups} />
</div>
);
};