Skip to content

Open source CapCut alternative.

TypeScript

fix(site): one outreach row at a time

David Le1b2329b112a6
diff --git a/site/src/app/cut/app/su/outreach/page.tsx b/site/src/app/cut/app/su/outreach/page.tsx
index 5d0a584e..5e95667b 100644
--- a/site/src/app/cut/app/su/outreach/page.tsx
+++ b/site/src/app/cut/app/su/outreach/page.tsx
@@ -22,6 +22,7 @@ import { cn } from "@/lib/utils";
import type { OutreachStatus } from "@/lib/marketing/campaigns";
import { OUTREACH_PLACEHOLDERS } from "@/lib/marketing/placeholders";
import {
+ useBusyOutreachIds,
useOutreach,
useOutreachAction,
useOutreachCounts,
@@ -98,6 +99,7 @@ export default function SuOutreachPage() {
const list = useOutreach(status);
const counts = useOutreachCounts().data?.counts;
const act = useOutreachAction();
+ const busy = useBusyOutreachIds();
const templates = useOutreachTemplates();
const saveTemplate = useSaveOutreachTemplate();
const deleteTemplate = useDeleteOutreachTemplate();
@@ -198,19 +200,26 @@ export default function SuOutreachPage() {
};
const submitSend = () => {
- if (!sendTarget || subject.trim() === "" || body.trim() === "") return;
+ const target = sendTarget;
+ if (!target || subject.trim() === "" || body.trim() === "") return;
act.mutate(
- { action: "send", body, outreachId: sendTarget.id, subject },
+ { action: "send", body, outreachId: target.id, subject },
{
onSuccess: () => {
remember({ body, subject });
- setSendTarget(null);
+ // A send in flight leaves the dialog free for the next row, so only
+ // close it if that row is still the one on screen.
+ setSendTarget((current) => (current?.id === target.id ? null : current));
},
},
);
};
const rows = list.data?.rows ?? [];
+ const sending = sendTarget !== null && busy.has(sendTarget.id);
+ // A failed send keeps its dialog open, so the reason belongs in there with
+ // the words that still need fixing.
+ const sendFailed = act.isError && act.variables?.action === "send";
return (
<div className="space-y-4 pb-9">
@@ -252,7 +261,7 @@ export default function SuOutreachPage() {
<div className="flex shrink-0 items-center gap-2">
{row.status === "sent" ? (
<Button
- disabled={act.isPending}
+ disabled={busy.has(row.id)}
onClick={() =>
act.mutate({ action: "replied", outreachId: row.id })
}
@@ -264,7 +273,7 @@ export default function SuOutreachPage() {
) : null}
{row.status === "ignored" ? (
<Button
- disabled={act.isPending}
+ disabled={busy.has(row.id)}
onClick={() =>
act.mutate({ action: "unignore", outreachId: row.id })
}
@@ -275,7 +284,7 @@ export default function SuOutreachPage() {
</Button>
) : (
<Button
- disabled={act.isPending}
+ disabled={busy.has(row.id)}
onClick={() =>
act.mutate({ action: "ignore", outreachId: row.id })
}
@@ -286,7 +295,7 @@ export default function SuOutreachPage() {
</Button>
)}
<Button
- disabled={act.isPending}
+ disabled={busy.has(row.id)}
onClick={() => openSend(row)}
size="sm"
>
@@ -303,10 +312,9 @@ export default function SuOutreachPage() {
)}
</div>
- {act.isError ? (
+ {act.isError && !sendFailed ? (
<p className="text-sm text-destructive">
- That didn&apos;t go through. The account may have unsubscribed since the
- last scan, or the text may name a placeholder that doesn&apos;t exist.
+ That didn&apos;t go through. Run a scan and try again.
</p>
) : null}
@@ -453,13 +461,20 @@ export default function SuOutreachPage() {
</Button>
))}
</div>
+ {sendFailed ? (
+ <p className="text-sm text-destructive">
+ That didn&apos;t go through. The account may have unsubscribed
+ since the last scan, or the text may name a placeholder that
+ doesn&apos;t exist.
+ </p>
+ ) : null}
</div>
</div>
<DialogFooter>
<DialogClose render={<Button variant="outline" />}>Cancel</DialogClose>
- <Button disabled={act.isPending || !sendable} onClick={submitSend}>
- {act.isPending ? "Sending…" : "Send"}
+ <Button disabled={sending || !sendable} onClick={submitSend}>
+ {sending ? "Sending…" : "Send"}
</Button>
</DialogFooter>
</DialogContent>
diff --git a/site/src/queries/outreach.ts b/site/src/queries/outreach.ts
index 09d4296b..00addeee 100644
--- a/site/src/queries/outreach.ts
+++ b/site/src/queries/outreach.ts
@@ -1,6 +1,11 @@
"use client";
-import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import {
+ useMutation,
+ useMutationState,
+ useQuery,
+ useQueryClient,
+} from "@tanstack/react-query";
import type { OutreachStatus } from "@/lib/marketing/campaigns";
import { apiFetch } from "@/queries/apiClient";
@@ -54,7 +59,11 @@ type OutreachAction =
| { action: "send"; outreachId: string; subject: string; body: string }
| { action: "ignore" | "unignore" | "replied"; outreachId: string };
-// Any action moves a row between lists, so every list is invalidated.
+const outreachActionKey = ["outreach", "action"] as const;
+
+// Any action moves a row between lists, so every list is invalidated. Actions
+// run one per row and in parallel: the key lets the list ask which rows are in
+// flight instead of freezing on any one of them.
export function useOutreachAction() {
const queryClient = useQueryClient();
return useMutation({
@@ -63,10 +72,21 @@ export function useOutreachAction() {
body: JSON.stringify(input),
method: "POST",
}),
+ mutationKey: outreachActionKey,
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["outreach"] }),
});
}
+// The rows with an action still running.
+export function useBusyOutreachIds(): Set<string> {
+ const running = useMutationState({
+ filters: { mutationKey: outreachActionKey, status: "pending" },
+ select: (mutation) =>
+ (mutation.state.variables as OutreachAction | undefined)?.outreachId,
+ });
+ return new Set(running.filter((id): id is string => typeof id === "string"));
+}
+
// Super-user only: roll credit usage into the list now and follow the job to
// completion, then refetch so the list updates in place.
export function useRunOutreachScan() {