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.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(onboarding): let people add their own competitors first (#859)
Gökhan Geyikf76412ffb0c6
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsxindex c1baa927d..94c42ad1e 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx@@ -8,7 +8,7 @@ import { syncDomains } from '@/lib/actions/brand-domain';import { createTopics } from '@/lib/actions/topic';import { getPromptCapacity, savePromptSet } from '@/lib/actions/prompt';import { addCompetitor } from '@/lib/actions/competitor';-import { addCompetitorChoice } from '@/lib/competitor-list';+import { addCompetitorChoice, mergeCompetitorSuggestions } from '@/lib/competitor-list';import { triggerTrackingCheck } from '@/lib/actions/tracking';import { bootstrapBrandVolumes } from '@/lib/actions/volumes';import { usePlanContext } from '@/components/providers/plan-provider';@@ -590,7 +590,7 @@ export default function NewBrandPage() {website: domain,language,});- setSuggestedCompetitors(competitors.map((c) => ({ ...c, selected: true })));+ setSuggestedCompetitors((prev) => mergeCompetitorSuggestions(prev, competitors));} catch (err) {console.error('Competitor suggestion error:', err);setCompetitorSuggestError(true);@@ -937,7 +937,7 @@ export default function NewBrandPage() {{topicSuggestError && (<div className="flex items-center justify-between gap-3 rounded-lg border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-sm"><span>- Couldn't fetch topic suggestions right now — add your own below.+ Couldn't fetch topic suggestions right now — add your own above.</span><Button variant="outline" size="sm" onClick={fetchTopicSuggestions}>Try again@@ -1166,6 +1166,36 @@ export default function NewBrandPage() {</p></div>+ {/* First, and outside the loading state: people arrive knowing their+ rivals and should not have to wait for, or scroll past, ours. */}+ <div className="mb-6 space-y-2">+ <p className="text-sm text-muted-foreground">Know your competitors? Add them here.</p>+ <div className="flex items-center gap-2">+ <Input+ placeholder="Company name"+ value={competitorName}+ onChange={(e) => setCompetitorName(e.target.value)}+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}+ className="text-sm"+ />+ <Input+ placeholder="domain.com"+ value={competitorDomain}+ onChange={(e) => setCompetitorDomain(e.target.value)}+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}+ className="text-sm"+ />+ <Button+ size="sm"+ variant="outline"+ onClick={addManualCompetitor}+ disabled={!competitorName.trim()}+ >+ <Plus className="h-4 w-4" />+ </Button>+ </div>+ </div>+{loadingCompetitors ? (<div className="flex flex-col items-center gap-3 py-16 text-muted-foreground"><Sparkles className="h-5 w-5 animate-pulse" />@@ -1178,7 +1208,7 @@ export default function NewBrandPage() {{competitorSuggestError && (<div className="flex items-center justify-between gap-3 rounded-lg border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-sm"><span>- Couldn't fetch competitor suggestions right now — add them manually below.+ Couldn't fetch competitor suggestions right now — add them manually above.</span><Button variant="outline" size="sm" onClick={fetchCompetitorSuggestions}>Try again@@ -1188,7 +1218,7 @@ export default function NewBrandPage() {{suggestedCompetitors.length > 0 && (<div className="space-y-2"><p className="text-xs font-medium text-muted-foreground uppercase tracking-wider">- Suggested competitors+ Competitors to track</p>{suggestedCompetitors.map((c, idx) => (<div@@ -1242,36 +1272,6 @@ export default function NewBrandPage() {</div>)}- <div className="space-y-2">- <p className="text-xs font-medium text-muted-foreground uppercase tracking-wider">- Add manually- </p>- <div className="flex items-center gap-2">- <Input- placeholder="Company name"- value={competitorName}- onChange={(e) => setCompetitorName(e.target.value)}- onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}- className="text-sm"- />- <Input- placeholder="domain.com"- value={competitorDomain}- onChange={(e) => setCompetitorDomain(e.target.value)}- onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}- className="text-sm"- />- <Button- size="sm"- variant="outline"- onClick={addManualCompetitor}- disabled={!competitorName.trim()}- >- <Plus className="h-4 w-4" />- </Button>- </div>- </div>-<Button className="w-full" onClick={handleFinish} disabled={savingCompetitors}>{savingCompetitors ? (<>diff --git a/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx b/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsxindex 575441ae0..1b9346846 100644--- a/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx+++ b/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx@@ -11,7 +11,7 @@ import { bootstrapBrandVolumes } from '@/lib/actions/volumes';import { addCompetitor, getCompetitors } from '@/lib/actions/competitor';import { getFaviconUrl } from '@/lib/favicon';import { slugify } from '@/lib/slug';-import { addCompetitorChoice } from '@/lib/competitor-list';+import { addCompetitorChoice, mergeCompetitorSuggestions } from '@/lib/competitor-list';import { useBrandStore } from '@/stores/use-brand-store';import {REGIONS,@@ -783,7 +783,7 @@ export default function OnboardingPage() {website: domain,language,});- setSuggestedCompetitors(competitors.map((c) => ({ ...c, selected: true })));+ setSuggestedCompetitors((prev) => mergeCompetitorSuggestions(prev, competitors));} catch (err) {console.error('Competitor suggestion error:', err);setCompetitorSuggestError(true);@@ -1243,7 +1243,7 @@ export default function OnboardingPage() {{topicSuggestError && (<div className="flex items-center justify-between gap-3 rounded-lg border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-sm"><span>- Couldn't fetch topic suggestions right now — add your own below.+ Couldn't fetch topic suggestions right now — add your own above.</span><Button variant="outline" size="sm" onClick={fetchTopicSuggestions}>Try again@@ -1470,7 +1470,14 @@ export default function OnboardingPage() {// ── Step 5: Competitors ──if (step === 5) {- const selectedCompetitorCount = suggestedCompetitors.filter((c) => c.selected).length;+ // Counted over the list Finish will save, which takes along a competitor+ // still sitting in the manual fields — otherwise the button stays+ // disabled for someone who typed theirs and never pressed "+".+ const selectedCompetitorCount = addCompetitorChoice(+ suggestedCompetitors,+ competitorName,+ competitorDomain,+ ).list.filter((c) => c.selected).length;const hasSelectedCompetitors = selectedCompetitorCount > 0;return (@@ -1485,6 +1492,36 @@ export default function OnboardingPage() {</p></div>+ {/* First, and outside the loading state: people arrive knowing their+ rivals and should not have to wait for, or scroll past, ours. */}+ <div className="mb-6 space-y-2">+ <p className="text-sm text-muted-foreground">Know your competitors? Add them here.</p>+ <div className="flex items-center gap-2">+ <Input+ placeholder="Company name"+ value={competitorName}+ onChange={(e) => setCompetitorName(e.target.value)}+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}+ className="text-sm"+ />+ <Input+ placeholder="domain.com"+ value={competitorDomain}+ onChange={(e) => setCompetitorDomain(e.target.value)}+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}+ className="text-sm"+ />+ <Button+ size="sm"+ variant="outline"+ onClick={addManualCompetitor}+ disabled={!competitorName.trim()}+ >+ <Plus className="h-4 w-4" />+ </Button>+ </div>+ </div>+{loadingCompetitors ? (<div className="flex flex-col items-center gap-3 py-16 text-muted-foreground"><Sparkles className="h-5 w-5 animate-pulse" />@@ -1500,7 +1537,7 @@ export default function OnboardingPage() {{competitorSuggestError && (<div className="flex items-center justify-between gap-3 rounded-lg border border-amber-500/40 bg-amber-500/10 px-4 py-3 text-sm"><span>- Couldn't fetch competitor suggestions right now — add your own below.+ Couldn't fetch competitor suggestions right now — add your own above.</span><Button variant="outline" size="sm" onClick={fetchCompetitorSuggestions}>Try again@@ -1510,7 +1547,7 @@ export default function OnboardingPage() {{suggestedCompetitors.length > 0 && (<div className="space-y-2"><p className="text-xs font-medium text-muted-foreground uppercase tracking-wider">- Suggested competitors+ Competitors to track</p>{suggestedCompetitors.map((c, idx) => (<div@@ -1564,36 +1601,6 @@ export default function OnboardingPage() {</div>)}- <div className="space-y-2">- <p className="text-xs font-medium text-muted-foreground uppercase tracking-wider">- Add manually- </p>- <div className="flex items-center gap-2">- <Input- placeholder="Company name"- value={competitorName}- onChange={(e) => setCompetitorName(e.target.value)}- onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}- className="text-sm"- />- <Input- placeholder="domain.com"- value={competitorDomain}- onChange={(e) => setCompetitorDomain(e.target.value)}- onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}- className="text-sm"- />- <Button- size="sm"- variant="outline"- onClick={addManualCompetitor}- disabled={!competitorName.trim()}- >- <Plus className="h-4 w-4" />- </Button>- </div>- </div>-<ButtonclassName="w-full"onClick={handleFinish}diff --git a/web/src/lib/competitor-list.test.ts b/web/src/lib/competitor-list.test.tsindex 80e39e5e3..72b8c83c1 100644--- a/web/src/lib/competitor-list.test.ts+++ b/web/src/lib/competitor-list.test.ts@@ -1,5 +1,9 @@import { describe, expect, it } from 'vitest';-import { addCompetitorChoice, normalizeCompetitorDomain } from './competitor-list';+import {+ addCompetitorChoice,+ mergeCompetitorSuggestions,+ normalizeCompetitorDomain,+} from './competitor-list';const listed = [{ name: 'Rival', domain: 'rival.com', selected: true },@@ -14,16 +18,16 @@ describe('normalizeCompetitorDomain', () => {});describe('addCompetitorChoice', () => {- it('appends a new competitor as selected', () => {+ it('puts a new competitor at the top, selected', () => {const { list, duplicate } = addCompetitorChoice(listed, ' New One ', 'https://new.io/');expect(duplicate).toBeNull();- expect(list.at(-1)).toEqual({ name: 'New One', domain: 'new.io', selected: true });+ expect(list[0]).toEqual({ name: 'New One', domain: 'new.io', selected: true });expect(list).toHaveLength(3);});it('adds a competitor typed without a domain', () => {const { list } = addCompetitorChoice(listed, 'Nameless', '');- expect(list.at(-1)).toEqual({ name: 'Nameless', domain: '', selected: true });+ expect(list[0]).toEqual({ name: 'Nameless', domain: '', selected: true });});it('ignores an empty name', () => {@@ -51,3 +55,23 @@ describe('addCompetitorChoice', () => {expect(list).toHaveLength(3);});});++describe('mergeCompetitorSuggestions', () => {+ it('keeps competitors typed in while suggestions loaded', () => {+ const typed = [{ name: 'Mine', domain: 'mine.com', selected: true }];+ const merged = mergeCompetitorSuggestions(typed, [+ { name: 'Rival', domain: 'rival.com' },+ { name: 'Other', domain: 'other.com' },+ ]);+ expect(merged.map((c) => c.name)).toEqual(['Mine', 'Rival', 'Other']);+ expect(merged.every((c) => c.selected)).toBe(true);+ });++ it('skips a suggestion the user already listed', () => {+ const typed = [{ name: 'Rival Co', domain: 'rival.com', selected: false }];+ const merged = mergeCompetitorSuggestions(typed, [+ { name: 'Rival', domain: 'https://www.rival.com/' },+ ]);+ expect(merged).toEqual(typed);+ });+});diff --git a/web/src/lib/competitor-list.ts b/web/src/lib/competitor-list.tsindex eea41a2ee..1e66da620 100644--- a/web/src/lib/competitor-list.ts+++ b/web/src/lib/competitor-list.ts@@ -17,10 +17,20 @@ function domainKey(domain: string): string {return domain.toLowerCase().replace(/^www\./, '');}+/** Same domain, or same name when either side has no domain. */+function findChoice(list: CompetitorChoice[], name: string, domain: string): number {+ return list.findIndex((c) =>+ domain && c.domain+ ? domainKey(c.domain) === domainKey(domain)+ : c.name.trim().toLowerCase() === name.toLowerCase(),+ );+}+/**- * Add a typed competitor to the step's list. One already listed — same- * domain, or same name when either side has no domain — is selected instead- * of added twice, and returned as `duplicate` so the caller can say so.+ * Add a typed competitor to the top of the step's list, right under the field+ * it was typed into. One already listed — same domain, or same name when+ * either side has no domain — is selected instead of added twice, and+ * returned as `duplicate` so the caller can say so.*/export function addCompetitorChoice(list: CompetitorChoice[],@@ -31,11 +41,7 @@ export function addCompetitorChoice(if (!name) return { list, duplicate: null };const domain = normalizeCompetitorDomain(rawDomain);- const index = list.findIndex((c) =>- domain && c.domain- ? domainKey(c.domain) === domainKey(domain)- : c.name.trim().toLowerCase() === name.toLowerCase(),- );+ const index = findChoice(list, name, domain);if (index !== -1) {const duplicate = list[index];return {@@ -43,5 +49,24 @@ export function addCompetitorChoice(duplicate,};}- return { list: [...list, { name, domain, selected: true }], duplicate: null };+ return { list: [{ name, domain, selected: true }, ...list], duplicate: null };+}++/**+ * Fold freshly fetched suggestions into the list without dropping anything+ * already on it: the user can type competitors in while suggestions load,+ * and a retry must not wipe them. Suggestions already listed are skipped.+ */+export function mergeCompetitorSuggestions(+ list: CompetitorChoice[],+ suggestions: { name: string; domain: string }[],+): CompetitorChoice[] {+ const merged = [...list];+ for (const s of suggestions) {+ const domain = normalizeCompetitorDomain(s.domain);+ if (findChoice(merged, s.name, domain) === -1) {+ merged.push({ name: s.name, domain, selected: true });+ }+ }+ return merged;}