-
Notifications
You must be signed in to change notification settings - Fork 0
fix: eeror #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: eeror #83
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| import "server-only"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import { updateViewCount } from "@sudan-codex/db"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { Suspense, use } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import { ErrorBoundary } from "react-error-boundary"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import type { GetDrugBySlugReturnType } from "@/services/server/getDrugs"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import { Skeleton } from "../ui/skeleton"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| type ViewCountProps = { | ||||||||||||||||||||||||||||||||||||||||||||||||
| id: number; | ||||||||||||||||||||||||||||||||||||||||||||||||
| createdAt: GetDrugBySlugReturnType["createdAt"]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,8 +21,11 @@ const ViewCount = async ({ | |||||||||||||||||||||||||||||||||||||||||||||||
| createdAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| updatedAt, | ||||||||||||||||||||||||||||||||||||||||||||||||
| entity, | ||||||||||||||||||||||||||||||||||||||||||||||||
| slug, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }: ViewCountProps) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const newView = await updateViewCount(entity, id).catch((error) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| console.error(error); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return { view_count: 0 }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| <div className='flex items-center gap-2'> | ||||||||||||||||||||||||||||||||||||||||||||||||
| <p className='text-muted-foreground text-sm'> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,53 +34,22 @@ const ViewCount = async ({ | |||||||||||||||||||||||||||||||||||||||||||||||
| <p className='text-muted-foreground text-sm'> | ||||||||||||||||||||||||||||||||||||||||||||||||
| Last updated: {updatedAt?.toISOString().split("T")[0]} | ||||||||||||||||||||||||||||||||||||||||||||||||
| </p> | ||||||||||||||||||||||||||||||||||||||||||||||||
| <ErrorBoundary fallback={<p>Error loading view count</p>}> | ||||||||||||||||||||||||||||||||||||||||||||||||
| <Suspense fallback={<Skeleton className='h-4 w-24' />}> | ||||||||||||||||||||||||||||||||||||||||||||||||
| <Count | ||||||||||||||||||||||||||||||||||||||||||||||||
| id={id} | ||||||||||||||||||||||||||||||||||||||||||||||||
| entity={entity} | ||||||||||||||||||||||||||||||||||||||||||||||||
| slug={slug} | ||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||
| </Suspense> | ||||||||||||||||||||||||||||||||||||||||||||||||
| </ErrorBoundary> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| <Count newView={newView} /> | ||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const Count = ({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| entity, | ||||||||||||||||||||||||||||||||||||||||||||||||
| slug, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }: Pick<ViewCountProps, "id" | "entity" | "slug">) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const newView = use(fetchCount({ entity, id, slug })); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const Count = async ({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| newView, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| newView: { view_count: number | null }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| <p className='text-muted-foreground text-sm'> | ||||||||||||||||||||||||||||||||||||||||||||||||
| View count: {newView.view_count} | ||||||||||||||||||||||||||||||||||||||||||||||||
| View count: {newView.view_count ?? 0} | ||||||||||||||||||||||||||||||||||||||||||||||||
| </p> | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+43
to
53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial
The component receives data via props and performs no async work. Dropping Proposed fix-const Count = async ({
+const Count = ({
newView,
}: {
newView: { view_count: number | null };
}) => {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| export default ViewCount; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| const fetchCount = async ({ | ||||||||||||||||||||||||||||||||||||||||||||||||
| entity, | ||||||||||||||||||||||||||||||||||||||||||||||||
| id, | ||||||||||||||||||||||||||||||||||||||||||||||||
| slug, | ||||||||||||||||||||||||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||||||||||||||||||||||||
| entity: ViewCountProps["entity"]; | ||||||||||||||||||||||||||||||||||||||||||||||||
| id: number; | ||||||||||||||||||||||||||||||||||||||||||||||||
| slug: string; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const baseUrl = process.env.NEXT_PUBLIC_SITE_URL; | ||||||||||||||||||||||||||||||||||||||||||||||||
| const res = await fetch( | ||||||||||||||||||||||||||||||||||||||||||||||||
| `${baseUrl}/api/v1/${entity}/${slug}/${id.toString()}/view`, | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| cache: "no-store", | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!res.ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| const error = await res.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw Error("view count error" + res.status + " " + error.message); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return (await res.json()) as { view_count: null | number }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||
| export default ViewCount; | ||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { eq, sql } from "drizzle-orm"; | ||
|
|
||
| import { db } from "../db"; | ||
| import { agentStatsTable } from "../schemas/agentsSchema"; | ||
| import { companyStatsTable } from "../schemas/companySchema"; | ||
| import { drugStatsTable } from "../schemas/drugsSchema"; | ||
| import { genericStatsTable } from "../schemas/genericSchema"; | ||
|
|
||
| export type EntityType = "agents" | "generics" | "companies" | "drugs"; | ||
|
|
||
| export async function updateViewCount(entity: EntityType, id: number) { | ||
| if (!Number.isFinite(id)) { | ||
| throw new Error(`Invalid id: ${id}`); | ||
| } | ||
| const tables = { | ||
| agents: { table: agentStatsTable, column: agentStatsTable.agent_id }, | ||
| generics: { | ||
| table: genericStatsTable, | ||
| column: genericStatsTable.generic_id, | ||
| }, | ||
| companies: { | ||
| table: companyStatsTable, | ||
| column: companyStatsTable.company_id, | ||
| }, | ||
| drugs: { table: drugStatsTable, column: drugStatsTable.drug_id }, | ||
| }; | ||
|
|
||
| const { table, column } = tables[entity]; | ||
|
|
||
| const result = await db | ||
| .update(table) | ||
| .set({ | ||
| view_count: sql`${table.view_count} + 1`, | ||
| updatedAt: new Date(), | ||
| }) | ||
| .where(eq(column, id)) | ||
| .returning({ view_count: table.view_count }); | ||
|
|
||
| if (!result[0]) { | ||
| throw new Error(`No stats row found for ${entity} with id ${id}`); | ||
| } | ||
| return result[0]; | ||
| } |
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.
🛠️ Refactor suggestion | 🟠 Major
Remove unused imports left over from the previous implementation.
Suspense,use,ErrorBoundary, andSkeletonare no longer referenced after the refactor.Proposed cleanup
📝 Committable suggestion
🤖 Prompt for AI Agents