@brimsites/brimjs 0.0.0-stage → 1.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.
Files changed (238) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +22 -2
  3. package/bin/brim.js +5 -0
  4. package/dist/cli.js +3802 -0
  5. package/editor-dist/editor.css +1 -0
  6. package/editor-dist/editor.js +91 -0
  7. package/package.json +71 -3
  8. package/skills/brim-convert/SKILL.md +256 -0
  9. package/skills/brim-hosting/SKILL.md +221 -0
  10. package/src/astro-shim.d.ts +5 -0
  11. package/src/auth/accounts.ts +341 -0
  12. package/src/auth/password.ts +34 -0
  13. package/src/auth/permissions.ts +44 -0
  14. package/src/blocks/accordion/Accordion.astro +48 -0
  15. package/src/blocks/accordion/accordion.css +63 -0
  16. package/src/blocks/analytics/Analytics.astro +45 -0
  17. package/src/blocks/analytics/ConsentBanner.astro +41 -0
  18. package/src/blocks/analytics/analytics.client.ts +125 -0
  19. package/src/blocks/analytics/analytics.css +51 -0
  20. package/src/blocks/analytics/consent.ts +51 -0
  21. package/src/blocks/button/Button.astro +64 -0
  22. package/src/blocks/button/button.css +100 -0
  23. package/src/blocks/carousel/Carousel.astro +140 -0
  24. package/src/blocks/carousel/carousel.client.ts +153 -0
  25. package/src/blocks/carousel/carousel.css +181 -0
  26. package/src/blocks/data/icons.ts +285 -0
  27. package/src/blocks/data/image-shapes.ts +91 -0
  28. package/src/blocks/data/social-icons.ts +41 -0
  29. package/src/blocks/data/social-networks.ts +256 -0
  30. package/src/blocks/divider/Divider.astro +27 -0
  31. package/src/blocks/divider/divider.css +17 -0
  32. package/src/blocks/form/Captcha.astro +27 -0
  33. package/src/blocks/form/Form.astro +250 -0
  34. package/src/blocks/form/FormField.astro +138 -0
  35. package/src/blocks/form/Newsletter.astro +80 -0
  36. package/src/blocks/form/form.client.ts +339 -0
  37. package/src/blocks/form/form.css +271 -0
  38. package/src/blocks/gallery/ImageGallery.astro +126 -0
  39. package/src/blocks/gallery/gallery.css +43 -0
  40. package/src/blocks/gallery/lightbox.client.ts +178 -0
  41. package/src/blocks/gallery/lightbox.css +134 -0
  42. package/src/blocks/icon/Icon.astro +41 -0
  43. package/src/blocks/icon/icon.css +16 -0
  44. package/src/blocks/image/Image.astro +158 -0
  45. package/src/blocks/image/image.css +42 -0
  46. package/src/blocks/images.ts +64 -0
  47. package/src/blocks/in-editor.client.ts +8 -0
  48. package/src/blocks/listing/Listing.astro +288 -0
  49. package/src/blocks/listing/ListingCards.astro +48 -0
  50. package/src/blocks/listing/listing.css +136 -0
  51. package/src/blocks/listing/query.ts +363 -0
  52. package/src/blocks/map/Map.astro +75 -0
  53. package/src/blocks/map/map.css +32 -0
  54. package/src/blocks/schemas/accordion.ts +118 -0
  55. package/src/blocks/schemas/button.ts +78 -0
  56. package/src/blocks/schemas/carousel.ts +133 -0
  57. package/src/blocks/schemas/divider.ts +29 -0
  58. package/src/blocks/schemas/form-field-types.ts +29 -0
  59. package/src/blocks/schemas/form.ts +350 -0
  60. package/src/blocks/schemas/icon.ts +24 -0
  61. package/src/blocks/schemas/image-gallery.ts +54 -0
  62. package/src/blocks/schemas/image.ts +87 -0
  63. package/src/blocks/schemas/index.ts +33 -0
  64. package/src/blocks/schemas/listing.ts +14 -0
  65. package/src/blocks/schemas/map.ts +81 -0
  66. package/src/blocks/schemas/newsletter.ts +324 -0
  67. package/src/blocks/schemas/social-media.ts +348 -0
  68. package/src/blocks/schemas/text.ts +151 -0
  69. package/src/blocks/schemas/video.ts +57 -0
  70. package/src/blocks/search/Search.astro +147 -0
  71. package/src/blocks/search/search.css +69 -0
  72. package/src/blocks/search/state.ts +28 -0
  73. package/src/blocks/settings.ts +94 -0
  74. package/src/blocks/social/SocialMedia.astro +102 -0
  75. package/src/blocks/social/social-links.ts +16 -0
  76. package/src/blocks/social/social.css +31 -0
  77. package/src/blocks/theme.ts +35 -0
  78. package/src/blocks/types.ts +52 -0
  79. package/src/blocks/video/Video.astro +90 -0
  80. package/src/blocks/video/video-source.ts +77 -0
  81. package/src/blocks/video/video.client.ts +43 -0
  82. package/src/blocks/video/video.css +29 -0
  83. package/src/check.ts +1119 -0
  84. package/src/cli.ts +493 -0
  85. package/src/components/Blocks.astro +68 -0
  86. package/src/components/Field.astro +61 -0
  87. package/src/components/Footer.astro +36 -0
  88. package/src/components/Link.astro +18 -0
  89. package/src/components/MobileMenu.astro +77 -0
  90. package/src/components/Navbar.astro +42 -0
  91. package/src/components/RichText.astro +32 -0
  92. package/src/components/Seo.astro +202 -0
  93. package/src/components/Setting.astro +46 -0
  94. package/src/components/Shared.astro +17 -0
  95. package/src/components/Text.astro +91 -0
  96. package/src/components/blocks.css +86 -0
  97. package/src/components/index.ts +27 -0
  98. package/src/components/mobile-menu.client.ts +21 -0
  99. package/src/components/mobile-menu.css +52 -0
  100. package/src/components/text.css +48 -0
  101. package/src/config.ts +413 -0
  102. package/src/content/block-layout.ts +63 -0
  103. package/src/content/block-values.ts +48 -0
  104. package/src/content/content-check.ts +174 -0
  105. package/src/content/content-types.ts +408 -0
  106. package/src/content/drafts.ts +115 -0
  107. package/src/content/feed.ts +237 -0
  108. package/src/content/field-types.ts +107 -0
  109. package/src/content/history.ts +140 -0
  110. package/src/content/ids.ts +50 -0
  111. package/src/content/image-size.ts +212 -0
  112. package/src/content/image-sizes.ts +126 -0
  113. package/src/content/item-fields.ts +110 -0
  114. package/src/content/item-head.ts +126 -0
  115. package/src/content/item-image.ts +26 -0
  116. package/src/content/item-path.ts +122 -0
  117. package/src/content/items.ts +580 -0
  118. package/src/content/language-scopes.ts +40 -0
  119. package/src/content/media.ts +271 -0
  120. package/src/content/menus.ts +51 -0
  121. package/src/content/page-templates.ts +52 -0
  122. package/src/content/redirects.ts +112 -0
  123. package/src/content/references.ts +165 -0
  124. package/src/content/rich-text.ts +63 -0
  125. package/src/content/search.ts +167 -0
  126. package/src/content/seed.ts +229 -0
  127. package/src/content/site-settings.ts +111 -0
  128. package/src/content/sitemap.ts +143 -0
  129. package/src/content/store.ts +225 -0
  130. package/src/content/tags.ts +67 -0
  131. package/src/content/text-default.ts +139 -0
  132. package/src/content/types.ts +137 -0
  133. package/src/content/validate.ts +253 -0
  134. package/src/content/validation-error.ts +2 -0
  135. package/src/db/cached.ts +140 -0
  136. package/src/db/database.ts +15 -0
  137. package/src/db/libsql.ts +29 -0
  138. package/src/db/schema.ts +189 -0
  139. package/src/db/url.ts +44 -0
  140. package/src/editor/client/BlockSettings.tsx +1063 -0
  141. package/src/editor/client/Chrome.tsx +284 -0
  142. package/src/editor/client/ContentBrowser.tsx +1299 -0
  143. package/src/editor/client/Editor.tsx +1756 -0
  144. package/src/editor/client/HistoryPanel.tsx +114 -0
  145. package/src/editor/client/ImagesWidget.tsx +113 -0
  146. package/src/editor/client/Inspector.tsx +127 -0
  147. package/src/editor/client/ItemForm.tsx +535 -0
  148. package/src/editor/client/LinkWidget.tsx +440 -0
  149. package/src/editor/client/ListingPanel.tsx +99 -0
  150. package/src/editor/client/Login.tsx +338 -0
  151. package/src/editor/client/Menu.tsx +64 -0
  152. package/src/editor/client/PageSettings.tsx +270 -0
  153. package/src/editor/client/RedirectsSettings.tsx +130 -0
  154. package/src/editor/client/Resize.tsx +204 -0
  155. package/src/editor/client/Sidebar.tsx +51 -0
  156. package/src/editor/client/SiteMap.tsx +568 -0
  157. package/src/editor/client/SiteSettings.tsx +409 -0
  158. package/src/editor/client/TextTools.tsx +391 -0
  159. package/src/editor/client/Toolbar.tsx +449 -0
  160. package/src/editor/client/VideoUpload.tsx +251 -0
  161. package/src/editor/client/api.ts +417 -0
  162. package/src/editor/client/brand.ts +20 -0
  163. package/src/editor/client/connection.ts +98 -0
  164. package/src/editor/client/context.ts +19 -0
  165. package/src/editor/client/css.d.ts +2 -0
  166. package/src/editor/client/editor.css +4656 -0
  167. package/src/editor/client/fields.tsx +812 -0
  168. package/src/editor/client/icons.tsx +174 -0
  169. package/src/editor/client/keys.ts +15 -0
  170. package/src/editor/client/kinds.ts +289 -0
  171. package/src/editor/client/languages.ts +32 -0
  172. package/src/editor/client/main.tsx +100 -0
  173. package/src/editor/client/responses.ts +26 -0
  174. package/src/editor/client/rich-text.ts +326 -0
  175. package/src/editor/client/site-tree.ts +239 -0
  176. package/src/editor/client/theme.ts +55 -0
  177. package/src/editor/client/tsconfig.json +18 -0
  178. package/src/editor/client/tus.ts +137 -0
  179. package/src/editor/client/use-rect.ts +50 -0
  180. package/src/editor/client/widgets.tsx +1218 -0
  181. package/src/forms/captcha.ts +83 -0
  182. package/src/forms/define.ts +166 -0
  183. package/src/forms/fields.ts +197 -0
  184. package/src/forms/file-types.ts +108 -0
  185. package/src/forms/files.ts +216 -0
  186. package/src/forms/form-page.ts +9 -0
  187. package/src/forms/handler.ts +622 -0
  188. package/src/forms/spec.ts +89 -0
  189. package/src/forms/store.ts +96 -0
  190. package/src/forms/work.ts +50 -0
  191. package/src/icons/library.ts +144 -0
  192. package/src/icons/load.ts +63 -0
  193. package/src/index.ts +99 -0
  194. package/src/init.ts +257 -0
  195. package/src/integration.ts +469 -0
  196. package/src/mail/mailer.ts +197 -0
  197. package/src/mail/notify.ts +272 -0
  198. package/src/mail/settings.ts +173 -0
  199. package/src/mail/templates.ts +191 -0
  200. package/src/mcp/page-nodes.ts +209 -0
  201. package/src/mcp/server.ts +111 -0
  202. package/src/mcp/tools.ts +637 -0
  203. package/src/runtime/TemplatePage.astro +39 -0
  204. package/src/runtime/TemplatePageStatic.astro +13 -0
  205. package/src/runtime/admin-route.ts +80 -0
  206. package/src/runtime/connect.ts +53 -0
  207. package/src/runtime/content.ts +188 -0
  208. package/src/runtime/context.ts +131 -0
  209. package/src/runtime/form-files.ts +18 -0
  210. package/src/runtime/forms-route.ts +33 -0
  211. package/src/runtime/forms.ts +28 -0
  212. package/src/runtime/icons.ts +5 -0
  213. package/src/runtime/media.ts +175 -0
  214. package/src/runtime/middleware.ts +186 -0
  215. package/src/runtime/nav.ts +13 -0
  216. package/src/runtime/query-reads.ts +49 -0
  217. package/src/runtime/query-store.ts +16 -0
  218. package/src/runtime/request-state.ts +69 -0
  219. package/src/runtime/robots.ts +29 -0
  220. package/src/runtime/rss-static.ts +16 -0
  221. package/src/runtime/rss.ts +29 -0
  222. package/src/runtime/server.ts +29 -0
  223. package/src/runtime/sitemap.ts +34 -0
  224. package/src/runtime/template-paths.ts +34 -0
  225. package/src/runtime/uploads-route.ts +13 -0
  226. package/src/seed-cli.ts +253 -0
  227. package/src/server/auth.ts +31 -0
  228. package/src/server/fetch-image.ts +219 -0
  229. package/src/server/handler.ts +2106 -0
  230. package/src/server/http.ts +162 -0
  231. package/src/server/locales.ts +73 -0
  232. package/src/server/node.ts +57 -0
  233. package/src/server/query-params.ts +249 -0
  234. package/src/server/rate-limit.ts +72 -0
  235. package/src/server/video-host.ts +162 -0
  236. package/src/starter.ts +70 -0
  237. package/src/version.ts +4 -0
  238. package/src/virtual.d.ts +65 -0
