Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/components/project/select.tsx
Views: 687
/* Select one of the projects the signed in user is a collaborator on. */12import { Alert, Divider, Select, Space } from "antd";3import { useMemo } from "react";45import Loading from "components/share/loading";6import useDatabase from "lib/hooks/database";78import { Icon } from "@cocalc/frontend/components/icon";9import { field_cmp } from "@cocalc/util/cmp";1011interface Props {12label?: string;13onChange: (project: { project_id: string; title: string }) => void;14defaultOpen?: boolean;15allowCreate?: boolean;16}1718export default function SelectProject({19label,20onChange,21defaultOpen,22allowCreate,23}: Props) {24const { error, value, loading } = useDatabase({25projects: [{ title: null, project_id: null, last_edited: null }],26});27const projects = useMemo(() => {28if (loading) {29return [];30}31const cmp = field_cmp("last_edited");32value.projects.sort((a, b) => cmp(b, a)); // so newest first33const v: { label: string | JSX.Element; value: string }[] = [];34for (const x of value.projects) {35v.push({36label: x.title,37value: JSON.stringify({ project_id: x.project_id, title: x.title }),38});39}40if (allowCreate) {41v.push({42label: (43<>44<Icon name="plus-circle" /> Create Project...45</>46),47value: JSON.stringify({ project_id: "", title: "Untitled Project" }),48});49}50return v;51}, [loading]);52return (53<div>54<Divider style={{ color: "#666" }}>{label ?? "Select a Project"}</Divider>55<Space direction="vertical" style={{ width: "100%" }}>56{error && <Alert type="error" message={error} showIcon />}57{loading && <Loading style={{ fontSize: "24pt" }} />}58{!loading &&59(!error && value && projects.length > 0 ? (60<Select61defaultOpen={defaultOpen}62showSearch63style={{ width: "100%" }}64placeholder={"Select a Project..."}65optionFilterProp="label"66options={projects}67onChange={(x) => (x ? onChange(JSON.parse(`${x}`)) : undefined)}68/>69) : (70<div>You do not have any recent projects.</div>71))}72</Space>73</div>74);75}767778