-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathstep-sidebar.tsx
More file actions
371 lines (337 loc) · 13.7 KB
/
Copy pathstep-sidebar.tsx
File metadata and controls
371 lines (337 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import '@xyflow/react/dist/style.css';
import React, {useEffect, useRef, useState} from 'react';
import {AutomationDetail, AutomationSendEmailAction, AutomationWaitAction} from '@tryghost/admin-x-framework/api/automations';
import {Button, Field, FieldError, FieldLabel, Input, InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput, InputGroupText} from '@tryghost/shade/components';
import {LucideIcon, cn, formatNumber} from '@tryghost/shade/utils';
import {MemberTier, StepSidebarDetail} from '../types';
import {TRIGGER_CANVAS_ID} from './nodes';
import {formatWait} from './format-wait';
const MAX_WAIT_DAYS = 30;
const WHOLE_NUMBER_PATTERN = /^\d+$/;
const getValidWaitDays = (value: string): number | null => {
const days = Number(value);
if (!WHOLE_NUMBER_PATTERN.test(value) || !Number.isInteger(days) || days < 1 || days > MAX_WAIT_DAYS) {
return null;
}
return days;
};
const SidebarField: React.FC<{label: string; children: React.ReactNode; htmlFor?: string}> = ({children, htmlFor, label}) => (
<Field>
<FieldLabel className='text-sm font-medium text-text-secondary' htmlFor={htmlFor}>
{label}
</FieldLabel>
{children}
</Field>
);
// Public beta limitation: only one trigger type exists, so the read-only Select
// in the sidebar header would just mirror the title above it.
// const ReadOnlySelect: React.FC<{value: string}> = ({value}) => (
// <Select value={value}>
// <SelectTrigger disabled>
// <span>{value}</span>
// </SelectTrigger>
// </Select>
// );
const DeleteStepButton: React.FC<{onClick: () => void}> = ({onClick}) => (
<Button
className='w-full border-destructive text-destructive hover:bg-destructive hover:text-destructive-foreground'
type='button'
variant='outline'
onClick={onClick}
>
<LucideIcon.Trash2 className='size-4' />
Delete step
</Button>
);
const MEMBER_TIER_LABELS: Record<MemberTier, string> = {
free: 'Free',
paid: 'Paid'
};
const TriggerSidebarBody: React.FC<{memberTiers: MemberTier[]}> = ({memberTiers}) => {
const selectedTiers = memberTiers.map(tier => MEMBER_TIER_LABELS[tier]).join(', ');
return (
<div className='flex flex-col gap-5'>
{/* Public beta limitation: only one trigger type exists, so the read-only Select
in the sidebar header would just mirror the title above it.
<SidebarField label='Trigger'>
<ReadOnlySelect value='New member sign up' />
</SidebarField> */}
{selectedTiers && (
<div className='flex flex-col'>
<span className='text-sm font-medium text-text-secondary'>Members</span>
<span className='text-base text-foreground'>{selectedTiers}</span>
</div>
)}
</div>
);
};
const WaitSidebarBody: React.FC<{
action: AutomationWaitAction;
onUpdate: (waitHours: number) => void;
onDelete: () => void;
}> = ({action, onUpdate, onDelete}) => {
if (action.data.wait_hours % 24 !== 0) {
throw new Error(`WaitSidebarBody: wait_hours must be a multiple of 24, received ${action.data.wait_hours}`);
}
const initialDays = action.data.wait_hours / 24;
const [daysText, setDaysText] = useState<string>(String(initialDays));
const [hasBlurredDaysInput, setHasBlurredDaysInput] = useState(false);
const days = Number(daysText);
const isValid = getValidWaitDays(daysText) !== null;
const showValidationError = hasBlurredDaysInput && !isValid;
const updateWaitDays = (nextDays: number) => {
const nextHours = nextDays * 24;
if (nextHours !== action.data.wait_hours) {
onUpdate(nextHours);
}
};
const stepWaitDays = (direction: -1 | 1) => {
const currentDays = getValidWaitDays(daysText);
if (currentDays === null) {
return;
}
const nextDays = Math.min(MAX_WAIT_DAYS, Math.max(1, currentDays + direction));
setDaysText(String(nextDays));
updateWaitDays(nextDays);
};
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const nextDaysText = event.target.value;
setDaysText(nextDaysText);
const nextDays = getValidWaitDays(nextDaysText);
if (nextDays === null) {
return;
}
updateWaitDays(nextDays);
};
return (
<div className='flex flex-1 flex-col gap-5'>
<SidebarField htmlFor='automation-wait-days' label='Wait for'>
<InputGroup
aria-label='Wait duration in days'
className='h-(--control-height)'
data-disabled={showValidationError ? 'true' : undefined}
>
<InputGroupInput
aria-describedby={showValidationError ? 'automation-wait-days-error' : undefined}
aria-invalid={showValidationError}
className='w-10 min-w-10 flex-none pr-1 font-mono tabular-nums'
id='automation-wait-days'
inputMode='numeric'
value={daysText}
onBlur={() => setHasBlurredDaysInput(true)}
onChange={handleChange}
onFocus={() => setHasBlurredDaysInput(false)}
/>
<InputGroupText className='mr-auto'>{days === 1 ? 'day' : 'days'}</InputGroupText>
<InputGroupAddon align='inline-end' className='gap-0.5 pr-2'>
<InputGroupButton
aria-label='Decrease wait by one day'
disabled={!isValid || days <= 1}
size='icon-xs'
title='Decrease wait by one day'
onClick={() => stepWaitDays(-1)}
>
<LucideIcon.Minus className='size-4' />
</InputGroupButton>
<InputGroupButton
aria-label='Increase wait by one day'
disabled={!isValid || days >= MAX_WAIT_DAYS}
size='icon-xs'
title='Increase wait by one day'
onClick={() => stepWaitDays(1)}
>
<LucideIcon.Plus className='size-4' />
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
{showValidationError && (
<FieldError className='text-xs' id='automation-wait-days-error'>
Enter a delay between 1 and {formatNumber(MAX_WAIT_DAYS)} days
</FieldError>
)}
</SidebarField>
<div className='mt-auto pt-6'>
<DeleteStepButton onClick={onDelete} />
</div>
</div>
);
};
const SendEmailSidebarBody: React.FC<{
action: AutomationSendEmailAction;
onUpdateSubject: (subject: string) => void;
onEditEmail: () => void;
onDelete: () => void;
}> = ({action, onUpdateSubject, onEditEmail, onDelete}) => {
const subjectInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
subjectInputRef.current?.focus({preventScroll: true});
}, [action.id]);
return (
<div className='flex flex-1 flex-col gap-5'>
<SidebarField label='Subject line'>
<Input
ref={subjectInputRef}
placeholder='Subject line'
value={action.data.email_subject}
onChange={e => onUpdateSubject(e.target.value)}
/>
</SidebarField>
<Button
className='w-full'
type='button'
variant='outline'
onClick={onEditEmail}
>
<LucideIcon.Pencil className='size-4' />
Edit email
</Button>
<div className='mt-auto pt-6'>
<DeleteStepButton onClick={onDelete} />
</div>
</div>
);
};
const StepSidebarBody: React.FC<{detail: StepSidebarDetail}> = ({detail}) => {
switch (detail.type) {
case 'trigger':
return <TriggerSidebarBody memberTiers={detail.memberTiers} />;
case 'wait':
return <WaitSidebarBody key={detail.action.id} action={detail.action} onDelete={detail.onDelete} onUpdate={detail.onUpdate} />;
case 'send_email':
return <SendEmailSidebarBody key={detail.action.id} action={detail.action} onDelete={detail.onDelete} onEditEmail={detail.onEditEmail} onUpdateSubject={detail.onUpdateSubject} />;
default: {
const _exhaustive: never = detail;
throw new Error(`Unknown sidebar type: ${_exhaustive}`);
}
}
};
const StepSidebarContent: React.FC<{detail: StepSidebarDetail}> = ({detail}) => {
const Icon = detail.icon;
return (
<div className='flex min-h-full flex-col gap-6'>
<div className='flex items-start gap-4'>
<div className='flex min-w-0 items-center gap-3'>
<div className='flex size-8 shrink-0 items-center justify-center rounded-md bg-muted text-text-secondary'>
<Icon className='size-4' />
</div>
<div className='min-w-0'>
<span className='block text-sm text-text-secondary'>{detail.label}</span>
<h2 className={cn('truncate text-base leading-tight font-medium text-foreground', detail.isPlaceholderTitle && 'opacity-50')}>{detail.title}</h2>
</div>
</div>
</div>
<StepSidebarBody detail={detail} />
</div>
);
};
const automationSlugMemberTiers: Record<string, MemberTier[]> = {
'member-welcome-email-free': ['free'],
'member-welcome-email-paid': ['paid']
};
type StepSidebarDetailOptions = {
automation: AutomationDetail;
onDelete: (actionId: string) => void;
onUpdateWait: (actionId: string, waitHours: number) => void;
onUpdateSubject: (actionId: string, subject: string) => void;
onEditEmail: (actionId: string) => void;
stepId: string | null;
};
const getStepSidebarDetail = ({automation, stepId, onDelete, onUpdateWait, onUpdateSubject, onEditEmail}: StepSidebarDetailOptions): StepSidebarDetail | null => {
if (!stepId) {
return null;
}
if (stepId === TRIGGER_CANVAS_ID) {
return {
icon: LucideIcon.Zap,
label: 'Trigger',
title: 'Member signs up',
memberTiers: automationSlugMemberTiers[automation.slug] ?? [],
type: 'trigger'
};
}
const action = automation.actions.find(item => item.id === stepId);
if (!action) {
return null;
}
switch (action.type) {
case 'wait': {
const waitValue = formatWait(action.data.wait_hours);
return {
icon: LucideIcon.Clock,
label: 'Wait',
title: waitValue,
action,
onDelete: () => onDelete(action.id),
onUpdate: (waitHours: number) => onUpdateWait(action.id, waitHours),
type: 'wait'
};
}
case 'send_email':
return {
icon: LucideIcon.Mail,
label: 'Send email',
isPlaceholderTitle: !action.data.email_subject,
title: action.data.email_subject || 'Untitled',
action,
onDelete: () => onDelete(action.id),
onUpdateSubject: (subject: string) => onUpdateSubject(action.id, subject),
onEditEmail: () => onEditEmail(action.id),
type: 'send_email'
};
default: {
const _exhaustive: never = action;
throw new Error(`Unknown automation action type: ${_exhaustive}`);
}
}
};
type StepSidebarProps = {
automation: AutomationDetail
stepId: string | null
onDelete: (actionId: string) => void;
onUpdateWait: (actionId: string, waitHours: number) => void;
onUpdateSubject: (actionId: string, subject: string) => void;
onEditEmail: (actionId: string) => void;
isEmailModalOpen: boolean
onClose: () => void
}
export const StepSidebar: React.FC<StepSidebarProps> = ({automation, stepId, onDelete, onUpdateWait, onUpdateSubject, onEditEmail, isEmailModalOpen, onClose}) => {
const detail = getStepSidebarDetail({
automation,
stepId,
onDelete,
onUpdateWait,
onUpdateSubject,
onEditEmail
});
useEffect(() => {
if (!detail) {
return;
}
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
if (isEmailModalOpen) {
return;
}
onClose();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [detail, isEmailModalOpen, onClose]);
return (
<aside
aria-hidden={!detail}
aria-label='Step details'
className={cn(
'absolute inset-y-0 right-0 z-[1] flex w-[calc(100%-6rem)] max-w-none translate-x-full flex-col gap-6 overflow-y-auto bg-surface-elevated p-6 shadow-sm transition-transform duration-200 ease-out sm:w-[36rem] dark:border-l dark:border-gray-950',
detail ? 'translate-x-0' : 'pointer-events-none'
)}
data-state={detail ? 'open' : 'closed'}
data-testid='automation-step-sidebar'
>
{detail && <StepSidebarContent detail={detail} />}
</aside>
);
};