-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d61ead0
commit 673987d
Showing
9 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Popular' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
673987d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
gallery – ./apps/gallery
gallery-thedaviddias1.vercel.app
thedaviddias-gallery.vercel.app
gallery-git-main-thedaviddias1.vercel.app
gallery.thedaviddias.dev
673987d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blog – ./apps/blog
blog-git-main-thedaviddias1.vercel.app
blog-thedaviddias1.vercel.app
thedaviddias-blog.vercel.app
thedaviddias.dev
www.thedaviddias.dev