Skip to content

Commit

Permalink
feat: add popular posts
Browse files Browse the repository at this point in the history
  • Loading branch information
thedaviddias committed Aug 5, 2023
1 parent d61ead0 commit 673987d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 8 deletions.
1 change: 1 addition & 0 deletions apps/blog/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
"first_published": "First published on",
"published": "Published",
"tableContent": "Table of Contents",
"popular": "Popular posts",
"previous": "Previous post",
"next": "Next post"
},
Expand Down
1 change: 1 addition & 0 deletions apps/blog/locales/fr/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
"published": "Initialement publié sur ",
"tableContent": "Table des matières",
"previous": "Publication précédente",
"popular": "Publications populaires",
"next": "Publication suivante"
},
"webmentions": {
Expand Down
6 changes: 3 additions & 3 deletions apps/blog/public/rss/feed.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions apps/blog/public/rss/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>The David Dias | Front-End Developer, Podcaster &amp; Content Creator</title>
<link>https://thedaviddias.dev</link>
<description>Hey, I'm David Dias, a Front-End Developer based in Toronto/Canada. I love discussing code, technology, expatriation and life.</description>
<lastBuildDate>Sat, 05 Aug 2023 00:41:46 GMT</lastBuildDate>
<lastBuildDate>Sat, 05 Aug 2023 14:39:44 GMT</lastBuildDate>
<docs>https://validator.w3.org/feed/docs/rss2.html</docs>
<generator>https://github.com/jpmonette/feed</generator>
<language>en-US</language>
Expand Down Expand Up @@ -211,13 +211,14 @@ c59.905-109.171,178.724-165.463,295.204-151.033v145.035L512,253z"></path></svg><
<author>thedaviddias@gmail.com (David Dias)</author>
</item>
<item>
<title><![CDATA[The Ideal Desk Setup: A Personal Journey]]></title>
<title><![CDATA[My Ideal Desk Setup: A Personal Journey]]></title>
<link>https://thedaviddias.dev/articles/ideal-desk-setup-personal-journey</link>
<guid>https://thedaviddias.dev/articles/ideal-desk-setup-personal-journey</guid>
<pubDate>Sun, 30 Jul 2023 01:48:39 GMT</pubDate>
<description><![CDATA[Join me as I take you on a tour of my desk setup, showing how it has evolved over the years, and how it enhances my productivity and creative pursuits. Let's discuss and share our ideal desk setups!]]></description>
<description><![CDATA[Join me as I take you on a tour of my ideal desk setup, showing how it has evolved over the years, and how it enhances my productivity and creative pursuits.]]></description>
<content:encoded><![CDATA[<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">I first embarked on my journey to create the perfect desk setup in 2018, when I made the big move from <span class="not-prose"><a href="https://en.wikipedia.org/wiki/Mauritius" rel="noopener noreferrer" target="_blank" class="external-link">Mauritius</a></span> to Canada.</p>
<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">The notion of an ideal setup has always been a tantalizing dream of mine. But when can we honestly say, &quot;This is it, this is the setup I always wanted.&quot;? Well, there&#x27;s no definitive answer to that. However, after a few years of relentless experimentation, I can confidently say that I&#x27;m truly happy with what I have.</p>
<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">The notion of an ideal desk setup has always been a tantalizing dream of mine. But when can we honestly say, &quot;This is it, this is the setup I always wanted.&quot;? Well, there&#x27;s no definitive answer to that. However, after a few years of relentless experimentation, I can confidently say that I&#x27;m truly happy with what I have.</p>
<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">I have always been passionate in exploring different areas of knowledge. Which is somehow reflected in all my past experiences. Most people focus on one specific domain of expertise and become experts in it. Even I have my expertises, I&#x27;m always looking to explore new domains. And this is what you will see reflected in my different desk setups.</p>
<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">(Check out the list of all the gear I use at https://kit.co/thedaviddias)</p>
<h2 class="leading-tight text-3xl md:text-4xl font-title font-medium tracking-tight !mt-0 text-black dark:text-white">Daily desk setup</h2>
<p class="prose max-w-full text-lg my-3 leading-relaxed dark:text-gray-300 ">Yes, my desk is usually that clean! No kidding! Almost every morning or night, I make a habit of removing from my desk what I don&#x27;t need, like a cup of tea or the last letter that came in the mail.</p>
Expand Down
2 changes: 1 addition & 1 deletion apps/blog/public/rss/fr/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>The David Dias | Développeur Front-End, podcasteur &amp; créateur de contenu</title>
<link>https://thedaviddias.dev</link>
<description>Salut toi! Je m'appele David Dias. Je suis développeur Front-End, podcasteur, créateur de contenu numérique passioné pour résoudre les problèmes digitaux et humains! J'aime rencontrer de nouvelles personnes, bâtir des communautées et parler de tech, d'expatriation et de web.</description>
<lastBuildDate>Sat, 05 Aug 2023 00:41:46 GMT</lastBuildDate>
<lastBuildDate>Sat, 05 Aug 2023 14:39:44 GMT</lastBuildDate>
<docs>https://validator.w3.org/feed/docs/rss2.html</docs>
<generator>https://github.com/jpmonette/feed</generator>
<language>fr</language>
Expand Down
35 changes: 35 additions & 0 deletions apps/blog/src/components/Popular/Popular.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import useTranslation from 'next-translate/useTranslation'
import useSWR from 'swr'

import { Loader } from '@/components/Loader'

import { TopPageHandlerRes } from '@/pages/api/plausible/top-pages'
import fetcher from '@/utils/fetcher'

import { CustomLink } from '../CustomLink'

export const Popular: React.FC = () => {
const { t } = useTranslation('common')
const { data, error } = useSWR<TopPageHandlerRes>(`/api/plausible/top-pages`, fetcher)

const results = data?.results

if (!data && !error) return <Loader />

return (
<>
{results ? (
<div className="mb-2 mt-10">
<h3 className="small-title">{t('posts.popular')}</h3>
<ul>
{results.map((result, index) => (
<li key={index} className="pb-3">
<CustomLink href={result.page}>{result.title}</CustomLink>
</li>
))}
</ul>
</div>
) : null}
</>
)
}
1 change: 1 addition & 0 deletions apps/blog/src/components/Popular/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Popular'
68 changes: 68 additions & 0 deletions apps/blog/src/pages/api/plausible/top-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { NextApiRequest, NextApiResponse } from 'next'

import fetcher from '@/utils/fetcher'
import { getAllPostsWithFrontMatter } from '@/utils/get-article-posts/getAllPostsWithFrontMatter'

type GetPlausibleTopPagesResults = {
visitors: number
page: string
}

export type GetPlausibleTopPagesRes = {
results: GetPlausibleTopPagesResults[]
}

async function getPlausibleTopPages() {
const url = `https://plausible.io/api/v1/stats/breakdown?site_id=thedaviddias.dev&period=6mo&property=event:page&limit=10`
return fetcher<GetPlausibleTopPagesRes>(url, {
headers: {
Authorization: `Bearer ${process.env.PLAUSIBLE_API_KEY}`,
Accept: 'application/json',
'cache-control': 'public, s-maxage=2592000, stale-while-revalidate=1296000',
},
})
}

export type TopPageHandlerRes = {
results: {
title: string
visitors: number
page: string
}[]
}

const topPageHandler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const data = await getPlausibleTopPages()

if (!data?.results) {
throw new Error('Could not get results from Plausible')
}

const allPostHeaders = getAllPostsWithFrontMatter({ dataType: 'articles', locale: 'en' })

const popularArticles = data.results
.map((result: GetPlausibleTopPagesResults) => {
const url = result.page
const slug = url.split('/').pop() || '' // Extracts the last part of the URL
const article = allPostHeaders.find((post) => post.slug === slug)

if (article) {
return { ...result, title: article.frontMatter.title }
} else {
return result
}
})
.filter((article: any) => article.title) // Keep only articles that have a title
.slice(0, 5) // Keep only the first 5 results

return res.status(200).json({ results: popularArticles })
} catch (error) {
// eslint-disable-next-line no-console
console.error(error)
res.json(error)
return res.status(500).end()
}
}

export default topPageHandler
2 changes: 2 additions & 0 deletions apps/blog/src/pages/articles/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { H1 } from '@/components/Headings'
import { Loader } from '@/components/Loader'
import { MDXComponents } from '@/components/MdxComponents'
import { Paragraph } from '@/components/Paragraph'
import { Popular } from '@/components/Popular'
import { RelatedPosts } from '@/components/RelatedPosts'
import { ScrollTop } from '@/components/ScrollTop'
import { ShareProps } from '@/components/Share'
Expand Down Expand Up @@ -293,6 +294,7 @@ const BlogPostPage: NextPage<BlogPostPageProps> = ({
{headings && (
<aside className="w-full mt-3">
<TableOfContents headings={headings} />
<Popular />
</aside>
)}
</div>
Expand Down

2 comments on commit 673987d

@vercel
Copy link

@vercel vercel bot commented on 673987d Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 673987d Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.