Skip to content

Feature: Add pagination and filtering for AI referral visits on traffic page - #454

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
chinmaywadhe26:feature/traffic-pagination-and-search
Jul 17, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
chinmaywadhe26:feature/traffic-pagination-and-search

Conversation

@chinmaywadhe26

@chinmaywadhe26 chinmaywadhe26 commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Traffic: Add pagination and filtering to AI Referral Visits table

Problem

The "Recent AI Referral Visits" table on the AI Traffic page is locked to the 10 most recent rows. A "N total" badge advertises how many visits exist, but there is no way to see past the first 10 — and no way to answer questions like "which of my pages get visits from Perplexity?".

Currently:

  • All traffic logs exist in the database
  • UI only displays the first 10, with no pagination
  • No way to filter by platform or search by URL
  • Users cannot explore all their AI traffic data

Fix

Server action (web/src/lib/actions/traffic.ts)

Extended getTrafficLogs to accept filtering options:

  • platform?: string → .eq('source_platform', platform) for exact AI platform filtering
  • search?: string → .ilike('url', ...) with wildcard escaping (% and _) for case-insensitive URL/path search
  • days?: number → .gte('created_at', ...) (future-proof for Traffic: add a 7 / 30 / 90-day time-range picker #451 time-range picker)

The returned total is computed using the same filtered query, so pagination and the badge always reflect the filtered result set.

UI (web/src/app/[locale]/(dashboard)/dashboard/traffic/page.tsx)

Filter bar

  • Platform dropdown populated from summary.platformBreakdown (plus "All platforms")
  • URL search input with a 300 ms debounced server search
  • "Clear filters" button shown only when filters are active

Pagination

  • Added table pagination using the existing TablePager pattern
  • Displays current row range (1–10 of 60)
  • Previous/Next controls with proper disabled states

State management

  • filters state stores the active platform and search filters
  • searchInput state keeps the search input responsive while debouncing server requests
  • Search debouncing is handled in the parent component before updating filters.search
  • page automatically resets to the first page whenever filters change
  • Separate loading states:
  • isLoadingSummary for KPIs/charts
  • isLoadingLogs for the visit log table

Empty states

  • Filtered with no results → "No matching visits"
  • No filters and no data → "No visits yet"
  • Loading spinner displayed while visit logs are being fetched

Implementation Details

  • Debounce is implemented with a 300 ms timeout in the parent component
  • User search strings escape % and _ before building the Supabase ILIKE query
  • All filters are composed into a single Supabase query, so both results and counts stay consistent
  • Pagination resets whenever filters change to prevent invalid page states
  • Platform options are generated dynamically from the summary data
  • The"AI referral visits" table data changes independently from the dashboard summary. Separating loadSummary() and loadLogs() prevents unnecessary re-fetching of KPIs and charts when users paginate or filter the visit log, reducing unnecessary requests

After this fix

  • All visits are reachable through pagination (10 rows per page)
  • Platform filter and URL search execute server-side
  • Platform and search filters can be combined
  • Changing filters resets pagination to the first page
  • Badge reflects the filtered total
  • Clearing filters restores the complete dataset
  • Friendly empty state for filtered results
  • KPIs/charts load once; only the visit log table reloads during filtering or pagination
  • Search input is debounced (~300 ms) to reduce unnecessary requests

Testing

Test data: traffic logs seeded across multiple AI platforms and URLs.

cases tested manually:

  • Filter by Perplexity → verify only Perplexity rows appear
  • Search for /docs → verify only matching URLs appear
  • Combine platform + search filters
  • Navigate to page 2, then change platform → verify pagination resets to page 1
  • Clear filters → verify all rows become available again
  • Verify the total badge reflects the filtered count
Screenshot 2026-07-17 105511 Screenshot 2026-07-17 105526

Related issue

closes #452

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

Checklist

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

@chinmaywadhe26 chinmaywadhe26 changed the title Feature: add pagination and filtering for AI referral visits on traffic page Feature: Add pagination and filtering for AI referral visits on traffic page Jul 17, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice structure overall — server-side filtered count, the identity-preserving debounce (prevFilters.search === searchInput ? prevFilters : ...), separate summary/log loading and the two empty states are all exactly right. Three behavioral issues before this can land:

1. Changing a filter while past page 1 double-fetches, and the stale response can win.
When filters change, two effects fire in the same commit: the page-reset effect (setPage(0)) and the loadLogs effect — which runs immediately with the new filters but the old page. The page update then triggers a second fetch with page 0. There's no sequencing guard, so whichever response lands last wins; if the stale one (old offset, likely empty) resolves last, the table shows the wrong result. Simplest fix: drop the page-reset effect and call setPage(0) in the same event handlers that change filters (handleFiltersChange and the debounce timeout when the search actually changes) — React batches the two updates, so loadLogs re-runs exactly once with the right (filters, page) pair. A requestId ref that ignores out-of-date responses would also work.

2. disabled={isLoading} on the search input drops focus mid-typing.
Type a few characters, pause 300ms → the debounce fires, the fetch starts, the input disables → the browser blurs the field and further keystrokes go nowhere. Please don't disable the search input (the Select and the pager's disabled states are enough — and arguably the Select doesn't need it either).

3. Page and filters survive a brand switch.
loadLogs correctly depends on brand, but switching brands keeps the old page (e.g. offset 50 into a brand with 12 visits → empty table) and a platform filter that may not exist for the new brand. Reset page, filters and searchInput when the brand changes.

Optional nit: the ILIKE escaping handles %/_ but not \ itself — a search containing a backslash changes the wildcard semantics of the character after it. Escaping \ first (.replace(/\\/g, '\\\\')) makes the input fully literal.

@chinmaywadhe26

Copy link
Copy Markdown
Contributor Author

Nice structure overall — server-side filtered count, the identity-preserving debounce (prevFilters.search === searchInput ? prevFilters : ...), separate summary/log loading and the two empty states are all exactly right. Three behavioral issues before this can land:

1. Changing a filter while past page 1 double-fetches, and the stale response can win. When filters change, two effects fire in the same commit: the page-reset effect (setPage(0)) and the loadLogs effect — which runs immediately with the new filters but the old page. The page update then triggers a second fetch with page 0. There's no sequencing guard, so whichever response lands last wins; if the stale one (old offset, likely empty) resolves last, the table shows the wrong result. Simplest fix: drop the page-reset effect and call setPage(0) in the same event handlers that change filters (handleFiltersChange and the debounce timeout when the search actually changes) — React batches the two updates, so loadLogs re-runs exactly once with the right (filters, page) pair. A requestId ref that ignores out-of-date responses would also work.

2. disabled={isLoading} on the search input drops focus mid-typing. Type a few characters, pause 300ms → the debounce fires, the fetch starts, the input disables → the browser blurs the field and further keystrokes go nowhere. Please don't disable the search input (the Select and the pager's disabled states are enough — and arguably the Select doesn't need it either).

3. Page and filters survive a brand switch. loadLogs correctly depends on brand, but switching brands keeps the old page (e.g. offset 50 into a brand with 12 visits → empty table) and a platform filter that may not exist for the new brand. Reset page, filters and searchInput when the brand changes.

Optional nit: the ILIKE escaping handles %/_ but not \ itself — a search containing a backslash changes the wildcard semantics of the character after it. Escaping \ first (.replace(/\\/g, '\\\\')) makes the input fully literal.

Thanks for the review.

Addressed the feedback:

  • Reset page together with filter updates to avoid double fetches.
  • Removed disabled state from the search input (and platform select).
  • Reset page and filters when switching brands.
  • Escaped , %, and _ in URL search.

@chinmaywadhe26
chinmaywadhe26 requested a review from gkhngyk July 17, 2026 10:24

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All four points verified in the new commit:

  1. ✅ Page reset now happens with the filter updates — setPage(0) inside handleFiltersChange, and in the debounce only when the search actually changed. Batched, single fetch. (Calling setPage inside the setFilters updater is technically an impure updater — works fine and StrictMode's double-invoke is harmless here since the value is constant, just noting it.)
  2. ✅ Search input and platform select no longer disable during loads; only the Clear button and pager do.
  3. ✅ Brand switch resets page/filters/searchInput.
  4. ✅ Backslash escaped before %/_ — correct order.

One residual micro-race, not blocking: on a brand switch, the loadLogs effect (brand dep changed) fires before the reset effect's state lands, so one fetch goes out with the new brand but the old page/filters, then the reset triggers the correct one — if the first resolves last, the table briefly shows the wrong slice until any interaction. A ~5-line requestId ref in loadLogs that ignores out-of-date responses would close this (and every future ordering race) permanently — fine as a follow-up, not required for this PR.

LGTM, thanks for the quick turnaround!

@gkhngyk
gkhngyk merged commit 916e4ae into ansvisor:main Jul 17, 2026
3 checks passed
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.

Traffic: paginate the visit log and add platform filter + URL search

2 participants