Skip to content

Open source CapCut alternative.

TypeScript

fix(site): fold an offer's email into its history row

David Le570092348f5e
diff --git a/site/src/app/su/outreach/UserHistory.tsx b/site/src/app/su/outreach/UserHistory.tsx
index 25466997..8480bf1b 100644
--- a/site/src/app/su/outreach/UserHistory.tsx
+++ b/site/src/app/su/outreach/UserHistory.tsx
@@ -6,7 +6,7 @@ import { formatBytes } from "@/lib/bytes";
import { formatUsdPlain } from "@/lib/credits/format-usd";
import { useAnalyticsRollup } from "@/queries/analytics";
import { type OutreachRow } from "@/queries/outreach";
-import { useUserHistory, type UserHistory } from "@/queries/userHistory";
+import { useUserHistory, type UserHistory, type UserHistoryEmail } from "@/queries/userHistory";
// What the site has already given and sent one person: paid money, storage
// held, and one timeline of the offers that reached them, the credit that
@@ -53,12 +53,25 @@ type Event = {
};
// The three lists as one story, newest first. An offer says where it stands
-// — claimed, still open, or missed — and a grant what is left of it.
+// — claimed, still open, or missed — and a grant what is left of it. The
+// email that carried an offer is part of the offer's row: when it went out,
+// whether by hand, and whether the person clicked.
function timeline(history: UserHistory, now: number): Event[] {
const events: Event[] = [];
+ const emailsByOffer = new Map<string, UserHistoryEmail[]>();
+ for (const email of history.emails) {
+ if (!email.offerId) continue;
+ emailsByOffer.set(email.offerId, [...(emailsByOffer.get(email.offerId) ?? []), email]);
+ }
for (const offer of history.offers) {
const notes: string[] = [];
- if (offer.emailSentAt) notes.push(`emailed ${day(offer.emailSentAt)}`);
+ const carriers = emailsByOffer.get(offer.id) ?? [];
+ const sentAt = carriers[0]?.sentAt ?? offer.emailSentAt;
+ if (sentAt) {
+ notes.push(`emailed ${day(sentAt)}${carriers.some((email) => email.kind === "promotion-hand") ? " by hand" : ""}`);
+ }
+ const clickedAt = carriers.find((email) => email.clickedAt)?.clickedAt;
+ if (clickedAt) notes.push(`clicked ${day(clickedAt)}`);
let tone: Event["tone"] = "open";
if (offer.claimedAt) {
notes.push(`claimed ${day(offer.claimedAt)}`);
@@ -97,6 +110,7 @@ function timeline(history: UserHistory, now: number): Event[] {
});
}
for (const email of history.emails) {
+ if (email.offerId && history.offers.some((offer) => offer.id === email.offerId)) continue;
const subject = email.promotion?.subject ?? email.subject;
events.push({
at: email.sentAt,
diff --git a/site/src/lib/marketing/userHistory.ts b/site/src/lib/marketing/userHistory.ts
index 482853ab..313b7f60 100644
--- a/site/src/lib/marketing/userHistory.ts
+++ b/site/src/lib/marketing/userHistory.ts
@@ -38,6 +38,8 @@ export type UserHistoryEmail = {
id: string;
kind: string;
promotion: UserHistoryPromotion | null;
+ // The credit offer this email carried, when it carried one.
+ offerId: string | null;
subject: string | null;
sentAt: string;
clickedAt: string | null;
@@ -134,13 +136,21 @@ export async function readUserHistory(userId: string): Promise<UserHistory> {
})),
emails: emails.flatMap((email) => {
if (!email.sentAt) return [];
- const payload = email.payload as { subject?: unknown } | null;
+ const payload = email.payload as { offerId?: unknown; subject?: unknown } | null;
const subject = typeof payload?.subject === "string" ? payload.subject : null;
+ // A promotion's email and its offer share the promotion; a credit
+ // offer's email names the offer.
+ const offerId = email.promotionId
+ ? (offers.find((offer) => promotionIdOfOffer(offer) === email.promotionId)?.id ?? null)
+ : typeof payload?.offerId === "string"
+ ? payload.offerId
+ : null;
return [
{
id: email.id,
kind: email.kind,
promotion: promotionOf(email.promotionId),
+ offerId,
subject,
sentAt: email.sentAt.toISOString(),
clickedAt: iso(email.clickedAt),