CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/licenses/select-license.tsx
Views: 687
1
/* Select component to choose from one of the licenses that you manage */
2
3
import { CSSProperties, ReactNode } from "react";
4
import useAPI from "lib/hooks/api";
5
import Loading from "components/share/loading";
6
import { Quota } from "./license";
7
import { Alert, Select } from "antd";
8
const { Option } = Select;
9
import { search_split, search_match } from "@cocalc/util/misc";
10
11
interface Props {
12
onSelect: (license_id: string) => void;
13
license?: string;
14
style?: CSSProperties;
15
disabled?: boolean;
16
}
17
18
export default function SelectLicense({
19
onSelect,
20
license,
21
style,
22
disabled,
23
}: Props) {
24
let { result, error } = useAPI("licenses/get-managed");
25
if (error) {
26
return <Alert type="error" message={error} />;
27
}
28
if (!result) {
29
return <Loading style={{ fontSize: "16pt", margin: "auto" }} />;
30
}
31
32
const v: ReactNode[] = [];
33
for (const x of result) {
34
v.push(
35
<Option
36
value={x.id}
37
key={x.id}
38
search={`${x.id} ${x.title} ${x.description} ${JSON.stringify(
39
x.quota,
40
)}`.toLowerCase()}
41
>
42
{x.title?.trim() ? `${x.title} - ` : ""}
43
<span style={{ fontFamily: "monospace" }}>{x.id}</span>
44
<br />
45
<Quota quota={x.quota} />
46
</Option>,
47
);
48
}
49
50
return (
51
<Select
52
style={{ ...style, ...(license ? { height: "80px" } : undefined) }}
53
disabled={disabled}
54
showSearch
55
allowClear
56
placeholder="Select a license"
57
optionFilterProp="children"
58
value={
59
license ? license : undefined /* no empty string so placeholder works */
60
}
61
onChange={onSelect}
62
filterOption={(input, option) => {
63
if (!input.trim()) return true;
64
return search_match(option?.search ?? "", search_split(input));
65
}}
66
>
67
{v}
68
</Select>
69
);
70
}
71
72