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.

fix(onboarding): keep a typed competitor that was never added with "+" (#851)

Gökhan Geyik0e9dedb74fd7
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx
index c6dfe6f86..c1baa927d 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/brands/new/page.tsx
@@ -8,6 +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 { triggerTrackingCheck } from '@/lib/actions/tracking';
import { bootstrapBrandVolumes } from '@/lib/actions/volumes';
import { usePlanContext } from '@/components/providers/plan-provider';
@@ -609,24 +610,40 @@ export default function NewBrandPage() {
};
const addManualCompetitor = () => {
- const name = competitorName.trim();
- const d = competitorDomain
- .trim()
- .replace(/^https?:\/\//, '')
- .replace(/\/+$/, '');
- if (!name) return;
- setSuggestedCompetitors((prev) => [...prev, { name, domain: d, selected: true }]);
+ const { list, duplicate } = addCompetitorChoice(
+ suggestedCompetitors,
+ competitorName,
+ competitorDomain,
+ );
+ if (list === suggestedCompetitors) return;
+ if (duplicate) toast.info(`${duplicate.name} is already in the list`);
+ setSuggestedCompetitors(list);
setCompetitorName('');
setCompetitorDomain('');
};
+ /**
+ * The list to save: a competitor still sitting in the manual fields — typed
+ * but never added with "+" — is one the user meant to track, so Finish
+ * takes it along instead of dropping it.
+ */
+ const takePendingCompetitor = () => {
+ const { list } = addCompetitorChoice(suggestedCompetitors, competitorName, competitorDomain);
+ if (list !== suggestedCompetitors) {
+ setSuggestedCompetitors(list);
+ setCompetitorName('');
+ setCompetitorDomain('');
+ }
+ return list;
+ };
+
// ── Step 5: final save ──
const handleFinish = async () => {
if (!createdBrand) return;
setSavingCompetitors(true);
try {
- const selected = suggestedCompetitors.filter((c) => c.selected);
+ const selected = takePendingCompetitor().filter((c) => c.selected);
if (selected.length > 0) {
await Promise.all(
selected.map((c) => addCompetitor(createdBrand.id, { name: c.name, domain: c.domain })),
@@ -1234,6 +1251,7 @@ export default function NewBrandPage() {
placeholder="Company name"
value={competitorName}
onChange={(e) => setCompetitorName(e.target.value)}
+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}
className="text-sm"
/>
<Input
diff --git a/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx b/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx
index 6dcff66d2..575441ae0 100644
--- a/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx
+++ b/web/src/app/[locale]/(onboarding)/dashboard/onboarding/page.tsx
@@ -11,6 +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 { useBrandStore } from '@/stores/use-brand-store';
import {
REGIONS,
@@ -802,22 +803,38 @@ export default function OnboardingPage() {
};
const addManualCompetitor = () => {
- const name = competitorName.trim();
- const domain = competitorDomain
- .trim()
- .replace(/^https?:\/\//, '')
- .replace(/\/+$/, '');
- if (!name) return;
- setSuggestedCompetitors((prev) => [...prev, { name, domain, selected: true }]);
+ const { list, duplicate } = addCompetitorChoice(
+ suggestedCompetitors,
+ competitorName,
+ competitorDomain,
+ );
+ if (list === suggestedCompetitors) return;
+ if (duplicate) toast.info(`${duplicate.name} is already in the list`);
+ setSuggestedCompetitors(list);
setCompetitorName('');
setCompetitorDomain('');
};
+ /**
+ * The list to save: a competitor still sitting in the manual fields — typed
+ * but never added with "+" — is one the user meant to track, so Finish
+ * takes it along instead of dropping it.
+ */
+ const takePendingCompetitor = () => {
+ const { list } = addCompetitorChoice(suggestedCompetitors, competitorName, competitorDomain);
+ if (list !== suggestedCompetitors) {
+ setSuggestedCompetitors(list);
+ setCompetitorName('');
+ setCompetitorDomain('');
+ }
+ return list;
+ };
+
// ── Step 5: final save ──
const handleFinish = async () => {
if (!createdBrand) return;
- const selected = suggestedCompetitors.filter((c) => c.selected);
+ const selected = takePendingCompetitor().filter((c) => c.selected);
if (selected.length === 0) {
toast.error('Add at least one competitor to continue.');
return;
@@ -1556,6 +1573,7 @@ export default function OnboardingPage() {
placeholder="Company name"
value={competitorName}
onChange={(e) => setCompetitorName(e.target.value)}
+ onKeyDown={(e) => e.key === 'Enter' && addManualCompetitor()}
className="text-sm"
/>
<Input
diff --git a/web/src/lib/competitor-list.test.ts b/web/src/lib/competitor-list.test.ts
new file mode 100644
index 000000000..80e39e5e3
--- /dev/null
+++ b/web/src/lib/competitor-list.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from 'vitest';
+import { addCompetitorChoice, normalizeCompetitorDomain } from './competitor-list';
+
+const listed = [
+ { name: 'Rival', domain: 'rival.com', selected: true },
+ { name: 'Other Co', domain: '', selected: false },
+];
+
+describe('normalizeCompetitorDomain', () => {
+ it('strips the protocol, trailing slashes and surrounding space', () => {
+ expect(normalizeCompetitorDomain(' https://rival.com/ ')).toBe('rival.com');
+ expect(normalizeCompetitorDomain('http://rival.com//')).toBe('rival.com');
+ });
+});
+
+describe('addCompetitorChoice', () => {
+ it('appends a new competitor as 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).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 });
+ });
+
+ it('ignores an empty name', () => {
+ const { list, duplicate } = addCompetitorChoice(listed, ' ', 'x.com');
+ expect(list).toBe(listed);
+ expect(duplicate).toBeNull();
+ });
+
+ it('treats the same domain as a duplicate, ignoring case and www', () => {
+ const { list, duplicate } = addCompetitorChoice(listed, 'Rival Inc', 'https://WWW.Rival.com');
+ expect(duplicate?.name).toBe('Rival');
+ expect(list).toHaveLength(2);
+ });
+
+ it('matches by name when a side has no domain, and selects the existing entry', () => {
+ const { list, duplicate } = addCompetitorChoice(listed, 'other co', 'otherco.com');
+ expect(duplicate?.name).toBe('Other Co');
+ expect(list).toHaveLength(2);
+ expect(list[1].selected).toBe(true);
+ });
+
+ it('does not match different domains that share a name', () => {
+ const { list, duplicate } = addCompetitorChoice(listed, 'Rival', 'rival.de');
+ expect(duplicate).toBeNull();
+ expect(list).toHaveLength(3);
+ });
+});
diff --git a/web/src/lib/competitor-list.ts b/web/src/lib/competitor-list.ts
new file mode 100644
index 000000000..eea41a2ee
--- /dev/null
+++ b/web/src/lib/competitor-list.ts
@@ -0,0 +1,47 @@
+/** A competitor on the brand-setup competitor step, suggested or typed in. */
+export interface CompetitorChoice {
+ name: string;
+ domain: string;
+ selected: boolean;
+}
+
+/** Same cleanup addCompetitor applies before saving. */
+export function normalizeCompetitorDomain(raw: string): string {
+ return raw
+ .trim()
+ .replace(/^https?:\/\//, '')
+ .replace(/\/+$/, '');
+}
+
+function domainKey(domain: string): string {
+ return domain.toLowerCase().replace(/^www\./, '');
+}
+
+/**
+ * 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.
+ */
+export function addCompetitorChoice(
+ list: CompetitorChoice[],
+ rawName: string,
+ rawDomain: string,
+): { list: CompetitorChoice[]; duplicate: CompetitorChoice | null } {
+ const name = rawName.trim();
+ 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(),
+ );
+ if (index !== -1) {
+ const duplicate = list[index];
+ return {
+ list: list.map((c, i) => (i === index ? { ...c, selected: true } : c)),
+ duplicate,
+ };
+ }
+ return { list: [...list, { name, domain, selected: true }], duplicate: null };
+}