"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/* Select one of the projects the signed in user is a collaborator on. */
const react_1 = require("react");
const database_1 = __importDefault(require("lib/hooks/database"));
const antd_1 = require("antd");
const loading_1 = __importDefault(require("components/share/loading"));
const cmp_1 = require("@cocalc/util/cmp");
function SelectProject({ label, onChange }) {
    const { error, value, loading } = (0, database_1.default)({
        projects: [{ title: null, project_id: null, last_edited: null }],
    });
    const projects = (0, react_1.useMemo)(() => {
        if (loading) {
            return [];
        }
        const cmp = (0, cmp_1.field_cmp)("last_edited");
        value.projects.sort((a, b) => cmp(b, a)); // so newest first
        const v = [];
        for (const x of value.projects) {
            v.push({
                label: x.title,
                value: JSON.stringify({ project_id: x.project_id, title: x.title }),
            });
        }
        return v;
    }, [loading]);
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(antd_1.Divider, { style: { color: "#666" }, children: label ?? "Select a Project" }), (0, jsx_runtime_1.jsxs)(antd_1.Space, { direction: "vertical", style: { width: "100%" }, children: [error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, showIcon: true }), loading && (0, jsx_runtime_1.jsx)(loading_1.default, { style: { fontSize: "24pt" } }), !loading &&
                        (!error && value && projects.length > 0 ? ((0, jsx_runtime_1.jsx)(antd_1.Select, { showSearch: true, style: { width: "100%" }, placeholder: "Select a project...", optionFilterProp: "label", options: projects, onChange: (x) => (x ? onChange(JSON.parse(`${x}`)) : undefined) })) : ((0, jsx_runtime_1.jsx)("div", { children: "You do not have any projects yet." })))] })] }));
}
exports.default = SelectProject;
//# sourceMappingURL=select.js.map