Skip to content

Commit

Permalink
feat: Refactor Sidebar component and add AboutPage (#1406)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt committed Dec 26, 2023
1 parent d01d7a9 commit ad66d22
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 57 deletions.
94 changes: 47 additions & 47 deletions frontend/src/component/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,65 @@ import MenuIcon from '@mui/icons-material/Menu';
import Collapse from '@mui/material/Collapse';
import { LoginButton } from './LoginButton';

export type LinkType = { title: string; url: string };

interface Props {
export type LinkType = { title: string; url: string; subLinks?: LinkType[] };
interface ListItemsProps {
links: LinkType[];
}

interface ListItemsProps {
links: LinkType[];
subLinks: LinkType[];
function NewComponent(props: { link: LinkType }) {
const [open, setOpen] = React.useState(true);
const navigate = useNavigate();

return (
<div>
<ListItemButton onClick={() => setOpen(!open)}>
<ListItemIcon>
<MenuIcon />
</ListItemIcon>
<ListItemText primary={props.link.title} sx={{ color: '#fff' }} />
</ListItemButton>
<Collapse in={open} timeout="auto" unmountOnExit>
<List disablePadding>
{props.link.subLinks?.map((subLink: LinkType) => (
<ListItemButton
key={subLink.url}
sx={{ pl: 4 }}
onClick={() => navigate(subLink.url)}
>
<ListItemText primary={subLink.title} sx={{ color: '#fff' }} />
</ListItemButton>
))}
</List>
</Collapse>
</div>
);
}

function ListItems(props: ListItemsProps) {
const { links, subLinks } = props;
const navigate = useNavigate();
const [open, setOpen] = React.useState(true);
const { links } = props;

return (
<>
{links.map((link) =>
link.title === 'Home' ? (
<div key={link.url}>
<ListItemButton onClick={() => setOpen(!open)}>
<ListItemIcon>
<MenuIcon />
</ListItemIcon>
<ListItemText primary={link.title} sx={{ color: '#fff' }} />
</ListItemButton>
<Collapse in={open} timeout="auto" unmountOnExit>
<List component="div" disablePadding>
{subLinks.map((subLink) => (
<ListItemButton
key={subLink.url}
sx={{ pl: 4 }}
onClick={() => navigate(subLink.url)}
>
<ListItemText
primary={subLink.title}
sx={{ color: '#fff' }}
/>
</ListItemButton>
))}
</List>
</Collapse>
</div>
) : (
<ListItemButton key={link.url} onClick={() => navigate(link.url)}>
<ListItemIcon>
<MenuIcon />
</ListItemIcon>
<ListItemText primary={link.title} sx={{ color: '#fff' }} />
</ListItemButton>
)
)}
{links.map((link) => (
<NewComponent key={link.url} link={link} />
))}
</>
);
}

function Sidebar(props: Props) {
const subLinks: LinkType[] = [{ title: 'LiveFeed', url: '/livefeed' }];
function Sidebar() {
const navigationItems: LinkType[] = [
{
title: 'Home',
url: '/',
subLinks: [{ title: 'LiveFeed', url: '/livefeed' }],
},
{
title: 'Statistics',
url: '/statistics',
subLinks: [{ title: 'About', url: '/about' }],
},
];

return (
<Box sx={{ width: 250, bgcolor: '#272727', height: '100vh' }}>
Expand All @@ -84,7 +84,7 @@ function Sidebar(props: Props) {
</Typography>
<Divider />
<List>
<ListItems links={props.links} subLinks={subLinks} />
<ListItems links={navigationItems} />
</List>
<LoginButton
sx={{ mx: 1, my: 1, color: '#fff', borderColor: '#fff', width: '90%' }}
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ResultView from './pages/ResultView';
import reportWebVitals from './reportWebVitals';
import { StatisticPage } from './pages/StatisticsPage';
import { LiveViewPage } from './pages/LiveViewPage';
import AboutPage from './pages/Aboutpage';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
Expand All @@ -39,7 +40,11 @@ const router = createBrowserRouter([
element: <LiveViewPage />,
errorElement: <LandingPage children={<DashBoard />} />,
},

{
path: '/about',
element: <AboutPage />,
errorElement: <LandingPage children={<DashBoard />} />,
},
{
path: '/mutation/addproject',
element: <AddProjectView />,
Expand Down
138 changes: 134 additions & 4 deletions frontend/src/pages/Aboutpage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,141 @@
import React from 'react';
import { Typography } from '@mui/material';
import React, { useEffect, useState } from 'react';
import {
Box,
Breadcrumbs,
Container,
Link,
Paper,
styled,
Typography,
} from '@mui/material';
import { Error } from '@mui/icons-material';
import PageLayout from './PageLayout';

export default function AboutPage() {
function getBreadcrumbs() {
return (
<Breadcrumbs aria-label="breadcrumb" sx={{ paddingTop: '10px' }}>
<Link color="inherit" href="/">
Home
</Link>
<Typography color="textPrimary">About</Typography>
</Breadcrumbs>
);
}

const AboutPage = () => {
const [data, setData] = useState<APIData | null>(null);

useEffect(() => {
fetch('https://laughing-train.keksdose.xyz/q/info')
.then((response) => response.json())
.then((data: APIData) => setData(data))
.catch((error) =>
console.error('Error while fetching API data: ', error)
);
}, []);

// Check if data is loaded
if (data === null)
return (
<PageLayout>
{getBreadcrumbs()}
<Box
padding="5px"
display="flex"
flexDirection="row"
alignItems="center"
>
<Error> </Error>
<Typography variant="h5">Loading...</Typography>
</Box>
</PageLayout>
);

return (
<PageLayout>
<Typography variant="h2">About page content goes here</Typography>
{getBreadcrumbs()}
<Container maxWidth="md" sx={{ alignSelf: 'left' }}>
<DataSection title="Git" data={data.git} />
<DataSection title="Java" data={data.java} />
<DataSection title="OS" data={data.os} />
<DataSection title="Build" data={data.build} />
</Container>
</PageLayout>
);
};

export default AboutPage;
interface Commit {
id: string;
time: string;
}

interface Git {
branch: string;
commit: Commit;
}

interface Java {
version: string;
}

interface OS {
name: string;
version: string;
arch: string;
}

interface Build {
group: string;
artifact: string;
version: string;
time: string;
}

interface APIData {
git: Git;
java: Java;
os: OS;
build: Build;
}
const DataSection = (props: { title: string; data: any }) => {
const { title, data } = props;
return (
<StyledBox>
<Paper elevation={3} sx={{ padding: '5px' }}>
<Typography variant="h5" gutterBottom align="left">
{title}
</Typography>
{Object.keys(data).map((key) => {
// If the key is 'commit', it's an object - list its keys and values separately
if (key === 'commit' && typeof data[key] === 'object') {
return (
<>
{Object.keys(data[key]).map((subKey) => {
return (
<Typography align="left" key={subKey}>{`Commit ${subKey
.charAt(0)
.toUpperCase()}${subKey.slice(1)}: ${
data[key][subKey]
}`}</Typography>
);
})}
</>
);
} else {
return (
<Typography align="left" key={key}>{`${key
.charAt(0)
.toUpperCase()}${key.slice(1)}: ${data[key]}`}</Typography>
);
}
})}
</Paper>
</StyledBox>
);
};
const StyledBox = styled(Box)({
backgroundColor: '#f5f5f5', // lighter gray background
borderRadius: '15px', // rounded corners
margin: '10px 0', // some vertical margin to separate the boxes
});
6 changes: 1 addition & 5 deletions frontend/src/pages/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ interface PageLayoutProps {
}

export default function PageLayout({ children }: PageLayoutProps) {
const navigationItems: LinkType[] = [
{ title: 'Home', url: '/' },
{ title: 'Statistics', url: '/statistics' },
];
return (
<div style={{ height: '100vh' }}>
<Grid
Expand All @@ -20,7 +16,7 @@ export default function PageLayout({ children }: PageLayoutProps) {
style={{ height: 'calc(100%) ', flexWrap: 'nowrap' }}
>
<Grid item xs={2.5}>
<Sidebar links={navigationItems} />
<Sidebar />
</Grid>
<Grid item xs={9} style={{ overflow: 'auto' }}>
{children}
Expand Down

0 comments on commit ad66d22

Please sign in to comment.