Skip to content

perf(topics): collapse 6 serialized topic-detail server actions into one - #338

Merged
gkhngyk merged 2 commits into
mainfrom
perf/topic-detail-consolidate-actions
Jul 4, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
perf/topic-detail-consolidate-actions

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

The topic detail page (/dashboard/topics/[id]) felt slow because it fired six server-action calls from the client inside one Promise.all. Next.js runs server actions sequentially (each is its own queued POST), so the wait was ~the sum of all six, not the slowest — the dominant cost (#312).

This collapses them into one server action whose body runs the reads in a real server-side Promise.all (genuinely parallel, one round trip).

Changes

  • actions/tracking.ts — add getTopicDetail(brandId, topicId): one action that runs the six reads in a server-side Promise.all and returns a single bundle { topic, summary, trend, sov, competitors, results } (new TopicDetailData type).
  • actions/topic.ts — add getTopicById(brandId, topicId); the page now fetches only the one topic's row instead of getTopics() (whole list) just to read one name.
  • topics/[id]/page.tsx — calls the single action and unpacks the bundle; the six imports drop to one.

Output is identical to before — this is a performance refactor, not a behaviour change.

Related issue

Closes #312 (the core fix). The stretch items in the issue — sharing the single prompt_results scan across summary/competitors/prompt rollup, per-section progressive render, and reading from the *_aggregates tables / short-TTL cache — are left as optional follow-ups.

Type of change

  • refactor — Code change that neither fixes a bug nor adds a feature (performance)

How to test

  1. Open a topic from /dashboard/topics; the detail page renders meaningfully faster (one round trip instead of six serialized ones).
  2. Every section (KPI strip, visibility trend, SoV by platform, competitor table, top/weak prompts) shows the same numbers as before.
  3. A topic with no data still shows the correct empty states; an unknown/foreign topic id renders "Topic not found".
  4. yarn typecheck, yarn lint, yarn format:check, yarn test all pass from web/.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (from web/)
  • yarn typecheck passes (from web/)
  • yarn format:check passes (from web/)
  • Changes are focused — one concern per PR

…nto one

The topic detail page fired six server-action calls from the client in a
Promise.all. Next.js runs server actions sequentially (each is its own
queued POST), so the wait was ~the sum of all six, not the slowest — the
dominant cost of the slow-feeling page (#312).

- Add getTopicDetail(brandId, topicId) in actions/tracking.ts: one server
  action whose body runs the six reads in a real server-side Promise.all
  (genuinely parallel, one round trip) and returns a single bundle
  { topic, summary, trend, sov, competitors, results }.
- Add getTopicById(brandId, topicId) in actions/topic.ts and use it instead
  of getTopics() — fetch only the one topic's row rather than the whole
  topics list just to read one name.
- Page now calls the single action and unpacks the bundle.

Output is identical to before — this is a performance refactor, not a
behaviour change. Closes #312 (core fix; the stretch items — shared
prompt_results scan, progressive render, aggregate caching — remain
optional follow-ups).

Copilot AI 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.

Pull request overview

This PR improves perceived performance on the topic detail page (/dashboard/topics/[id]) by reducing client-initiated server action round trips from six serialized calls to a single consolidated server action that runs the underlying reads in a server-side Promise.all.

Changes:

  • Added getTopicDetail(brandId, topicId) in tracking.ts to fetch all topic-detail data in one server action call.
  • Added getTopicById(brandId, topicId) in topic.ts to avoid fetching the full topics list just to resolve a single topic name.
  • Updated the topic detail page to call getTopicDetail and unpack the returned bundle.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
web/src/lib/actions/tracking.ts Adds a consolidated getTopicDetail server action and bundles the six reads into one call.
web/src/lib/actions/topic.ts Adds getTopicById helper to fetch only the requested topic row.
web/src/app/[locale]/(dashboard)/dashboard/topics/[id]/page.tsx Switches client fetching logic from six actions to the single bundled action.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread web/src/lib/actions/topic.ts Resolved
Comment thread web/src/lib/actions/tracking.ts Resolved
…review)

- getTopicById now filters is_active = true, matching getTopics() which the
  page used before via .find() over the active list. Without it an inactive
  topic (previously "not found") would render — a behavior change. Also
  narrows the select to the columns mapTopicRow reads.
- Switch the cross-action import to the `@/lib/actions/topic` alias to match
  the convention used elsewhere (e.g. prompt-suggestions.ts).

Addresses the Copilot review on #338.
@gkhngyk
gkhngyk merged commit a9387d2 into main Jul 4, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the perf/topic-detail-consolidate-actions branch July 4, 2026 15:48
@gkhngyk gkhngyk mentioned this pull request Jul 6, 2026
5 tasks done
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.

perf(topics): speed up the topic detail page — collapse 6 serialized server actions into one

2 participants