diff --git a/apps/www/components/Nav/DevelopersDropdown.tsx b/apps/www/components/Nav/DevelopersDropdown.tsx index 76dcf53941b..e3cffcda2f7 100644 --- a/apps/www/components/Nav/DevelopersDropdown.tsx +++ b/apps/www/components/Nav/DevelopersDropdown.tsx @@ -1,13 +1,8 @@ -import React from 'react' -import Link from 'next/link' import { ChevronRight } from 'lucide-react' +import Link from 'next/link' import { data as DevelopersData } from 'data/Developers' -import { BlogPost } from 'contentlayer/generated' - -type Props = { - blogPosts?: BlogPost[] -} +import blogPosts from '~/.contentlayer/generated/LatestBlogPost/_index.json' assert { type: 'json' } type LinkProps = { text: string @@ -17,7 +12,7 @@ type LinkProps = { svg?: any } -const DevelopersDropdown = ({ blogPosts }: Props) => ( +const DevelopersDropdown = () => (
{DevelopersData['navigation'].map((column) => ( @@ -55,7 +50,7 @@ const DevelopersDropdown = ({ blogPosts }: Props) => (
    - {blogPosts?.map((post: any) => ( + {blogPosts?.map((post) => (
  • { const [open, setOpen] = useState(false) const isLoggedIn = useIsLoggedIn() const isUserLoading = useIsUserLoading() - const latestBlogPosts = allBlogPosts.sort(sortDates).slice(0, 2) - const menu = getMenu(latestBlogPosts) + const menu = getMenu() const isHomePage = router.pathname === '/' const isLaunchWeekPage = router.pathname.includes('/launch-week') diff --git a/apps/www/data/nav.tsx b/apps/www/data/nav.tsx index 4ac31ec1373..c74aa12c919 100644 --- a/apps/www/data/nav.tsx +++ b/apps/www/data/nav.tsx @@ -3,9 +3,8 @@ import ProductDropdown from '~/components/Nav/ProductDropdown' import { data as DevelopersData } from 'data/Developers' import SolutionsData from 'data/Solutions' -import { BlogPost } from 'contentlayer/generated' -export const getMenu = (latestBlogPosts: BlogPost[]) => ({ +export const getMenu = () => ({ primaryNav: [ { title: 'Product', @@ -17,7 +16,7 @@ export const getMenu = (latestBlogPosts: BlogPost[]) => ({ { title: 'Developers', hasDropdown: true, - dropdown: , + dropdown: , dropdownContainerClassName: 'rounded-xl', subMenu: DevelopersData, }, diff --git a/apps/www/lib/helpers.tsx b/apps/www/lib/helpers.tsx index 9f928c9b11e..c94a49722f1 100644 --- a/apps/www/lib/helpers.tsx +++ b/apps/www/lib/helpers.tsx @@ -44,22 +44,3 @@ export const stripEmojis = (str: string) => ) .replace(/\s+/g, ' ') .trim() - -/** - * Fixes Safari dates sorting bug - */ -export function sortDates(a: any, b: any, direction: 'asc' | 'desc' = 'desc') { - const isAsc = direction === 'asc' - var reg = /-|:|T|\+/ //The regex on which matches the string should be split (any used delimiter) -> could also be written like /[.:T\+]/ - var parsed = [ - //an array which holds the date parts for a and b - a.date.split(reg), //Split the datestring by the regex to get an array like [Year,Month,Day] - b.date.split(reg), - ] - var dates = [ - //Create an array of dates for a and b - new Date(parsed[0][0], parsed[0][1], parsed[0][2]), //Constructs an date of the above parsed parts (Year,Month... - new Date(parsed[1][0], parsed[1][1], parsed[1][2]), - ] - return isAsc ? (dates[0] as any) - (dates[1] as any) : (dates[1] as any) - (dates[0] as any) //Returns the difference between the date (if b > a then a - b < 0) -} diff --git a/apps/www/package.json b/apps/www/package.json index 48833eec172..863dea91528 100644 --- a/apps/www/package.json +++ b/apps/www/package.json @@ -11,7 +11,7 @@ "lint": "next lint", "clean": "rimraf node_modules", "typecheck": "npm run content:build && tsc --noEmit", - "content:build": "contentlayer2 build", + "content:build": "contentlayer2 build && node scripts/generateLatestBlogPosts.mjs", "prettier": "prettier --write \"./{pages,components,lib,stores,styles,tests}/**/*.{ts,tsx,md,js,jsx,json}\"", "postbuild": "node ./internals/generate-sitemap.mjs" }, diff --git a/apps/www/scripts/generateLatestBlogPosts.mjs b/apps/www/scripts/generateLatestBlogPosts.mjs new file mode 100644 index 00000000000..3a8a36f3cfe --- /dev/null +++ b/apps/www/scripts/generateLatestBlogPosts.mjs @@ -0,0 +1,42 @@ +import { promises as fs } from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' +import { allBlogPosts } from '../.contentlayer/generated/index.mjs' + +/** + * Fixes Safari dates sorting bug + */ +const sortDates = (a, b, direction = 'desc') => { + const isAsc = direction === 'asc' + var reg = /-|:|T|\+/ //The regex on which matches the string should be split (any used delimiter) -> could also be written like /[.:T\+]/ + var parsed = [ + //an array which holds the date parts for a and b + a.date.split(reg), //Split the datestring by the regex to get an array like [Year,Month,Day] + b.date.split(reg), + ] + var dates = [ + //Create an array of dates for a and b + new Date(parsed[0][0], parsed[0][1], parsed[0][2]), //Constructs an date of the above parsed parts (Year,Month... + new Date(parsed[1][0], parsed[1][1], parsed[1][2]), + ] + return isAsc ? dates[0] - dates[1] : dates[1] - dates[0] //Returns the difference between the date (if b > a then a - b < 0) +} + +const latestBlogPosts = allBlogPosts + .sort(sortDates) + .slice(0, 2) + .map(({ title, url, description }) => ({ title, url, description })) + +const __dirname = path.dirname(fileURLToPath(import.meta.url)) +const folderPath = path.join(__dirname, '../.contentlayer/generated/LatestBlogPost') +try { + await fs.mkdir(folderPath, { recursive: true }) +} catch (error) { + if (error.code !== 'EEXIST') { + throw error + } + // Folder already exists, continue silently +} + +const filePath = path.join(__dirname, '../.contentlayer/generated/LatestBlogPost/_index.json') +await fs.writeFile(filePath, JSON.stringify(latestBlogPosts), 'utf8')