|
| 1 | +import { useState, useContext } from "react"; |
| 2 | +import { MuiMarkdown, defaultOverrides } from "mui-markdown"; |
| 3 | + |
| 4 | +import { |
| 5 | + Box, |
| 6 | + Typography, |
| 7 | + ListItemButton, |
| 8 | + IconButton, |
| 9 | + List, |
| 10 | + ListItemText, |
| 11 | + Divider, |
| 12 | + Modal, |
| 13 | + Collapse, |
| 14 | + useTheme, |
| 15 | +} from "@mui/material"; |
| 16 | + |
| 17 | +import { grey } from "@mui/material/colors"; |
| 18 | + |
| 19 | +import CloseIcon from "@mui/icons-material/Close"; |
| 20 | + |
| 21 | +import ExpandLess from "@mui/icons-material/ExpandLess"; |
| 22 | +import ExpandMore from "@mui/icons-material/ExpandMore"; |
| 23 | +import { ApplicationsContext } from "../../contexts"; |
| 24 | + |
| 25 | +/** |
| 26 | + * |
| 27 | + * @param isOpen Boolean indicating if the help page is open |
| 28 | + * @param closeHelp Function to close the help page |
| 29 | + * @returns Component to display the help overlay with user documentation |
| 30 | + */ |
| 31 | +export default function HelpOverlay({ |
| 32 | + isOpen, |
| 33 | + closeHelp, |
| 34 | +}: { |
| 35 | + isOpen: boolean; |
| 36 | + closeHelp: () => void; |
| 37 | +}) { |
| 38 | + const [selected, setSelected] = useState<string>("general"); |
| 39 | + const [expandedApps, setExpandedApps] = useState<Record<string, boolean>>({}); |
| 40 | + |
| 41 | + const userDocumentation = useContext(ApplicationsContext)[5]; |
| 42 | + const theme = useTheme(); |
| 43 | + |
| 44 | + const backgroundColor = theme.palette.mode === "dark" ? grey[900] : grey[50]; |
| 45 | + |
| 46 | + const customOverrides = { |
| 47 | + ...defaultOverrides, |
| 48 | + h1: { |
| 49 | + component: Typography, |
| 50 | + props: { |
| 51 | + variant: "h3", |
| 52 | + gutterBottom: true, |
| 53 | + sx: { |
| 54 | + marginTop: "1.5rem", |
| 55 | + marginBottom: "1.5rem", |
| 56 | + }, |
| 57 | + } as React.HTMLProps<HTMLHeadingElement>, |
| 58 | + }, |
| 59 | + h2: { |
| 60 | + component: Typography, |
| 61 | + props: { |
| 62 | + variant: "h4", |
| 63 | + gutterBottom: true, |
| 64 | + sx: { |
| 65 | + marginTop: "1rem", |
| 66 | + marginBottom: "0.5rem", |
| 67 | + }, |
| 68 | + } as React.HTMLProps<HTMLHeadingElement>, |
| 69 | + }, |
| 70 | + h3: { |
| 71 | + component: Typography, |
| 72 | + props: { |
| 73 | + variant: "h5", |
| 74 | + gutterBottom: true, |
| 75 | + sx: { |
| 76 | + marginTop: "0.75rem", |
| 77 | + marginBottom: "0.25rem", |
| 78 | + }, |
| 79 | + } as React.HTMLProps<HTMLHeadingElement>, |
| 80 | + }, |
| 81 | + p: { |
| 82 | + component: Typography, |
| 83 | + props: { |
| 84 | + paragraph: true, |
| 85 | + } as React.HTMLProps<HTMLParagraphElement>, |
| 86 | + }, |
| 87 | + }; |
| 88 | + // Handle application section toggle |
| 89 | + const toggleApp = (appName: string) => { |
| 90 | + setExpandedApps((prev) => ({ |
| 91 | + ...prev, |
| 92 | + [appName]: !prev[appName], |
| 93 | + })); |
| 94 | + }; |
| 95 | + |
| 96 | + // Render content based on selected item |
| 97 | + const RenderContent = () => { |
| 98 | + if (selected === "general") { |
| 99 | + return ( |
| 100 | + <> |
| 101 | + <MuiMarkdown overrides={customOverrides}> |
| 102 | + {userDocumentation.generalUsage} |
| 103 | + </MuiMarkdown> |
| 104 | + </> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + const [appName, sectionTitle] = selected.split("::"); |
| 109 | + const app = userDocumentation.applications.find( |
| 110 | + (a) => a.appName === appName, |
| 111 | + ); |
| 112 | + const section = app?.sections.find((s) => s.title === sectionTitle); |
| 113 | + |
| 114 | + return ( |
| 115 | + <> |
| 116 | + <MuiMarkdown overrides={customOverrides}> |
| 117 | + {section?.content} |
| 118 | + </MuiMarkdown> |
| 119 | + </> |
| 120 | + ); |
| 121 | + }; |
| 122 | + |
| 123 | + return ( |
| 124 | + <Modal open={isOpen} onClose={closeHelp}> |
| 125 | + <Box |
| 126 | + sx={{ |
| 127 | + position: "absolute", |
| 128 | + display: "flex", |
| 129 | + width: "80vw", |
| 130 | + height: "80vh", |
| 131 | + bgcolor: backgroundColor, |
| 132 | + borderRadius: 2, |
| 133 | + overflow: "hidden", |
| 134 | + top: "50%", |
| 135 | + left: "50%", |
| 136 | + transform: "translate(-50%, -50%)", |
| 137 | + }} |
| 138 | + > |
| 139 | + {/* Left vertical menu */} |
| 140 | + <IconButton |
| 141 | + onClick={closeHelp} |
| 142 | + sx={{ position: "absolute", top: 8, right: 8 }} |
| 143 | + aria-label="Close" |
| 144 | + > |
| 145 | + <CloseIcon /> |
| 146 | + </IconButton> |
| 147 | + <Box |
| 148 | + sx={{ width: 250, borderRight: "1px solid #ddd", overflowY: "auto" }} |
| 149 | + > |
| 150 | + <List> |
| 151 | + <ListItemButton |
| 152 | + selected={selected === "general"} |
| 153 | + onClick={() => setSelected("general")} |
| 154 | + > |
| 155 | + <ListItemText primary="General Usage" /> |
| 156 | + </ListItemButton> |
| 157 | + <Divider /> |
| 158 | + {userDocumentation.applications.map((app, index) => ( |
| 159 | + <Box key={index}> |
| 160 | + <ListItemButton onClick={() => toggleApp(app.appName)}> |
| 161 | + <ListItemText primary={app.appName} /> |
| 162 | + {expandedApps[app.appName] ? <ExpandLess /> : <ExpandMore />} |
| 163 | + </ListItemButton> |
| 164 | + <Collapse |
| 165 | + in={expandedApps[app.appName]} |
| 166 | + timeout="auto" |
| 167 | + unmountOnExit |
| 168 | + > |
| 169 | + <List component="div" disablePadding> |
| 170 | + {app.sections.map((section) => ( |
| 171 | + <ListItemButton |
| 172 | + key={section.title} |
| 173 | + sx={{ pl: 4 }} |
| 174 | + selected={ |
| 175 | + selected === `${app.appName}::${section.title}` |
| 176 | + } |
| 177 | + onClick={() => |
| 178 | + setSelected(`${app.appName}::${section.title}`) |
| 179 | + } |
| 180 | + > |
| 181 | + <ListItemText primary={section.title} /> |
| 182 | + </ListItemButton> |
| 183 | + ))} |
| 184 | + </List> |
| 185 | + </Collapse> |
| 186 | + </Box> |
| 187 | + ))} |
| 188 | + </List> |
| 189 | + </Box> |
| 190 | + |
| 191 | + {/* Right content pane */} |
| 192 | + <Box sx={{ flex: 1, p: 3, overflowY: "auto" }}>{RenderContent()}</Box> |
| 193 | + </Box> |
| 194 | + </Modal> |
| 195 | + ); |
| 196 | +} |
0 commit comments