import { useMemo, useState } from 'react'; import { Link as RouterLink } from 'react-router-dom'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { PageContent } from 'component/common/PageContent/PageContent'; import { PageHeader } from 'component/common/PageHeader/PageHeader'; import { UPDATE_PROJECT } from 'component/providers/AccessProvider/permissions'; import ApiError from 'component/common/ApiError/ApiError'; import useToast from 'hooks/useToast'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import { Alert, styled, TableBody, TableRow, Link } from '@mui/material'; import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi'; import PermissionSwitch from 'component/common/PermissionSwitch/PermissionSwitch'; import type { IProjectEnvironment } from 'interfaces/environments'; import { getEnabledEnvs } from './helpers'; import { usePageTitle } from 'hooks/usePageTitle'; import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; import { useGlobalFilter, useTable } from 'react-table'; import { SortableTableHeader, Table, TableCell, TablePlaceholder, } from 'component/common/Table'; import { SearchHighlightProvider } from 'component/common/Table/SearchHighlightContext/SearchHighlightContext'; import { Search } from 'component/common/Search/Search'; import { EnvironmentNameCell } from 'component/environments/EnvironmentTable/EnvironmentNameCell/EnvironmentNameCell'; import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell'; import { ActionCell } from 'component/common/Table/cells/ActionCell/ActionCell'; import { EnvironmentHideDialog } from './EnvironmentHideDialog/EnvironmentHideDialog'; import { useProjectEnvironments } from 'hooks/api/getters/useProjectEnvironments/useProjectEnvironments'; import { TextCell } from 'component/common/Table/cells/TextCell/TextCell'; import useProjectOverview, { useProjectOverviewNameOrId, } from 'hooks/api/getters/useProjectOverview/useProjectOverview'; const StyledAlert = styled(Alert)(({ theme }) => ({ marginBottom: theme.spacing(4), })); const StyledDivContainer = styled('div')(({ theme }) => ({ display: 'flex', flexWrap: 'wrap', [theme.breakpoints.down('sm')]: { justifyContent: 'center', }, })); const StyledApiError = styled(ApiError)(({ theme }) => ({ maxWidth: '400px', marginBottom: theme.spacing(2), })); const ProjectEnvironmentList = () => { const projectId = useRequiredPathParam('projectId'); const projectName = useProjectOverviewNameOrId(projectId); usePageTitle(`Project environments – ${projectName}`); // api state const { setToastData, setToastApiError } = useToast(); const { environments, loading, error, refetchEnvironments } = useProjectEnvironments(projectId); const { project, refetch: refetchProject } = useProjectOverview(projectId); const { removeEnvironmentFromProject, addEnvironmentToProject } = useProjectApi(); // local state const [selectedEnvironment, setSelectedEnvironment] = useState(); const [hideDialog, setHideDialog] = useState(false); const { isOss } = useUiConfig(); const projectEnvironments = useMemo( () => environments.map((environment) => ({ ...environment, projectVisible: project?.environments .map((projectEnvironment) => projectEnvironment.environment) .includes(environment.name), })), [environments, project?.environments], ); const refetch = () => { refetchEnvironments(); refetchProject(); }; const renderError = () => { return ( ); }; const errorMsg = (enable: boolean): string => { return `Got an API error when trying to set the environment as ${ enable ? 'visible' : 'hidden' }`; }; const toggleEnv = async (env: IProjectEnvironment) => { if (env.projectVisible) { const enabledEnvs = getEnabledEnvs(projectEnvironments); if (enabledEnvs > 1) { setSelectedEnvironment(env); setHideDialog(true); return; } setToastData({ title: 'One environment must be visible', text: 'You must always have at least one visible environment per project', type: 'error', }); } else { try { await addEnvironmentToProject(projectId, env.name); refetch(); setToastData({ title: 'Environment set as visible', text: 'Environment successfully set as visible.', type: 'success', }); } catch (error) { setToastApiError(errorMsg(true)); } } }; const onHideConfirm = async () => { if (selectedEnvironment) { try { await removeEnvironmentFromProject( projectId, selectedEnvironment.name, ); refetch(); setToastData({ title: 'Environment set as hidden', text: 'Environment successfully set as hidden.', type: 'success', }); } catch (e) { setToastApiError(errorMsg(false)); } finally { setHideDialog(false); } } }; const envIsDisabled = (projectName: string) => { return isOss() && projectName === 'default'; }; const COLUMNS = useMemo( () => [ { Header: 'Name', accessor: 'name', Cell: ({ row: { original } }: any) => ( ), }, { Header: 'Type', accessor: 'type', Cell: HighlightCell, }, { Header: 'Project API tokens', accessor: (row: IProjectEnvironment) => row.projectApiTokenCount === 1 ? '1 token' : `${row.projectApiTokenCount} tokens`, Cell: TextCell, }, { Header: 'Visible in project', accessor: 'enabled', align: 'center', width: 1, Cell: ({ row: { original } }: any) => ( toggleEnv(original)} /> ), disableGlobalFilter: true, }, ], [projectEnvironments], ); const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, state: { globalFilter }, setGlobalFilter, } = useTable( { columns: COLUMNS as any, data: projectEnvironments, disableSortBy: true, }, useGlobalFilter, ); const header = ( {!isOss() ? ( <> Configure environments ) : null} } /> ); return ( Important! In order for your application to retrieve configured activation strategies for a specific environment, the application must use an environment specific API token. You can look up the environment-specific{' '} API tokens here.

Your administrator can configure an environment-specific API token to be used in the SDK. If you are an administrator you can{' '} create a new API token here .
{rows.map((row) => { prepareRow(row); return ( {row.cells.map((cell) => ( {cell.render('Cell')} ))} ); })}
0} show={ No environments found matching “ {globalFilter} ” } elseShow={ No environments available. Get started by adding one. } /> } />
); }; export default ProjectEnvironmentList;