// components/layout/BackstoryLayout.tsx import React, { ReactElement, useEffect, useState } from "react"; import { Outlet, useLocation, Routes, Route } from "react-router-dom"; import { Box, Container, Paper } from "@mui/material"; import { useNavigate } from "react-router-dom"; import { SxProps, Theme } from "@mui/material"; import { darken } from "@mui/material/styles"; import { Header } from "components/layout/Header"; import { Scrollable } from "components/Scrollable"; import { Footer } from "components/layout/Footer"; import { Snack, SetSnackType } from "components/Snack"; import { User } from "types/types"; import { LoadingComponent } from "components/LoadingComponent"; import { AuthProvider, useAuth, ProtectedRoute } from "hooks/AuthContext"; import { useAppState, useSelectedCandidate } from "hooks/GlobalContext"; import { getMainNavigationItems, getAllRoutes } from "config/navigationConfig"; import { NavigationItem } from "types/navigation"; // Legacy type for backward compatibility export type NavigationLinkType = { label: ReactElement | string; path: string; icon?: ReactElement; }; interface BackstoryPageContainerProps { children?: React.ReactNode; sx?: SxProps; } const BackstoryPageContainer = (props: BackstoryPageContainerProps) => { const { children, sx } = props; return ( {children} ); }; interface BackstoryLayoutProps { page: string; chatRef: React.Ref; } const BackstoryLayout: React.FC = ( props: BackstoryLayoutProps ) => { const { page, chatRef } = props; const { setSnack } = useAppState(); const navigate = useNavigate(); const location = useLocation(); const { guest, user } = useAuth(); const [navigationItems, setNavigationItems] = useState([]); useEffect(() => { const userType = user?.userType || null; setNavigationItems( getMainNavigationItems(userType, user?.isAdmin ? true : false) ); }, [user]); // Generate dynamic routes from navigation config const generateRoutes = () => { if (!guest && !user) return null; const userType = user?.userType || null; const isAdmin = user?.isAdmin ? true : false; const routes = getAllRoutes(userType, isAdmin); return routes .map((route, index) => { if (!route.path || !route.component) return null; // Clone the component and pass necessary props if it's a page component const componentWithProps = React.cloneElement( route.component as ReactElement, { ...(route.id === "chat" && { ref: chatRef }), ...(route.component.props || {}), } ); return ( ); }) .filter(Boolean); }; return (
darken(theme.palette.background.default, 0.4), height: "100%", maxHeight: "100%", minHeight: "100%", minWidth: "min-content", }} > {!guest && !user && ( )} {(guest || user) && ( <> {generateRoutes()} )} {location.pathname === "/" &&