Render roadmaps on homepage

This commit is contained in:
Kamran Ahmed
2021-08-29 16:05:19 +02:00
parent 7c3c552ff1
commit 964a87f605
17 changed files with 241 additions and 72 deletions

36
lib/gtag.ts Normal file
View File

@@ -0,0 +1,36 @@
declare global {
interface Window {
gtag: any;
}
}
// https://developers.google.com/analytics/devguides/collection/gtagjs/pages
export const firePageView = (url: string) => {
if (!window.gtag) {
console.warn('Missing GTAG Analytics disabled');
return;
}
window.gtag('config', process.env.GA_SECRET, {
page_path: url
});
};
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
export const event = (props: { action: string; category: string; label: string; value: string; }) => {
const { action, category, label, value } = props;
if (!window.gtag) {
console.warn('Missing GTAG Analytics disabled');
return;
}
window.gtag(
'event',
action,
{
event_category: category,
event_label: label,
value: value
}
);
};

41
lib/roadmap.ts Normal file
View File

@@ -0,0 +1,41 @@
import { NextApiRequest } from 'next';
import roadmaps from '../content/roadmaps.json';
export type RoadmapType = {
seo: {
title: string;
description: string;
keywords: string[]
},
title: string,
description: string,
featuredTitle: string;
featuredDescription: string,
author: {
name: string,
url: string
},
featured: boolean,
imagePath?: string,
contentPath?: string;
resourcesPath: string;
isCommunity: boolean;
url: string;
};
export function getRequestedRoadmap(req: NextApiRequest): RoadmapType | undefined {
// remove trailing slashes
const normalizedUrl = req.url?.replace(/\/$/, '') || '';
return (roadmaps as RoadmapType[]).find(roadmap => normalizedUrl.startsWith(roadmap.url));
}
export function getAllRoadmaps(): RoadmapType[] {
return (roadmaps as RoadmapType[]);
}
export function getFeaturedRoadmaps(): RoadmapType[] {
const roadmaps: RoadmapType[] = getAllRoadmaps();
return roadmaps.filter(roadmap => roadmap.featured);
}

27
lib/url.ts Normal file
View File

@@ -0,0 +1,27 @@
import siteConfig from '../content/site.json';
export const prefixHost = (url: string) => {
// if it starts with forward slash then prefix site URL
// @todo proper handling of full URLs and trailing slashes
return /^\//.test(url) ? `${siteConfig?.url?.web}${url}` : url;
};
export const getTwitterUrl = (username: string) => {
return `https://twitter.com/${username}`;
};
export const getTwitterShareUrl = ({ text, url }: { text: string, url: string }) => {
return `https://twitter.com/intent/tweet?text=${text}&url=${encodeURI(prefixHost(url))}`;
};
export const getFacebookShareUrl = ({ text, url }: { text: string, url: string }) => {
return `https://www.facebook.com/sharer/sharer.php?quote=${text}&u=${encodeURI(prefixHost(url))}`;
};
export const getRedditShareUrl = ({ text, url }: { text: string, url: string }) => {
return `https://www.reddit.com/submit?title=${text}&url=${encodeURI(prefixHost(url))}`;
};
export const getHnShareUrl = ({ text, url }: { text: string, url: string }) => {
return `https://news.ycombinator.com/submitlink?t=${text}&u=${prefixHost(url)}`;
};