Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(insights,citations): keep filters in the URL (#985)

Gökhan Geyik69c4e5ffa2ee
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx
index 89d2b9b9c..b106db667 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx
@@ -23,6 +23,7 @@ import {
RotateCw,
} from 'lucide-react';
import { toast } from 'sonner';
+import { readUrlChoice, readUrlParam, writeUrlParams } from '@/lib/url-state';
import { useBrandStore } from '@/stores/use-brand-store';
import { Link } from '@/i18n/navigation';
import { AddCompetitorButton } from '@/components/citations/add-competitor-button';
@@ -30,6 +31,7 @@ import {
CitationsFilterBar,
SourceScopeFilter,
DEFAULT_CITATIONS_FILTERS as DEFAULT_FILTERS,
+ DATE_PRESETS,
buildPlatformOptions,
buildCitationDetailHref,
getDateRange,
@@ -802,6 +804,45 @@ function triggerDownload(csv: string, filename: string) {
URL.revokeObjectURL(url);
}
+const SOURCE_SCOPE_VALUES = ['own', 'competitors', 'third_party', 'all'] as const;
+
+/**
+ * The filters as kept in the URL (#917). The names match the ones the per-URL
+ * detail page reads (buildCitationDetailHref): ?preset=, ?from=/&to= for a
+ * custom range, ?platform=, ?region=, plus ?topic=, ?prompt= and ?scope=.
+ * Defaults are left out; an unknown preset or scope falls back to the default.
+ */
+function filtersFromUrl(): UIFilters {
+ const datePreset = readUrlChoice('preset', DATE_PRESETS, DEFAULT_FILTERS.datePreset);
+ return {
+ datePreset,
+ dateFrom: datePreset === 'custom' ? readUrlParam('from') : '',
+ dateTo: datePreset === 'custom' ? readUrlParam('to') : '',
+ platform: readUrlParam('platform'),
+ region: readUrlParam('region'),
+ topic: readUrlParam('topic'),
+ prompt: readUrlParam('prompt'),
+ sourceScope: readUrlChoice('scope', SOURCE_SCOPE_VALUES, DEFAULT_FILTERS.sourceScope),
+ };
+}
+
+function writeFiltersToUrl(f: UIFilters) {
+ const custom = f.datePreset === 'custom';
+ writeUrlParams(
+ {
+ preset: f.datePreset,
+ from: custom ? f.dateFrom : null,
+ to: custom ? f.dateTo : null,
+ platform: f.platform,
+ region: f.region,
+ topic: f.topic,
+ prompt: f.prompt,
+ scope: f.sourceScope,
+ },
+ { preset: DEFAULT_FILTERS.datePreset, scope: DEFAULT_FILTERS.sourceScope },
+ );
+}
+
export default function CitationsPage() {
const t = useTranslations('citations');
const tCommon = useTranslations('common');
@@ -809,6 +850,17 @@ export default function CitationsPage() {
const brand = getActiveBrand();
const [filters, setFilters] = useState<UIFilters>(DEFAULT_FILTERS);
+ // Read after mount (window isn't available during the server render); the
+ // loads below wait for it so a linked view doesn't load the defaults first.
+ const [urlRead, setUrlRead] = useState(false);
+ useEffect(() => {
+ setFilters(filtersFromUrl());
+ setUrlRead(true);
+ }, []);
+
+ useEffect(() => {
+ if (urlRead) writeFiltersToUrl(filters);
+ }, [urlRead, filters]);
const [data, setData] = useState<CitationsOverview | null>(null);
const [hasAnyCitations, setHasAnyCitations] = useState<boolean | null>(null);
const [loadFailed, setLoadFailed] = useState(false);
@@ -987,6 +1039,7 @@ export default function CitationsPage() {
}, [activeBrandId]);
useEffect(() => {
+ if (!urlRead) return;
loadData();
// Invalidate whatever is in flight when the filters change or the page
// unmounts, so a slow earlier response can't overwrite newer data.
@@ -999,13 +1052,13 @@ export default function CitationsPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps
loadGen.current++;
};
- }, [loadData]);
+ }, [loadData, urlRead]);
// Lazy-load Competitor Gaps only when its tab is active; re-fetch when the
// shared scoping filters change while it's open. Uses `gapFilters` (not
// `apiFilters`) so the domain-list flags don't trigger a no-op refetch.
useEffect(() => {
- if (sourceTab !== 'gaps' || !activeBrandId) return;
+ if (sourceTab !== 'gaps' || !activeBrandId || !urlRead) return;
let cancelled = false;
setGapsLoading(true);
getCitationGaps(activeBrandId, gapFilters)
@@ -1021,7 +1074,7 @@ export default function CitationsPage() {
return () => {
cancelled = true;
};
- }, [sourceTab, activeBrandId, gapFilters]);
+ }, [sourceTab, activeBrandId, gapFilters, urlRead]);
const totals = data?.totals;
const kpis = useMemo(
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
index 86278a72c..566456ea9 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx
@@ -111,6 +111,7 @@ import { getAIProviderDisplayName, resolveAIProvider } from '@/components/ai-pro
import { usePlanContext } from '@/components/providers/plan-provider';
import { formatCompactNumber } from '@/lib/format';
import { toast } from 'sonner';
+import { readUrlChoice, readUrlParam, writeUrlParams } from '@/lib/url-state';
// ─── Filter Types ─────────────────────────────────────────────────────────────
@@ -136,6 +137,42 @@ const DEFAULT_FILTERS: InsightsFilters = {
topic: '',
};
+/**
+ * The filters as kept in the URL (#917): ?range=, plus ?from=/&to= for a
+ * custom range, and ?region=, ?model=, ?topic=. Defaults are left out, and an
+ * unknown range falls back to the default.
+ */
+function filtersFromUrl(): InsightsFilters {
+ const datePreset = readUrlChoice<DatePreset>(
+ 'range',
+ ALL_DATE_PRESETS as readonly DatePreset[],
+ DEFAULT_FILTERS.datePreset,
+ );
+ return {
+ datePreset,
+ dateFrom: datePreset === 'custom' ? readUrlParam('from') : '',
+ dateTo: datePreset === 'custom' ? readUrlParam('to') : '',
+ region: readUrlParam('region'),
+ model: readUrlParam('model'),
+ topic: readUrlParam('topic'),
+ };
+}
+
+function writeFiltersToUrl(f: InsightsFilters) {
+ const custom = f.datePreset === 'custom';
+ writeUrlParams(
+ {
+ range: f.datePreset,
+ from: custom ? f.dateFrom : null,
+ to: custom ? f.dateTo : null,
+ region: f.region,
+ model: f.model,
+ topic: f.topic,
+ },
+ { range: DEFAULT_FILTERS.datePreset },
+ );
+}
+
const INSIGHT_EXPORT_HEADERS = [
'created_at',
'prompt',
@@ -988,6 +1025,20 @@ export default function InsightsPage() {
const filtersRef = useRef(filters);
filtersRef.current = filters;
+ // The filters are read from the URL after mount (window isn't available
+ // during the server render), and the first load waits for them.
+ const [urlRead, setUrlRead] = useState(false);
+ useEffect(() => {
+ const fromUrl = filtersFromUrl();
+ filtersRef.current = fromUrl;
+ setFilters(fromUrl);
+ setUrlRead(true);
+ }, []);
+
+ useEffect(() => {
+ if (urlRead) writeFiltersToUrl(filters);
+ }, [urlRead, filters]);
+
// Keyed on the id, not the brand object: the store hands out a new object
// for the same brand, and a new loadData re-runs the load effect below.
const brandId = brand?.id;
@@ -1099,22 +1150,28 @@ export default function InsightsPage() {
[brandId],
);
+ // A brand switch clears the brand-specific filters. Not on the first brand,
+ // so filters linked in the URL survive the initial load.
+ const filteredBrandRef = useRef<string | undefined>(undefined);
useEffect(() => {
if (!brand?.id) return;
setAvailableRegions([]);
setAvailableModels([]);
setAvailableTopics([]);
- const next = { ...filtersRef.current, region: '', model: '', topic: '' };
- filtersRef.current = next;
- setFilters(next);
+ if (filteredBrandRef.current && filteredBrandRef.current !== brand.id) {
+ const next = { ...filtersRef.current, region: '', model: '', topic: '' };
+ filtersRef.current = next;
+ setFilters(next);
+ }
+ filteredBrandRef.current = brand.id;
getTopics(brand.id)
.then(setAvailableTopics)
.catch(() => {});
}, [brand?.id]);
useEffect(() => {
- loadData();
- }, [loadData]);
+ if (urlRead) loadData();
+ }, [loadData, urlRead]);
// Watch the tracking-run ledger so server-started (cron) runs surface too:
// the job banner below only knows about jobs this browser started via
@@ -1154,7 +1211,7 @@ export default function InsightsPage() {
saveTrackingJob({ jobId: urlJobId, brandId: brand.id, startedAt: Date.now() });
setActiveJobId(urlJobId);
setIsRunning(true);
- window.history.replaceState({}, '', window.location.pathname);
+ writeUrlParams({ jobId: null });
return;
}