-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pull most used tags from DB instead of using hardcoded list (#795)
* feat: pull most used tags from DB instead of using hardcoded list * Update server/api/router/tag.ts * chore: die prettier die
- Loading branch information
1 parent
6924ace
commit 01cdafe
Showing
10 changed files
with
181 additions
and
54 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
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,31 @@ | ||
"use server"; | ||
|
||
import Link from "next/link"; | ||
import { GetTags } from "@/server/lib/tags"; | ||
import { getCamelCaseFromLower } from "@/utils/utils"; | ||
|
||
export default async function PopularTags() { | ||
const tags = await GetTags({ take: 10 }); | ||
// Refactor with option to refresh | ||
if (!tags) | ||
return ( | ||
<div className="relative mt-4 text-lg font-semibold md:col-span-7"> | ||
Something went wrong loading topics... Please refresh the page. | ||
</div> | ||
); | ||
|
||
return ( | ||
<> | ||
{tags.map((tag) => ( | ||
<Link | ||
// only reason this is toLowerCase is to make url look nicer. Not needed for functionality | ||
href={`/articles?tag=${tag.title.toLowerCase()}`} | ||
key={tag.title} | ||
className="border border-neutral-300 bg-white px-6 py-2 text-neutral-900 dark:border-neutral-600 dark:bg-neutral-900 dark:text-neutral-50" | ||
> | ||
{getCamelCaseFromLower(tag.title)} | ||
</Link> | ||
))} | ||
</> | ||
); | ||
} |
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,34 @@ | ||
//className="border border-neutral-300 bg-white px-6 py-2 text-neutral-900 dark:border-neutral-600 dark:bg-neutral-900 dark:text-neutral-50" | ||
|
||
function PopularTagsLoading() { | ||
return ( | ||
<div className=" w-full shadow dark:bg-black"> | ||
<div> | ||
<div className="my-2 flex h-10"> | ||
<div className="h-10 w-1/2 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-2/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
</div> | ||
<div className="my-2 flex h-10"> | ||
<div className="h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
</div> | ||
<div className="my-2x flex h-10"> | ||
<div className="h-10 w-1/2 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-2/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
</div> | ||
<div className="my-2 flex h-10"> | ||
<div className="h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-1/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
</div> | ||
<div className="my-2x flex h-10"> | ||
<div className="h-10 w-1/2 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
<div className="ml-2 h-10 w-2/3 animate-pulse rounded-md bg-neutral-300 dark:bg-neutral-800" /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default PopularTagsLoading; |
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,5 @@ | ||
import z from "zod"; | ||
|
||
export const GetTagsSchema = z.object({ | ||
take: z.number(), | ||
}); |
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,26 @@ | ||
import { createTRPCRouter, publicProcedure } from "../trpc"; | ||
import { GetTagsSchema } from "../../../schema/tag"; | ||
import { TRPCError } from "@trpc/server"; | ||
|
||
export const tagRouter = createTRPCRouter({ | ||
get: publicProcedure.input(GetTagsSchema).query(async ({ ctx, input }) => { | ||
try { | ||
const count = await ctx.db.tag.count({}); | ||
const response = await ctx.db.tag.findMany({ | ||
orderBy: { | ||
PostTag: { | ||
_count: "desc", | ||
}, | ||
}, | ||
take: input.take, | ||
}); | ||
|
||
return { data: response, count }; | ||
} catch (error) { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: "Failed to fetch tags", | ||
}); | ||
} | ||
}), | ||
}); |
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,34 @@ | ||
import db from "@/server/db/client"; | ||
import * as Sentry from "@sentry/nextjs"; | ||
import "server-only"; | ||
import { z } from "zod"; | ||
|
||
export const GetTagsSchema = z.object({ | ||
take: z.number(), | ||
}); | ||
|
||
type GetTags = z.infer<typeof GetTagsSchema>; | ||
|
||
export async function GetTags({ take }: GetTags) { | ||
try { | ||
GetTagsSchema.parse({ take }); | ||
|
||
const response = await db.tag.findMany({ | ||
orderBy: { | ||
PostTag: { | ||
_count: "desc", | ||
}, | ||
}, | ||
take: take, | ||
}); | ||
|
||
if (!response) { | ||
return null; | ||
} | ||
|
||
return response; | ||
} catch (error) { | ||
Sentry.captureException(error); | ||
throw new Error("Error fetching tags"); | ||
} | ||
} |
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