@shware/workflow-ui 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shware/workflow-ui",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/ShwareHQ/shware-sdk.git"
@@ -40,7 +40,7 @@
40
40
  "@floating-ui/react": "^0.27.20",
41
41
  "@fontsource-variable/inter": "^5.3.0",
42
42
  "@react-email/render": "^2.1.1",
43
- "@shware/workflow": "1.0.0",
43
+ "@shware/workflow": "2.0.0",
44
44
  "@tailwindcss/vite": "^4.3.3",
45
45
  "@tanstack/react-query": "^5.104.3",
46
46
  "@tanstack/react-router": "^1.170.45",
@@ -1,5 +1,6 @@
1
1
  import { render } from '@react-email/render';
2
- import type { EmailTemplate } from '@shware/workflow';
2
+ import { type EmailConfig, type EmailTemplate, resolveEmail } from '@shware/workflow';
3
+ import { EmailProvider } from '@shware/workflow/email-context';
3
4
  import { useQuery } from '@tanstack/react-query';
4
5
  import { createElement } from 'react';
5
6
  import {
@@ -25,17 +26,37 @@ export type EmailPreviewResult =
25
26
  * again, and a refusal is this render's result rather than an error sitting
26
27
  * beside the last success. A definition parses its props first (see
27
28
  * prepareEmail); a component that throws is a failure too.
29
+ *
30
+ * The component renders inside the `EmailProvider` the Worker's renderer gives it, with the
31
+ * template resolved against the config's `email` the same way (`resolveEmail`): a layout's
32
+ * unsubscribe link is the one the email would be sent with. The resolved URL is in the key, so a
33
+ * changed config default renders again.
28
34
  */
29
- export function useEmailPreview(mod: EmailTemplate | undefined, key: string, props?: Props) {
35
+ export function useEmailPreview(
36
+ mod: EmailTemplate | undefined,
37
+ key: string,
38
+ config: EmailConfig,
39
+ props?: Props
40
+ ) {
41
+ const email = mod === undefined ? undefined : resolveEmail(mod, config);
30
42
  return useQuery({
31
- queryKey: ['email-preview', key, entryIdentity(mod), props ?? null],
43
+ queryKey: [
44
+ 'email-preview',
45
+ key,
46
+ entryIdentity(mod),
47
+ email?.unsubscribeUrl ?? null,
48
+ props ?? null,
49
+ ],
32
50
  queryFn: async (): Promise<EmailPreviewResult> => {
33
- if (mod === undefined) throw new Error(`no email registered for '${key}'`);
51
+ if (mod === undefined || email === undefined) {
52
+ throw new Error(`no email registered for '${key}'`);
53
+ }
34
54
  const prepared = prepareEmail(mod, props);
35
55
  if (!prepared.ok) return { status: 'invalid', failure: prepared.failure };
36
56
  const { component, props: parsed, subject } = prepared.email;
37
57
  try {
38
- return { status: 'ok', html: await render(createElement(component, parsed)), subject };
58
+ const element = createElement(EmailProvider, { email }, createElement(component, parsed));
59
+ return { status: 'ok', html: await render(element), subject };
39
60
  } catch (error) {
40
61
  return { status: 'invalid', failure: renderFailure(error) };
41
62
  }
@@ -184,6 +184,7 @@ export default {
184
184
  subject: 'Subject',
185
185
  from: 'From',
186
186
  replyTo: 'Reply to',
187
+ unsubscribeUrl: 'Unsubscribe link',
187
188
  sentBy: 'Sent by',
188
189
  rendering: 'Rendering…',
189
190
  contentTitle: 'Title',
@@ -212,9 +213,11 @@ export default {
212
213
  slackBlockUnsupported: 'A {{type}} block (not drawn in the preview)',
213
214
  },
214
215
  settings: {
215
- emailDefaults: 'Email envelope',
216
+ emailDefaults: 'Email',
216
217
  emailDefaultsHint:
217
- 'Every email is sent with these — emails.from / replyTo in workflow.config.ts.',
218
+ 'Shared by every email template — email in workflow.config.ts. Each template names its own sender, and its own unsubscribe link overrides this one.',
219
+ quickClick: 'Quick click',
220
+ quickClickValue: 'Within {{ms}} ms of delivery: a weak machine signal',
218
221
  title: 'Settings',
219
222
  language: 'Language',
220
223
  languageHint: 'Applies to the studio interface only.',
@@ -183,6 +183,7 @@ export default {
183
183
  subject: '主题',
184
184
  from: '发件人',
185
185
  replyTo: '回复至',
186
+ unsubscribeUrl: '退订链接',
186
187
  sentBy: '发送方',
187
188
  rendering: '渲染中…',
188
189
  contentTitle: '标题',
@@ -207,9 +208,11 @@ export default {
207
208
  slackBlockUnsupported: '{{type}} 块(预览中不绘制)',
208
209
  },
209
210
  settings: {
210
- emailDefaults: '邮件信封',
211
+ emailDefaults: '邮件',
211
212
  emailDefaultsHint:
212
- '所有邮件都用这里的发件人发送,对应 workflow.config.ts 的 emails.from / replyTo。',
213
+ '所有邮件模板共用,对应 workflow.config.ts 的 email。发件人由每个模板自己声明,模板自己的退订链接会覆盖这里的默认值。',
214
+ quickClick: '快速点击',
215
+ quickClickValue: '送达后 {{ms}} 毫秒内:较弱的机器信号',
213
216
  title: '设置',
214
217
  language: '语言',
215
218
  languageHint: '仅影响 studio 界面文案。',
@@ -10,7 +10,7 @@ import { ThemeProvider } from './integrations/theme/root-provider';
10
10
  import { routeTree } from './routes';
11
11
 
12
12
  /**
13
- * The studio owns the URL, so views are shareable: `/workflows/$name`,
13
+ * The studio owns the URL, so views are shareable: `/workflows/$id`,
14
14
  * `/emails/$key`, `/segments`. The exported components stay router-free, so a
15
15
  * host app that embeds them keeps ownership of its own routing.
16
16
  */
@@ -130,7 +130,7 @@ function EmailView() {
130
130
  /* Only email renders a document; every other channel's content is data. */
131
131
  const found = lookupTemplate(config, activeChannel, key);
132
132
  const selected = found?.channel === 'email' ? found.entry : undefined;
133
- const { data, error, isPending } = useEmailPreview(selected, key, previewProps);
133
+ const { data, error, isPending } = useEmailPreview(selected, key, config.email, previewProps);
134
134
 
135
135
  return (
136
136
  <div className="flex flex-1 flex-col">
@@ -169,7 +169,7 @@ function EmailView() {
169
169
  defaultScheme={studioScheme}
170
170
  selected={key}
171
171
  selectedChannel={activeChannel}
172
- emailDefaults={config.emailDefaults}
172
+ emailConfig={config.email}
173
173
  previewProps={previewProps}
174
174
  onPreviewPropsChange={(props) =>
175
175
  setOverride(props === undefined ? undefined : { id, props })
@@ -68,7 +68,10 @@ function Settings() {
68
68
  </div>
69
69
  </section>
70
70
 
71
- <EmailDefaults from={config.emailDefaults.from} replyTo={config.emailDefaults.replyTo} />
71
+ <EmailDefaults
72
+ unsubscribeUrl={config.email.unsubscribeUrl}
73
+ quickClickMs={config.email.tracking?.quickClickMs}
74
+ />
72
75
 
73
76
  <section
74
77
  className="border-border bg-card mt-4 max-w-xl rounded-2xl border p-5"
@@ -103,7 +103,7 @@ export const workflowsIndexRoute = createRoute({
103
103
 
104
104
  const TABS = [
105
105
  { to: '/workflows/$id', label: 'workflows.tabs.overview', exact: true },
106
- { to: '/workflows/$name/workflow', label: 'workflows.tabs.canvas', exact: true },
106
+ { to: '/workflows/$id/workflow', label: 'workflows.tabs.canvas', exact: true },
107
107
  ] as const;
108
108
 
109
109
  function WorkflowDetail() {
@@ -251,7 +251,7 @@ function CanvasTab() {
251
251
  const message = selected?.type === 'message' ? selected : undefined;
252
252
  const emailModule =
253
253
  message?.channel === 'email' ? lookup(config.emails, message.template) : undefined;
254
- const emailPreview = useEmailPreview(emailModule, message?.template ?? '');
254
+ const emailPreview = useEmailPreview(emailModule, message?.template ?? '', config.email);
255
255
  /* A push has no document: its notification is drawn into the OS banner. */
256
256
  const pushModule =
257
257
  message?.channel === 'push' ? lookup(config.pushes, message.template) : undefined;
@@ -2,20 +2,28 @@ import { useTranslation } from 'react-i18next';
2
2
  import { superellipse } from './corner-shape';
3
3
 
4
4
  /**
5
- * The envelope every email goes out with: `emails.from` / `replyTo` in
6
- * workflow.config.ts. Read-only — the config is where it changes; the studio
5
+ * What every email template shares: `email` in workflow.config.ts — the
6
+ * unsubscribe link of templates that name none of their own, and how opens
7
+ * and clicks are judged. Senders are each template's own (its detail page
8
+ * shows them). Read-only — the config is where it changes; the studio
7
9
  * refreshes when it does.
8
10
  */
9
11
  export interface EmailDefaultsProps {
10
- from?: string | undefined;
11
- replyTo?: string | undefined;
12
+ unsubscribeUrl?: string | undefined;
13
+ quickClickMs?: number | undefined;
12
14
  }
13
15
 
14
- export function EmailDefaults({ from, replyTo }: EmailDefaultsProps) {
16
+ /** The reporting classifier's default (`email.tracking.quickClickMs`). */
17
+ const QUICK_CLICK_MS = 1_000;
18
+
19
+ export function EmailDefaults({ unsubscribeUrl, quickClickMs }: EmailDefaultsProps) {
15
20
  const { t } = useTranslation();
16
21
  const rows = [
17
- { label: t('emails.from'), value: from },
18
- { label: t('emails.replyTo'), value: replyTo },
22
+ { label: t('emails.unsubscribeUrl'), value: unsubscribeUrl },
23
+ {
24
+ label: t('settings.quickClick'),
25
+ value: t('settings.quickClickValue', { ms: quickClickMs ?? QUICK_CLICK_MS }),
26
+ },
19
27
  ];
20
28
 
21
29
  return (
@@ -1,4 +1,12 @@
1
- import type { DiscordTemplate, EmailTemplate, PushTemplate, SlackTemplate } from '@shware/workflow';
1
+ import {
2
+ type DiscordTemplate,
3
+ type EmailConfig,
4
+ type EmailTemplate,
5
+ type PushTemplate,
6
+ type SlackTemplate,
7
+ formatEmailAddress,
8
+ resolveEmail,
9
+ } from '@shware/workflow';
2
10
  import { Minus, Monitor, Moon, Plus, Smartphone, Sun } from 'lucide-react';
3
11
  import { type ReactNode, useState } from 'react';
4
12
  import { useTranslation } from 'react-i18next';
@@ -56,8 +64,8 @@ export interface TemplatesPageProps {
56
64
  selectedChannel?: string | undefined;
57
65
  /** Rendered output for the selected template, produced by the caller. */
58
66
  preview: TemplatePreview;
59
- /** The project's envelope (`emails.from` / `replyTo`): every email is sent with it. */
60
- emailDefaults?: { from?: string; replyTo?: string };
67
+ /** The config's `email`: an email template's unsubscribe link falls back to its default. */
68
+ emailConfig?: EmailConfig;
61
69
  /**
62
70
  * In-memory props override for the selected template's preview; absent
63
71
  * renders the template's own `preview`. Never written anywhere.
@@ -242,7 +250,7 @@ export function TemplatesPage({
242
250
  selected,
243
251
  selectedChannel,
244
252
  preview,
245
- emailDefaults,
253
+ emailConfig,
246
254
  previewProps,
247
255
  onPreviewPropsChange,
248
256
  defaultScheme,
@@ -270,6 +278,8 @@ export function TemplatesPage({
270
278
  const isPush = active?.channel === 'push';
271
279
  const isEmail = active?.channel === 'email';
272
280
  const activeEmail = active !== undefined && isEmail ? emails[active.key] : undefined;
281
+ /* What this template is sent with: its own sender, the unsubscribe link resolved like a send's. */
282
+ const resolved = activeEmail === undefined ? undefined : resolveEmail(activeEmail, emailConfig);
273
283
  const activePush = active !== undefined && isPush ? pushes[active.key] : undefined;
274
284
  const isDiscord = active?.channel === 'discord';
275
285
  const isSlack = active?.channel === 'slack';
@@ -340,15 +350,18 @@ export function TemplatesPage({
340
350
  {((isEmail && activeEmail !== undefined) || push !== undefined) && (
341
351
  <div className="border-border bg-card border-b px-6 py-4">
342
352
  <dl className="grid grid-cols-[max-content_1fr] gap-x-8 gap-y-2.5 text-sm">
343
- {isEmail && activeEmail !== undefined && (
353
+ {isEmail && activeEmail !== undefined && resolved !== undefined && (
344
354
  <>
345
- <Field label={t('emails.from')}>
346
- {emailDefaults?.from ?? t('common.none')}
347
- </Field>
348
- {emailDefaults?.replyTo !== undefined && (
349
- <Field label={t('emails.replyTo')}>{emailDefaults.replyTo}</Field>
355
+ <Field label={t('emails.from')}>{formatEmailAddress(resolved.from)}</Field>
356
+ {resolved.replyTo !== undefined && (
357
+ <Field label={t('emails.replyTo')}>
358
+ {formatEmailAddress(resolved.replyTo)}
359
+ </Field>
350
360
  )}
351
361
  <Field label={t('emails.subject')}>{subject ?? activeEmail.subject}</Field>
362
+ <Field label={t('emails.unsubscribeUrl')}>
363
+ {resolved.unsubscribeUrl ?? t('common.none')}
364
+ </Field>
352
365
  </>
353
366
  )}
354
367
  {push !== undefined && (
package/src/config.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  DiscordTemplate,
3
+ EmailConfig,
3
4
  EmailTemplate,
4
5
  PushTemplate,
5
6
  SegmentIR,
@@ -280,8 +281,8 @@ export interface ResolvedStudioConfig {
280
281
  slacks: Record<string, SlackTemplate>;
281
282
  /** Compiled named segments. */
282
283
  segments: SegmentIR[];
283
- /** The project's envelope (`emails.from` / `replyTo`), formatted as `Name <email>`. */
284
- emailDefaults: { from?: string; replyTo?: string };
284
+ /** The config's `email`: the default unsubscribe link, and how opens and clicks are judged. */
285
+ email: EmailConfig;
285
286
  /** Set when the project failed to load or compile; the shell shows it instead of the views. */
286
287
  error?: StudioError;
287
288
  stats?: StatsSource;
@@ -1,9 +1,7 @@
1
1
  import {
2
2
  ConfigCompileError,
3
- type EmailAddress,
4
3
  type JourneyConfig,
5
4
  compileConfig,
6
- formatEmailAddress,
7
5
  isConfig,
8
6
  isDiscordTemplate,
9
7
  isEmailTemplate,
@@ -18,7 +16,7 @@ import type { ResolvedStudioConfig, StudioError } from '../config';
18
16
  *
19
17
  * workflow.config.ts default-exports `defineConfig({...})` from
20
18
  * `@shware/workflow`, and the studio reads exactly that: its workflows,
21
- * segments, actions, template registries, title and email envelope. It is
19
+ * segments, actions, template registries, title and what the email templates share. It is
22
20
  * compiled with `compileConfig`, the same compile the Worker deploys, so the
23
21
  * studio lists exactly what would ship.
24
22
  *
@@ -139,17 +137,13 @@ function errorOf(error: unknown): StudioError {
139
137
  };
140
138
  }
141
139
 
142
- function formatted(address: EmailAddress | undefined): string | undefined {
143
- return address === undefined ? undefined : formatEmailAddress(address);
144
- }
145
-
146
140
  function failed(error: unknown, title?: string): StudioSnapshot {
147
141
  return {
148
142
  payload: {
149
143
  ...(title !== undefined ? { title } : {}),
150
144
  workflows: {},
151
145
  segments: [],
152
- emailDefaults: {},
146
+ email: {},
153
147
  error: errorOf(error),
154
148
  },
155
149
  registries: { email: undefined, push: undefined, discord: undefined, slack: undefined },
@@ -188,13 +182,13 @@ export async function loadSnapshot(options: {
188
182
  return failed(error);
189
183
  }
190
184
 
191
- const from = formatted(config.emails?.from);
192
- const replyTo = formatted(config.emails?.replyTo);
185
+ const unsubscribeUrl = config.email?.unsubscribeUrl;
186
+ const quickClickMs = config.email?.tracking?.quickClickMs;
193
187
  const shared = {
194
188
  ...(config.title !== undefined ? { title: config.title } : {}),
195
- emailDefaults: {
196
- ...(from !== undefined ? { from } : {}),
197
- ...(replyTo !== undefined ? { replyTo } : {}),
189
+ email: {
190
+ ...(unsubscribeUrl !== undefined ? { unsubscribeUrl } : {}),
191
+ ...(quickClickMs !== undefined ? { tracking: { quickClickMs } } : {}),
198
192
  },
199
193
  };
200
194
 
package/src/server.ts CHANGED
@@ -476,8 +476,10 @@ export async function startStudio(options: StartOptions = {}): Promise<ViteDevSe
476
476
  ],
477
477
  },
478
478
  },
479
- // The app and the user's project each have their own react; one copy only
480
- resolve: { dedupe: ['react', 'react-dom'] },
479
+ // The app and the user's project each have their own react; one copy only. One
480
+ // @shware/workflow too: the preview's <EmailProvider> and the project's layout calling
481
+ // useEmailContext() must share the context object
482
+ resolve: { dedupe: ['react', 'react-dom', '@shware/workflow'] },
481
483
  /*
482
484
  * @react-email/render imports react-dom/server lazily, so the dep scan never
483
485
  * sees it: the first email preview made Vite discover it, re-optimize and