@startsimpli/integrations 0.2.49 → 0.2.51
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 +3 -3
- package/src/comms/__tests__/announcement-composer.test.tsx +56 -0
- package/src/comms/__tests__/customer-emails-page.test.tsx +49 -5
- package/src/comms/__tests__/email-viewer.test.tsx +233 -0
- package/src/comms/__tests__/fixtures.ts +1 -0
- package/src/comms/__tests__/recipient-local-send.test.tsx +358 -0
- package/src/comms/components/AnnouncementComposer.tsx +51 -1
- package/src/comms/components/DefaultTimeZoneSetting.tsx +67 -0
- package/src/comms/components/DraftReview.tsx +12 -9
- package/src/comms/components/EmailPreview.tsx +155 -12
- package/src/comms/components/EmailPreviewPage.tsx +161 -0
- package/src/comms/components/SchedulePicker.tsx +90 -18
- package/src/comms/components/ScheduledSends.tsx +98 -10
- package/src/comms/components/SendLog.tsx +4 -2
- package/src/comms/components/SequenceSettings.tsx +3 -7
- package/src/comms/components/_shared.tsx +8 -0
- package/src/comms/components/index.ts +2 -0
- package/src/comms/components/pages/CustomerEmailsPage.tsx +20 -1
- package/src/comms/hooks/index.ts +29 -2
- package/src/comms/types/index.ts +62 -3
|
@@ -1,28 +1,171 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* The email viewer: ONE page-sized view of an email the way a recipient meets it, not a panel
|
|
3
|
+
* inside a list inside a fold. A back link and a slim toolbar on one line, the inbox row
|
|
4
|
+
* (sender, subject, preheader) as a mail client lists it, then the email in a single frame at
|
|
5
|
+
* its full height, so the page scrolls and the frame does not.
|
|
6
|
+
*
|
|
7
|
+
* It covers the viewport (a portal-free `fixed` layer), so it reads the same whether a host
|
|
8
|
+
* routes to it or the Customer emails page opens it in place. The html is the SERVER's render
|
|
9
|
+
* through the brand wrapper, shown in a sandboxed <iframe srcDoc>, never injected into the admin
|
|
10
|
+
* page (email CSS would leak, and DOMPurify would strip it). The frame runs no scripts; it is
|
|
11
|
+
* same-origin only so the page can read its height. Viewing never writes anything.
|
|
5
12
|
*/
|
|
6
|
-
import type
|
|
13
|
+
import { useCallback, useEffect, useRef, useState, type ReactElement } from 'react';
|
|
14
|
+
import { Button } from '@startsimpli/ui';
|
|
15
|
+
|
|
16
|
+
import type { Sender } from '../hooks/index';
|
|
7
17
|
import type { RenderedEmail } from '../types/index';
|
|
8
18
|
|
|
9
19
|
export interface EmailPreviewProps {
|
|
10
20
|
email: RenderedEmail;
|
|
21
|
+
sender: Sender;
|
|
22
|
+
/** Where the back link goes (and what it is called). */
|
|
23
|
+
backHref?: string;
|
|
24
|
+
backLabel?: string;
|
|
25
|
+
/** Handle Back in place (client navigation, or closing an overlay) instead of following the href. */
|
|
26
|
+
onBack?: () => void;
|
|
11
27
|
title?: string;
|
|
12
28
|
}
|
|
13
29
|
|
|
14
|
-
|
|
30
|
+
type Width = 'desktop' | 'mobile';
|
|
31
|
+
type Part = 'email' | 'text';
|
|
32
|
+
|
|
33
|
+
const WIDTH_PX: Record<Width, number> = { desktop: 700, mobile: 375 };
|
|
34
|
+
|
|
35
|
+
function Toggle<T extends string>({ label, value, options, onChange }: {
|
|
36
|
+
label: string;
|
|
37
|
+
value: T;
|
|
38
|
+
options: readonly (readonly [T, string])[];
|
|
39
|
+
onChange: (v: T) => void;
|
|
40
|
+
}) {
|
|
41
|
+
return (
|
|
42
|
+
<div role="group" aria-label={label} className="flex gap-1">
|
|
43
|
+
{options.map(([v, text]) => (
|
|
44
|
+
<Button
|
|
45
|
+
key={v}
|
|
46
|
+
type="button"
|
|
47
|
+
size="sm"
|
|
48
|
+
variant={value === v ? 'default' : 'outline'}
|
|
49
|
+
aria-pressed={value === v}
|
|
50
|
+
onClick={() => onChange(v)}
|
|
51
|
+
>
|
|
52
|
+
{text}
|
|
53
|
+
</Button>
|
|
54
|
+
))}
|
|
55
|
+
</div>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The email at `width`, as tall as its content: the page scrolls, not the frame. */
|
|
60
|
+
function EmailFrame({ html, width, title }: { html: string; width: number; title: string }) {
|
|
61
|
+
const ref = useRef<HTMLIFrameElement>(null);
|
|
62
|
+
const observer = useRef<ResizeObserver | null>(null);
|
|
63
|
+
const [height, setHeight] = useState(600);
|
|
64
|
+
|
|
65
|
+
const measure = useCallback(() => {
|
|
66
|
+
const root = ref.current?.contentDocument?.documentElement;
|
|
67
|
+
if (!root) return;
|
|
68
|
+
const h = Math.ceil(root.getBoundingClientRect().height);
|
|
69
|
+
if (h > 0) setHeight(h);
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
const onLoad = useCallback(() => {
|
|
73
|
+
measure();
|
|
74
|
+
const root = ref.current?.contentDocument?.documentElement;
|
|
75
|
+
observer.current?.disconnect();
|
|
76
|
+
if (root && typeof ResizeObserver !== 'undefined') {
|
|
77
|
+
observer.current = new ResizeObserver(measure);
|
|
78
|
+
observer.current.observe(root);
|
|
79
|
+
}
|
|
80
|
+
}, [measure]);
|
|
81
|
+
|
|
82
|
+
useEffect(() => () => observer.current?.disconnect(), []);
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<iframe
|
|
86
|
+
ref={ref}
|
|
87
|
+
title={title}
|
|
88
|
+
// No allow-scripts: nothing in the email runs. allow-same-origin only lets us read its height.
|
|
89
|
+
sandbox="allow-same-origin"
|
|
90
|
+
scrolling="no"
|
|
91
|
+
srcDoc={html}
|
|
92
|
+
onLoad={onLoad}
|
|
93
|
+
style={{ width, maxWidth: '100%', height }}
|
|
94
|
+
className="block border-0 bg-white"
|
|
95
|
+
/>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function EmailPreview({
|
|
100
|
+
email,
|
|
101
|
+
sender,
|
|
102
|
+
backHref = '#',
|
|
103
|
+
backLabel = 'Back',
|
|
104
|
+
onBack,
|
|
105
|
+
title = 'Email preview',
|
|
106
|
+
}: EmailPreviewProps): ReactElement {
|
|
107
|
+
const [width, setWidth] = useState<Width>('desktop');
|
|
108
|
+
const [part, setPart] = useState<Part>('email');
|
|
109
|
+
const px = WIDTH_PX[width];
|
|
110
|
+
const device = width === 'mobile' ? 'rounded-[28px] border-8 border-neutral-800' : 'shadow-sm';
|
|
111
|
+
|
|
15
112
|
return (
|
|
16
|
-
<div className="
|
|
17
|
-
<div className="
|
|
18
|
-
<
|
|
19
|
-
|
|
113
|
+
<div data-email-viewer className="fixed inset-0 z-50 overflow-y-auto bg-neutral-100 text-neutral-900 dark:bg-neutral-900 dark:text-neutral-100">
|
|
114
|
+
<div className="mx-auto flex max-w-4xl flex-wrap items-center justify-between gap-x-4 gap-y-2 px-4 py-3">
|
|
115
|
+
<a
|
|
116
|
+
href={backHref}
|
|
117
|
+
className="text-sm text-neutral-600 hover:text-neutral-900 hover:underline dark:text-neutral-400 dark:hover:text-neutral-100"
|
|
118
|
+
onClick={(e) => {
|
|
119
|
+
if (!onBack) return;
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
onBack();
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
{`← ${backLabel}`}
|
|
125
|
+
</a>
|
|
126
|
+
<div className="flex flex-wrap items-center gap-3">
|
|
127
|
+
<Toggle
|
|
128
|
+
label="Screen width"
|
|
129
|
+
value={width}
|
|
130
|
+
options={[['desktop', 'Desktop'], ['mobile', 'Mobile']] as const}
|
|
131
|
+
onChange={setWidth}
|
|
132
|
+
/>
|
|
133
|
+
<Toggle
|
|
134
|
+
label="Email or plain text"
|
|
135
|
+
value={part}
|
|
136
|
+
options={[['email', 'Email'], ['text', 'Plain text']] as const}
|
|
137
|
+
onChange={setPart}
|
|
138
|
+
/>
|
|
139
|
+
</div>
|
|
140
|
+
</div>
|
|
141
|
+
|
|
142
|
+
{/* The inbox row, as a mail client lists it: sender, then the subject with the preheader greyed after it. */}
|
|
143
|
+
<div
|
|
144
|
+
role="group"
|
|
145
|
+
aria-label="Inbox row"
|
|
146
|
+
className="mx-auto flex max-w-4xl items-baseline gap-4 bg-white px-4 py-3 text-sm text-neutral-900 dark:bg-neutral-800 dark:text-neutral-100"
|
|
147
|
+
>
|
|
148
|
+
<span className="w-40 shrink-0 truncate font-semibold">{sender.name}</span>
|
|
149
|
+
<span className="min-w-0 flex-1 truncate">
|
|
20
150
|
<span className="font-medium">{email.subject}</span>
|
|
151
|
+
{email.preheader ? <span className="text-neutral-500">{` - ${email.preheader}`}</span> : null}
|
|
152
|
+
</span>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
<div className="px-4 py-8">
|
|
156
|
+
<div className={`mx-auto w-fit max-w-full overflow-hidden bg-white ${device}`}>
|
|
157
|
+
{part === 'email' ? (
|
|
158
|
+
<EmailFrame html={email.html} width={px} title={title} />
|
|
159
|
+
) : (
|
|
160
|
+
<pre
|
|
161
|
+
style={{ width: px, maxWidth: '100%' }}
|
|
162
|
+
className="whitespace-pre-wrap break-words p-6 font-mono text-sm text-neutral-900"
|
|
163
|
+
>
|
|
164
|
+
{email.text}
|
|
165
|
+
</pre>
|
|
166
|
+
)}
|
|
21
167
|
</div>
|
|
22
|
-
{email.preheader ? <div className="text-muted-foreground">{email.preheader}</div> : null}
|
|
23
168
|
</div>
|
|
24
|
-
{/* sandbox="" : no scripts, no same-origin, no navigation of the admin page. */}
|
|
25
|
-
<iframe title={title} sandbox="" srcDoc={email.html} className="h-[480px] w-full bg-white" />
|
|
26
169
|
</div>
|
|
27
170
|
);
|
|
28
171
|
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The viewer, wired to the comms API: fetches the SERVER's render of a draft or of a comeback step
|
|
3
|
+
* and who it is from, and shows it in the one-page viewer. Reading only; nothing here approves,
|
|
4
|
+
* sends or writes.
|
|
5
|
+
*
|
|
6
|
+
* `itemId` is what a URL carries: a draft id, or `step.<programKey>.<index>` for a comeback step.
|
|
7
|
+
* Hosts that can route mount this at a page of their own; hosts that cannot get the same viewer
|
|
8
|
+
* from <PreviewAsInbox> opening it in place.
|
|
9
|
+
*/
|
|
10
|
+
import { createContext, useContext, useEffect, useState, type ReactElement, type ReactNode } from 'react';
|
|
11
|
+
import { Button } from '@startsimpli/ui';
|
|
12
|
+
|
|
13
|
+
import type { CommsApi } from '../api/comms';
|
|
14
|
+
import { useDraftPreview, useSender, useStepPreview } from '../hooks/index';
|
|
15
|
+
import { EmailPreview } from './EmailPreview';
|
|
16
|
+
import { ErrorNotice, Note } from './_shared';
|
|
17
|
+
|
|
18
|
+
export type PreviewSource =
|
|
19
|
+
| { kind: 'draft'; id: string }
|
|
20
|
+
| { kind: 'step'; programKey: string; index: number };
|
|
21
|
+
|
|
22
|
+
export function previewItemId(source: PreviewSource): string {
|
|
23
|
+
return source.kind === 'draft' ? source.id : `step.${source.programKey}.${source.index}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function parsePreviewItemId(itemId: string): PreviewSource {
|
|
27
|
+
const m = itemId.match(/^step\.([^.]+)\.(\d+)$/);
|
|
28
|
+
return m ? { kind: 'step', programKey: m[1], index: Number(m[2]) } : { kind: 'draft', id: itemId };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface EmailPreviewPageProps {
|
|
32
|
+
api: CommsApi;
|
|
33
|
+
itemId: string;
|
|
34
|
+
/** Shown as the sender when the foundry has none configured: the foundry's own name. */
|
|
35
|
+
fallbackSenderName: string;
|
|
36
|
+
backHref?: string;
|
|
37
|
+
backLabel?: string;
|
|
38
|
+
onBack?: () => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function EmailPreviewPage({
|
|
42
|
+
api,
|
|
43
|
+
itemId,
|
|
44
|
+
fallbackSenderName,
|
|
45
|
+
backHref = '#',
|
|
46
|
+
backLabel = 'Back to Customer emails',
|
|
47
|
+
onBack,
|
|
48
|
+
}: EmailPreviewPageProps): ReactElement {
|
|
49
|
+
const source = parsePreviewItemId(itemId);
|
|
50
|
+
const draft = useDraftPreview(api, source.kind === 'draft' ? source.id : '', source.kind === 'draft');
|
|
51
|
+
const step = useStepPreview(
|
|
52
|
+
api,
|
|
53
|
+
source.kind === 'step' ? source.programKey : '',
|
|
54
|
+
source.kind === 'step' ? source.index : 0,
|
|
55
|
+
source.kind === 'step',
|
|
56
|
+
);
|
|
57
|
+
const sender = useSender(api, true, fallbackSenderName);
|
|
58
|
+
const query = source.kind === 'draft' ? draft : step;
|
|
59
|
+
|
|
60
|
+
if (query.data) {
|
|
61
|
+
return <EmailPreview email={query.data} sender={sender} backHref={backHref} backLabel={backLabel} onBack={onBack} />;
|
|
62
|
+
}
|
|
63
|
+
return (
|
|
64
|
+
<div data-email-viewer className="fixed inset-0 z-50 overflow-y-auto bg-neutral-100 px-4 py-3 dark:bg-neutral-900">
|
|
65
|
+
<a
|
|
66
|
+
href={backHref}
|
|
67
|
+
className="text-sm text-neutral-600 hover:underline dark:text-neutral-400"
|
|
68
|
+
onClick={(e) => {
|
|
69
|
+
if (!onBack) return;
|
|
70
|
+
e.preventDefault();
|
|
71
|
+
onBack();
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
{`← ${backLabel}`}
|
|
75
|
+
</a>
|
|
76
|
+
<div className="mx-auto max-w-4xl py-8">
|
|
77
|
+
{query.error ? <ErrorNotice error={query.error} /> : <Note>Loading…</Note>}
|
|
78
|
+
</div>
|
|
79
|
+
</div>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ---- launch points ------------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
interface ViewerLinks {
|
|
86
|
+
/** Where the viewer lives for an item id, when the host routes to it. */
|
|
87
|
+
href?: (itemId: string) => string;
|
|
88
|
+
/** Follow that href in the host (client navigation) instead of a full page load. */
|
|
89
|
+
open?: (href: string) => void;
|
|
90
|
+
/** Where Back goes in the host. */
|
|
91
|
+
foundryName: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const ViewerLinksContext = createContext<ViewerLinks>({ foundryName: '' });
|
|
95
|
+
|
|
96
|
+
export function ViewerLinksProvider({ value, children }: { value: ViewerLinks; children: ReactNode }): ReactElement {
|
|
97
|
+
return <ViewerLinksContext.Provider value={value}>{children}</ViewerLinksContext.Provider>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* "Preview as inbox" / "Preview": a link to the host's viewer page when it has one, otherwise a
|
|
102
|
+
* button that opens the same viewer over the page and closes with Back.
|
|
103
|
+
*/
|
|
104
|
+
export function PreviewAsInbox({
|
|
105
|
+
api,
|
|
106
|
+
source,
|
|
107
|
+
label,
|
|
108
|
+
autoOpen = false,
|
|
109
|
+
}: {
|
|
110
|
+
api: CommsApi;
|
|
111
|
+
source: PreviewSource;
|
|
112
|
+
label: string;
|
|
113
|
+
/** Open the viewer by itself (a draft just loaded from a file). */
|
|
114
|
+
autoOpen?: boolean;
|
|
115
|
+
}): ReactElement {
|
|
116
|
+
const { href, open, foundryName } = useContext(ViewerLinksContext);
|
|
117
|
+
const [shown, setShown] = useState(false);
|
|
118
|
+
const itemId = previewItemId(source);
|
|
119
|
+
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!autoOpen) return;
|
|
122
|
+
if (!href) return setShown(true);
|
|
123
|
+
const to = href(itemId);
|
|
124
|
+
if (open) open(to);
|
|
125
|
+
else window.location.assign(to);
|
|
126
|
+
// Only when asked to open, not when the host's callbacks change identity.
|
|
127
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
|
+
}, [autoOpen]);
|
|
129
|
+
|
|
130
|
+
if (href) {
|
|
131
|
+
const to = href(itemId);
|
|
132
|
+
return (
|
|
133
|
+
<Button asChild size="sm" variant="outline">
|
|
134
|
+
<a
|
|
135
|
+
href={to}
|
|
136
|
+
onClick={(e) => {
|
|
137
|
+
if (!open || e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return;
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
open(to);
|
|
140
|
+
}}
|
|
141
|
+
>
|
|
142
|
+
{label}
|
|
143
|
+
</a>
|
|
144
|
+
</Button>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return (
|
|
148
|
+
<>
|
|
149
|
+
<Button type="button" size="sm" variant="outline" onClick={() => setShown(true)}>{label}</Button>
|
|
150
|
+
{shown ? (
|
|
151
|
+
<EmailPreviewPage
|
|
152
|
+
api={api}
|
|
153
|
+
itemId={itemId}
|
|
154
|
+
fallbackSenderName={foundryName || api.slug}
|
|
155
|
+
backLabel="Back"
|
|
156
|
+
onBack={() => setShown(false)}
|
|
157
|
+
/>
|
|
158
|
+
) : null}
|
|
159
|
+
</>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -1,51 +1,123 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pick when an approved draft goes out
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Pick when an approved draft goes out. Two ways:
|
|
3
|
+
* - "At this exact time": a local date and time plus the time zone it is meant
|
|
4
|
+
* in (default: the viewer's). The server stores the instant in UTC.
|
|
5
|
+
* - "At this time in each recipient's own time zone": one wall-clock time that
|
|
6
|
+
* every recipient gets on their own clock (9:00 for everyone, wherever they
|
|
7
|
+
* are). Shows how many recipients have no known zone and which fallback
|
|
8
|
+
* applies to them (the foundry's default time zone, else UTC).
|
|
5
9
|
*/
|
|
6
10
|
import { useState, type ReactElement } from 'react';
|
|
7
11
|
import { Button, Input, Label } from '@startsimpli/ui';
|
|
8
12
|
|
|
9
|
-
import type {
|
|
13
|
+
import type { CommsApi } from '../api/comms';
|
|
14
|
+
import { useCommsSettings, useDraftAudience } from '../hooks/index';
|
|
15
|
+
import type { ScheduleInput, SendMode } from '../types/index';
|
|
10
16
|
import { NativeSelect, localTimeZone, timeZoneOptions, useFieldId } from './_shared';
|
|
11
17
|
|
|
12
18
|
export interface SchedulePickerProps {
|
|
19
|
+
api: CommsApi;
|
|
20
|
+
/** The draft being scheduled: its audience says how many recipients have no known zone. */
|
|
21
|
+
draftId: string;
|
|
13
22
|
submitLabel: string;
|
|
14
23
|
pending?: boolean;
|
|
24
|
+
/** Start on the per-recipient mode (rescheduling a recipient-local draft). */
|
|
25
|
+
initialMode?: SendMode;
|
|
26
|
+
/** Hide the exact-time choice: a rolling recipient-local draft can only take a local time. */
|
|
27
|
+
localOnly?: boolean;
|
|
15
28
|
onSubmit: (schedule: ScheduleInput) => void;
|
|
16
29
|
onCancel: () => void;
|
|
17
30
|
}
|
|
18
31
|
|
|
19
|
-
|
|
32
|
+
/** Who has no zone of their own, and what they get instead. */
|
|
33
|
+
function ZoneNotice({ api, draftId }: { api: CommsApi; draftId: string }): ReactElement | null {
|
|
34
|
+
const audience = useDraftAudience(api, draftId, true).data?.preview;
|
|
35
|
+
const settings = useCommsSettings(api).data;
|
|
36
|
+
if (audience?.no_timezone === undefined) return null;
|
|
37
|
+
const n = audience.no_timezone;
|
|
38
|
+
if (n === 0) {
|
|
39
|
+
return <p className="basis-full text-xs text-muted-foreground">Every recipient has a known time zone.</p>;
|
|
40
|
+
}
|
|
41
|
+
const fallback = settings
|
|
42
|
+
? settings.default_timezone
|
|
43
|
+
? `They get ${settings.default_timezone}, your default time zone.`
|
|
44
|
+
: 'No default time zone is set, so they get UTC.'
|
|
45
|
+
: '';
|
|
46
|
+
return (
|
|
47
|
+
<p className="basis-full text-xs text-muted-foreground">
|
|
48
|
+
{`${n} ${n === 1 ? 'recipient has' : 'recipients have'} no known time zone. ${fallback}`.trim()}
|
|
49
|
+
</p>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function SchedulePicker({
|
|
54
|
+
api,
|
|
55
|
+
draftId,
|
|
56
|
+
submitLabel,
|
|
57
|
+
pending,
|
|
58
|
+
initialMode = 'fixed',
|
|
59
|
+
localOnly,
|
|
60
|
+
onSubmit,
|
|
61
|
+
onCancel,
|
|
62
|
+
}: SchedulePickerProps): ReactElement {
|
|
63
|
+
const [mode, setMode] = useState<SendMode>(localOnly ? 'recipient_local' : initialMode);
|
|
20
64
|
const [when, setWhen] = useState('');
|
|
21
65
|
const [zone, setZone] = useState(localTimeZone);
|
|
22
66
|
const whenId = useFieldId('comms-send-at');
|
|
23
67
|
const zoneId = useFieldId('comms-send-zone');
|
|
68
|
+
const modeName = useFieldId('comms-send-mode');
|
|
69
|
+
const local = mode === 'recipient_local';
|
|
24
70
|
return (
|
|
25
71
|
<form
|
|
26
72
|
className="mt-2 flex flex-wrap items-end gap-3 rounded-md border p-3"
|
|
27
73
|
onSubmit={(e) => {
|
|
28
74
|
e.preventDefault();
|
|
29
|
-
if (when)
|
|
75
|
+
if (!when) return;
|
|
76
|
+
onSubmit(local ? { send_at: when, send_mode: 'recipient_local' } : { send_at: when, timezone: zone });
|
|
30
77
|
}}
|
|
31
78
|
>
|
|
79
|
+
{localOnly ? null : (
|
|
80
|
+
<fieldset className="basis-full space-y-1">
|
|
81
|
+
<legend className="sr-only">When it goes out</legend>
|
|
82
|
+
<label className="flex items-center gap-2 text-sm">
|
|
83
|
+
<input type="radio" name={modeName} checked={!local} onChange={() => setMode('fixed')} />
|
|
84
|
+
At this exact time
|
|
85
|
+
</label>
|
|
86
|
+
<label className="flex items-center gap-2 text-sm">
|
|
87
|
+
<input type="radio" name={modeName} checked={local} onChange={() => setMode('recipient_local')} />
|
|
88
|
+
At this time in each recipient's own time zone
|
|
89
|
+
</label>
|
|
90
|
+
</fieldset>
|
|
91
|
+
)}
|
|
32
92
|
<div className="space-y-1">
|
|
33
93
|
<Label htmlFor={whenId}>Send at</Label>
|
|
34
94
|
<Input id={whenId} type="datetime-local" required value={when} onChange={(e) => setWhen(e.target.value)} />
|
|
35
95
|
</div>
|
|
36
|
-
|
|
37
|
-
<
|
|
38
|
-
|
|
39
|
-
<
|
|
40
|
-
{
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
96
|
+
{local ? null : (
|
|
97
|
+
<div className="space-y-1">
|
|
98
|
+
<Label htmlFor={zoneId}>Time zone</Label>
|
|
99
|
+
<div>
|
|
100
|
+
<NativeSelect id={zoneId} value={zone} onChange={(e) => setZone(e.target.value)}>
|
|
101
|
+
{timeZoneOptions().map((z) => (
|
|
102
|
+
<option key={z} value={z}>{z}</option>
|
|
103
|
+
))}
|
|
104
|
+
</NativeSelect>
|
|
105
|
+
</div>
|
|
44
106
|
</div>
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
107
|
+
)}
|
|
108
|
+
{local ? (
|
|
109
|
+
<>
|
|
110
|
+
<p className="basis-full text-xs text-muted-foreground">
|
|
111
|
+
Goes out at that time on each recipient's own clock, starting with the earliest zones. A worldwide
|
|
112
|
+
audience takes up to 26 hours to finish. Zones where that time has passed send immediately.
|
|
113
|
+
</p>
|
|
114
|
+
<ZoneNotice api={api} draftId={draftId} />
|
|
115
|
+
</>
|
|
116
|
+
) : (
|
|
117
|
+
<p className="basis-full text-xs text-muted-foreground">
|
|
118
|
+
A time that has already passed sends immediately.
|
|
119
|
+
</p>
|
|
120
|
+
)}
|
|
49
121
|
<Button type="submit" size="sm" disabled={pending || !when}>{submitLabel}</Button>
|
|
50
122
|
<Button type="button" size="sm" variant="ghost" onClick={onCancel}>Close</Button>
|
|
51
123
|
</form>
|
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
* Approved drafts waiting for their send time: when they go out, and the three
|
|
3
3
|
* things you can still do before then (reschedule, send now, cancel). Once the
|
|
4
4
|
* send starts the server refuses cancel and reschedule (409), shown as-is.
|
|
5
|
+
*
|
|
6
|
+
* A recipient-local draft ("9:00 in each recipient's own time zone") is listed
|
|
7
|
+
* too, and once its first zone has gone out it is "rolling": a per-zone progress
|
|
8
|
+
* table, and cancel / reschedule act on the zones that have not started only.
|
|
5
9
|
*/
|
|
6
10
|
import { useState, type ReactElement } from 'react';
|
|
7
11
|
import { Button } from '@startsimpli/ui';
|
|
@@ -15,9 +19,9 @@ import {
|
|
|
15
19
|
useDrafts,
|
|
16
20
|
useRescheduleDraft,
|
|
17
21
|
} from '../hooks/index';
|
|
18
|
-
import type { Draft } from '../types/index';
|
|
22
|
+
import type { Draft, DraftGroup } from '../types/index';
|
|
19
23
|
import { SchedulePicker } from './SchedulePicker';
|
|
20
|
-
import { ErrorNotice, formatDateTime } from './_shared';
|
|
24
|
+
import { ErrorNotice, Note, formatDateTime, formatLocalTime } from './_shared';
|
|
21
25
|
|
|
22
26
|
export interface ScheduledSendsProps {
|
|
23
27
|
api: CommsApi;
|
|
@@ -26,12 +30,58 @@ export interface ScheduledSendsProps {
|
|
|
26
30
|
pageSize?: number;
|
|
27
31
|
}
|
|
28
32
|
|
|
33
|
+
function groupCell(g: DraftGroup): string {
|
|
34
|
+
if (g.state === 'waiting') return 'Waiting';
|
|
35
|
+
if (g.state === 'sending') return g.late ? 'Sending (sent late: its time had already passed)' : 'Sending';
|
|
36
|
+
if (g.state === 'sent') return g.late ? 'Sent late (its time had already passed)' : 'Sent';
|
|
37
|
+
if (g.state === 'failed') return `Failed${g.failure ? `: ${g.failure.reason}` : ''}`;
|
|
38
|
+
if (g.state === 'expired') return `Expired: ${g.reason ?? 'comms were paused at its time'}`;
|
|
39
|
+
return 'Cancelled';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function GroupProgress({ groups }: { groups: DraftGroup[] }) {
|
|
43
|
+
return (
|
|
44
|
+
<table className="mt-2 w-full text-left text-sm" aria-label="Progress by time zone">
|
|
45
|
+
<thead>
|
|
46
|
+
<tr className="text-muted-foreground">
|
|
47
|
+
<th className="py-1 pr-3 font-medium">Time zone</th>
|
|
48
|
+
<th className="py-1 pr-3 font-medium">Goes out</th>
|
|
49
|
+
<th className="py-1 pr-3 font-medium">State</th>
|
|
50
|
+
<th className="py-1 font-medium">Sent</th>
|
|
51
|
+
</tr>
|
|
52
|
+
</thead>
|
|
53
|
+
<tbody>
|
|
54
|
+
{groups.map((g) => (
|
|
55
|
+
<tr key={g.zone} className="border-t">
|
|
56
|
+
<td className="py-1 pr-3">{g.zone}</td>
|
|
57
|
+
<td className="py-1 pr-3">
|
|
58
|
+
{formatDateTime(g.at, g.zone)}
|
|
59
|
+
{g.note === 'gap'
|
|
60
|
+
? ' (a clock change skips this time here, so it goes out at the first moment after the change)'
|
|
61
|
+
: g.note === 'overlap'
|
|
62
|
+
? ' (this time happens twice here; the first is used)'
|
|
63
|
+
: ''}
|
|
64
|
+
</td>
|
|
65
|
+
<td className="py-1 pr-3">{groupCell(g)}</td>
|
|
66
|
+
<td className="py-1">{`${g.sent} of ${g.count}`}</td>
|
|
67
|
+
</tr>
|
|
68
|
+
))}
|
|
69
|
+
</tbody>
|
|
70
|
+
</table>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
29
74
|
function ScheduledItem({ api, draft }: { api: CommsApi; draft: Draft }) {
|
|
30
75
|
const [moving, setMoving] = useState(false);
|
|
76
|
+
const [confirmingNow, setConfirmingNow] = useState(false);
|
|
31
77
|
const reschedule = useRescheduleDraft(api);
|
|
32
78
|
const cancel = useCancelDraft(api);
|
|
33
79
|
const sendNow = useApproveDraft(api);
|
|
34
80
|
const zone = draft.send_timezone || undefined;
|
|
81
|
+
const perZone = draft.send_mode === 'recipient_local';
|
|
82
|
+
const isRolling = draft.state === 'rolling' || draft.approval === 'rolling';
|
|
83
|
+
const groups = draft.groups ?? [];
|
|
84
|
+
const started = groups.filter((g) => g.state !== 'waiting').length;
|
|
35
85
|
const live = useDraftAudience(api, draft.id, true).data?.preview.count;
|
|
36
86
|
const frozen = draft.recipient_count;
|
|
37
87
|
const count =
|
|
@@ -45,25 +95,60 @@ function ScheduledItem({ api, draft }: { api: CommsApi; draft: Draft }) {
|
|
|
45
95
|
<div className="flex flex-wrap items-center gap-x-4 gap-y-2">
|
|
46
96
|
<span className="min-w-[10rem] flex-1 font-medium">{draft.subject || '(no subject)'}</span>
|
|
47
97
|
<span className="text-sm text-muted-foreground">
|
|
48
|
-
{
|
|
49
|
-
|
|
98
|
+
{perZone
|
|
99
|
+
? `Sends ${formatLocalTime(draft.local_time)} in each recipient's own time zone`
|
|
100
|
+
: `Sends ${formatDateTime(draft.send_at, zone)}`}
|
|
101
|
+
{perZone && !isRolling ? ` (first zone ${formatDateTime(draft.send_at)})` : ''}
|
|
102
|
+
{!perZone && zone && zone !== 'UTC' ? ` (${formatDateTime(draft.send_at, 'UTC')})` : ''}
|
|
50
103
|
</span>
|
|
51
104
|
<div className="flex flex-wrap gap-2">
|
|
52
105
|
<Button type="button" size="sm" variant="outline" onClick={() => setMoving((v) => !v)}>Reschedule</Button>
|
|
53
|
-
<Button type="button" size="sm" variant="outline" disabled={sendNow.isPending} onClick={() => sendNow.mutate(draft.id)}>
|
|
106
|
+
<Button type="button" size="sm" variant="outline" disabled={sendNow.isPending} onClick={() => (isRolling ? setConfirmingNow(true) : sendNow.mutate(draft.id))}>
|
|
54
107
|
Send now
|
|
55
108
|
</Button>
|
|
56
109
|
<Button type="button" size="sm" variant="ghost" disabled={cancel.isPending} onClick={() => cancel.mutate(draft.id)}>
|
|
57
|
-
Cancel schedule
|
|
110
|
+
{isRolling ? 'Cancel remaining zones' : 'Cancel schedule'}
|
|
58
111
|
</Button>
|
|
59
112
|
</div>
|
|
60
113
|
</div>
|
|
61
114
|
{count ? <p className="text-sm text-muted-foreground">{count}</p> : null}
|
|
115
|
+
{perZone ? (
|
|
116
|
+
<p className="text-sm text-muted-foreground">
|
|
117
|
+
People who join after the first zone starts are not included.
|
|
118
|
+
</p>
|
|
119
|
+
) : null}
|
|
120
|
+
{isRolling && confirmingNow ? (
|
|
121
|
+
<div className="flex flex-wrap items-center gap-2 text-sm">
|
|
122
|
+
<span>Send now starts every waiting time zone immediately.</span>
|
|
123
|
+
<Button
|
|
124
|
+
type="button"
|
|
125
|
+
size="sm"
|
|
126
|
+
disabled={sendNow.isPending}
|
|
127
|
+
onClick={() => sendNow.mutate(draft.id, { onSuccess: () => setConfirmingNow(false) })}
|
|
128
|
+
>
|
|
129
|
+
Yes, start every waiting zone
|
|
130
|
+
</Button>
|
|
131
|
+
<Button type="button" size="sm" variant="ghost" onClick={() => setConfirmingNow(false)}>Keep waiting</Button>
|
|
132
|
+
</div>
|
|
133
|
+
) : null}
|
|
134
|
+
{isRolling ? (
|
|
135
|
+
<>
|
|
136
|
+
<p className="text-sm text-muted-foreground">
|
|
137
|
+
{`${started} of ${groups.length} time zones started. Cancel stops the zones that have not started; zones already sent cannot be recalled.`}
|
|
138
|
+
</p>
|
|
139
|
+
{groups.length ? <GroupProgress groups={groups} /> : null}
|
|
140
|
+
{draft.notice ? <Note>{draft.notice.reason}</Note> : null}
|
|
141
|
+
</>
|
|
142
|
+
) : null}
|
|
62
143
|
{draft.dispatch_error ? (
|
|
63
144
|
<p className="text-sm text-muted-foreground">{`Trying again: ${draft.dispatch_error}`}</p>
|
|
64
145
|
) : null}
|
|
65
146
|
{moving ? (
|
|
66
147
|
<SchedulePicker
|
|
148
|
+
api={api}
|
|
149
|
+
draftId={draft.id}
|
|
150
|
+
initialMode={perZone ? 'recipient_local' : 'fixed'}
|
|
151
|
+
localOnly={isRolling}
|
|
67
152
|
submitLabel="Move send"
|
|
68
153
|
pending={reschedule.isPending}
|
|
69
154
|
onCancel={() => setMoving(false)}
|
|
@@ -78,17 +163,20 @@ function ScheduledItem({ api, draft }: { api: CommsApi; draft: Draft }) {
|
|
|
78
163
|
export function ScheduledSends({ api, pollIntervalMs = DEFAULT_POLL_MS, pageSize = 10 }: ScheduledSendsProps): ReactElement | null {
|
|
79
164
|
const [page, setPage] = useState(1);
|
|
80
165
|
const drafts = useDrafts(api, { status: 'scheduled', page, page_size: pageSize }, pollIntervalMs);
|
|
81
|
-
|
|
82
|
-
|
|
166
|
+
const rolling = useDrafts(api, { status: 'rolling', page: 1, page_size: 50 }, pollIntervalMs);
|
|
167
|
+
const error = drafts.error ?? rolling.error;
|
|
168
|
+
if (error) return <ErrorNotice error={error} />;
|
|
169
|
+
const rows = [...(rolling.data?.results ?? []), ...(drafts.data?.results ?? [])];
|
|
170
|
+
if (!rows.length) return null;
|
|
83
171
|
return (
|
|
84
172
|
<div className="space-y-1">
|
|
85
173
|
<h3 className="text-sm font-medium">Scheduled to send</h3>
|
|
86
174
|
<ul className="divide-y">
|
|
87
|
-
{
|
|
175
|
+
{rows.map((d) => (
|
|
88
176
|
<ScheduledItem key={d.id} api={api} draft={d} />
|
|
89
177
|
))}
|
|
90
178
|
</ul>
|
|
91
|
-
{drafts.data.count > pageSize ? (
|
|
179
|
+
{drafts.data && drafts.data.count > pageSize ? (
|
|
92
180
|
<div className="flex items-center gap-2 text-sm">
|
|
93
181
|
<Button type="button" size="sm" variant="outline" disabled={!drafts.data.previous} onClick={() => setPage(page - 1)}>
|
|
94
182
|
Previous
|