import React from 'react'; import { Box, IconButton, List, ListItem, ListItemIcon, ListItemText, Tooltip, } from '@mui/material'; import { useTheme } from '@mui/material/styles'; import { useLocation } from 'react-router-dom'; import { CollapseLeftIcon } from './collapse-icons'; import { getMainElements, getBottomElements } from './elements'; import { ACTIVITY_BAR_COLLAPSED_WIDTH, ACTIVITY_BAR_EXPANDED_WIDTH, ActivityBar, ActivityBarHeader, SidebarContent, SidebarPanelHeader, StyledDrawer, StyledNavLink, } from './styles'; import { useAppContext } from '../../hooks'; import { useLastDataLakeRoute } from '../../hooks/useLastDataLakeRoute'; import { useGetSelectedProject } from '../../controllers'; import { logo, rosettaIcon } from '../../../../assets'; type Props = { content?: React.ReactNode; panelHeaderLeft?: React.ReactNode; panelTitle?: string; }; export const Sidebar: React.FC = ({ content, panelHeaderLeft, panelTitle, }) => { const theme = useTheme(); const { data: selectedProject } = useGetSelectedProject(); const { isSidebarOpen, setIsSidebarOpen } = useAppContext(); const location = useLocation(); const [isBarExpanded, setIsBarExpanded] = React.useState(false); const lastDataLakeRoute = useLastDataLakeRoute(); const isProjectSelected = Boolean(selectedProject?.id); const activeItemPath = React.useMemo(() => { const path = location.pathname; if (path.includes('dbt-project')) return '/app/dbt-project'; if (path.includes('cloud-explorer')) return '/app/cloud-explorer'; if (path.includes('data-lake') || path.includes('datalake')) return '/app/data-lake'; if (path.includes('flows')) return '/app/flows'; if (path.includes('connection')) return '/app/connections'; if (path.includes('notebooks')) return '/app/notebooks'; if (path.includes('sql')) return '/app/sql'; if (path.includes('settings')) return '/app/settings'; if (path === '/app') return '/app'; return ''; }, [location.pathname]); const mainElements = getMainElements(isProjectSelected); const bottomElements = getBottomElements(); const activityBarWidth = isBarExpanded ? ACTIVITY_BAR_EXPANDED_WIDTH : ACTIVITY_BAR_COLLAPSED_WIDTH; const isPanelOpen = Boolean(content) && isSidebarOpen; // Auto-collapse sidebar panel when on Home (/app) and auto-expand when navigating to any module React.useEffect(() => { if (location.pathname === '/app') { setIsSidebarOpen(false); } else { setIsSidebarOpen(true); } setIsBarExpanded(false); }, [location.pathname, setIsSidebarOpen]); const renderItem = (element: (typeof mainElements)[0], isActive: boolean) => { const isDisabled = element.disabled; const targetPath = element.path === '/app/data-lake' ? lastDataLakeRoute : element.path; const listItem = ( *': { transform: 'scale(1.08)' }, }} > {isBarExpanded && ( )} ); const wrapped = ( {listItem} ); if (isBarExpanded) { return {wrapped}; } return ( {wrapped} ); }; return ( {isBarExpanded ? ( Logo setIsBarExpanded(false)} sx={{ opacity: 0.6, '&:hover': { opacity: 1 } }} > ) : ( setIsBarExpanded(true)} > Icon )} {/* Main items */} {mainElements.map((el) => renderItem(el, activeItemPath === el.path), )} {bottomElements.map((el) => renderItem(el, activeItemPath === el.path), )} {content && ( {panelHeaderLeft && ( {panelHeaderLeft} )} {!panelHeaderLeft && panelTitle && ( {panelTitle} )} setIsSidebarOpen(false)} sx={{ ml: 'auto', opacity: 0.6, '&:hover': { opacity: 1 } }} > {content} )} ); };