Repository navigation
Feature: Add pagination and filtering for AI referral visits on traffic page - #454
Conversation
gkhngyk
left a comment
There was a problem hiding this comment.
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:
|
gkhngyk
left a comment
There was a problem hiding this comment.
All four points verified in the new commit:
- ✅ Page reset now happens with the filter updates —
setPage(0)insidehandleFiltersChange, and in the debounce only when the search actually changed. Batched, single fetch. (CallingsetPageinside thesetFiltersupdater is technically an impure updater — works fine and StrictMode's double-invoke is harmless here since the value is constant, just noting it.) - ✅ Search input and platform select no longer disable during loads; only the Clear button and pager do.
- ✅ Brand switch resets page/filters/searchInput.
- ✅ 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!
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:
Fix
Server action (
web/src/lib/actions/traffic.ts)Extended
getTrafficLogsto accept filtering options:platform?: string→.eq('source_platform', platform)for exact AI platform filteringsearch?: string→.ilike('url', ...)with wildcard escaping (%and_) for case-insensitive URL/path searchdays?: number→.gte('created_at', ...)(future-proof for Traffic: add a 7 / 30 / 90-day time-range picker #451 time-range picker)The returned
totalis 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
summary.platformBreakdown(plus "All platforms")Pagination
TablePagerpattern1–10 of 60)State management
filtersstate stores the active platform and search filterssearchInputstate keeps the search input responsive while debouncing server requestsfilters.searchpageautomatically resets to the first page whenever filters changeisLoadingSummaryfor KPIs/chartsisLoadingLogsfor the visit log tableEmpty states
Implementation Details
%and_before building the SupabaseILIKEqueryAfter this fix
Testing
Test data: traffic logs seeded across multiple AI platforms and URLs.
cases tested manually:
/docs→ verify only matching URLs appearRelated issue
closes #452
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsChecklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)