← Back to Pull requests

feat(web): display cited Expert Review sources on Product Detail and API (#2031) #2032

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 29, 2026 04:55 UTC
Author

Summary


Implements Phase 1B of the Expert Review architecture (#2019), connecting rate.vn Product Detail and GET /api/v1/rate/products/[slug] to rate.expert_sources and rate.expert_citations (DOS/DOS.Me#2050, PR #2051):
  • apps/web/src/lib/expert-citations.ts: Pure types, strict http:/https: URL sanitizer (sanitizeCitationUrl), locale-aware raw score formatter (formatCitationRawScore), row normalizer/deduplicator (normalizeExpertCitationRows), and explicit public column projection (EXPERT_CITATION_PUBLIC_SELECT omitting internal verified_by/metadata to match column-scoped anon grants).

  • apps/web/src/lib/rate-api.ts & apps/web/src/server/rate/index.ts: Cached server reader getCachedMasterProductExpertCitations with graceful MISSING_SCHEMA_CODES (42P01, 42703, PGRST200, PGRST204, PGRST205) fallback returning [] before migration rollout, while propagating genuine DB outages.

  • apps/web/src/components/reviews/JReviewsSection.tsx & apps/web/src/app/[locale]/smartphones/[slug]/page.tsx: Renders cited source count <Badge>, publication name, raw score, normalized /10 score, publication date, and direct rel="noopener noreferrer" links inside the Điểm chuyên gia card, preserving #2009's concise empty state and zero "Rate Lab" branding.

  • apps/web/src/app/api/v1/rate/products/[slug]/route.ts & apps/web/src/server/rate/openapi.ts: Exposes expert_score_10 and expert_citations cleanly.

  • apps/web/locales/{vi,en,cs}.json: Adds expertSourcesCountBadge, expertSourceLinkLabel, expertSourceRawLabel with full placeholder parity and zero em/en-dashes.

  • Tests: New apps/web/src/lib/__tests__/expert-citations.test.ts + extended review-display.test.ts and spec-scope.test.ts.
  • Verification (Lightweight Local Checks; Cloud CI runs E2E)


  • pnpm --filter web exec tsc --noEmit: passed

  • pnpm --filter web test: passed

  • pnpm --filter web lint (--max-warnings 0): passed

  • pnpm --filter web build: passed (35/35 static pages)

  • Dash hygiene check (0 em-dash/en-dash): passed
  • Closes #2031
    Refs #2019
    Claimant: antigravity:6882bcaf-c72a-491c-9d10-b07094104c2c

    ---

    <!-- dos-pr-summary -->

    📌 TL;DR


    This PR introduces a new "Expert Citations" feature that displays external expert review sources (e.g., RTINGS, Notebookcheck) on product detail pages and the public API. It includes robust data sanitization (URL validation, schema-migration fallbacks), localized formatting for raw/normalized scores, and comprehensive unit/integration tests.

    🎯 Type of Change


  • 🚀 New feature

  • 🐛 Bugfix

  • 🧹 Refactor

  • ⚡ Performance

  • 📚 Documentation

  • ⚙️ CI / Configuration
  • 🔍 Changes Walkthrough


    | File | Summary of Changes |
    | :--- | :--- |
    | apps/web/locales/{cs,en,vi}.json | Added i18n keys for source count badges, outbound link labels, and raw score formatting. |
    | apps/web/package.json | Registered the new expert-citations.test.ts suite in the test script. |
    | apps/web/src/lib/expert-citations.ts | New Core Library: Defines types, explicit SQL column projections, URL sanitization (blocking javascript:, data:, etc.), score formatting utilities, and row normalization logic (deduplication, filtering inactive/draft sources). |
    | apps/web/src/lib/__tests__/expert-citations.test.ts | New Test Suite: Validates URL sanitization, score formatting across locales, row normalization, and integration with the PostgREST API (including schema-migration error handling). |
    | apps/web/src/lib/__tests__/review-display.test.ts | Extended existing tests to verify the new citation UI renders correctly, handles unsafe URLs, and preserves empty states. |
    | apps/web/src/app/api/v1/rate/products/[slug]/route.ts | Updated the public API endpoint to fetch expert citations and expose expert_score_10 and expert_citations in the response payload. |
    | apps/web/src/app/api/v1/rate/__tests__/spec-scope.test.ts | Updated API contract tests to assert the presence of the new expert_citations and expert_score_10 fields. |
    | apps/web/src/app/[locale]/smartphones/[slug]/page.tsx | Updated the product detail page to fetch expert citations (with timing instrumentation) and pass them to the reviews component. |
    | apps/web/src/components/reviews/JReviewsSection.tsx | Updated the reviews UI to render a list of expert citations (max 5), including source name, raw score, normalized score, publication date, and a safe external link. Added a source count badge. |

    📊 Architectural Flow


    sequenceDiagram
        participant Client as Web Client
        participant Page as Product Detail Page
        participant API as Public API Route
        participant Lib as Expert Citations Lib
        participant DB as PostgREST / Supabase
    
        Note over Client, DB: Product Detail Page Render
        Client->>Page: Request /smartphones/[slug]
        Page->>Lib: getCachedMasterProductExpertCitations()
        Lib->>DB: SELECT (explicit columns) FROM expert_citations
        DB-->>Lib: Raw Citation Rows
        Lib->>Lib: Sanitize URLs & Normalize Rows
        Lib-->>Page: Clean ExpertCitationItems
        Page->>Client: Render JReviewsSection with citations
    
        Note over Client, DB: Public API Request
        Client->>API: GET /api/v1/rate/products/[slug]
        API->>Lib: getCachedMasterProductExpertCitations()
        Lib->>DB: SELECT (explicit columns) FROM expert_citations
        DB-->>Lib: Raw Citation Rows
        Lib->>Lib: Sanitize & Normalize
        Lib-->>API: Clean ExpertCitationItems
        API->>API: Map to ApiExpertCitation (snake_case)
        API-->>Client: JSON Response { expert_citations, expert_score_10 }
    

    <!-- /dos-pr-summary -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: claude[bot] Review duration: 4m
    All Vouch review confidence checks passed
    Residual risk 0.43 = Change Risk (0.52) × (1 − Review Confidence (0.17)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 29% · Reviewer familiarity: 35%
    16%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2032"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 16.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 16.6% · Lower = rushed or shallow review
    16.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 43.4% · Moderate risk
    43.4%
    Repository