forked from accordproject/template-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleDropdown.tsx
More file actions
62 lines (56 loc) · 1.56 KB
/
SampleDropdown.tsx
File metadata and controls
62 lines (56 loc) · 1.56 KB
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
import { Button, Dropdown, Space, message, MenuProps } from "antd";
import { DownOutlined } from "@ant-design/icons";
import { memo, useCallback, useMemo, useState } from "react";
import useAppStore from "../store/store";
import { shallow } from "zustand/shallow";
function SampleDropdown({
setLoading,
}: {
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
}): JSX.Element {
const { samples, loadSample } = useAppStore(
(state) => ({
samples: state.samples,
loadSample: state.loadSample as (key: string) => Promise<void>,
}),
shallow
);
const [selectedSample, setSelectedSample] = useState<string | null>(null);
const items: MenuProps["items"] = useMemo(
() =>
samples.map((s) => ({
label: s.NAME,
key: s.NAME,
})),
[samples]
);
const handleMenuClick = useCallback(
async (e: any) => {
if (e.key) {
setLoading(true);
try {
await loadSample(e.key);
message.info(`Loaded ${e.key} sample`);
setSelectedSample(e.key);
} catch (error) {
message.error("Failed to load sample");
} finally {
setLoading(false);
}
}
},
[loadSample, setLoading]
);
return (
<Space>
<Dropdown menu={{ items, onClick: handleMenuClick }} trigger={["click"]}>
<div className="samples-element">
<Button>
{selectedSample ? selectedSample : "Load Sample"} <DownOutlined />
</Button>
</div>
</Dropdown>
</Space>
);
}
export default memo(SampleDropdown);