Skip to content

fix: eeror#83

Open
Stbs0 wants to merge 4 commits into
mainfrom
hotfix/web/view-count-error
Open

fix: eeror#83
Stbs0 wants to merge 4 commits into
mainfrom
hotfix/web/view-count-error

Conversation

@Stbs0

@Stbs0 Stbs0 commented Feb 10, 2026

Copy link
Copy Markdown
Owner

@coderabbitai

[Web] Fix View Count Error

Target: Next.js Web

  • Behind-the-scenes fix that unifies how the site records views for drugs, companies, agents, and generics.
  • Makes view tracking more reliable so counts are updated consistently across the site.
  • Simplifies how view updates are handled, reducing chances of future errors.
  • Clearer internal error reporting to help diagnose any remaining issues faster.
  • No visible changes to the user interface; browsing and counts should feel smoother and more accurate.

@vercel

vercel Bot commented Feb 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
sudan-codex-web Ready Ready Preview, Comment Feb 11, 2026 0:50am

@coderabbitai

coderabbitai Bot commented Feb 10, 2026

Copy link
Copy Markdown

Walkthrough

Centralizes view-count updates by introducing updateViewCount(entity, id) in the DB package and replacing inline drizzle updates across four API routes and the view-count component; routes and component now call the shared utility and error messages were adjusted accordingly.

Changes

Cohort / File(s) Summary
API Route Endpoints
apps/web-nextjs/src/app/api/v1/agents/.../view/route.ts, apps/web-nextjs/src/app/api/v1/companies/.../view/route.ts, apps/web-nextjs/src/app/api/v1/drugs/.../view/route.ts, apps/web-nextjs/src/app/api/v1/generics/.../view/route.ts
Replaced inline drizzle/db update logic with updateViewCount(entity, id) calls; removed direct table/db imports; updated logs and 500 responses to reference view-count updates.
View Count Component
apps/web-nextjs/src/components/drugInfo/view-count.tsx
Removed slug-based fetch and suspense/error-boundary flow; imports updateViewCount and directly awaits it; component props/signatures simplified (slug removed); fallback handling preserved.
DB Utility Module
packages/db/src/queries/view-count.ts
New module exporting EntityType and updateViewCount(entity, id) that maps entity→table/ID column, validates input, increments view_count, sets updatedAt, and returns updated row.
DB Index Export
packages/db/src/index.ts
Added export * from "./queries/view-count" to re-export the new utility.

Sequence Diagram(s)

sequenceDiagram
    participant View as ViewCount Component
    participant API as API Route
    participant DBMod as DB Module
    participant DB as Database

    View->>DBMod: updateViewCount(entity, id)
    DBMod->>DB: UPDATE <entity_stats> SET view_count = view_count + 1, updatedAt = now() RETURNING *
    DB-->>DBMod: updated row
    DBMod-->>View: { view_count }

    API->>DBMod: updateViewCount(entity, id)
    DBMod->>DB: UPDATE <entity_stats> ...
    DB-->>DBMod: updated row
    DBMod-->>API: { view_count }
    API-->>Client: JSON { result }
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • fix: eeror #84: Modifies similar view-count routes and component behavior (overlaps on view-count updates and error-handling changes).
🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'fix: eeror' is vague and does not clearly describe the actual changes in the pull request, which refactor view count logic across multiple API routes and components. Revise the title to clearly describe the main change, such as 'fix: refactor view count updates to use centralized updateViewCount function' or 'fix: consolidate view count logic into reusable utility function'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hotfix/web/view-count-error

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@alwaysmeticulous

alwaysmeticulous Bot commented Feb 10, 2026

Copy link
Copy Markdown

🤖 Meticulous evaluated 50 user flows and took 72 visual snapshots. Meticulous has not yet run on 5c8c5e2 of the main branch and so there was nothing to compare against.

If you recently setup Meticulous, this is expected. Meticulous will start reporting comparisons for new pull requests after the next commit to the main branch.

If you are using Vercel, please make sure you have disabled automatic job cancellation as documented here.

Last updated for commit bdf73e8. This comment will update as new commits are pushed.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/web-nextjs/src/components/drugInfo/view-count.tsx (1)

10-16: 🛠️ Refactor suggestion | 🟠 Major

slug is declared in ViewCountProps but never used.

Neither ViewCount nor Count uses slug. Remove it from the type to avoid confusion.

Proposed fix
 type ViewCountProps = {
   id: number;
   createdAt: GetDrugBySlugReturnType["createdAt"];
   updatedAt: GetDrugBySlugReturnType["updatedAt"];
   entity: "agents" | "generics" | "companies" | "drugs";
-  slug: string;
 };
🤖 Fix all issues with AI agents
In `@apps/web-nextjs/src/components/drugInfo/view-count.tsx`:
- Around line 42-49: The Count component is performing a DB mutation during
render by calling use(updateViewCount(entity, id)), which causes
double/inconsistent increments; move the updateViewCount call out of the render
path—either invoke the mutation once in the parent async server component
ViewCount (perform the update there and pass the resulting view count into
Count) or keep the previous API-route boundary and fetch the updated count from
that API inside the component; locate the use(updateViewCount(...)) call in
Count and replace it with a plain prop or a single server-side call in ViewCount
so the DB write happens exactly once.

