1
0
mirror of https://github.com/kamranahmedse/developer-roadmap.git synced 2025-02-24 03:23:08 +01:00

197 lines
5.1 KiB
TypeScript
Raw Normal View History

import { useFetch } from 'use-http';
import { useEffect, useRef, useState } from 'react';
import { Box, Container } from '@chakra-ui/react';
2021-12-01 22:53:40 +01:00
import { GlobalHeader } from '../../components/global-header';
import { OpensourceBanner } from '../../components/opensource-banner';
import { Footer } from '../../components/footer';
import { getAllRoadmaps, getRoadmapById, RoadmapType } from '../../lib/roadmap';
import Helmet from '../../components/helmet';
import { wireframeJSONToSVG } from '../../lib/renderer';
2021-12-03 19:58:25 +01:00
import { RoadmapPageHeader } from '../../components/roadmap/roadmap-page-header';
2021-12-04 16:04:31 +01:00
import { ContentDrawer } from '../../components/roadmap/content-drawer';
import { RoadmapError } from '../../components/roadmap/roadmap-error';
import { RoadmapLoader } from '../../components/roadmap/roadmap-loader';
2021-12-09 16:39:09 +01:00
import { removeSortingInfo } from '../../lib/renderer/utils';
2021-12-01 22:53:40 +01:00
type RoadmapProps = {
roadmap: RoadmapType;
};
export function InteractiveRoadmapRenderer(props: RoadmapProps) {
const { roadmap } = props;
const { loading: isLoading, error: hasErrorLoading, get } = useFetch();
2021-12-01 22:53:40 +01:00
const roadmapRef = useRef(null);
2021-12-06 21:54:14 +01:00
const [isRendering, setIsRendering] = useState(true);
const [roadmapJson, setRoadmapJson] = useState(null);
2021-12-04 16:04:31 +01:00
const [groupId, setGroupId] = useState('');
const [hasErrorRendering, setHasErrorRendering] = useState(false);
2021-12-01 22:53:40 +01:00
useEffect(() => {
2021-12-09 16:39:09 +01:00
if (!roadmap.jsonUrl) {
return;
}
2021-12-07 14:05:13 +01:00
get(roadmap.jsonUrl)
.then((roadmapJson) => {
setRoadmapJson(roadmapJson);
})
.catch((err) => {
console.error(err);
setHasErrorRendering(true);
});
2021-12-07 14:05:13 +01:00
}, [get, roadmap.id, roadmap.jsonUrl]);
// Event bindings for the roadmap interactivity
2021-12-01 22:53:40 +01:00
useEffect(() => {
function keydownListener(event: KeyboardEvent) {
2021-12-04 16:04:31 +01:00
if (event.key.toLowerCase() === 'escape') {
setGroupId('');
}
}
2021-12-04 16:04:31 +01:00
function clickListener(event: MouseEvent) {
2021-12-01 22:53:40 +01:00
const targetGroup = (event?.target as HTMLElement)?.closest('g');
2021-12-04 16:04:31 +01:00
const groupId = targetGroup?.dataset?.groupId;
if (!targetGroup || !groupId) {
2021-12-01 22:53:40 +01:00
return;
}
if (groupId.startsWith('ext_link:')) {
window.open(`https://${groupId.replace('ext_link:', '')}`);
return;
}
2021-12-04 16:04:31 +01:00
// e.g. 100-internet:how-does-the-internet-work
// will be translated to `internet:how-does-the-internet-work`
2021-12-09 16:39:09 +01:00
setGroupId(removeSortingInfo(groupId));
}
window.addEventListener('keydown', keydownListener);
window.addEventListener('click', clickListener);
return () => {
window.removeEventListener('keydown', keydownListener);
window.removeEventListener('click', clickListener);
2021-12-06 21:54:14 +01:00
};
}, []);
2021-12-01 22:53:40 +01:00
useEffect(() => {
if (!roadmapJson) {
return;
}
2021-12-06 21:54:14 +01:00
setIsRendering(true);
wireframeJSONToSVG(roadmapJson)
2021-12-01 22:53:40 +01:00
.then((svgElement) => {
const container: HTMLElement = roadmapRef.current!;
if (!container) {
return;
}
if (container.firstChild) {
container.removeChild(container.firstChild);
}
container.appendChild(svgElement);
})
.catch((err) => {
setHasErrorRendering(true);
2021-12-06 21:54:14 +01:00
})
.finally(() => {
setIsRendering(false);
2021-12-01 22:53:40 +01:00
});
}, [roadmapJson]);
2021-12-01 22:53:40 +01:00
2021-12-09 16:39:09 +01:00
if (!roadmap.jsonUrl) {
return null;
}
if (hasErrorLoading || hasErrorRendering) {
2021-12-09 16:39:09 +01:00
return <RoadmapError roadmap={roadmap} />;
}
let minHeight: string[] = [];
if (roadmap.id === 'frontend') {
minHeight = ['970px', '970px', '2100px', '2800px', '2800px'];
}
if (roadmap.id === 'backend') {
minHeight = ['870px', '1130px', '1900px', '2500px', '2520px', '2520px'];
}
if (roadmap.id === 'devops') {
minHeight = ['870px', '1920px', '2505px', '2591px', '2591px', '2591px'];
}
2021-12-01 22:53:40 +01:00
return (
<Container maxW={'container.lg'} position="relative" minHeight={minHeight}>
{(isLoading || isRendering) && <RoadmapLoader />}
2021-12-04 16:04:31 +01:00
<ContentDrawer
roadmap={roadmap}
groupId={groupId}
onClose={() => setGroupId('')}
/>
2021-12-04 14:04:44 +01:00
2021-12-01 22:53:40 +01:00
<div ref={roadmapRef} />
</Container>
);
}
export default function InteractiveRoadmap(props: RoadmapProps) {
const { roadmap } = props;
2021-12-01 22:53:40 +01:00
return (
<Box bg="white" minH="100vh">
<GlobalHeader />
<Helmet
title={roadmap?.seo?.title || roadmap.title}
description={roadmap?.seo?.description || roadmap.description}
keywords={roadmap?.seo.keywords || []}
/>
<Box mb="60px">
2021-12-03 19:58:25 +01:00
<RoadmapPageHeader roadmap={roadmap} />
<InteractiveRoadmapRenderer roadmap={roadmap} />
2021-12-01 22:53:40 +01:00
</Box>
<OpensourceBanner />
<Footer />
</Box>
);
}
type StaticPathItem = {
params: {
roadmap: string;
};
};
export async function getStaticPaths() {
const roadmaps = getAllRoadmaps();
const paramsList: StaticPathItem[] = roadmaps.map((roadmap) => ({
params: { roadmap: roadmap.id },
}));
return {
paths: paramsList,
fallback: false,
};
}
type ContextType = {
params: {
roadmap: string;
};
};
export async function getStaticProps(context: ContextType) {
const roadmapId: string = context?.params?.roadmap;
return {
props: {
roadmap: getRoadmapById(roadmapId),
},
};
}