Skip to content

Open source CapCut alternative.

TypeScript

fix(site): keep a promotion open on save and land the saved list

David Leecb9f4b90bef
diff --git a/site/src/app/su/promotions/PromotionDialog.tsx b/site/src/app/su/promotions/PromotionDialog.tsx
index 7aa33d99..8f8658ac 100644
--- a/site/src/app/su/promotions/PromotionDialog.tsx
+++ b/site/src/app/su/promotions/PromotionDialog.tsx
@@ -205,10 +205,11 @@ export function PromotionDialog({
}
};
+ // The dialog stays open on a save, so the draft can go on being edited.
const onSave = async () => {
setNotice(null);
const id = await ensureSaved();
- if (id) onOpenChange(false);
+ if (id) setNotice("Draft saved.");
};
const onSaveTemplate = () => {
@@ -278,7 +279,10 @@ export function PromotionDialog({
: "One email to everyone in the segment. Unsubscribed accounts are always left out."}
</DialogDescription>
{!readOnly && templates.data?.templates.length ? (
- <Select onValueChange={(id: string | null) => id && loadTemplate(id)}>
+ <Select
+ items={Object.fromEntries(templates.data.templates.map((t) => [t.id, t.name]))}
+ onValueChange={(id: string | null) => id && loadTemplate(id)}
+ >
<SelectTrigger><SelectValue placeholder="Load saved template" /></SelectTrigger>
<SelectContent>
{templates.data.templates.map((template) => (
@@ -304,6 +308,7 @@ export function PromotionDialog({
<Select
disabled={readOnly}
value={draft.sender}
+ items={{ bulk: senderLabel("bulk"), personal: senderLabel("personal") }}
onValueChange={(v) => setDraft({ ...draft, sender: v as PromotionSender })}
>
<SelectTrigger id="promo-sender">
@@ -460,7 +465,7 @@ export function PromotionDialog({
{save.isPending ? "Saving…" : "Save draft"}
</Button>
<Button type="button" disabled={busy} onClick={onSend}>
- {count.isPending ? "Counting…" : "Send…"}
+ {count.isPending ? "Counting…" : "Send"}
</Button>
</>
)}
diff --git a/site/src/queries/promotions.ts b/site/src/queries/promotions.ts
index 2f0a07f7..8ffa8361 100644
--- a/site/src/queries/promotions.ts
+++ b/site/src/queries/promotions.ts
@@ -17,6 +17,18 @@ export const promotionsQueryKey = ["su", "promotions"] as const;
type Body = { promotions: PromotionSummary[]; senders: Record<PromotionSender, string> };
+// A write answers with the whole list, and it lands in the cache as the
+// answer arrives, so a row reopened right after a save reads what was saved.
+function useTakeList() {
+ const queryClient = useQueryClient();
+ return (result: { promotions: PromotionSummary[] }) => {
+ queryClient.setQueryData<Body>(promotionsQueryKey, (body) =>
+ body ? { ...body, promotions: result.promotions } : body,
+ );
+ void queryClient.invalidateQueries({ queryKey: promotionsQueryKey });
+ };
+}
+
// Super-user only: every promotion with how far its send got, and the address
// each sender stands for. Polls while a send is running.
export function usePromotions() {
@@ -30,25 +42,25 @@ export function usePromotions() {
// A new draft, or an edit of one. Answers with the saved id.
export function useSavePromotion() {
- const queryClient = useQueryClient();
+ const takeList = useTakeList();
return useMutation({
mutationFn: ({ id, ...input }: PromotionInput & { id: string | null }) =>
apiFetch<{ id: string; promotions: PromotionSummary[] }>(
id ? `/api/su/promotions/${encodeURIComponent(id)}` : "/api/su/promotions",
{ body: JSON.stringify(input), method: id ? "PUT" : "POST" },
),
- onSuccess: () => queryClient.invalidateQueries({ queryKey: promotionsQueryKey }),
+ onSuccess: takeList,
});
}
export function useDeletePromotion() {
- const queryClient = useQueryClient();
+ const takeList = useTakeList();
return useMutation({
mutationFn: (id: string) =>
apiFetch<{ promotions: PromotionSummary[] }>(`/api/su/promotions/${encodeURIComponent(id)}`, {
method: "DELETE",
}),
- onSuccess: () => queryClient.invalidateQueries({ queryKey: promotionsQueryKey }),
+ onSuccess: takeList,
});
}
@@ -66,14 +78,14 @@ export function useSendPromotion() {
}
export function useCancelPromotion() {
- const queryClient = useQueryClient();
+ const takeList = useTakeList();
return useMutation({
mutationFn: (id: string) =>
apiFetch<{ promotions: PromotionSummary[] }>(
`/api/su/promotions/${encodeURIComponent(id)}/cancel`,
{ method: "POST" },
),
- onSuccess: () => queryClient.invalidateQueries({ queryKey: promotionsQueryKey }),
+ onSuccess: takeList,
});
}