Skip to content

Commit c546673

Browse files
committed
Share the dashboard's mail building blocks from email-blocks
Hosts such as CourseLit were re-implementing pieces of the dashboard and drifting from it (a host's preview kept the "View in browser" link after the setting was turned off). Move them into @sendlit/email-blocks and use them from the dashboard too: - SequenceAnalytics takes onRefresh and shows a refresh button - sequenceStatsMetrics maps SequenceStats to analytics metrics - ViewInBrowserSetting is the shared "View in browser" switch - footer exports marketingPreviewContext and DEFAULT_FOOTER_LABELS; createFooterBlock() defaults to those labels
1 parent d2cea48 commit c546673

16 files changed

Lines changed: 347 additions & 90 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@sendlit/email-blocks": minor
3+
---
4+
5+
Share more of the dashboard's mail building blocks so hosts don't re-implement them:
6+
7+
- `SequenceAnalytics` takes `onRefresh` and shows a refresh button in its header.
8+
- `sequenceStatsMetrics` maps `SequenceStats` to `SequenceAnalytics` metrics.
9+
- `ViewInBrowserSetting` is the "View in browser" switch for a broadcast or sequence.
10+
- `@sendlit/email-blocks/footer` exports `marketingPreviewContext`, which leaves the "View in browser" link out of previews when the setting is off, and `DEFAULT_FOOTER_LABELS`; `createFooterBlock()` uses those labels when none are given.

‎apps/docs/content/docs/email-blocks/email-editor.mdx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ final block and provide the real address and unsubscribe URL:
219219
import { EmailEditor } from "@sendlit/email-editor";
220220
import { createFooterBlock } from "@sendlit/email-blocks/footer";
221221

222-
const footer = createFooterBlock({ labels });
222+
// English labels by default; pass `{ labels }` to translate them.
223+
const footer = createFooterBlock();
223224

224225
<EmailEditor
225226
email={email}
@@ -234,5 +235,22 @@ const footer = createFooterBlock({ labels });
234235
/>;
235236
```
236237

238+
Before an email is sent there's no real address or link, so editors and
239+
previews can use `marketingPreviewContext()` from the same entry point. Pass
240+
the broadcast's or sequence's `viewInBrowser` setting so the preview shows the
241+
"View in browser" link only when sent emails will have it:
242+
243+
```tsx
244+
import { marketingPreviewContext } from "@sendlit/email-blocks/footer";
245+
246+
<EmailEditor
247+
blocks={[Text, Separator, Link, ImageBlock, footer]}
248+
renderContext={marketingPreviewContext({
249+
viewInBrowser: sequence.viewInBrowser,
250+
})}
251+
{...rest}
252+
/>;
253+
```
254+
237255
The independently hosted editor remains generic: omit this add-on and context
238256
for ordinary or transactional designs.

‎apps/docs/content/docs/email-blocks/sequence-analytics.mdx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,46 @@ function Example() {
5050
return <SequenceAnalytics metrics={analyticsMetrics} />;
5151
}
5252
```
53+
54+
## Building the metrics
55+
56+
`sequenceStatsMetrics` turns a sequence's or broadcast's `SequenceStats` into
57+
these metrics, with the same labels and help text SendLit uses. Pass the number
58+
of contacts waiting for their next email to add a "Waiting" metric.
59+
60+
```tsx
61+
import { SequenceAnalytics, sequenceStatsMetrics } from "@sendlit/email-blocks";
62+
63+
<SequenceAnalytics metrics={sequenceStatsMetrics(stats, schedule?.waiting)} />;
64+
```
65+
66+
## Refreshing
67+
68+
Pass `onRefresh` to show a refresh button in the header. It spins until the
69+
promise you return settles, so the numbers update without reloading the page.
70+
71+
```tsx
72+
<SequenceAnalytics
73+
title="Performance"
74+
metrics={sequenceStatsMetrics(stats)}
75+
onRefresh={async () => setStats(await getSequenceStats(sequenceId))}
76+
/>;
77+
```
78+
79+
## View in browser
80+
81+
`ViewInBrowserSetting` is the switch for a broadcast's or sequence's
82+
`viewInBrowser` setting, with its explanation. It saves as soon as it's
83+
flipped: give it an async `onCheckedChange` that saves the value, and a
84+
rejection is shown under the switch.
85+
86+
```tsx
87+
import { ViewInBrowserSetting } from "@sendlit/email-blocks";
88+
89+
<ViewInBrowserSetting
90+
checked={sequence.viewInBrowser}
91+
onCheckedChange={async (viewInBrowser) => {
92+
setSequence(await updateSequence(sequence.sequenceId, { viewInBrowser }));
93+
}}
94+
/>;
95+
```

‎apps/web/app/(dashboard)/broadcasts/[sequenceId]/page.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type Sequence,
99
SequenceAnalytics,
1010
type SequenceStats,
11+
sequenceStatsMetrics,
1112
} from "@sendlit/email-blocks";
1213
import {
1314
Archive,
@@ -84,7 +85,6 @@ import {
8485
presentDeliverySourceError,
8586
sendingOptionFor,
8687
} from "@/lib/delivery-source";
87-
import { sequenceStatsMetrics } from "@/lib/stats";
8888
import { useSegments } from "@/lib/use-segments";
8989

9090
interface BroadcastMeta {
@@ -544,7 +544,11 @@ export default function BroadcastEditorPage({
544544
)}
545545

546546
{stats && (
547-
<SequenceAnalytics className="mb-6" metrics={sequenceStatsMetrics(stats)} />
547+
<SequenceAnalytics
548+
className="mb-6"
549+
metrics={sequenceStatsMetrics(stats)}
550+
onRefresh={async () => setStats(await getSequenceStats(sequenceId))}
551+
/>
548552
)}
549553

550554
<div className="space-y-6">

‎apps/web/app/(dashboard)/sequences/[sequenceId]/page.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type SequenceEmail,
1313
SequenceEmailList,
1414
type SequenceStats,
15+
sequenceStatsMetrics,
1516
TagEditor,
1617
} from "@sendlit/email-blocks";
1718
import { Check, Pause, Pencil, Play, Trash2 } from "lucide-react";
@@ -74,7 +75,6 @@ import {
7475
isDeletedDeliverySource,
7576
presentDeliverySourceError,
7677
} from "@/lib/delivery-source";
77-
import { sequenceStatsMetrics } from "@/lib/stats";
7878

7979
interface SequenceMeta {
8080
title: string;
@@ -406,6 +406,7 @@ export default function SequenceEditorPage({
406406
<SequenceAnalytics
407407
className="mb-6"
408408
metrics={sequenceStatsMetrics(stats, schedule?.waiting)}
409+
onRefresh={async () => setStats(await getSequenceStats(sequenceId))}
409410
/>
410411
)}
411412

‎apps/web/components/dashboard/email-editor-screen.tsx‎

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { TemplatePurpose } from "@sendlit/api-contract";
55
import { type Email, EmailEditor } from "@sendlit/email-blocks";
66
import {
77
createFooterBlock,
8+
marketingPreviewContext,
89
type SendLitEmailRenderContext,
910
} from "@sendlit/email-blocks/footer";
1011
import { normalizeEmail } from "@sendlit/email-editor";
@@ -105,24 +106,7 @@ const BASE_EMAIL_EDITOR_BLOCKS = [
105106
HtmlSnippet,
106107
];
107108

108-
const SENDLIT_FOOTER_BLOCK = createFooterBlock({
109-
labels: {
110-
displayName: "Footer",
111-
description: "Required mailing address and unsubscribe footer",
112-
unsubscribe: "Unsubscribe",
113-
viewInBrowser: "View in browser",
114-
alignment: "Alignment",
115-
alignmentLeft: "Left",
116-
alignmentCenter: "Center",
117-
alignmentRight: "Right",
118-
foregroundColor: "Text color",
119-
backgroundColor: "Background color",
120-
fontSize: "Font size",
121-
paddingTop: "Top padding",
122-
paddingBottom: "Bottom padding",
123-
paddingX: "Horizontal padding",
124-
},
125-
});
109+
const SENDLIT_FOOTER_BLOCK = createFooterBlock();
126110

127111
// Conditional sections read the subscriber's tags, which only marketing sends have.
128112
export const MARKETING_EMAIL_EDITOR_BLOCKS = [
@@ -132,23 +116,7 @@ export const MARKETING_EMAIL_EDITOR_BLOCKS = [
132116
];
133117
export const TRANSACTIONAL_EMAIL_EDITOR_BLOCKS = BASE_EMAIL_EDITOR_BLOCKS;
134118

135-
/** What a marketing email's managed footer shows in previews. The "View in
136-
* browser" link is left out when the sequence has it turned off. */
137-
export function marketingPreviewContext({
138-
mailingAddress = "Your workspace mailing address",
139-
viewInBrowser = true,
140-
}: {
141-
mailingAddress?: string;
142-
viewInBrowser?: boolean;
143-
} = {}): SendLitEmailRenderContext {
144-
return {
145-
footer: {
146-
mailingAddress,
147-
unsubscribeUrl: "#unsubscribe-preview",
148-
...(viewInBrowser && { viewInBrowserUrl: "#view-in-browser-preview" }),
149-
},
150-
};
151-
}
119+
export { marketingPreviewContext };
152120

153121
/**
154122
* Full-screen email editing screen — the only place `EmailEditor` is rendered,
Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,25 @@
11
"use client";
22

3-
import type { Sequence } from "@sendlit/email-blocks";
4-
import { useState } from "react";
5-
import { Label } from "@/components/ui/codelit/label";
6-
import { Switch } from "@/components/ui/codelit/switch";
3+
import {
4+
type Sequence,
5+
ViewInBrowserSetting as ViewInBrowserSwitch,
6+
} from "@sendlit/email-blocks";
77
import { updateSequence } from "@/lib/api";
8-
import { ApiError } from "@/lib/api-client";
98

10-
/**
11-
* The broadcast's or sequence's "View in browser" switch. It saves as soon
12-
* as it's flipped, separately from the rest of the form, because it stays
13-
* changeable after sending: turning it off takes down the web versions
14-
* behind links already sent.
15-
*/
9+
/** Saves the shared "View in browser" switch through the dashboard API. */
1610
export function ViewInBrowserSetting({
1711
sequence,
1812
onSaved,
1913
}: {
2014
sequence: Sequence;
2115
onSaved: (sequence: Sequence) => void;
2216
}) {
23-
const [saving, setSaving] = useState(false);
24-
const [error, setError] = useState<string | null>(null);
25-
26-
async function toggle(viewInBrowser: boolean) {
27-
setSaving(true);
28-
setError(null);
29-
try {
30-
onSaved(await updateSequence(sequence.sequenceId, { viewInBrowser }));
31-
} catch (err) {
32-
setError(err instanceof ApiError ? err.message : "Failed to save");
33-
} finally {
34-
setSaving(false);
35-
}
36-
}
37-
3817
return (
39-
<div className="flex items-start gap-3">
40-
<Switch
41-
id="view-in-browser"
42-
checked={sequence.viewInBrowser}
43-
onCheckedChange={toggle}
44-
disabled={saving}
45-
/>
46-
<div className="space-y-1">
47-
<Label htmlFor="view-in-browser">View in browser</Label>
48-
<p className="text-sm text-muted-foreground">
49-
Links each email to a hosted copy of it. Turn off for emails that
50-
shouldn&apos;t be on the web, such as onboarding or trial emails; copies
51-
linked from emails already sent stop working too.
52-
</p>
53-
{error && <p className="text-sm text-destructive">{error}</p>}
54-
</div>
55-
</div>
18+
<ViewInBrowserSwitch
19+
checked={sequence.viewInBrowser}
20+
onCheckedChange={async (viewInBrowser) => {
21+
onSaved(await updateSequence(sequence.sequenceId, { viewInBrowser }));
22+
}}
23+
/>
5624
);
5725
}

‎packages/email-blocks/src/footer/footer.test.tsx‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { describe, expect, it } from "bun:test";
22
import { defaultEmail, type Email, renderEmailToHtml } from "@sendlit/email-editor";
3-
import { createFooterBlock, createFooterEmailBlock } from ".";
3+
import {
4+
createFooterBlock,
5+
createFooterEmailBlock,
6+
DEFAULT_FOOTER_LABELS,
7+
marketingPreviewContext,
8+
} from ".";
49

510
function labelsFor(viewInBrowser?: string) {
611
return {
@@ -173,3 +178,30 @@ describe("SendLit footer block", () => {
173178
});
174179
});
175180
});
181+
182+
describe("marketingPreviewContext", () => {
183+
it("shows placeholder footer values with the view-in-browser link", () => {
184+
expect(marketingPreviewContext().footer).toEqual({
185+
mailingAddress: "Your mailing address",
186+
unsubscribeUrl: "#unsubscribe-preview",
187+
viewInBrowserUrl: "#view-in-browser-preview",
188+
});
189+
});
190+
191+
it("leaves the link out when view in browser is off", () => {
192+
const footer = marketingPreviewContext({
193+
mailingAddress: "1 Main St",
194+
viewInBrowser: false,
195+
}).footer;
196+
expect(footer?.viewInBrowserUrl).toBeUndefined();
197+
expect(footer?.mailingAddress).toBe("1 Main St");
198+
});
199+
});
200+
201+
describe("createFooterBlock defaults", () => {
202+
it("uses the English labels when none are given", () => {
203+
expect(createFooterBlock().metadata.displayName).toBe(
204+
DEFAULT_FOOTER_LABELS.displayName,
205+
);
206+
});
207+
});

‎packages/email-blocks/src/footer/index.tsx‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,51 @@ import { FooterBlock } from "./block";
44
import { FooterSettingsPanel } from "./settings";
55
import type {
66
FooterBlockConfig,
7+
FooterLabels,
78
FooterSettings,
89
SendLitEmailRenderContext,
910
} from "./types";
1011

12+
/** English labels for the footer block and its settings panel. */
13+
export const DEFAULT_FOOTER_LABELS: FooterLabels = {
14+
displayName: "Footer",
15+
description: "Required mailing address and unsubscribe footer",
16+
unsubscribe: "Unsubscribe",
17+
viewInBrowser: "View in browser",
18+
alignment: "Alignment",
19+
alignmentLeft: "Left",
20+
alignmentCenter: "Center",
21+
alignmentRight: "Right",
22+
foregroundColor: "Text color",
23+
backgroundColor: "Background color",
24+
fontSize: "Font size",
25+
paddingTop: "Top padding",
26+
paddingBottom: "Bottom padding",
27+
paddingX: "Horizontal padding",
28+
};
29+
30+
/**
31+
* What a marketing email's managed footer shows in editors and previews,
32+
* where there's no real address or links yet. Pass the broadcast's or
33+
* sequence's `viewInBrowser` setting: off leaves the link out, as it is
34+
* from the emails that get sent.
35+
*/
36+
export function marketingPreviewContext({
37+
mailingAddress = "Your mailing address",
38+
viewInBrowser = true,
39+
}: {
40+
mailingAddress?: string;
41+
viewInBrowser?: boolean;
42+
} = {}): SendLitEmailRenderContext {
43+
return {
44+
footer: {
45+
mailingAddress,
46+
unsubscribeUrl: "#unsubscribe-preview",
47+
...(viewInBrowser && { viewInBrowserUrl: "#view-in-browser-preview" }),
48+
},
49+
};
50+
}
51+
1152
export const DEFAULT_FOOTER_SETTINGS: FooterSettings = {
1253
alignment: "center",
1354
fontSize: "12px",
@@ -28,8 +69,8 @@ export function createFooterEmailBlock(
2869
}
2970

3071
export function createFooterBlock({
31-
labels,
32-
}: FooterBlockConfig): BlockComponent<FooterSettings, SendLitEmailRenderContext> {
72+
labels = DEFAULT_FOOTER_LABELS,
73+
}: FooterBlockConfig = {}): BlockComponent<FooterSettings, SendLitEmailRenderContext> {
3374
return {
3475
metadata: {
3576
name: "footer",

‎packages/email-blocks/src/footer/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ export interface FooterLabels {
3939
}
4040

4141
export interface FooterBlockConfig {
42-
labels: FooterLabels;
42+
/** Defaults to `DEFAULT_FOOTER_LABELS` (English). */
43+
labels?: FooterLabels;
4344
}

0 commit comments

Comments
 (0)