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/misc/select-site-license.tsx
Views: 687
1
/*
2
Select a site license, either by pasting one in, or
3
from a list of all of the licenses the signed in user
4
is a manager of.
5
*/
6
import { useMemo } from "react";
7
import { Alert } from "antd";
8
import SelectLicense, {
9
License,
10
} from "@cocalc/frontend/site-licenses/select-license";
11
import useQuery from "lib/hooks/database";
12
import Loading from "components/share/loading";
13
14
interface Props {
15
onChange: (licenseId: string | undefined) => void; // called with undefined if user doesn't want to select a license
16
defaultLicenseId?: string;
17
}
18
19
export default function SelectSiteLicense({
20
onChange,
21
defaultLicenseId,
22
}: Props) {
23
const { error, value, loading } = useQuery({
24
manager_site_licenses: [
25
{ id: null, expires: null, title: null, quota: null },
26
],
27
});
28
const managedLicenses: { [id: string]: License } = useMemo(() => {
29
const x: { [id: string]: License } = {};
30
if (!value) return x;
31
const { manager_site_licenses } = value;
32
for (const license of manager_site_licenses) {
33
if (license.expires) {
34
// comes back from database as ISO string.
35
license.expires = new Date(license.expires);
36
}
37
x[license.id] = license;
38
}
39
return x;
40
}, [value]);
41
42
if (loading || value == null) {
43
return <Loading />;
44
}
45
if (error) {
46
return <Alert type="error" message={error} />;
47
}
48
return (
49
<SelectLicense
50
onChange={onChange}
51
defaultLicenseId={defaultLicenseId}
52
managedLicenses={managedLicenses}
53
/>
54
);
55
}
56
57