import React, { MouseEventHandler, useMemo, VFC } from 'react'; import { MenuItem, Typography } from '@mui/material'; import DropdownMenu, { IDropdownMenuProps } from '../DropdownMenu/DropdownMenu'; import useProjects from 'hooks/api/getters/useProjects/useProjects'; import { IProjectCard } from 'interfaces/project'; const ALL_PROJECTS = { id: '*', name: '> All projects' }; interface IProjectSelectProps { currentProjectId: string; updateCurrentProject: (id: string) => void; } const ProjectSelect: VFC> = ({ currentProjectId, updateCurrentProject, ...rest }) => { const { projects } = useProjects(); const setProject = (value?: string | null) => { const id = value && typeof value === 'string' ? value.trim() : '*'; updateCurrentProject(id); }; const currentProject = useMemo(() => { const project = projects.find(i => i.id === currentProjectId); return project || ALL_PROJECTS; }, [currentProjectId, projects]); const handleChangeProject: MouseEventHandler = event => { const target = (event.target as Element).getAttribute('data-target'); setProject(target); }; const renderProjectItem = (selectedId: string, item: IProjectCard) => ( {item.name} ); const renderProjectOptions = () => [ {ALL_PROJECTS.name} , ...projects.map(project => renderProjectItem(currentProjectId, project) ), ]; return ( ); }; export default ProjectSelect;