Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
Finish AI visibility setup on the page that started it, without a flicker (#887)
Carter Smithba5a83ac39e8
diff --git a/src/client/features/ai-visibility/AiResearchSetupGate.tsx b/src/client/features/ai-visibility/AiResearchSetupGate.tsxindex d19f3035..af479738 100644--- a/src/client/features/ai-visibility/AiResearchSetupGate.tsx+++ b/src/client/features/ai-visibility/AiResearchSetupGate.tsx@@ -1,6 +1,5 @@-import { useEffect, useRef, type ReactNode } from "react";+import { useEffect, useState, type ReactNode } from "react";import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";-import { useNavigate } from "@tanstack/react-router";import { Sparkles } from "lucide-react";import { Alert, AlertDescription } from "@/client/components/ui/alert";import { Button } from "@/client/components/ui/button";@@ -14,7 +13,10 @@ import type { SaveProjectWebsiteSetup } from "@/types/schemas/projectWebsite";import { SkeletonPageContent } from "@/client/components/SkeletonPresets";import {AiQueryError,+ aiResearchKeywordsQueryOptions,aiResearchSetupQueryOptions,+ aiRunResultsQueryOptions,+ aiTrackerQueryOptions,aiVisibilityKey,} from "./shared";@@ -24,7 +26,7 @@ import {* research and the onboarding competitor review when the overview or* competitors are missing, otherwise one keyword and prompt generation. The* run lives on the server, so leaving or reloading the page finds it again.- * Both paths end on Prompt Tracking.+ * Both paths end on the page that started setup.*/export function AiResearchSetupGate({projectId,@@ -34,7 +36,6 @@ export function AiResearchSetupGate({children: ReactNode;}) {const queryClient = useQueryClient();- const navigate = useNavigate();const setupOptions = aiResearchSetupQueryOptions(projectId);const setup = useQuery(setupOptions);const refresh = () =>@@ -51,28 +52,53 @@ export function AiResearchSetupGate({mutationFn: () => startAiResearchSetup({ data: { projectId } }),onSuccess: (data) => queryClient.setQueryData(setupOptions.queryKey, data),});+ // Setup writes the tracker, research keywords and a first run. Load what the+ // AI visibility pages read before setup reads ready, so the page that+ // started it doesn't flash what it cached before. A failed load drops that+ // cache instead, so the page loads it again with its own retry and error.+ const loadSetupResults = async () => {+ const tracker = aiTrackerQueryOptions(projectId);+ const keywords = aiResearchKeywordsQueryOptions(projectId);+ await Promise.all([+ queryClient.fetchQuery(tracker).then(+ ({ recentRuns }) => {+ const runId = recentRuns[0]?.id;+ if (runId)+ return queryClient.prefetchQuery(+ aiRunResultsQueryOptions(projectId, runId),+ );+ },+ () => queryClient.removeQueries({ queryKey: tracker.queryKey }),+ ),+ queryClient+ .fetchQuery({ ...keywords, staleTime: 0 })+ .catch(() =>+ queryClient.removeQueries({ queryKey: keywords.queryKey }),+ ),+ ]);+ await refresh();+ };const save = useMutation({mutationFn: (accepted: Omit<SaveProjectWebsiteSetup, "projectId">) =>saveProjectWebsiteSetup({ data: { ...accepted, projectId } }),- onSuccess: refresh,+ onSuccess: loadSetupResults,});const status = setup.data?.status;- // Once the setup step settled is seen here, ready lands on the generated- // topics and prompts. Keyword generation refreshes AI visibility queries.- const settingUp = useRef(false);+ // Setup that finishes anywhere but this gate's save (a background run,+ // another tab) holds the progress screen until the results load, as a saved+ // review keeps its saving state.+ const [lastStatus, setLastStatus] = useState(status);+ const [finishing, setFinishing] = useState(false);+ if (status !== lastStatus) {+ setLastStatus(status);+ const done = lastStatus && lastStatus !== "ready" && status === "ready";+ if (done && !save.isPending) setFinishing(true);+ }useEffect(() => {- if (status && status !== "ready") settingUp.current = true;- else if (status === "ready" && settingUp.current) {- settingUp.current = false;- void refresh();- void navigate({- to: "/p/$projectId/ai-visibility",- params: { projectId },- });- }- // refresh and navigate are stable enough; only status transitions matter.+ if (finishing) void loadSetupResults().finally(() => setFinishing(false));+ // loadSetupResults only reads projectId, which keys this gate.// eslint-disable-next-line react-hooks/exhaustive-deps- }, [status]);+ }, [finishing]);if (setup.isPending) return <SkeletonPageContent />;if (setup.isError)return (@@ -83,7 +109,7 @@ export function AiResearchSetupGate({}}/>);- if (status === "ready") return children;+ if (status === "ready" && !finishing) return children;if (setup.data.review)return (<div className="grid min-h-[calc(100dvh-8rem)] place-items-center">@@ -96,7 +122,7 @@ export function AiResearchSetupGate({/></div>);- if (status === "running" || start.isPending)+ if (status === "running" || start.isPending || finishing)return (<div className="grid min-h-[calc(100dvh-8rem)] place-items-center"><WebsiteResearchProgressdiff --git a/src/client/features/ai-visibility/PromptResearchPage.tsx b/src/client/features/ai-visibility/PromptResearchPage.tsxindex b7474e9a..c6453a3c 100644--- a/src/client/features/ai-visibility/PromptResearchPage.tsx+++ b/src/client/features/ai-visibility/PromptResearchPage.tsx@@ -18,11 +18,10 @@ import {TableHeader,TableRow,} from "@/client/components/ui/table";-import { listAiResearchKeywords } from "@/serverFunctions/ai-visibility";import { PromptResearchKeyword } from "./PromptResearchKeyword";import {AiQueryError,- aiVisibilityKey,+ aiResearchKeywordsQueryOptions,useAiVisibilityTracker,} from "./shared";@@ -105,10 +104,7 @@ export function PromptResearchPage({function KeywordList({ projectId }: { projectId: string }) {const navigate = useNavigate();- const query = useQuery({- queryKey: [...aiVisibilityKey(projectId), "researchKeywords"],- queryFn: () => listAiResearchKeywords({ data: { projectId } }),- });+ const query = useQuery(aiResearchKeywordsQueryOptions(projectId));return (<div className="overflow-hidden rounded-lg border bg-card border-border"><div className="border-b p-4 border-border">diff --git a/src/client/features/ai-visibility/shared.tsx b/src/client/features/ai-visibility/shared.tsxindex 38672027..fc703d93 100644--- a/src/client/features/ai-visibility/shared.tsx+++ b/src/client/features/ai-visibility/shared.tsx@@ -12,6 +12,7 @@ import {getAiVisibilityRun,getAiVisibilityTracker,getAiVisibilityResults,+ listAiResearchKeywords,} from "@/serverFunctions/ai-visibility";import type { AiRun } from "@/shared/ai-visibility";@@ -56,6 +57,12 @@ export const aiResearchSetupQueryOptions = (projectId: string) =>query.state.data?.status === "running" ? 3000 : false,});+export const aiResearchKeywordsQueryOptions = (projectId: string) =>+ queryOptions({+ queryKey: [...aiVisibilityKey(projectId), "researchKeywords"],+ queryFn: () => listAiResearchKeywords({ data: { projectId } }),+ });+/** Every answer in a run, so each prompt can show its per-engine result. */export const aiRunResultsQueryOptions = (projectId: string,