/******************************************************************************** * Copyright (c) 2020 TypeFox and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 ********************************************************************************/ import { FunctionComponent, useContext, useEffect, useState, useRef, ReactNode } from 'react'; import { Box, Typography, Tabs, Tab, useTheme, useMediaQuery, Link } from '@mui/material'; import { Namespace, UserData } from '../../extension-registry-types'; import { DelayedLoadIndicator } from '../../components/delayed-load-indicator'; import { MainContext } from '../../context'; import { NamespaceDetail } from './user-settings-namespace-detail'; import { CreateNamespaceDialog } from './create-namespace-dialog'; interface NamespaceTabProps { chosenNamespace: Namespace, onChange: (value: Namespace) => void, namespaces: Namespace[] } const NamespacesTabs = (props: NamespaceTabProps) => { const theme = useTheme(); const isATablet = useMediaQuery(theme.breakpoints.down('md')); return props.onChange(value)} variant={isATablet ? 'scrollable' : 'standard'} scrollButtons={isATablet ? 'auto' : false} indicatorColor='secondary' sx={{ width: { xs: '80%', sm: '80%', md: '80%', lg: '160px', xl: '160px' } }} > { props.namespaces.map(namespace => { return ; }) } ; }; export const UserSettingsNamespaces: FunctionComponent = () => { const [loading, setLoading] = useState(true); const [namespaces, setNamespaces] = useState>([]); const [chosenNamespace, setChosenNamespace] = useState(); const { pageSettings, service, user, handleError } = useContext(MainContext); const abortController = useRef(new AbortController()); useEffect(() => { initNamespaces(); return () => { abortController.current.abort(); }; }, []); const handleChangeNamespace = (value: Namespace): void => { doHandleChangeNamespace(value); }; const doHandleChangeNamespace = async(chosenNamespace: Namespace): Promise => { setChosenNamespace(chosenNamespace); }; const initNamespaces = async(): Promise => { try { const namespaces = await service.getNamespaces(abortController.current); const chosenNamespace = namespaces.length ? namespaces[0] : undefined; setNamespaces(namespaces); setChosenNamespace(chosenNamespace); setLoading(false); } catch (err) { handleError(err); setLoading(false); } }; const handleNamespaceCreated = () => { setLoading(true); initNamespaces(); }; let namespaceContainer: ReactNode = null; const namespaceAccessUrl = pageSettings.urls.namespaceAccessInfo; if (namespaces.length > 0 && chosenNamespace) { namespaceContainer = setLoading(loading)} filterUsers={(foundUser: UserData) => foundUser.provider !== user?.provider || foundUser.loginName !== user?.loginName} fixSelf={true} namespaceAccessUrl={namespaceAccessUrl} theme={pageSettings.themeType} /> ; } else if (!loading) { namespaceContainer = No namespaces available. Read here about claiming namespaces.; } return <> Namespaces {namespaceContainer} ; };