Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Show the prompts behind each citation (#881)
Carter Smith7050d3c1211b
diff --git a/src/client/features/ai-visibility/CitationsTab.tsx b/src/client/features/ai-visibility/CitationsTab.tsxindex d4d7dd02..df87d622 100644--- a/src/client/features/ai-visibility/CitationsTab.tsx+++ b/src/client/features/ai-visibility/CitationsTab.tsx@@ -1,7 +1,7 @@import { useState } from "react";import { useQuery } from "@tanstack/react-query";import { Link } from "@tanstack/react-router";-import { ArrowRight } from "lucide-react";+import { ArrowRight, ChevronDown, ChevronRight } from "lucide-react";import { SafeExternalLink } from "@/client/components/SafeExternalLink";import { SegmentedToggle } from "@/client/components/SegmentedToggle";import { SkeletonTableRows } from "@/client/components/SkeletonPresets";@@ -26,6 +26,7 @@ import { getAiVisibilitySources } from "@/serverFunctions/ai-visibility";import {AI_ENGINE_LABELS,type AiEngine,+ type AiPrompt,type AiSourceRow,type AiTrackerState,} from "@/shared/ai-visibility";@@ -158,6 +159,8 @@ export function CitationsTab({</p>) : (<SourcesTable+ projectId={projectId}+ prompts={state.prompts}rows={query.data.rows}groupBy={groupBy}engines={state.engines}@@ -195,14 +198,19 @@ const OWNERSHIP_ITEMS = [] as const;function SourcesTable({+ projectId,+ prompts,rows,groupBy,engines,}: {+ projectId: string;+ prompts: AiPrompt[];rows: AiSourceRow[];groupBy: "url" | "domain";engines: AiEngine[];}) {+ const [expanded, setExpanded] = useState<string | null>(null);return (<Table><TableHeader>@@ -214,50 +222,117 @@ function SourcesTable({<TableHead>Answers</TableHead><TableHead>Prompts</TableHead><TableHead>Engines</TableHead>+ <TableHead /></TableRow></TableHeader><TableBody>{rows.map((row) => (- <TableRow key={row.key}>- <TableCell className="min-w-60 max-w-lg whitespace-normal">- <SafeExternalLink- url={row.url || `https://${row.domain}`}- label={row.title || row.url || row.domain}- className="flex items-start gap-1.5 break-all text-sm text-primary hover:underline"- />- <p className="mt-1 text-xs text-muted-foreground">{row.domain}</p>- </TableCell>- <TableCell className="whitespace-nowrap text-xs">- {row.ownership === "own"- ? "Your domain"- : row.ownership === "competitor"- ? "Competitor"- : "Third party"}- </TableCell>- <TableCell>{row.answerCount}</TableCell>- <TableCell>{row.promptCount}</TableCell>- <TableCell>- <div className="flex flex-wrap items-center gap-3">- {engines.map((tracked) => {- const count = row.engines.find(- (item) => item.engine === tracked,- )?.answerCount;- return (- <span- key={tracked}- title={AI_ENGINE_LABELS[tracked]}- className={`flex items-center gap-1 text-xs tabular-nums ${count ? "" : "text-muted-foreground/60"}`}- >- <EngineLabel engine={tracked} />- {count ?? "–"}- </span>- );- })}- </div>- </TableCell>- </TableRow>+ <SourceRow+ key={row.key}+ projectId={projectId}+ prompts={prompts.filter((prompt) =>+ row.promptIds.includes(prompt.id),+ )}+ row={row}+ engines={engines}+ expanded={expanded === row.key}+ onExpand={() => setExpanded(expanded === row.key ? null : row.key)}+ />))}</TableBody></Table>);}++function SourceRow({+ projectId,+ prompts,+ row,+ engines,+ expanded,+ onExpand,+}: {+ projectId: string;+ prompts: AiPrompt[];+ row: AiSourceRow;+ engines: AiEngine[];+ expanded: boolean;+ onExpand: () => void;+}) {+ return (+ <>+ <TableRow+ className="cursor-pointer"+ onClick={(event) => {+ // Opening the cited page should not also toggle the row.+ if (!(event.target instanceof Element && event.target.closest("a")))+ onExpand();+ }}+ >+ <TableCell className="min-w-60 max-w-lg whitespace-normal">+ <SafeExternalLink+ url={row.url || `https://${row.domain}`}+ label={row.title || row.url || row.domain}+ className="flex items-start gap-1.5 break-all text-sm text-primary hover:underline"+ />+ <p className="mt-1 text-xs text-muted-foreground">{row.domain}</p>+ </TableCell>+ <TableCell className="whitespace-nowrap text-xs">+ {row.ownership === "own"+ ? "Your domain"+ : row.ownership === "competitor"+ ? "Competitor"+ : "Third party"}+ </TableCell>+ <TableCell>{row.answerCount}</TableCell>+ <TableCell>{row.promptCount}</TableCell>+ <TableCell>+ <div className="flex flex-wrap items-center gap-3">+ {engines.map((tracked) => {+ const count = row.engines.find(+ (item) => item.engine === tracked,+ )?.answerCount;+ return (+ <span+ key={tracked}+ title={AI_ENGINE_LABELS[tracked]}+ className={`flex items-center gap-1 text-xs tabular-nums ${count ? "" : "text-muted-foreground/60"}`}+ >+ <EngineLabel engine={tracked} />+ {count ?? "–"}+ </span>+ );+ })}+ </div>+ </TableCell>+ <TableCell className="w-10">+ {/* The click bubbles to the row, which does the toggling. */}+ <Button+ variant="ghost"+ size="icon-sm"+ aria-expanded={expanded}+ aria-label={`Show prompts that cited ${row.title || row.url || row.domain}`}+ >+ {expanded ? <ChevronDown /> : <ChevronRight />}+ </Button>+ </TableCell>+ </TableRow>+ {expanded &&+ prompts.map((prompt) => (+ <TableRow key={prompt.id} className="bg-muted/30">+ {/* The link fills the cell so the whole row opens the prompt. */}+ <TableCell colSpan={6} className="p-0 whitespace-normal">+ <Link+ to="/p/$projectId/ai-visibility/prompts/$promptId"+ params={{ projectId, promptId: prompt.id }}+ className="block py-1.5 pr-4 pl-8 text-sm whitespace-pre-wrap"+ data-ph-mask+ >+ {prompt.text}+ </Link>+ </TableCell>+ </TableRow>+ ))}+ </>+ );+}diff --git a/src/server/features/ai-visibility/services/aiVisibilityResults.test.ts b/src/server/features/ai-visibility/services/aiVisibilityResults.test.tsindex bd31a6be..74d339f6 100644--- a/src/server/features/ai-visibility/services/aiVisibilityResults.test.ts+++ b/src/server/features/ai-visibility/services/aiVisibilityResults.test.ts@@ -188,7 +188,12 @@ describe("AI visibility results", () => {ownership: "all",});expect(sources.rows).toMatchObject([- { url: "https://openseo.so/pricing", ownership: "own", answerCount: 2 },+ {+ url: "https://openseo.so/pricing",+ ownership: "own",+ answerCount: 2,+ promptIds: ["prompt-0", "prompt-1"],+ },{ url: "https://ahrefs.com/blog", ownership: "competitor" },]);});diff --git a/src/server/features/ai-visibility/services/aiVisibilityResults.ts b/src/server/features/ai-visibility/services/aiVisibilityResults.tsindex bbb6403c..53f5f3b4 100644--- a/src/server/features/ai-visibility/services/aiVisibilityResults.ts+++ b/src/server/features/ai-visibility/services/aiVisibilityResults.ts@@ -312,6 +312,7 @@ export async function loadAiSources(input: AiSourcesInput): Promise<AiSources> {ownership,answerCount: 0,promptCount: 0,+ promptIds: [],engines: [],observationIds: [],truncated: false,@@ -332,6 +333,7 @@ export async function loadAiSources(input: AiSourcesInput): Promise<AiSources> {...g.row,answerCount: g.answers.size,promptCount: g.prompts.size,+ promptIds: [...g.prompts],engines: [...g.engines].map(([engine, answers]) => ({engine,answerCount: answers.size,diff --git a/src/server/mcp/tools/ai-visibility-output-schemas.ts b/src/server/mcp/tools/ai-visibility-output-schemas.tsindex d0280618..7ada39c1 100644--- a/src/server/mcp/tools/ai-visibility-output-schemas.ts+++ b/src/server/mcp/tools/ai-visibility-output-schemas.ts@@ -160,6 +160,7 @@ export const aiSourcesOutput = z.looseObject({ownership: z.enum(["own", "competitor", "other"]),answerCount: count,promptCount: count,+ promptIds: z.array(text),engines: z.array(z.looseObject({ engine, answerCount: count })),observationIds: z.array(text),truncated: flag,diff --git a/src/shared/ai-visibility.ts b/src/shared/ai-visibility.tsindex d129fb0c..2c33b7e6 100644--- a/src/shared/ai-visibility.ts+++ b/src/shared/ai-visibility.ts@@ -192,6 +192,8 @@ export interface AiSourceRow {ownership: "own" | "competitor" | "other";answerCount: number;promptCount: number;+ /** The tracked prompts whose answers cited this source. */+ promptIds: string[];engines: { engine: AiEngine; answerCount: number }[];observationIds: string[];truncated: boolean;