@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,568 @@
1
+ import { type FormEvent, useEffect, useId, useMemo, useState } from "react";
2
+ import { type ContentType, type Item, type NewPage, type PageTemplateInfo, type SitePage, api } from "./api.ts";
3
+ import { slugify } from "../../content/content-types.ts";
4
+ import { SelectWidget } from "./fields.tsx";
5
+ import {
6
+ ChevronDownIcon,
7
+ ChevronRightIcon,
8
+ CloseIcon,
9
+ FolderIcon,
10
+ PageIcon,
11
+ PlusIcon,
12
+ SearchIcon,
13
+ TrashIcon,
14
+ } from "./icons.tsx";
15
+ import { useSiteLocales } from "./languages.ts";
16
+ import {
17
+ type GroupField,
18
+ ITEMS_SHOWN,
19
+ type TreeBranch,
20
+ type TreeGroup,
21
+ type TreeItem,
22
+ buildSiteTree,
23
+ } from "./site-tree.ts";
24
+ import { pageTitle } from "./Toolbar.tsx";
25
+
26
+ const indent = (depth: number) => 12 + depth * 20;
27
+ const trimPath = (path: string) => (path.length > 1 ? path.replace(/\/+$/, "") : path);
28
+ const countLabel = (count: number) => `${count} ${count === 1 ? "item" : "items"}`;
29
+ const branchKey = (branch: TreeBranch) => `type:${branch.type.key}`;
30
+ const groupKey = (group: TreeGroup) => `group:${group.key}`;
31
+
32
+ function Chevron({ open }: { open: boolean }) {
33
+ return <span className="breadcrumb-navigation-chevron">{open ? <ChevronDownIcon /> : <ChevronRightIcon />}</span>;
34
+ }
35
+
36
+ function NewButton({ label, onClick }: { label: string; onClick: () => void }) {
37
+ return (
38
+ <button
39
+ type="button"
40
+ className="brim-toolbar-btn brim-toolbar-btn--outline"
41
+ aria-label={label}
42
+ title={label}
43
+ onClick={onClick}
44
+ >
45
+ <PlusIcon />
46
+ New
47
+ </button>
48
+ );
49
+ }
50
+
51
+ /**
52
+ * The drawer that lists the site as one tree, by address. Pages written in code are only opened here; pages added
53
+ * from one of the site's templates can also be created and deleted. The items of a content type hang under the page
54
+ * their address starts with, split by the field after it (products under their category); clicking one opens its
55
+ * page, and each branch and group has its + to add an item there. Types without pages are listed after the pages,
56
+ * and opened in the content browser.
57
+ */
58
+ export function SiteMap({
59
+ pages,
60
+ templates,
61
+ currentPath,
62
+ onNavigate,
63
+ onBrowse,
64
+ onCreate,
65
+ onDelete,
66
+ onClose,
67
+ }: {
68
+ pages: SitePage[];
69
+ templates: PageTemplateInfo[];
70
+ currentPath: string;
71
+ onNavigate: (path: string) => void;
72
+ /**
73
+ * Opens the content browser: on everything, on one content type, or on that type's new item form, which starts
74
+ * with `data` (the category of the group it was added from).
75
+ */
76
+ onBrowse: (folder?: string, create?: boolean, data?: Record<string, unknown>) => void;
77
+ /** Resolves to false when the page could not be created; the reason is shown in the toolbar. */
78
+ onCreate: (page: NewPage) => Promise<boolean>;
79
+ onDelete: (page: SitePage) => Promise<boolean>;
80
+ onClose: () => void;
81
+ }) {
82
+ const [adding, setAdding] = useState(false);
83
+ const [deleting, setDeleting] = useState<SitePage | null>(null);
84
+ const [types, setTypes] = useState<ContentType[]>([]);
85
+ const [itemsByType, setItemsByType] = useState<Record<string, Item[]>>({});
86
+ const [loaded, setLoaded] = useState(false);
87
+ /** The branches and groups shown open, by key. */
88
+ const [open, setOpen] = useState<Set<string>>(new Set());
89
+ const [query, setQuery] = useState("");
90
+ const locales = useSiteLocales();
91
+
92
+ useEffect(() => {
93
+ let current = true;
94
+ void (async () => {
95
+ const { types: list } = await api.types();
96
+ const lists = await Promise.all(list.map((type) => api.items(type.key)));
97
+ if (!current) return;
98
+ setTypes(list);
99
+ setItemsByType(Object.fromEntries(list.map((type, index) => [type.key, lists[index]?.items ?? []])));
100
+ setLoaded(true);
101
+ })();
102
+ return () => {
103
+ current = false;
104
+ };
105
+ }, []);
106
+
107
+ const tree = useMemo(
108
+ () => buildSiteTree(pages, types, itemsByType, locales ?? undefined),
109
+ [pages, types, itemsByType, locales],
110
+ );
111
+ /** The pages of terms some branch hangs under: those branches are listed in the term's group, not at the top. */
112
+ const termPageParents = useMemo(() => {
113
+ const paths = new Set<string>();
114
+ const visit = (group: TreeGroup) => {
115
+ if (group.page) paths.add(trimPath(group.page));
116
+ group.groups.forEach(visit);
117
+ };
118
+ for (const branch of tree.branches) branch.groups.forEach(visit);
119
+ return paths;
120
+ }, [tree]);
121
+
122
+ // The branches and groups that lead to the item being edited start open, so it shows where it sits.
123
+ useEffect(() => {
124
+ if (!loaded) return;
125
+ const branchPath = (branch: TreeBranch): string[] | null => {
126
+ for (const group of branch.groups) {
127
+ const inner = groupPath(group);
128
+ if (inner) return [branchKey(branch), ...inner];
129
+ }
130
+ return branch.items.some((entry) => entry.path === currentPath) ? [branchKey(branch)] : null;
131
+ };
132
+ const groupPath = (group: TreeGroup): string[] | null => {
133
+ if (group.page === currentPath) return [];
134
+ for (const branch of nestedIn(group)) {
135
+ const inner = branchPath(branch);
136
+ if (inner) return [groupKey(group), ...inner];
137
+ }
138
+ for (const sub of group.groups) {
139
+ const inner = groupPath(sub);
140
+ if (inner) return [groupKey(group), ...inner];
141
+ }
142
+ return group.items.some((entry) => entry.path === currentPath) ? [groupKey(group)] : null;
143
+ };
144
+ setOpen(new Set(tree.branches.flatMap((branch) => branchPath(branch) ?? [])));
145
+ // eslint-disable-next-line react-hooks/exhaustive-deps
146
+ }, [loaded]);
147
+
148
+ /** The branches whose items hang under the page of a group's term (the catalogs of the ceramics). */
149
+ function nestedIn(group: TreeGroup): TreeBranch[] {
150
+ return group.page ? tree.branches.filter((branch) => branch.parent === trimPath(group.page!)) : [];
151
+ }
152
+
153
+ const needle = query.trim().toLocaleLowerCase();
154
+ const matches = (...texts: string[]) =>
155
+ needle === "" || texts.some((text) => text.toLocaleLowerCase().includes(needle));
156
+ const isOpen = (key: string) => needle !== "" || open.has(key);
157
+ const toggle = (key: string) =>
158
+ setOpen((keys) => {
159
+ const next = new Set(keys);
160
+ if (!next.delete(key)) next.add(key);
161
+ return next;
162
+ });
163
+
164
+ const shownPages = pages.filter(
165
+ (page) =>
166
+ matches(pageTitle(page.path, page.title), page.path) ||
167
+ tree.branches.some((branch) => branch.parent === trimPath(page.path) && branchShown(branch)),
168
+ );
169
+
170
+ function branchShown(branch: TreeBranch): boolean {
171
+ return (
172
+ matches(branch.type.label) ||
173
+ branch.items.some((entry) => matches(entry.item.title, entry.path)) ||
174
+ branch.groups.some(groupShown)
175
+ );
176
+ }
177
+
178
+ function groupShown(group: TreeGroup): boolean {
179
+ return (
180
+ matches(group.label) ||
181
+ group.items.some((entry) => matches(entry.item.title, entry.path)) ||
182
+ nestedIn(group).some(branchShown)
183
+ );
184
+ }
185
+
186
+ function itemRows(branch: TreeBranch, entries: TreeItem[], depth: number) {
187
+ const found = entries.filter((entry) => matches(entry.item.title, entry.path));
188
+ const shown = needle === "" ? found.slice(0, ITEMS_SHOWN) : found;
189
+ return (
190
+ <>
191
+ {shown.map((entry) => (
192
+ <div key={entry.item.id} className="breadcrumb-navigation-row">
193
+ <button
194
+ type="button"
195
+ className="breadcrumb-navigation-item"
196
+ aria-current={entry.path === currentPath ? "page" : undefined}
197
+ style={{ paddingInlineStart: indent(depth) }}
198
+ onClick={() => onNavigate(entry.path)}
199
+ >
200
+ <PageIcon />
201
+ <span className="breadcrumb-navigation-item__title">{entry.item.title}</span>
202
+ <code>{entry.path}</code>
203
+ {entry.item.visibility === "private" && <span className="cb-badge cb-badge-status private">private</span>}
204
+ </button>
205
+ </div>
206
+ ))}
207
+ {shown.length < found.length && (
208
+ <button
209
+ type="button"
210
+ className="breadcrumb-navigation-more"
211
+ style={{ paddingInlineStart: indent(depth) }}
212
+ onClick={() => onBrowse(branch.type.key)}
213
+ >
214
+ Show all {found.length} {branch.type.label.toLowerCase()}
215
+ </button>
216
+ )}
217
+ </>
218
+ );
219
+ }
220
+
221
+ function branchRows(branch: TreeBranch, depth: number) {
222
+ if (!branchShown(branch)) return null;
223
+ const { type, groupBy } = branch;
224
+ const key = branchKey(branch);
225
+ const vocabulary = groupBy?.to ? types.find((candidate) => candidate.key === groupBy.to) : undefined;
226
+ return (
227
+ <div key={key} role="group" aria-label={type.label}>
228
+ <div className="breadcrumb-navigation-row">
229
+ <button
230
+ type="button"
231
+ className="breadcrumb-navigation-item"
232
+ aria-expanded={isOpen(key)}
233
+ title={isOpen(key) ? `Hide ${type.label}` : `Show ${type.label}`}
234
+ style={{ paddingInlineStart: indent(depth) }}
235
+ onClick={() => toggle(key)}
236
+ >
237
+ <Chevron open={isOpen(key)} />
238
+ <FolderIcon />
239
+ <span className="breadcrumb-navigation-item__title">{type.label}</span>
240
+ <code>{countLabel(branch.items.length)}</code>
241
+ </button>
242
+ <NewButton label={`New ${type.singular}`} onClick={() => onBrowse(type.key, true)} />
243
+ </div>
244
+ {isOpen(key) &&
245
+ (groupBy ? (
246
+ <>
247
+ {branch.groups.map((group) => groupRows(branch, group, groupBy, depth + 1))}
248
+ {vocabulary && needle === "" && (
249
+ <button
250
+ type="button"
251
+ className="breadcrumb-navigation-more"
252
+ style={{ paddingInlineStart: indent(depth + 1) }}
253
+ onClick={() => onBrowse(vocabulary.key, true)}
254
+ >
255
+ <PlusIcon /> New {vocabulary.singular.toLowerCase()}
256
+ </button>
257
+ )}
258
+ </>
259
+ ) : (
260
+ itemRows(branch, branch.items, depth + 1)
261
+ ))}
262
+ </div>
263
+ );
264
+ }
265
+
266
+ /**
267
+ * A group and, when open, what it holds: the types whose items hang under its term's page, its subgroups, then
268
+ * its items in no subgroup.
269
+ */
270
+ function groupRows(branch: TreeBranch, group: TreeGroup, field: GroupField, depth: number) {
271
+ if (!groupShown(group)) return null;
272
+ const { type } = branch;
273
+ const key = groupKey(group);
274
+ return (
275
+ <div key={key} role="group" aria-label={group.label}>
276
+ <div className="breadcrumb-navigation-row">
277
+ {group.page ? (
278
+ // The term has its own page: its name opens it, the chevron shows its items.
279
+ <>
280
+ <button
281
+ type="button"
282
+ className="breadcrumb-navigation-toggle"
283
+ aria-expanded={isOpen(key)}
284
+ aria-label={`${isOpen(key) ? "Hide" : "Show"} the ${type.label.toLowerCase()} in ${group.label}`}
285
+ style={{ marginInlineStart: indent(depth) - 8 }}
286
+ onClick={() => toggle(key)}
287
+ >
288
+ <Chevron open={isOpen(key)} />
289
+ </button>
290
+ <button
291
+ type="button"
292
+ className="breadcrumb-navigation-item breadcrumb-navigation-item--after-toggle"
293
+ aria-current={group.page === currentPath ? "page" : undefined}
294
+ title={`Open the page of ${group.label}`}
295
+ onClick={() => onNavigate(group.page!)}
296
+ >
297
+ <PageIcon />
298
+ <span className="breadcrumb-navigation-item__title">{group.label}</span>
299
+ <code>
300
+ {group.page} · {countLabel(group.items.length)}
301
+ </code>
302
+ {group.term?.visibility === "private" && (
303
+ <span className="cb-badge cb-badge-status private">private</span>
304
+ )}
305
+ </button>
306
+ </>
307
+ ) : (
308
+ <button
309
+ type="button"
310
+ className="breadcrumb-navigation-item"
311
+ aria-expanded={isOpen(key)}
312
+ title={`${field.label}: ${group.label}`}
313
+ style={{ paddingInlineStart: indent(depth) }}
314
+ onClick={() => toggle(key)}
315
+ >
316
+ <Chevron open={isOpen(key)} />
317
+ <FolderIcon />
318
+ <span className="breadcrumb-navigation-item__title">{group.label}</span>
319
+ <code>{countLabel(group.items.length)}</code>
320
+ </button>
321
+ )}
322
+ <NewButton
323
+ label={`New ${type.singular} in ${group.label}`}
324
+ onClick={() => onBrowse(type.key, true, Object.keys(group.data).length > 0 ? group.data : undefined)}
325
+ />
326
+ </div>
327
+ {isOpen(key) && (
328
+ <>
329
+ {nestedIn(group).map((nested) => branchRows(nested, depth + 1))}
330
+ {group.subgroupBy && group.groups.map((sub) => groupRows(branch, sub, group.subgroupBy!, depth + 1))}
331
+ {itemRows(branch, group.rest, depth + 1)}
332
+ </>
333
+ )}
334
+ </div>
335
+ );
336
+ }
337
+
338
+ const homeless = tree.branches.filter(
339
+ (branch) =>
340
+ branch.parent === null ||
341
+ (!pages.some((page) => trimPath(page.path) === branch.parent) && !termPageParents.has(branch.parent)),
342
+ );
343
+ const unplaced = tree.unplaced.filter((type) => matches(type.label));
344
+
345
+ return (
346
+ <div className="breadcrumb-navigation-layer" onClick={onClose}>
347
+ <aside
348
+ className="breadcrumb-navigation-drawer"
349
+ aria-label="Site map"
350
+ onClick={(event) => event.stopPropagation()}
351
+ >
352
+ <header className="breadcrumb-navigation-header">
353
+ <div>
354
+ <small>Site map</small>
355
+ <h2>Pages and content</h2>
356
+ </div>
357
+ <button type="button" className="brim-sidebar-close-btn" aria-label="Close site map" onClick={onClose}>
358
+ <CloseIcon />
359
+ </button>
360
+ </header>
361
+ {adding && <NewPageForm templates={templates} onCancel={() => setAdding(false)} onCreate={onCreate} />}
362
+ <div className="breadcrumb-navigation-search">
363
+ <SearchIcon />
364
+ <input
365
+ type="search"
366
+ className="field-input"
367
+ aria-label="Find a page or an item"
368
+ placeholder="Find a page or an item"
369
+ value={query}
370
+ onChange={(event) => setQuery(event.target.value)}
371
+ />
372
+ </div>
373
+ <nav className="breadcrumb-navigation-list">
374
+ {shownPages.map((page) => {
375
+ const depth = page.path.split("/").filter(Boolean).length - 1;
376
+ return (
377
+ <div key={page.path}>
378
+ <div className="breadcrumb-navigation-row">
379
+ <button
380
+ type="button"
381
+ className="breadcrumb-navigation-item"
382
+ aria-current={page.path === currentPath ? "page" : undefined}
383
+ // Nested pages are indented under their parent.
384
+ style={{ paddingInlineStart: indent(Math.max(depth, 0)) }}
385
+ onClick={() => onNavigate(page.path)}
386
+ >
387
+ <PageIcon />
388
+ <span className="breadcrumb-navigation-item__title">{pageTitle(page.path, page.title)}</span>
389
+ <code>{page.path}</code>
390
+ {page.visibility === "private" && <span className="cb-badge cb-badge-status private">private</span>}
391
+ </button>
392
+ {page.template && (
393
+ <button
394
+ type="button"
395
+ className="brim-toolbar-btn brim-toolbar-btn--icon"
396
+ aria-label={`Delete ${pageTitle(page.path, page.title)}`}
397
+ title="Delete page"
398
+ onClick={() => setDeleting(page)}
399
+ >
400
+ <TrashIcon />
401
+ </button>
402
+ )}
403
+ </div>
404
+ {tree.branches
405
+ .filter((branch) => branch.parent === trimPath(page.path))
406
+ .map((branch) => branchRows(branch, Math.max(depth, 0) + 1))}
407
+ </div>
408
+ );
409
+ })}
410
+ {homeless.map((branch) => branchRows(branch, 0))}
411
+ {unplaced.length > 0 && (
412
+ <>
413
+ <h3 className="breadcrumb-navigation-heading">Without a page</h3>
414
+ {unplaced.map((type) => (
415
+ <div key={type.key} className="breadcrumb-navigation-row">
416
+ <button
417
+ type="button"
418
+ className="breadcrumb-navigation-item"
419
+ title={`Open ${type.label}`}
420
+ onClick={() => onBrowse(type.key)}
421
+ >
422
+ <FolderIcon />
423
+ <span className="breadcrumb-navigation-item__title">{type.label}</span>
424
+ <code>{countLabel((itemsByType[type.key] ?? []).filter((item) => item.locale === "").length)}</code>
425
+ </button>
426
+ <NewButton label={`New ${type.singular}`} onClick={() => onBrowse(type.key, true)} />
427
+ </div>
428
+ ))}
429
+ </>
430
+ )}
431
+ {needle !== "" &&
432
+ shownPages.length === 0 &&
433
+ homeless.every((branch) => !branchShown(branch)) &&
434
+ unplaced.length === 0 && <p className="brim-sidebar-note">Nothing matches "{query.trim()}".</p>}
435
+ </nav>
436
+ <footer className="breadcrumb-navigation-footer">
437
+ {templates.length > 0 && !adding && (
438
+ <button type="button" className="cb-new-btn" onClick={() => setAdding(true)}>
439
+ <PlusIcon />
440
+ New page
441
+ </button>
442
+ )}
443
+ <button type="button" className="brim-toolbar-btn brim-toolbar-btn--action" onClick={() => onBrowse()}>
444
+ All content
445
+ </button>
446
+ </footer>
447
+ </aside>
448
+
449
+ {deleting && (
450
+ <div
451
+ className="cb-modal-overlay"
452
+ onClick={(event) => {
453
+ event.stopPropagation();
454
+ setDeleting(null);
455
+ }}
456
+ >
457
+ <div
458
+ className="cb-modal"
459
+ role="alertdialog"
460
+ aria-label="Delete page"
461
+ onClick={(event) => event.stopPropagation()}
462
+ >
463
+ <h3>Delete Page</h3>
464
+ <p>
465
+ Are you sure you want to delete "{pageTitle(deleting.path, deleting.title)}" ({deleting.path}) and
466
+ everything written on it? This action cannot be undone.
467
+ </p>
468
+ <div className="cb-modal-actions">
469
+ <button type="button" className="cb-toolbar-btn" onClick={() => setDeleting(null)}>
470
+ Cancel
471
+ </button>
472
+ <button
473
+ type="button"
474
+ className="cb-new-btn danger"
475
+ onClick={() => void onDelete(deleting).then((done) => done && setDeleting(null))}
476
+ >
477
+ Delete
478
+ </button>
479
+ </div>
480
+ </div>
481
+ </div>
482
+ )}
483
+ </div>
484
+ );
485
+ }
486
+
487
+ /** Template, title and address of a new page. The address follows the title until the owner edits it. */
488
+ function NewPageForm({
489
+ templates,
490
+ onCreate,
491
+ onCancel,
492
+ }: {
493
+ templates: PageTemplateInfo[];
494
+ onCreate: (page: NewPage) => Promise<boolean>;
495
+ onCancel: () => void;
496
+ }) {
497
+ const id = useId();
498
+ const [templateKey, setTemplateKey] = useState(templates[0]?.key ?? "");
499
+ const [title, setTitle] = useState("");
500
+ const [path, setPath] = useState("");
501
+ const [pathEdited, setPathEdited] = useState(false);
502
+ const [busy, setBusy] = useState(false);
503
+ const template = templates.find((candidate) => candidate.key === templateKey);
504
+
505
+ useEffect(() => {
506
+ if (pathEdited) return;
507
+ const slug = slugify(title);
508
+ setPath(slug ? `/${template?.folder ? `${template.folder}/` : ""}${slug}` : "");
509
+ }, [title, template, pathEdited]);
510
+
511
+ async function submit(event: FormEvent): Promise<void> {
512
+ event.preventDefault();
513
+ setBusy(true);
514
+ const created = await onCreate({ template: templateKey, title, ...(path && { path }) });
515
+ setBusy(false);
516
+ if (created) onCancel();
517
+ }
518
+
519
+ return (
520
+ <form className="breadcrumb-navigation-new" onSubmit={(event) => void submit(event)}>
521
+ <SelectWidget
522
+ label="Template"
523
+ description={template?.description}
524
+ options={templates.map((candidate) => ({ value: candidate.key, label: candidate.label }))}
525
+ value={templateKey}
526
+ onChange={setTemplateKey}
527
+ />
528
+ <div className="field-group">
529
+ <label className="field-label" htmlFor={`${id}-title`}>
530
+ Title
531
+ </label>
532
+ <input
533
+ id={`${id}-title`}
534
+ className="field-input"
535
+ value={title}
536
+ required
537
+ autoFocus
538
+ placeholder="e.g. Lawn care"
539
+ onChange={(event) => setTitle(event.target.value)}
540
+ />
541
+ </div>
542
+ <div className="field-group">
543
+ <label className="field-label" htmlFor={`${id}-path`}>
544
+ Address
545
+ </label>
546
+ <p className="field-description">The page starts private: only you see it until you make it public.</p>
547
+ <input
548
+ id={`${id}-path`}
549
+ className="field-input"
550
+ value={path}
551
+ placeholder="/services/lawn-care"
552
+ onChange={(event) => {
553
+ setPathEdited(true);
554
+ setPath(event.target.value);
555
+ }}
556
+ />
557
+ </div>
558
+ <div className="cb-modal-actions">
559
+ <button type="button" className="cb-toolbar-btn" onClick={onCancel}>
560
+ Cancel
561
+ </button>
562
+ <button type="submit" className="cb-new-btn" disabled={busy || title.trim() === ""}>
563
+ Create page
564
+ </button>
565
+ </div>
566
+ </form>
567
+ );
568
+ }