In `@packages/db/src/queries/view-count.ts`:
- Around line 11-36: In updateViewCount, validate the incoming id (e.g., const
numericId = Number(id); if (!Number.isFinite(numericId) ||
Number.isNaN(numericId)) throw new Error(...)) before using it, then perform the
db.update as currently done (db.update(table)...where(eq(column,
numericId)).returning(...)), and after the update check the returned result
length — if result[0] is undefined/empty either create the stats row (insert
into the same table with view_count: 1 and the appropriate key column from the
tables map) or throw an explicit error/return a distinguishable value to the
caller so "no row" is not silently treated as view_count: 0; use the existing
tables map, column variable, and the db.update(...).returning call to locate
where to add the checks and the insert/upsert fallback.

Comment thread apps/web-nextjs/src/components/drugInfo/view-count.tsx Outdated
Comment thread packages/db/src/queries/view-count.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/web-nextjs/src/components/drugInfo/view-count.tsx (1)

11-17: 🛠️ Refactor suggestion | 🟠 Major

slug is still declared in ViewCountProps but never used.

The prop was part of the old fetch-based approach and is now dead code. Remove it from the type (and from call sites).

Proposed fix
 type ViewCountProps = {
   id: number;
   createdAt: GetDrugBySlugReturnType["createdAt"];
   updatedAt: GetDrugBySlugReturnType["updatedAt"];
   entity: "agents" | "generics" | "companies" | "drugs";
-  slug: string;
 };
🤖 Fix all issues with AI agents
In `@apps/web-nextjs/src/components/drugInfo/view-count.tsx`:
- Around line 43-53: The Count component is declared async but performs no
asynchronous work; remove the async keyword from the Count function declaration
(the Count component that takes newView: { view_count: number | null }) so it
returns JSX synchronously and avoids creating an unnecessary Promise/microtask
wrapper; update the Count declaration accordingly and leave the JSX return (View
count: {newView.view_count ?? 0}) unchanged.
- Around line 4-9: Remove the now-unused imports left from the prior
implementation: delete the named imports Suspense and use from "react",
ErrorBoundary from "react-error-boundary", and Skeleton from "../ui/skeleton" so
only actually used symbols remain in the import list; update the import
statements that reference these identifiers (e.g., the top-level imports
containing Suspense/use and ErrorBoundary/Skeleton) to only import what the
current component uses, and run a quick lint/TS check to ensure no unused-import
errors remain.

Comment on lines 4 to 9
import { Suspense, use } from "react";
import { ErrorBoundary } from "react-error-boundary";

import type { GetDrugBySlugReturnType } from "@/services/server/getDrugs";

import { Skeleton } from "../ui/skeleton";

Copy link
Copy Markdown

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, and Skeleton are no longer referenced after the refactor.

Proposed cleanup
-import { Suspense, use } from "react";
-import { ErrorBoundary } from "react-error-boundary";
 
 import type { GetDrugBySlugReturnType } from "@/services/server/getDrugs";
 
-import { Skeleton } from "../ui/skeleton";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { Suspense, use } from "react";
import { ErrorBoundary } from "react-error-boundary";
import type { GetDrugBySlugReturnType } from "@/services/server/getDrugs";
import { Skeleton } from "../ui/skeleton";
import type { GetDrugBySlugReturnType } from "@/services/server/getDrugs";
🤖 Prompt for AI Agents
In `@apps/web-nextjs/src/components/drugInfo/view-count.tsx` around lines 4 - 9,
Remove the now-unused imports left from the prior implementation: delete the
named imports Suspense and use from "react", ErrorBoundary from
"react-error-boundary", and Skeleton from "../ui/skeleton" so only actually used
symbols remain in the import list; update the import statements that reference
these identifiers (e.g., the top-level imports containing Suspense/use and
ErrorBoundary/Skeleton) to only import what the current component uses, and run
a quick lint/TS check to ensure no unused-import errors remain.

Comment on lines +43 to 53
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>
);
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Count doesn't need to be async.

The component receives data via props and performs no async work. Dropping async avoids an unnecessary microtask/promise wrapper.

Proposed fix
-const Count = async ({
+const Count = ({
   newView,
 }: {
   newView: { view_count: number | null };
 }) => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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>
);
};
const Count = ({
newView,
}: {
newView: { view_count: number | null };
}) => {
return (
<p className='text-muted-foreground text-sm'>
View count: {newView.view_count ?? 0}
</p>
);
};
🤖 Prompt for AI Agents
In `@apps/web-nextjs/src/components/drugInfo/view-count.tsx` around lines 43 - 53,
The Count component is declared async but performs no asynchronous work; remove
the async keyword from the Count function declaration (the Count component that
takes newView: { view_count: number | null }) so it returns JSX synchronously
and avoids creating an unnecessary Promise/microtask wrapper; update the Count
declaration accordingly and leave the JSX return (View count:
{newView.view_count ?? 0}) unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant