UNPKG

@datalayer/core

Version:
36 lines (35 loc) 1.92 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; /* * Copyright (c) 2023-2025 Datalayer, Inc. * Distributed under the terms of the Modified BSD License. */ import { useState, useEffect, useCallback } from "react"; import { Select } from "@primer/react"; import { useCache, useUser } from "./../../hooks"; import { useLayoutStore } from "../../state"; const NO_ORGANIZATION_SELECTED_VALUE = "NO_ORGANIZATION_SELECTED_VALUE"; export const OrganizationSelect = () => { const user = useUser(); const { organization, updateLayoutOrganization, updateLayoutSpace } = useLayoutStore(); const { refreshUserOrganizations, getUserOrganizations } = useCache(); const [organizations, setOrganizations] = useState(getUserOrganizations()); const [_, setSelection] = useState(organization); useEffect(() => { refreshUserOrganizations(user).then(resp => { if (resp.success) { setOrganizations(getUserOrganizations()); } }); }, [user]); const onSelectionChange = useCallback((e) => { const selectedOrganization = e.target.value; const org = selectedOrganization === NO_ORGANIZATION_SELECTED_VALUE ? undefined : organizations[parseInt(selectedOrganization, 10)]; setSelection(org); updateLayoutOrganization(org); updateLayoutSpace(undefined); }, [setSelection, organizations]); return (_jsx(_Fragment, { children: _jsxs(Select, { block: true, width: "medium", onChange: onSelectionChange, children: [_jsx(Select.Option, { value: NO_ORGANIZATION_SELECTED_VALUE, selected: organization === undefined, children: "Please select an organization..." }), organizations.map((org, index) => (_jsx(Select.Option, { value: `${index}`, selected: org.id === organization?.id, children: org.name })))] }) })); }; export default OrganizationSelect;