import { useContext, useEffect, useMemo, useState } from 'react'; import { Link, useNavigate, useSearchParams } from 'react-router-dom'; import { mutate } from 'swr'; import { getProjectFetcher } from 'hooks/api/getters/useProject/getProjectFetcher'; import useProjects from 'hooks/api/getters/useProjects/useProjects'; import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender'; import { ProjectCard } from '../ProjectCard/ProjectCard'; import { useStyles } from './ProjectList.styles'; import { IProjectCard } from 'interfaces/project'; import loadingData from './loadingData'; import useLoading from 'hooks/useLoading'; import { PageContent } from 'component/common/PageContent/PageContent'; import AccessContext from 'contexts/AccessContext'; import { PageHeader } from 'component/common/PageHeader/PageHeader'; import ResponsiveButton from 'component/common/ResponsiveButton/ResponsiveButton'; import { CREATE_PROJECT } from 'component/providers/AccessProvider/permissions'; import { Add } from '@mui/icons-material'; import ApiError from 'component/common/ApiError/ApiError'; import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig'; import { TablePlaceholder } from 'component/common/Table'; import { useMediaQuery } from '@mui/material'; import theme from 'themes/theme'; import { Search } from 'component/common/Search/Search'; type PageQueryType = Partial>; type projectMap = { [index: string]: boolean; }; function resolveCreateButtonData(isOss: boolean, hasAccess: boolean) { if (isOss) { return { title: 'You must be on a paid subscription to create new projects', disabled: true, }; } else if (!hasAccess) { return { title: 'You do not have permission to create new projects', disabled: true, }; } else { return { title: 'Click to create a new project', disabled: false, }; } } export const ProjectListNew = () => { const { hasAccess } = useContext(AccessContext); const navigate = useNavigate(); const { classes: styles } = useStyles(); const { projects, loading, error, refetch } = useProjects(); const [fetchedProjects, setFetchedProjects] = useState({}); const ref = useLoading(loading); const { isOss } = useUiConfig(); const isSmallScreen = useMediaQuery(theme.breakpoints.down('md')); const [searchParams, setSearchParams] = useSearchParams(); const [searchValue, setSearchValue] = useState( searchParams.get('search') || '' ); useEffect(() => { const tableState: PageQueryType = {}; if (searchValue) { tableState.search = searchValue; } setSearchParams(tableState, { replace: true, }); }, [searchValue, setSearchParams]); const filteredProjects = useMemo(() => { const regExp = new RegExp(searchValue, 'i'); return searchValue ? projects.filter(project => regExp.test(project.name)) : projects; }, [projects, searchValue]); const handleHover = (projectId: string) => { if (fetchedProjects[projectId]) { return; } const { KEY, fetcher } = getProjectFetcher(projectId); mutate(KEY, fetcher); setFetchedProjects(prev => ({ ...prev, [projectId]: true })); }; const createButtonData = resolveCreateButtonData( isOss(), hasAccess(CREATE_PROJECT) ); const renderError = () => { return ( ); }; const renderProjects = () => { if (loading) { return renderLoading(); } return filteredProjects.map((project: IProjectCard) => { return ( handleHover(project.id)} name={project.name} memberCount={project.memberCount ?? 0} health={project.health} id={project.id} featureCount={project.featureCount} /> ); }); }; const renderLoading = () => { return loadingData.map((project: IProjectCard) => { return ( {}} key={project.id} name={project.name} id={project.id} memberCount={2} health={95} featureCount={4} /> ); }); }; return (
} /> navigate('/projects/create')} maxWidth="700px" permission={CREATE_PROJECT} disabled={createButtonData.disabled} > New project } > } /> } >
0} show={ No projects found matching “ {searchValue} ” } elseShow={ No projects available. } /> } elseShow={renderProjects()} />
); };