@@ -0,0 +1,409 @@
1
+ import { type ReactNode, useEffect, useState } from "react";
2
+ import {
3
+ type ContentType,
4
+ type MailSettings,
5
+ type MailSettingsUpdate,
6
+ type SiteSettingsView,
7
+ type SmtpServerView,
8
+ api,
9
+ } from "./api.ts";
10
+ import { ItemField } from "./ItemForm.tsx";
11
+ import { CheckboxWidget, TextWidget } from "./fields.tsx";
12
+ import { RedirectsSettings } from "./RedirectsSettings.tsx";
13
+ import { type FormSummary, formsOf } from "./responses.ts";
14
+ import { Fieldset, Sidebar } from "./Sidebar.tsx";
15
+
16
+ const DELIVERY: Record<MailSettings["delivery"], string> = {
17
+ site: "Emails are sent from your own email server, set below.",
18
+ server: "Emails are sent by the site's server. Nothing to set up.",
19
+ service: "Emails are sent by your host. Nothing to set up.",
20
+ none: "Emails are not set up yet: messages are only kept in Content → Form responses. Add your own email server below to receive them by email.",
21
+ };
22
+
23
+ type ServerDraft = Omit<SmtpServerView, "hasPassword"> & { password: string }; // betterleaks:allow
24
+
25
+ const EMPTY_SERVER: ServerDraft = { host: "", port: 587, secure: false, user: "", password: "", from: "" }; // betterleaks:allow
26
+
27
+ /**
28
+ * Settings of the whole site. First the site's own (src/site-settings.ts: phone, email, social profiles…), which
29
+ * anyone who edits content changes; then, for its owners, who gets an email when a visitor writes through a form,
30
+ * the sender's name, the owner's own email server, and the site's redirects.
31
+ */
32
+ export function SiteSettings({
33
+ onClose,
34
+ canManage = true,
35
+ locale,
36
+ focus,
37
+ onSaved,
38
+ onOpenResponses,
39
+ }: {
40
+ onClose: () => void;
41
+ /** Opens a form's responses in the content browser (all of them without a name); absent for who cannot read them. */
42
+ onOpenResponses?: ((form?: string) => void) | undefined;
43
+ /** A setting clicked on the page: its section opens and it is brought into view. */
44
+ focus?: string | undefined;
45
+ /** A setting was saved: the page is drawn again to show it. */
46
+ onSaved?: (() => void) | undefined;
47
+ /** Owners also see the form emails and the redirects. */
48
+ canManage?: boolean;
49
+ /** The language whose settings are shown: the page's. */
50
+ locale?: string | undefined;
51
+ }) {
52
+ if (!canManage) {
53
+ return (
54
+ <Sidebar title="Site Settings" onClose={onClose}>
55
+ <OwnSettings locale={locale} focus={focus} onSaved={onSaved} />
56
+ {onOpenResponses && <FormsSection onOpen={onOpenResponses} />}
57
+ </Sidebar>
58
+ );
59
+ }
60
+ return (
61
+ <OwnerSettings
62
+ onClose={onClose}
63
+ locale={locale}
64
+ focus={focus}
65
+ onSaved={onSaved}
66
+ forms={onOpenResponses && <FormsSection onOpen={onOpenResponses} />}
67
+ />
68
+ );
69
+ }
70
+
71
+ /**
72
+ * The site's forms that visitors wrote through, each with how many messages and the newest's day: one opens its
73
+ * messages in the content browser, under Form responses.
74
+ */
75
+ function FormsSection({ onOpen }: { onOpen: (form?: string) => void }) {
76
+ const [forms, setForms] = useState<FormSummary[] | null>(null);
77
+ useEffect(() => {
78
+ api.responses().then(
79
+ ({ responses }) => setForms(formsOf(responses)),
80
+ () => setForms([]),
81
+ );
82
+ }, []);
83
+ const total = forms?.reduce((sum, form) => sum + form.count, 0) ?? 0;
84
+
85
+ return (
86
+ <Fieldset title="Forms">
87
+ {forms === null ? (
88
+ <p className="brim-sidebar-note">Loading…</p>
89
+ ) : forms.length === 0 ? (
90
+ <p className="brim-sidebar-note">No messages yet. What visitors send through the site's forms shows here.</p>
91
+ ) : (
92
+ <ul className="site-forms">
93
+ {forms.map((form) => (
94
+ <li key={form.title}>
95
+ <button type="button" className="site-forms__row" onClick={() => onOpen(form.title)}>
96
+ <span className="site-forms__title">{form.title}</span>
97
+ <span className="site-forms__meta">
98
+ {form.count} {form.count === 1 ? "message" : "messages"} · last{" "}
99
+ {new Date(form.latest).toLocaleDateString(undefined, { day: "numeric", month: "short" })}
100
+ </span>
101
+ </button>
102
+ </li>
103
+ ))}
104
+ </ul>
105
+ )}
106
+ <div className="field-group">
107
+ <button type="button" className="url-browse-btn" onClick={() => onOpen()}>
108
+ {total > 0 ? `All ${total} messages in Content` : "Open Form responses in Content"}
109
+ </button>
110
+ </div>
111
+ </Fieldset>
112
+ );
113
+ }
114
+
115
+ /** The roles whose users can be emailed every form message: those who may read the messages in the editor. */
116
+ const NOTIFIED_ROLES = [
117
+ {
118
+ role: "owner",
119
+ label: "Email the site's owners",
120
+ description: "Every owner of the site gets each message a visitor sends through a form.",
121
+ },
122
+ {
123
+ role: "editor",
124
+ label: "Email the site's editors",
125
+ description: "Every editor of the site gets each message too.",
126
+ },
127
+ ] as const;
128
+
129
+ /** A pseudo content type for the site's settings, which the item fields' widgets take. */
130
+ const SETTINGS_TYPE = {
131
+ key: "site_settings",
132
+ label: "Site settings",
133
+ singular: "Site settings",
134
+ slug: "site-settings",
135
+ fields: [],
136
+ hasPage: false,
137
+ } as ContentType;
138
+
139
+ /**
140
+ * The site's own settings, grouped as the site declared them, edited with the same widgets as an item's fields and
141
+ * saved as soon as they change. Nothing shows when the site declares none.
142
+ */
143
+ function OwnSettings({
144
+ locale,
145
+ focus,
146
+ onSaved,
147
+ onLoaded,
148
+ }: {
149
+ locale?: string | undefined;
150
+ focus?: string | undefined;
151
+ onSaved?: (() => void) | undefined;
152
+ /** Tells whether the site declares settings of its own, so the panel opens on them rather than form emails. */
153
+ onLoaded?: ((declares: boolean) => void) | undefined;
154
+ }) {
155
+ const [view, setView] = useState<SiteSettingsView | null>(null);
156
+ const [error, setError] = useState("");
157
+ useEffect(() => {
158
+ api.siteSettings(locale).then(
159
+ (loaded) => {
160
+ setView(loaded);
161
+ onLoaded?.(loaded.fields.length > 0);
162
+ },
163
+ (failure: unknown) => setError(failure instanceof Error ? failure.message : String(failure)),
164
+ );
165
+ // eslint-disable-next-line react-hooks/exhaustive-deps
166
+ }, [locale]);
167
+
168
+ // The setting clicked on the page comes into view once its section is drawn.
169
+ useEffect(() => {
170
+ if (!view || !focus) return;
171
+ document.getElementById(`site-setting-${focus}`)?.scrollIntoView({ block: "center" });
172
+ document
173
+ .querySelector<HTMLElement>(
174
+ `#site-setting-${CSS.escape(focus)} input, #site-setting-${CSS.escape(focus)} textarea`,
175
+ )
176
+ ?.focus();
177
+ }, [view, focus]);
178
+
179
+ if (error) return <p className="field-error">{error}</p>;
180
+ if (!view) return <p className="brim-sidebar-note">Loading…</p>;
181
+ if (view.fields.length === 0) return null;
182
+
183
+ async function change(name: string, value: unknown): Promise<void> {
184
+ setError("");
185
+ try {
186
+ setView(await api.saveSiteSettings({ [name]: value ?? null }, locale));
187
+ onSaved?.();
188
+ } catch (failure) {
189
+ setError(failure instanceof Error ? failure.message : String(failure));
190
+ }
191
+ }
192
+
193
+ const groups = [...new Set(view.fields.map((field) => field.group ?? "Site"))];
194
+ const focusGroup = view.fields.find((field) => field.name === focus)?.group ?? (focus ? "Site" : undefined);
195
+ return (
196
+ <>
197
+ {groups.map((group, index) => (
198
+ <Fieldset key={group} title={group} open={focusGroup ? group === focusGroup : index === 0}>
199
+ {view.fields
200
+ .filter((field) => (field.group ?? "Site") === group)
201
+ .map((field) => (
202
+ <div key={field.name} id={`site-setting-${field.name}`}>
203
+ <ItemField
204
+ type={SETTINGS_TYPE}
205
+ field={field}
206
+ value={view.values[field.name] ?? undefined}
207
+ onChange={(value) => void change(field.name, value)}
208
+ />
209
+ </div>
210
+ ))}
211
+ </Fieldset>
212
+ ))}
213
+ {error && <p className="field-error">{error}</p>}
214
+ </>
215
+ );
216
+ }
217
+
218
+ function OwnerSettings({
219
+ onClose,
220
+ locale,
221
+ focus,
222
+ onSaved,
223
+ forms,
224
+ }: {
225
+ onClose: () => void;
226
+ /** The site's forms and their messages, for who can read them. */
227
+ forms?: ReactNode;
228
+ locale?: string | undefined;
229
+ focus?: string | undefined;
230
+ onSaved?: (() => void) | undefined;
231
+ }) {
232
+ const [settings, setSettings] = useState<MailSettings | null>(null);
233
+ const [declares, setDeclares] = useState(false);
234
+ const [error, setError] = useState("");
235
+ const [note, setNote] = useState("");
236
+ const [ownServer, setOwnServer] = useState(false);
237
+ const [server, setServer] = useState<ServerDraft>(EMPTY_SERVER);
238
+ const [busy, setBusy] = useState(false);
239
+
240
+ useEffect(() => {
241
+ api.mail().then(
242
+ (loaded) => {
243
+ setSettings(loaded);
244
+ setOwnServer(loaded.smtp !== null);
245
+ if (loaded.smtp) setServer({ ...loaded.smtp, password: "" }); // betterleaks:allow
246
+ },
247
+ (failure: unknown) => setError(failure instanceof Error ? failure.message : String(failure)),
248
+ );
249
+ }, []);
250
+
251
+ async function save(change: Partial<MailSettingsUpdate>): Promise<boolean> {
252
+ if (!settings) return false;
253
+ setError("");
254
+ setNote("");
255
+ const { delivery: _, smtp, ...current } = settings;
256
+ try {
257
+ const saved = await api.saveMail({
258
+ ...current,
259
+ smtp: smtp ? { ...smtp, password: "" } : null, // betterleaks:allow (empty keeps the saved one)
260
+ ...change,
261
+ });
262
+ setSettings(saved);
263
+ return true;
264
+ } catch (failure) {
265
+ setError(failure instanceof Error ? failure.message : String(failure));
266
+ return false;
267
+ }
268
+ }
269
+
270
+ async function saveServer(): Promise<void> {
271
+ setBusy(true);
272
+ if (await save({ smtp: server })) {
273
+ setServer({ ...server, password: "" }); // betterleaks:allow
274
+ setNote("Email server saved.");
275
+ }
276
+ setBusy(false);
277
+ }
278
+
279
+ async function sendTest(): Promise<void> {
280
+ setBusy(true);
281
+ setError("");
282
+ setNote("");
283
+ try {
284
+ const { sentTo } = await api.testMail();
285
+ setNote(`A test email was sent to ${sentTo}.`);
286
+ } catch (failure) {
287
+ setError(failure instanceof Error ? failure.message : String(failure));
288
+ }
289
+ setBusy(false);
290
+ }
291
+
292
+ return (
293
+ <Sidebar title="Site Settings" onClose={onClose}>
294
+ {!settings ? (
295
+ error ? (
296
+ <p className="field-error">{error}</p>
297
+ ) : (
298
+ <p className="brim-sidebar-note">Loading…</p>
299
+ )
300
+ ) : (
301
+ <>
302
+ <OwnSettings locale={locale} focus={focus} onSaved={onSaved} onLoaded={setDeclares} />
303
+ {forms}
304
+ {/* One section is open at a time: the site's own settings come first when it declares some. */}
305
+ <Fieldset title="Form emails" open={!declares && !focus}>
306
+ <p className="brim-sidebar-note">{DELIVERY[settings.delivery]}</p>
307
+ {NOTIFIED_ROLES.map(({ role, label, description }) => (
308
+ <CheckboxWidget
309
+ key={role}
310
+ label={label}
311
+ description={description}
312
+ checked={settings.notifyRoles.includes(role)}
313
+ onChange={(on) =>
314
+ void save({
315
+ notifyRoles: on
316
+ ? [...settings.notifyRoles, role]
317
+ : settings.notifyRoles.filter((chosen) => chosen !== role),
318
+ })
319
+ }
320
+ />
321
+ ))}
322
+ <TextWidget
323
+ label="Also send to"
324
+ description="More addresses that get every message, separated by commas."
325
+ placeholder="office@example.com"
326
+ value={settings.notify.join(", ")}
327
+ onChange={(notify) => void save({ notify })}
328
+ />
329
+ <TextWidget
330
+ label="Sender name"
331
+ description="The name the emails come from. Empty uses the site's address."
332
+ value={settings.senderName}
333
+ onChange={(senderName) => void save({ senderName })}
334
+ />
335
+ {settings.delivery !== "none" && (
336
+ <div className="field-group">
337
+ <button type="button" className="url-browse-btn" disabled={busy} onClick={() => void sendTest()}>
338
+ Send a test email
339
+ </button>
340
+ </div>
341
+ )}
342
+ </Fieldset>
343
+ <Fieldset title="Own email server" open={settings.smtp !== null}>
344
+ <CheckboxWidget
345
+ label="Send from my own email server"
346
+ description="Emails then come from your own address. Your email provider gives these details (SMTP)."
347
+ checked={ownServer}
348
+ onChange={(checked) => {
349
+ setOwnServer(checked);
350
+ if (!checked && settings.smtp) void save({ smtp: null }).then(() => setServer(EMPTY_SERVER));
351
+ }}
352
+ />
353
+ {ownServer && (
354
+ <>
355
+ <TextWidget
356
+ label="From address"
357
+ type="email"
358
+ placeholder="hello@yourcompany.com"
359
+ value={server.from}
360
+ onChange={(from) => setServer({ ...server, from })}
361
+ />
362
+ <TextWidget
363
+ label="Server address"
364
+ placeholder="smtp.yourcompany.com"
365
+ value={server.host}
366
+ onChange={(host) => setServer({ ...server, host })}
367
+ />
368
+ <TextWidget
369
+ label="Port"
370
+ description="Usually 587, or 465."
371
+ value={String(server.port)}
372
+ onChange={(port) => {
373
+ const value = Number(port) || 587;
374
+ setServer({ ...server, port: value, secure: value === 465 });
375
+ }}
376
+ />
377
+ <TextWidget label="Username" value={server.user} onChange={(user) => setServer({ ...server, user })} />
378
+ <TextWidget
379
+ label="Password"
380
+ type="password"
381
+ description={settings.smtp?.hasPassword ? "Saved. Write a new one only to change it." : undefined}
382
+ placeholder={settings.smtp?.hasPassword ? "••••••••" : ""}
383
+ value={server.password}
384
+ onChange={(password) => setServer({ ...server, password })} // betterleaks:allow
385
+ />
386
+ <div className="field-group">
387
+ <button type="button" className="url-browse-btn" disabled={busy} onClick={() => void saveServer()}>
388
+ Save email server
389
+ </button>
390
+ </div>
391
+ </>
392
+ )}
393
+ </Fieldset>
394
+ <RedirectsSettings />
395
+ {error && (
396
+ <p className="field-error" role="alert">
397
+ {error}
398
+ </p>
399
+ )}
400
+ {note && (
401
+ <p className="brim-sidebar-note" role="status">
402
+ {note}
403
+ </p>
404
+ )}
405
+ </>
406
+ )}
407
+ </Sidebar>
408
+ );
409
+ }