Added total articles card to dashboard

This commit is contained in:
r-freeman 2023-01-28 21:59:01 +00:00
parent 51e96f05d1
commit 99d70b1d98
4 changed files with 27 additions and 31 deletions

View File

@ -1,4 +1,6 @@
import {getTotalFollowers, getTotalRepos, getTotalStars} from '@/lib/github'
import {getAllArticles} from '@/lib/getAllArticles'
import {getViews} from '@/lib/views'
export async function dashboard() {
const [totalRepos, totalFollowers] = await Promise.all([
@ -7,6 +9,8 @@ export async function dashboard() {
])
const totalStars = await getTotalStars(totalRepos)
const articles = await getAllArticles()
const {views} = await getViews()
return [
{
@ -23,6 +27,16 @@ export async function dashboard() {
title: "GitHub Stars",
total: totalStars,
href: "https://github.com/r-freeman/"
},
{
title: "Total Articles",
total: articles.length,
href: "/writing"
},
{
title: "Total Article Views",
total: views,
href: "/writing"
}
]
}

13
lib/views.ts Normal file
View File

@ -0,0 +1,13 @@
import {prisma} from '@/lib/prisma'
export async function getViews() {
const totalViews: { _sum: { count: any } } = await prisma.views.aggregate({
_sum: {
count: true
}
})
return {
views: totalViews._sum.count.toString()
}
}

View File

@ -1,18 +0,0 @@
import type {NextApiRequest, NextApiResponse} from 'next'
import {prisma} from '@/lib/prisma'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const totalViews: { _sum: { count: any } } = await prisma.views.aggregate({
_sum: {
count: true
}
})
return res.status(200).json({
views: totalViews._sum.count.toString()
})
} catch (e: any) {
return res.status(500).json({message: e.message})
}
}

View File

@ -1,11 +1,9 @@
import Head from 'next/head'
import {GetStaticProps} from 'next'
import useSWR from 'swr'
import {SimpleLayout} from '@/components/SimpleLayout'
import {Card} from '@/components/Card'
import {numberFormat} from '@/lib/numberFormat'
import {dashboard} from '@/lib/dashboard'
import fetcher from '@/lib/fetcher'
type CardProps = {
title: string
@ -14,9 +12,6 @@ type CardProps = {
}
export default function Dashboard({cards}: { cards: CardProps[] }) {
const {data} = useSWR('/api/views/', fetcher)
const totalArticleViews = Number(data?.views)
return (
<>
<Head>
@ -53,14 +48,6 @@ export default function Dashboard({cards}: { cards: CardProps[] }) {
</Card.Description>
</Card>
))}
<Card as="li">
<h2 className="text-base font-semibold transition group-hover:text-indigo-500 text-zinc-800 dark:text-zinc-400">
<Card.Link href="/writing">Total Article Views</Card.Link>
</h2>
<Card.Description className="text-zinc-800 dark:text-zinc-100 font-semibold text-5xl">
{totalArticleViews > 0 ? numberFormat(totalArticleViews) : '—'}
</Card.Description>
</Card>
</ul>
</SimpleLayout>
</>