Skip to content

Open source alternative to Semrush and Ahrefs

Change the topic of tracked prompts from Prompt Research and the prompts table (EVE-392) (#888)

Carter Smithd38d55a046be
diff --git a/src/client/features/ai-visibility/PromptActions.tsx b/src/client/features/ai-visibility/PromptActions.tsx
index b645e397..ca264e1e 100644
--- a/src/client/features/ai-visibility/PromptActions.tsx
+++ b/src/client/features/ai-visibility/PromptActions.tsx
@@ -28,7 +28,7 @@ export function PromptActionsMenu({
{onEdit && (
<DropdownMenuItem onClick={onEdit}>
<Pencil />
- Edit {kind}
+ {kind === "topic" ? "Rename topic" : "Edit prompt"}
</DropdownMenuItem>
)}
<DropdownMenuItem
diff --git a/src/client/features/ai-visibility/PromptInventory.tsx b/src/client/features/ai-visibility/PromptInventory.tsx
index fb96a7a5..667fb5b8 100644
--- a/src/client/features/ai-visibility/PromptInventory.tsx
+++ b/src/client/features/ai-visibility/PromptInventory.tsx
@@ -23,6 +23,7 @@ import {
} from "@/client/components/ui/table";
import { EngineLabel } from "./EngineLabel";
import { PromptActionsMenu } from "./PromptActions";
+import { RenameTopicDialog } from "./RenameTopicDialog";
import { aiRate } from "./shared";
import type { AiTrackerPatch } from "@/types/schemas/ai-visibility";
@@ -53,8 +54,9 @@ export function PromptInventory({
scheduled: boolean;
pending: boolean;
onEdit: (prompt: AiPrompt) => void;
+ /** Saves with no cost review: pause, archive and topic changes add no answers. */
onReduce: (patch: AiTrackerPatch) => void;
- onReview: (patch: AiTrackerPatch, description: string) => void;
+ onReview: (patch: AiTrackerPatch) => void;
}) {
const [collapsed, setCollapsed] = useState<string[]>([]);
const [archiving, setArchiving] = useState<{
@@ -62,6 +64,7 @@ export function PromptInventory({
name: string;
promptIds: string[];
} | null>(null);
+ const [renaming, setRenaming] = useState<string | null>(null);
const matches = (prompt: AiPrompt) =>
!search ||
prompt.text.toLocaleLowerCase().includes(search.toLocaleLowerCase());
@@ -142,6 +145,7 @@ export function PromptInventory({
paused={paused}
scheduled={scheduled}
pending={pending}
+ onEdit={() => setRenaming(topic)}
onTogglePause={() => {
const patch = {
prompts: topicPrompts.map((prompt) => ({
@@ -150,8 +154,7 @@ export function PromptInventory({
paused: !paused,
})),
};
- if (paused)
- onReview(patch, `Resume prompts in ${topic}.`);
+ if (paused) onReview(patch);
else onReduce(patch);
}}
onArchive={() =>
@@ -255,11 +258,7 @@ export function PromptInventory({
},
],
};
- if (prompt.paused)
- onReview(
- patch,
- "Resume this prompt in your tracker.",
- );
+ if (prompt.paused) onReview(patch);
else onReduce(patch);
}}
onArchive={() =>
@@ -279,6 +278,25 @@ export function PromptInventory({
);
})}
</Table>
+ {renaming && (
+ <RenameTopicDialog
+ topic={renaming}
+ onClose={() => setRenaming(null)}
+ onRename={(name) => {
+ // A topic is the name its prompts share, so renaming moves them.
+ onReduce({
+ prompts: prompts
+ .filter((prompt) => prompt.topic === renaming)
+ .map((prompt) => ({
+ id: prompt.id,
+ text: prompt.text,
+ topic: name,
+ })),
+ });
+ setRenaming(null);
+ }}
+ />
+ )}
{archiving && (
<ConfirmDialog
title={`Archive ${archiving.kind}?`}
diff --git a/src/client/features/ai-visibility/PromptList.tsx b/src/client/features/ai-visibility/PromptList.tsx
index c1ef74c7..b4d2de2a 100644
--- a/src/client/features/ai-visibility/PromptList.tsx
+++ b/src/client/features/ai-visibility/PromptList.tsx
@@ -33,7 +33,7 @@ export function PromptList({
onSetup: () => void;
onEdit: (prompt: AiPrompt) => void;
onReduce: (patch: AiTrackerPatch) => void;
- onReview: (patch: AiTrackerPatch, description: string) => void;
+ onReview: (patch: AiTrackerPatch) => void;
reducePending: boolean;
}) {
const [search, setSearch] = useState("");
diff --git a/src/client/features/ai-visibility/PromptResearchKeyword.tsx b/src/client/features/ai-visibility/PromptResearchKeyword.tsx
index 35dbba67..f68e7589 100644
--- a/src/client/features/ai-visibility/PromptResearchKeyword.tsx
+++ b/src/client/features/ai-visibility/PromptResearchKeyword.tsx
@@ -9,8 +9,8 @@ import {
import { researchAiVisibilityPrompts } from "@/serverFunctions/ai-visibility";
import { normalizeAiSuggestion } from "@/shared/ai-prompt-suggestions";
import type { AiTrackerState } from "@/shared/ai-visibility";
-import type { AiTrackerPatch } from "@/types/schemas/ai-visibility";
import { PromptResearchTable } from "./PromptResearchTable";
+import { TopicField } from "./TopicField";
import { TrackerPatchReview } from "./TrackerPatchReview";
import { AiQueryError, aiVisibilityKey } from "./shared";
@@ -25,9 +25,10 @@ export function PromptResearchKeyword({
}) {
const queryClient = useQueryClient();
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
+ // The prompts to track and their topic, while the user reviews them.
const [change, setChange] = useState<{
- patch: AiTrackerPatch;
- description: string;
+ prompts: string[];
+ topic: string;
} | null>(null);
// The tracked prompts refresh tracked flags after any tracker edit. The
// server reuses its cached provider response, so this costs no extra credits.
@@ -48,17 +49,17 @@ export function PromptResearchKeyword({
const selected = (research.data?.prompts ?? [])
.filter((prompt) => rowSelection[prompt.text])
.map((prompt) => prompt.text);
- const track = () => {
- const topic =
- state.topics.find(
- (name) =>
- normalizeAiSuggestion(name) === normalizeAiSuggestion(keyword),
- ) ?? keyword;
+ // Defaults to the saved topic that matches the keyword, else the keyword.
+ const track = () =>
setChange({
- description: `Track ${selected.length} researched ${selected.length === 1 ? "prompt" : "prompts"} in the ${topic} topic.`,
- patch: { prompts: selected.map((text) => ({ text, topic })) },
+ prompts: selected,
+ topic:
+ state.topics.find(
+ (name) =>
+ normalizeAiSuggestion(name) === normalizeAiSuggestion(keyword),
+ ) ?? keyword,
});
- };
+ const topic = change?.topic.trim();
return (
<>
<TableBulkActionBar
@@ -104,8 +105,13 @@ export function PromptResearchKeyword({
<TrackerPatchReview
projectId={projectId}
state={state}
- patch={change.patch}
- description={change.description}
+ patch={{
+ prompts: change.prompts.map((text) => ({
+ text,
+ topic: topic || undefined,
+ })),
+ }}
+ canSave={Boolean(topic)}
onClose={() => setChange(null)}
onSaved={(next) => {
queryClient.setQueryData(
@@ -115,7 +121,13 @@ export function PromptResearchKeyword({
setChange(null);
setRowSelection({});
}}
- />
+ >
+ <TopicField
+ topics={state.topics}
+ value={change.topic}
+ onChange={(name) => setChange({ ...change, topic: name })}
+ />
+ </TrackerPatchReview>
)}
</>
);
diff --git a/src/client/features/ai-visibility/PromptTrackingPage.tsx b/src/client/features/ai-visibility/PromptTrackingPage.tsx
index 0f2389a8..f6f94063 100644
--- a/src/client/features/ai-visibility/PromptTrackingPage.tsx
+++ b/src/client/features/ai-visibility/PromptTrackingPage.tsx
@@ -103,10 +103,7 @@ function PromptTrackingContent({
const [selectedRun, setSelectedRun] = useState<AiRun | null>(null);
const [costMode, setCostMode] = useState<"check" | "schedule" | null>(null);
const [editingPrompt, setEditingPrompt] = useState<AiPrompt | null>(null);
- const [change, setChange] = useState<{
- patch: AiTrackerPatch;
- description: string;
- } | null>(null);
+ const [change, setChange] = useState<AiTrackerPatch | null>(null);
const currentRun = selectedRun ?? state.recentRuns[0] ?? null;
const progress = useAiRunProgress(projectId, currentRun);
const tracker = state.tracker;
@@ -249,9 +246,7 @@ function PromptTrackingContent({
onSetup={() => setEditorMode(TrackerEditorMode.Prompts)}
onEdit={setEditingPrompt}
onReduce={(patch) => reduceTracking.mutate(patch)}
- onReview={(patch, description) =>
- setChange({ patch, description })
- }
+ onReview={setChange}
reducePending={reduceTracking.isPending}
/>
)}
@@ -328,23 +323,20 @@ function PromptTrackingContent({
<PromptEditor
prompt={editingPrompt}
state={state}
+ pending={reduceTracking.isPending}
onClose={() => setEditingPrompt(null)}
onReview={(patch) => {
setEditingPrompt(null);
- setChange({
- patch,
- description:
- "Save this exact prompt and topic. Changed wording archives this prompt and adds the new wording as a new prompt; earlier answers keep the prompt they were collected for.",
- });
+ setChange(patch);
}}
+ onMove={(patch) => reduceTracking.mutate(patch)}
/>
)}
{change && (
<TrackerPatchReview
projectId={projectId}
state={state}
- patch={change.patch}
- description={change.description}
+ patch={change}
onClose={() => setChange(null)}
onSaved={saved}
/>
diff --git a/src/client/features/ai-visibility/RenameTopicDialog.tsx b/src/client/features/ai-visibility/RenameTopicDialog.tsx
new file mode 100644
index 00000000..38daa58f
--- /dev/null
+++ b/src/client/features/ai-visibility/RenameTopicDialog.tsx
@@ -0,0 +1,60 @@
+import { useId, useState } from "react";
+import { FormDialog } from "@/client/components/FormDialog";
+import { Button } from "@/client/components/ui/button";
+import { Field, FieldLabel } from "@/client/components/ui/field";
+import { Input } from "@/client/components/ui/input";
+
+/** Renames a topic. A saved topic's name moves its prompts into that topic. */
+export function RenameTopicDialog({
+ topic,
+ onClose,
+ onRename,
+}: {
+ topic: string;
+ onClose: () => void;
+ onRename: (name: string) => void;
+}) {
+ const formId = useId();
+ const inputId = useId();
+ const [name, setName] = useState(topic);
+ const next = name.trim();
+ return (
+ <FormDialog
+ title="Rename topic"
+ onClose={onClose}
+ actions={
+ <>
+ <Button variant="ghost" onClick={onClose}>
+ Cancel
+ </Button>
+ <Button
+ type="submit"
+ form={formId}
+ disabled={!next || next === topic}
+ >
+ Rename
+ </Button>
+ </>
+ }
+ >
+ <form
+ id={formId}
+ onSubmit={(event) => {
+ event.preventDefault();
+ onRename(next);
+ }}
+ >
+ <Field>
+ <FieldLabel htmlFor={inputId}>Topic name</FieldLabel>
+ <Input
+ id={inputId}
+ autoFocus
+ maxLength={100}
+ value={name}
+ onChange={(event) => setName(event.target.value)}
+ />
+ </Field>
+ </form>
+ </FormDialog>
+ );
+}
diff --git a/src/client/features/ai-visibility/TopicField.tsx b/src/client/features/ai-visibility/TopicField.tsx
new file mode 100644
index 00000000..fe30bf19
--- /dev/null
+++ b/src/client/features/ai-visibility/TopicField.tsx
@@ -0,0 +1,70 @@
+import { useId, useState } from "react";
+import { Field, FieldLabel } from "@/client/components/ui/field";
+import { Input } from "@/client/components/ui/input";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/client/components/ui/select";
+
+/** Picks a saved topic or names a new one. */
+export function TopicField({
+ topics,
+ value,
+ onChange,
+}: {
+ topics: string[];
+ value: string;
+ onChange: (topic: string) => void;
+}) {
+ const topicId = useId();
+ // Kept separately so typing a saved topic's name doesn't hide the input.
+ const [creating, setCreating] = useState(!topics.includes(value));
+ const items = [
+ ...topics.map((name) => ({ value: name, label: name })),
+ { value: "", label: "New topic" },
+ ];
+ return (
+ <>
+ {topics.length > 0 && (
+ <Field>
+ <FieldLabel>Topic</FieldLabel>
+ <Select
+ items={items}
+ value={creating ? "" : value}
+ onValueChange={(name) => {
+ setCreating(!name);
+ onChange(name ?? "");
+ }}
+ >
+ <SelectTrigger className="w-full" aria-label="Topic">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {items.map((item) => (
+ <SelectItem key={item.value} value={item.value}>
+ {item.label}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </Field>
+ )}
+ {creating && (
+ <Field>
+ <FieldLabel htmlFor={topicId}>Topic name</FieldLabel>
+ <Input
+ id={topicId}
+ required
+ maxLength={100}
+ placeholder="e.g. SEO tools"
+ value={value}
+ onChange={(event) => onChange(event.target.value)}
+ />
+ </Field>
+ )}
+ </>
+ );
+}
diff --git a/src/client/features/ai-visibility/TrackerPatchReview.tsx b/src/client/features/ai-visibility/TrackerPatchReview.tsx
index a603449a..32464e87 100644
--- a/src/client/features/ai-visibility/TrackerPatchReview.tsx
+++ b/src/client/features/ai-visibility/TrackerPatchReview.tsx
@@ -1,5 +1,5 @@
-import { useId, useState } from "react";
-import { useMutation, useQuery } from "@tanstack/react-query";
+import { useId, useState, type ReactNode } from "react";
+import { keepPreviousData, useMutation, useQuery } from "@tanstack/react-query";
import { Loader2, X } from "lucide-react";
import { Button } from "@/client/components/ui/button";
import {
@@ -10,13 +10,6 @@ import {
DialogTitle,
} from "@/client/components/ui/dialog";
import { Field, FieldLabel } from "@/client/components/ui/field";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/client/components/ui/select";
import { Textarea } from "@/client/components/ui/textarea";
import {
estimateAiVisibilityCost,
@@ -24,20 +17,25 @@ import {
} from "@/serverFunctions/ai-visibility";
import type { AiTrackerPatch } from "@/types/schemas/ai-visibility";
import type { AiPrompt, AiTrackerState } from "@/shared/ai-visibility";
-import { AiLoading, AiQueryError, aiMoney, aiVisibilityKey } from "./shared";
+import { TopicField } from "./TopicField";
+import { AiLoading, AiQueryError, aiVisibilityKey } from "./shared";
export function TrackerPatchReview({
projectId,
state,
patch,
- description,
+ children,
+ canSave = true,
onClose,
onSaved,
}: {
projectId: string;
state: AiTrackerState;
patch: AiTrackerPatch;
- description: string;
+ /** Fields that edit the patch, such as its topic. */
+ children?: ReactNode;
+ /** False while those fields are incomplete. */
+ canSave?: boolean;
onClose: () => void;
onSaved: (state: AiTrackerState) => void;
}) {
@@ -49,6 +47,10 @@ export function TrackerPatchReview({
gcTime: 0,
refetchOnWindowFocus: false,
retry: false,
+ // The estimate checks the change before Save, for example the topic
+ // limit. Editing the patch in place keeps the last result instead of a
+ // spinner, and Save waits for the new one.
+ placeholderData: keepPreviousData,
});
const save = useMutation({
mutationFn: () =>
@@ -76,7 +78,7 @@ export function TrackerPatchReview({
<X />
</Button>
</DialogHeader>
- <p className="text-sm">{description}</p>
+ {children}
{patch.prompts?.map((prompt) => (
<p
key={prompt.id ?? prompt.text}
@@ -97,23 +99,6 @@ export function TrackerPatchReview({
/>
) : (
<>
- {estimate.data ? (
- <div className="space-y-1 text-sm text-muted-foreground">
- <p>
- Each run collects {estimate.data.observations} answers for{" "}
- {aiMoney(estimate.data.costUsd)}
- </p>
- <p>
- {aiMoney(estimate.data.monthlyCostUsd)} estimated per month on
- the {estimate.data.scheduleInterval} schedule.
- </p>
- </div>
- ) : (
- <p className="text-sm text-muted-foreground">
- Tracking is paused. Saving this change does not start a paid
- run.
- </p>
- )}
{save.error && <AiQueryError error={save.error} />}
<DialogFooter>
<Button
@@ -123,7 +108,12 @@ export function TrackerPatchReview({
>
Cancel
</Button>
- <Button disabled={save.isPending} onClick={() => save.mutate()}>
+ <Button
+ disabled={
+ save.isPending || !canSave || estimate.isPlaceholderData
+ }
+ onClick={() => save.mutate()}
+ >
{save.isPending && <Loader2 className="size-4 animate-spin" />}
Save change
</Button>
@@ -138,19 +128,26 @@ export function TrackerPatchReview({
export function PromptEditor({
prompt,
state,
+ pending,
onClose,
onReview,
+ onMove,
}: {
prompt: AiPrompt;
state: AiTrackerState;
+ /** A move is saving. */
+ pending: boolean;
onClose: () => void;
+ /** New wording adds a prompt, so its cost needs a review. */
onReview: (patch: AiTrackerPatch) => void;
+ /** A topic change keeps the prompt and its answers, so it saves directly. */
+ onMove: (patch: AiTrackerPatch) => void;
}) {
const [text, setText] = useState(prompt.text);
const [topic, setTopic] = useState(prompt.topic);
const textId = useId();
- const topicId = useId();
- const topicItems = state.topics.map((name) => ({ value: name, label: name }));
+ // The server compares trimmed wording, so whitespace alone is no rewording.
+ const reworded = text.trim() !== prompt.text;
return (
<Dialog
open
@@ -170,10 +167,6 @@ export function PromptEditor({
<X />
</Button>
</DialogHeader>
- <p className="text-xs text-muted-foreground">
- Changing the wording archives this prompt and adds the new wording as
- a new prompt. Earlier answers keep the prompt they were collected for.
- </p>
<Field>
<FieldLabel htmlFor={textId}>Exact prompt</FieldLabel>
<Textarea
@@ -185,40 +178,26 @@ export function PromptEditor({
data-ph-mask
/>
</Field>
- <Field>
- <FieldLabel htmlFor={topicId}>Topic</FieldLabel>
- <Select
- items={topicItems}
- value={topic}
- onValueChange={(name) => {
- if (name) setTopic(name);
- }}
- >
- <SelectTrigger id={topicId} className="w-full">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {topicItems.map((item) => (
- <SelectItem key={item.value} value={item.value}>
- {item.label}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- </Field>
+ <TopicField topics={state.topics} value={topic} onChange={setTopic} />
<DialogFooter>
<Button variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button
disabled={
- !text.trim() || (text === prompt.text && topic === prompt.topic)
- }
- onClick={() =>
- onReview({ prompts: [{ id: prompt.id, text, topic }] })
+ pending ||
+ !text.trim() ||
+ !topic.trim() ||
+ (!reworded && topic === prompt.topic)
}
+ onClick={() => {
+ const patch = { prompts: [{ id: prompt.id, text, topic }] };
+ if (reworded) onReview(patch);
+ else onMove(patch);
+ }}
>
- Review change
+ {pending && <Loader2 className="size-4 animate-spin" />}
+ {reworded ? "Review change" : "Save"}
</Button>
</DialogFooter>
</DialogContent>
diff --git a/src/client/features/ai-visibility/TrackerPromptFields.tsx b/src/client/features/ai-visibility/TrackerPromptFields.tsx
index 8c282469..55e1b462 100644
--- a/src/client/features/ai-visibility/TrackerPromptFields.tsx
+++ b/src/client/features/ai-visibility/TrackerPromptFields.tsx
@@ -1,4 +1,4 @@
-import { useId, useState } from "react";
+import { useId } from "react";
import { useMutation } from "@tanstack/react-query";
import Papa from "papaparse";
import { Plus, Loader2, Sparkles } from "lucide-react";
@@ -8,18 +8,12 @@ import { toast } from "sonner";
import { Button } from "@/client/components/ui/button";
import { Field, FieldLabel } from "@/client/components/ui/field";
import { Input } from "@/client/components/ui/input";
-import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
-} from "@/client/components/ui/select";
import type { AiTrackerState } from "@/shared/ai-visibility";
import {
MAX_PROMPTS_PER_ADDITION,
type TrackerDraft,
} from "./TrackerTrackingFields";
+import { TopicField } from "./TopicField";
export function TrackerPromptFields({
projectId,
@@ -34,11 +28,8 @@ export function TrackerPromptFields({
onChange: (value: TrackerDraft) => void;
onGeneratingChange: (pending: boolean) => void;
}) {
- const topicId = useId();
const promptsId = useId();
const group = value.topic;
- // Kept separately so typing a saved topic's name doesn't hide the input.
- const [creating, setCreating] = useState(!state.topics.includes(group.name));
const topicPrompts = state.prompts.filter(
(prompt) => !prompt.archived && prompt.topic === group.name,
);
@@ -67,10 +58,6 @@ export function TrackerPromptFields({
});
},
});
- const items = [
- ...state.topics.map((name) => ({ value: name, label: name })),
- { value: "", label: "New topic" },
- ];
const update = (change: Partial<typeof group>) =>
onChange({ ...value, topic: { ...group, ...change } });
const insertPrompts = (index: number, prompts: string[]) => {
@@ -87,42 +74,11 @@ export function TrackerPromptFields({
};
return (
<section className="space-y-4">
- {state.topics.length > 0 && (
- <Field>
- <FieldLabel>Topic</FieldLabel>
- <Select
- items={items}
- value={creating ? "" : group.name}
- onValueChange={(name) => {
- setCreating(!name);
- update({ name: name ?? "" });
- }}
- >
- <SelectTrigger className="w-full" aria-label="Topic">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {items.map((item) => (
- <SelectItem key={item.value} value={item.value}>
- {item.label}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- </Field>
- )}
- {creating && (
- <Field>
- <FieldLabel htmlFor={topicId}>Topic name</FieldLabel>
- <Input
- id={topicId}
- required
- placeholder="e.g. SEO tools"
- value={group.name}
- onChange={(event) => update({ name: event.target.value })}
- />
- </Field>
- )}
+ <TopicField
+ topics={state.topics}
+ value={group.name}
+ onChange={(name) => update({ name })}
+ />
{topicPrompts.length > 0 &&
topicPrompts.every((prompt) => prompt.paused) && (
<p className="text-xs text-muted-foreground">
diff --git a/src/types/schemas/ai-visibility.ts b/src/types/schemas/ai-visibility.ts
index 6adbbce4..b98baf0f 100644
--- a/src/types/schemas/ai-visibility.ts
+++ b/src/types/schemas/ai-visibility.ts
@@ -58,7 +58,7 @@ export const aiTrackerPatchSchema = z.object({
topic: topicSchema
.optional()
.describe(
- "Topic name that groups the prompt. New prompts default to General; edits keep the saved topic.",
+ "Topic name that groups the prompt. New prompts default to General; edits without a topic keep the saved topic. Set it on an existing prompt to move the prompt; to rename a topic, move every prompt in it.",
),
paused: z.boolean().optional(),
}),