import { useEffect, useState } from 'react'; import { useNavigate, useLocation } from 'react-router'; import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { roleVar, userVar } from '../../graphql/state'; import { Box, ContextMenu, IconButton, MessagingSidebar, SidebarItem, } from '..'; import { Add, Messaging } from '../../assets'; import { Wrapper } from './styles'; import { CategoryOutput, FeatureOutput, GetAllCategoriesQuery, GetAllCategoriesQueryVariables, GetAllFeaturesQuery, GetAllFeaturesQueryVariables, GetAllProjectsByClientIdQuery, GetAllProjectsByClientIdQueryVariables, GetAllProjectsQuery, GetAllProjectsQueryVariables, GetAllTemplatesQuery, GetAllTemplatesQueryVariables, ProjectOutput, TemplateOutput, } from '../../graphql/types'; import { GET_ALL_CATEGORIES } from '../../graphql/category.api'; import { GET_ALL_PROJECTS, GET_ALL_PROJECTS_BY_CLIENT_ID, } from '../../graphql/project.api'; import { GET_ALL_TEMPLATES } from '../../graphql/template.api'; import { GET_ALL_FEATURES } from '../../graphql/feature.api'; const Sidebar = () => { const role = useReactiveVar(roleVar); const currentUser = useReactiveVar(userVar); const location = useLocation(); const navigate = useNavigate(); const [projects, setProjects] = useState>(); const [templates, setTemplates] = useState>(); const [features, setFeatures] = useState>(); const [categories, setCategories] = useState>(); const [messagingSidebarOpen, setMessagingSidebarOpen] = useState(false); const [getProjectsByClientId] = useLazyQuery< GetAllProjectsByClientIdQuery, GetAllProjectsByClientIdQueryVariables >(GET_ALL_PROJECTS_BY_CLIENT_ID, { variables: { id: currentUser?.id!, }, onCompleted({ getAllProjectsByClientId }) { setProjects(getAllProjectsByClientId); } }); const [getProjects] = useLazyQuery< GetAllProjectsQuery, GetAllProjectsQueryVariables >(GET_ALL_PROJECTS, { onCompleted({ getAllProjects }) { setProjects(getAllProjects); } }); const [getTemplates] = useLazyQuery< GetAllTemplatesQuery, GetAllTemplatesQueryVariables >(GET_ALL_TEMPLATES, { onCompleted({ getAllTemplates }) { setTemplates(getAllTemplates); }, }); const [getFeatures] = useLazyQuery< GetAllFeaturesQuery, GetAllFeaturesQueryVariables >(GET_ALL_FEATURES, { onCompleted({ getAllFeatures }) { setFeatures(getAllFeatures); }, }); const [getCategories] = useLazyQuery< GetAllCategoriesQuery, GetAllCategoriesQueryVariables >(GET_ALL_CATEGORIES, { onCompleted({ getAllCategories }) { setCategories(getAllCategories); }, }); useEffect(() => { if (/project/i.test(location.pathname)) { if (role !== 'client') getProjects(); else getProjectsByClientId({ variables: { id: currentUser?.id! } }); } if (/template/i.test(location.pathname)) { getTemplates(); } if (/feature/i.test(location.pathname)) { getFeatures(); } if (/category/i.test(location.pathname)) { getCategories(); } return () => { setProjects([]); setTemplates([]); setFeatures([]); setCategories([]); }; }, [location.pathname]); return ( {role !== 'admin' && ( <> {projects && new RegExp(/project/, 'i').test(location.pathname) && projects.map((project, index) => (
navigate(`/project/${project.id}`)} />
))} {templates && new RegExp(/template/, 'i').test(location.pathname) && templates.map((template, index) => (
navigate(`/template/${template.id}`)} />
))} {features && new RegExp(/feature/, 'i').test(location.pathname) && features.map((feature, index) => (
navigate(`/feature/${feature.id}`)} />
))} {categories && new RegExp(/category/, 'i').test(location.pathname) && categories.map((category, index) => (
navigate(`/category/${category.id}`)} />
))}
} color={role} onClick={() => { if (/project/i.test(location.pathname)) { navigate('/add-project'); } if (/template/i.test(location.pathname)) { navigate('/add-template'); } if (/feature/i.test(location.pathname)) { navigate('/add-feature'); } if (/category/i.test(location.pathname)) { navigate('/add-category'); } }} /> {/\/project/i.test(location.pathname) && ( } color={role} onClick={() => setMessagingSidebarOpen(!messagingSidebarOpen)} /> )} )} {messagingSidebarOpen && ( setMessagingSidebarOpen(false)} /> )}
); }; export default Sidebar;