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

256 lines
7.0 KiB
TypeScript
Raw Normal View History

import { useFetch } from 'use-http';
import React, { useEffect, useRef, useState } from 'react';
import { Box, Container } from '@chakra-ui/react';
2022-08-28 03:11:00 +04:00
import { wireframeJSONToSVG } from 'roadmap-renderer';
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';
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';
2022-08-28 03:11:00 +04:00
import { removeSortingInfo } from '../../lib/renderer';
2022-10-10 16:19:33 +04:00
import { TeamsBanner } from '../../components/teams-banner';
import { ShareIcons } from '../../components/share-icons';
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'];
}
if (roadmap.id === 'vue') {
minHeight = ['600px', '820px', '1340px', '1680px', '1750px', '1750px'];
}
2022-08-12 16:48:48 +04:00
if (roadmap.id === 'react') {
minHeight = ['400px', '865px', '1065px', '1400px', '1400px', '1400px'];
}
2022-08-16 15:45:27 +04:00
if (roadmap.id === 'blockchain') {
minHeight = ['780px', '1120px', '1770px', '2235px', '2235px', '2235px'];
}
2022-08-16 18:40:40 +04:00
if (roadmap.id === 'golang') {
minHeight = ['590px', '1201px', '1201px', '1625px', '1625px', '1625px'];
}
if (roadmap.id === 'javascript') {
minHeight = ['892px', '1835px', '1835px', '2475px', '2475px', '2475px'];
}
2022-08-25 20:14:03 +04:00
if (roadmap.id === 'nodejs') {
minHeight = ['865px', '1855px', '1855px', '2500px', '2500px', '2500px'];
}
2022-09-01 16:24:00 +04:00
if (roadmap.id === 'qa') {
minHeight = ['865px', '1610px', '1610px', '2200px', '2200px', '2200px'];
}
2022-09-13 15:36:22 +04:00
if (roadmap.id === 'design-system') {
minHeight = ['915px', '1760px', '1880px', '2370px', '2370px', '2370px'];
}
2022-09-22 16:58:02 +04:00
if (roadmap.id === 'angular') {
minHeight = ['925px', '1365px', '1740px', '2370px', '2370px', '2370px'];
}
2022-09-26 20:42:32 +04:00
if (roadmap.id === 'software-architect') {
minHeight = ['685px', '1170px', '1470px', '1980px', '1980px', '1980px'];
}
2022-10-03 21:17:32 +04:00
if (roadmap.id === 'software-design-architecture') {
minHeight = ['515px', '1230px', '1310px', '1765px', '1765px', '1765px'];
}
2022-10-24 13:45:28 +05:00
if (roadmap.id === 'aspnet-core') {
2022-10-25 09:05:59 +05:00
minHeight = ['1278px', '2105px', '2748px', '2845px', '2845px', '2845px'];
2022-10-24 13:45:28 +05:00
}
2022-10-28 19:22:48 +04:00
if (roadmap.id === 'flutter') {
minHeight = ['830px', '1534px', '1553px', '2093px', '2093px', '2093px'];
}
2022-11-07 13:15:37 +04:00
if (roadmap.id === 'computer-science') {
minHeight = ['1222px', '1393px', '2288px', '3084px', '3084px', '3084px'];
}
2021-12-01 22:53:40 +01:00
return (
<Container maxW={'container.lg'} position="relative" minHeight={minHeight} pos='relative'>
{(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
2022-11-04 17:26:30 +04:00
{ (!isLoading && !isRendering) && <ShareIcons url={`https://roadmap.sh/${roadmap.id}`} text={roadmap.description} /> }
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 />
2022-10-10 16:19:33 +04:00
<TeamsBanner />
2021-12-01 22:53:40 +01:00
<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),
},
};
}