-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailPreviewModal.svelte
More file actions
342 lines (309 loc) · 10.2 KB
/
Copy pathEmailPreviewModal.svelte
File metadata and controls
342 lines (309 loc) · 10.2 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
<!--
Staging-only preview of the notification email cryptify *would* send
to a recipient. On staging, cryptify runs with `staging_mode = true`
(see cryptify/src/email.rs::send_email) and logs the email instead of
dispatching via SMTP — which makes it tedious to grab a download link
for testing.
Source of truth for the rendering lives in cryptify, exposed via
`GET /staging/preview/<uuid>`. This component fetches that JSON and
drops the per-recipient HTML body into a sandboxed iframe via
`srcdoc`, so the email's own inline styles render in isolation. The
header bar (from / to / subject / reply-to + recipient switcher) is
the only locally-rendered chrome.
-->
<script lang="ts">
import { _ } from 'svelte-i18n'
import { onMount } from 'svelte'
import { FILEHOST_URL } from '$lib/env'
interface RenderedEmail {
recipient: string
subject: string
from: string
reply_to: string | null
html: string
text: string
}
interface PreviewResponse {
recipients: RenderedEmail[]
confirmation: RenderedEmail | null
}
interface Props {
uuid: string
onClose: () => void
}
let { uuid, onClose }: Props = $props()
type Status = 'loading' | 'ready' | 'empty' | 'error'
let status: Status = $state('loading')
let errorMessage = $state('')
let data = $state<PreviewResponse | null>(null)
/** Index into the flat list of [...recipients, confirmation?] */
let activeIdx = $state(0)
let allEmails = $derived.by<RenderedEmail[]>(() => {
if (!data) return []
return data.confirmation
? [...data.recipients, data.confirmation]
: data.recipients
})
let active = $derived(allEmails[activeIdx])
let isConfirmation = $derived(
!!data?.confirmation && activeIdx === allEmails.length - 1
)
onMount(async () => {
try {
const url = `${FILEHOST_URL.replace(
/\/$/,
''
)}/staging/preview/${encodeURIComponent(uuid)}`
const res = await fetch(url, { credentials: 'omit' })
if (!res.ok) {
status = 'error'
errorMessage = `${res.status} ${res.statusText}`
return
}
data = (await res.json()) as PreviewResponse
if (allEmails.length === 0) {
status = 'empty'
return
}
status = 'ready'
} catch (e) {
status = 'error'
errorMessage = e instanceof Error ? e.message : String(e)
}
})
function handleKey(e: KeyboardEvent) {
if (e.key === 'Escape') onClose()
}
</script>
<svelte:window onkeydown={handleKey} />
<div
class="backdrop"
role="dialog"
aria-modal="true"
aria-labelledby="email-preview-title"
>
<button
class="backdrop-close"
type="button"
aria-label={$_('filesharing.emailPreview.close')}
onclick={onClose}
></button>
<div class="modal">
<header class="modal-header">
<div>
<h2 id="email-preview-title">
{$_('filesharing.emailPreview.title')}
</h2>
<p class="modal-subtitle">
{$_('filesharing.emailPreview.subtitle')}
</p>
</div>
<button
class="close-btn"
type="button"
aria-label={$_('filesharing.emailPreview.close')}
onclick={onClose}>×</button
>
</header>
{#if status === 'loading'}
<div class="state">{$_('filesharing.emailPreview.loading')}</div>
{:else if status === 'error'}
<div class="state state-error">
<p>{$_('filesharing.emailPreview.error')}</p>
<p class="error-detail">{errorMessage}</p>
</div>
{:else if status === 'empty'}
<div class="state">{$_('filesharing.emailPreview.empty')}</div>
{:else if active}
<section class="meta">
<div class="meta-row">
<span class="meta-label"
>{$_('filesharing.emailPreview.from')}</span
>
<span class="meta-value">{active.from}</span>
</div>
{#if active.reply_to}
<div class="meta-row">
<span class="meta-label"
>{$_('filesharing.emailPreview.replyTo')}</span
>
<span class="meta-value">{active.reply_to}</span>
</div>
{/if}
<div class="meta-row">
<span class="meta-label"
>{$_('filesharing.emailPreview.subject')}</span
>
<span class="meta-value">{active.subject}</span>
</div>
<div class="meta-row meta-row-recipient">
<label class="meta-label" for="recipient-switcher"
>{$_('filesharing.emailPreview.to')}</label
>
{#if allEmails.length > 1}
<select
id="recipient-switcher"
class="recipient-select"
bind:value={activeIdx}
>
{#each allEmails as r, i (i)}
<option value={i}
>{r.recipient}{data?.confirmation &&
i === allEmails.length - 1
? ` (${$_(
'filesharing.emailPreview.confirmationTag'
)})`
: ''}</option
>
{/each}
</select>
{:else}
<span class="meta-value"
>{active.recipient}{isConfirmation
? ` (${$_(
'filesharing.emailPreview.confirmationTag'
)})`
: ''}</span
>
{/if}
</div>
</section>
<iframe
class="email-frame"
title={$_('filesharing.emailPreview.iframeTitle')}
srcdoc={active.html}
sandbox="allow-popups allow-popups-to-escape-sandbox"
></iframe>
{/if}
</div>
</div>
<style lang="scss">
.backdrop {
position: fixed;
inset: 0;
background: rgba(3, 14, 23, 0.55);
display: flex;
align-items: flex-start;
justify-content: center;
z-index: 1000;
padding: 2rem 1rem;
overflow-y: auto;
}
.backdrop-close {
position: absolute;
inset: 0;
background: transparent;
border: 0;
padding: 0;
cursor: pointer;
}
.modal {
position: relative;
background: var(--pg-general-background);
border-radius: var(--pg-border-radius-lg);
max-width: 720px;
width: 100%;
box-shadow: 0 16px 48px rgba(0, 0, 0, 0.3);
z-index: 1;
overflow: hidden;
display: flex;
flex-direction: column;
}
.modal-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 1rem;
padding: 1.25rem 1.5rem;
background: var(--pg-soft-background);
border-bottom: 1px solid var(--pg-strong-background);
}
.modal-header h2 {
margin: 0;
font-size: var(--pg-font-size-lg);
font-weight: var(--pg-font-weight-bold);
color: var(--pg-text);
}
.modal-subtitle {
margin: 0.25rem 0 0;
font-size: var(--pg-font-size-sm);
color: var(--pg-text-secondary);
font-family: var(--pg-font-family);
}
.close-btn {
background: transparent;
border: 0;
font-size: 1.75rem;
line-height: 1;
cursor: pointer;
color: var(--pg-text-secondary);
padding: 0 0.25rem;
}
.close-btn:hover {
color: var(--pg-text);
}
.close-btn:focus-visible,
.backdrop-close:focus-visible {
outline: 2px solid var(--pg-text);
outline-offset: 2px;
}
.meta {
display: flex;
flex-direction: column;
gap: 0.4rem;
padding: 1rem 1.5rem;
border-bottom: 1px solid var(--pg-strong-background);
font-family: var(--pg-font-family);
font-size: var(--pg-font-size-sm);
color: var(--pg-text);
}
.meta-row {
display: flex;
gap: 0.75rem;
align-items: baseline;
}
.meta-row-recipient {
align-items: center;
}
.meta-label {
flex: 0 0 5.5rem;
color: var(--pg-text-secondary);
font-weight: var(--pg-font-weight-bold);
}
.meta-value {
word-break: break-word;
}
.recipient-select {
flex: 1;
padding: 0.35rem 0.6rem;
border: 1px solid var(--pg-strong-background);
border-radius: var(--pg-border-radius-sm);
background: var(--pg-general-background);
color: var(--pg-text);
font-family: var(--pg-font-family);
font-size: var(--pg-font-size-sm);
}
.email-frame {
width: 100%;
height: min(70vh, 720px);
border: 0;
background: var(--pg-soft-background);
}
.state {
padding: 2rem 1.5rem;
text-align: center;
font-family: var(--pg-font-family);
font-size: var(--pg-font-size-md);
color: var(--pg-text-secondary);
}
.state-error {
color: var(--pg-text);
}
.error-detail {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: var(--pg-font-size-sm);
color: var(--pg-text-secondary);
margin: 0.5rem 0 0;
word-break: break-all;
}
</style>