-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathApp.tsx
144 lines (126 loc) · 4.59 KB
/
App.tsx
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
import type { VNode } from "preact";
import { useRef, useState } from "preact/hooks";
import { api, setSecret } from "./api";
import { useApiCall } from "./hooks";
import { BodyEditor, MailBodyEditorRef } from "./components";
import IconCheck from "~icons/mdi/check-bold";
import IconAlert from "~icons/mdi/alert";
import IconSettings from "~icons/mdi/gear";
import IconGithub from "~icons/mdi/github";
import IconEmail from "~icons/mdi/email";
import config from "../config.json";
type AppProps = {
section?: string
slots?: {
navLinks?: VNode
footerSide?: VNode
notice?: VNode
}
}
export default function App({ slots, section = "Compose" }: AppProps) {
const [refBody, setRefBody] = useState<MailBodyEditorRef>();
const [senderMode, setSenderMode] = useState<"preset" | "manual">("preset");
const hasPresetSenders = (config?.senders?.length || 0) > 0;
const isCustomSender = senderMode === "manual" || !hasPresetSenders;
const [localError, setLocalError] = useState("");
const { isLoading, isSuccess, isError, errorMessage, call: send } = useApiCall(api.sendEmail);
const onSubmit = (ev: Event) => {
setLocalError("");
ev.preventDefault();
ev.stopPropagation();
if (!refBody) return;
const { text, html } = refBody.getBody() || {};
if (!text && !html) {
setLocalError("Missing Body");
return;
}
const {
secret,
from,
to,
subject,
} = Object.fromEntries(new FormData(ev.target as any)) as Record<string, string>; // force string as no file is present
setSecret(secret);
send({
from,
to,
subject,
bodyText: text,
bodyHtml: html,
});
};
return (
<>
<nav id="navbar">
<span>
<a href="/" class="title"><IconEmail class="icon" /> EmailFlare</a>
<span>{"/"}</span>
<span class="visible-lg">{section}</span>
</span>
<span class="spacer" />
{slots?.navLinks || (
<span>
<a class="icon-link" href={BUILDER} target="_blank"><IconSettings class="icon" /></a>
<a class="icon-link" href={GITHUB} target="_blank"><IconGithub class="icon" /></a>
</span>
)}
</nav>
<main style={{ pointerEvents: isLoading ? "none" : undefined }}>
{slots?.notice}
<form id="form-compose" method="POST" action="/api/send-email" onSubmit={onSubmit}>
<div class="field">
<label for="secret">Auth</label>
<input id="secret" type="password" name="secret" required />
</div>
<div class="field">
<label for="from">From</label>
{isCustomSender &&
<input id="from" name="from" type="text" required />
}
{!isCustomSender &&
<select id="from" name="from" required>
{config?.senders?.map(x => <option value={x}>{x}</option>)}
</select>
}
{config.customSender && hasPresetSenders &&
<button type="button" class="btn action-btn btn-primary" onClick={() => setSenderMode(v => v === "manual" ? "preset" : "manual")}>
{isCustomSender ? "Presets" : "Manual"}
</button>
}
</div>
<div class="field">
<label for="to">To</label>
<input id="to" type="text" name="to" required />
</div>
<div class="field">
<label for="subject">Subject</label>
<input id="subject" type="subject" name="subject" required />
</div>
<div class="field-body">
<BodyEditor accessor={setRefBody} />
</div>
<div class="form-footer">
{isLoading ? <span class="loader" /> : null}
{isSuccess ? <p class="status success"><IconCheck /> Sent!</p> : null}
{(isError || localError) ? <p class="status error"><IconAlert /> {errorMessage || localError}</p> : null}
<div class="form-buttons">
<button class="btn btn-secondary" type="reset">Reset</button>
<button class="btn btn-primary" type="submit">Send</button>
</div>
</div>
</form>
</main>
<footer id="footer">
<span>
<span>v{import.meta.env.VITE_APP_VERSION}</span>
<span>|</span>
<a href={GITHUB + "/tree/master/CHANGELOG.md"} target="_black">Changelog</a>
</span>
<span class="spacer" />
{slots?.footerSide}
</footer>
</>
);
}
const GITHUB = "https://github.com/giuseppelt/emailflare";
const BUILDER = "https://emailflare.breakp.dev/builder";