@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.
- package/LICENSE +110 -0
- package/README.md +22 -2
- package/bin/brim.js +5 -0
- package/dist/cli.js +3802 -0
- package/editor-dist/editor.css +1 -0
- package/editor-dist/editor.js +91 -0
- package/package.json +71 -3
- package/skills/brim-convert/SKILL.md +256 -0
- package/skills/brim-hosting/SKILL.md +221 -0
- package/src/astro-shim.d.ts +5 -0
- package/src/auth/accounts.ts +341 -0
- package/src/auth/password.ts +34 -0
- package/src/auth/permissions.ts +44 -0
- package/src/blocks/accordion/Accordion.astro +48 -0
- package/src/blocks/accordion/accordion.css +63 -0
- package/src/blocks/analytics/Analytics.astro +45 -0
- package/src/blocks/analytics/ConsentBanner.astro +41 -0
- package/src/blocks/analytics/analytics.client.ts +125 -0
- package/src/blocks/analytics/analytics.css +51 -0
- package/src/blocks/analytics/consent.ts +51 -0
- package/src/blocks/button/Button.astro +64 -0
- package/src/blocks/button/button.css +100 -0
- package/src/blocks/carousel/Carousel.astro +140 -0
- package/src/blocks/carousel/carousel.client.ts +153 -0
- package/src/blocks/carousel/carousel.css +181 -0
- package/src/blocks/data/icons.ts +285 -0
- package/src/blocks/data/image-shapes.ts +91 -0
- package/src/blocks/data/social-icons.ts +41 -0
- package/src/blocks/data/social-networks.ts +256 -0
- package/src/blocks/divider/Divider.astro +27 -0
- package/src/blocks/divider/divider.css +17 -0
- package/src/blocks/form/Captcha.astro +27 -0
- package/src/blocks/form/Form.astro +250 -0
- package/src/blocks/form/FormField.astro +138 -0
- package/src/blocks/form/Newsletter.astro +80 -0
- package/src/blocks/form/form.client.ts +339 -0
- package/src/blocks/form/form.css +271 -0
- package/src/blocks/gallery/ImageGallery.astro +126 -0
- package/src/blocks/gallery/gallery.css +43 -0
- package/src/blocks/gallery/lightbox.client.ts +178 -0
- package/src/blocks/gallery/lightbox.css +134 -0
- package/src/blocks/icon/Icon.astro +41 -0
- package/src/blocks/icon/icon.css +16 -0
- package/src/blocks/image/Image.astro +158 -0
- package/src/blocks/image/image.css +42 -0
- package/src/blocks/images.ts +64 -0
- package/src/blocks/in-editor.client.ts +8 -0
- package/src/blocks/listing/Listing.astro +288 -0
- package/src/blocks/listing/ListingCards.astro +48 -0
- package/src/blocks/listing/listing.css +136 -0
- package/src/blocks/listing/query.ts +363 -0
- package/src/blocks/map/Map.astro +75 -0
- package/src/blocks/map/map.css +32 -0
- package/src/blocks/schemas/accordion.ts +118 -0
- package/src/blocks/schemas/button.ts +78 -0
- package/src/blocks/schemas/carousel.ts +133 -0
- package/src/blocks/schemas/divider.ts +29 -0
- package/src/blocks/schemas/form-field-types.ts +29 -0
- package/src/blocks/schemas/form.ts +350 -0
- package/src/blocks/schemas/icon.ts +24 -0
- package/src/blocks/schemas/image-gallery.ts +54 -0
- package/src/blocks/schemas/image.ts +87 -0
- package/src/blocks/schemas/index.ts +33 -0
- package/src/blocks/schemas/listing.ts +14 -0
- package/src/blocks/schemas/map.ts +81 -0
- package/src/blocks/schemas/newsletter.ts +324 -0
- package/src/blocks/schemas/social-media.ts +348 -0
- package/src/blocks/schemas/text.ts +151 -0
- package/src/blocks/schemas/video.ts +57 -0
- package/src/blocks/search/Search.astro +147 -0
- package/src/blocks/search/search.css +69 -0
- package/src/blocks/search/state.ts +28 -0
- package/src/blocks/settings.ts +94 -0
- package/src/blocks/social/SocialMedia.astro +102 -0
- package/src/blocks/social/social-links.ts +16 -0
- package/src/blocks/social/social.css +31 -0
- package/src/blocks/theme.ts +35 -0
- package/src/blocks/types.ts +52 -0
- package/src/blocks/video/Video.astro +90 -0
- package/src/blocks/video/video-source.ts +77 -0
- package/src/blocks/video/video.client.ts +43 -0
- package/src/blocks/video/video.css +29 -0
- package/src/check.ts +1119 -0
- package/src/cli.ts +493 -0
- package/src/components/Blocks.astro +68 -0
- package/src/components/Field.astro +61 -0
- package/src/components/Footer.astro +36 -0
- package/src/components/Link.astro +18 -0
- package/src/components/MobileMenu.astro +77 -0
- package/src/components/Navbar.astro +42 -0
- package/src/components/RichText.astro +32 -0
- package/src/components/Seo.astro +202 -0
- package/src/components/Setting.astro +46 -0
- package/src/components/Shared.astro +17 -0
- package/src/components/Text.astro +91 -0
- package/src/components/blocks.css +86 -0
- package/src/components/index.ts +27 -0
- package/src/components/mobile-menu.client.ts +21 -0
- package/src/components/mobile-menu.css +52 -0
- package/src/components/text.css +48 -0
- package/src/config.ts +413 -0
- package/src/content/block-layout.ts +63 -0
- package/src/content/block-values.ts +48 -0
- package/src/content/content-check.ts +174 -0
- package/src/content/content-types.ts +408 -0
- package/src/content/drafts.ts +115 -0
- package/src/content/feed.ts +237 -0
- package/src/content/field-types.ts +107 -0
- package/src/content/history.ts +140 -0
- package/src/content/ids.ts +50 -0
- package/src/content/image-size.ts +212 -0
- package/src/content/image-sizes.ts +126 -0
- package/src/content/item-fields.ts +110 -0
- package/src/content/item-head.ts +126 -0
- package/src/content/item-image.ts +26 -0
- package/src/content/item-path.ts +122 -0
- package/src/content/items.ts +580 -0
- package/src/content/language-scopes.ts +40 -0
- package/src/content/media.ts +271 -0
- package/src/content/menus.ts +51 -0
- package/src/content/page-templates.ts +52 -0
- package/src/content/redirects.ts +112 -0
- package/src/content/references.ts +165 -0
- package/src/content/rich-text.ts +63 -0
- package/src/content/search.ts +167 -0
- package/src/content/seed.ts +229 -0
- package/src/content/site-settings.ts +111 -0
- package/src/content/sitemap.ts +143 -0
- package/src/content/store.ts +225 -0
- package/src/content/tags.ts +67 -0
- package/src/content/text-default.ts +139 -0
- package/src/content/types.ts +137 -0
- package/src/content/validate.ts +253 -0
- package/src/content/validation-error.ts +2 -0
- package/src/db/cached.ts +140 -0
- package/src/db/database.ts +15 -0
- package/src/db/libsql.ts +29 -0
- package/src/db/schema.ts +189 -0
- package/src/db/url.ts +44 -0
- package/src/editor/client/BlockSettings.tsx +1063 -0
- package/src/editor/client/Chrome.tsx +284 -0
- package/src/editor/client/ContentBrowser.tsx +1299 -0
- package/src/editor/client/Editor.tsx +1756 -0
- package/src/editor/client/HistoryPanel.tsx +114 -0
- package/src/editor/client/ImagesWidget.tsx +113 -0
- package/src/editor/client/Inspector.tsx +127 -0
- package/src/editor/client/ItemForm.tsx +535 -0
- package/src/editor/client/LinkWidget.tsx +440 -0
- package/src/editor/client/ListingPanel.tsx +99 -0
- package/src/editor/client/Login.tsx +338 -0
- package/src/editor/client/Menu.tsx +64 -0
- package/src/editor/client/PageSettings.tsx +270 -0
- package/src/editor/client/RedirectsSettings.tsx +130 -0
- package/src/editor/client/Resize.tsx +204 -0
- package/src/editor/client/Sidebar.tsx +51 -0
- package/src/editor/client/SiteMap.tsx +568 -0
- package/src/editor/client/SiteSettings.tsx +409 -0
- package/src/editor/client/TextTools.tsx +391 -0
- package/src/editor/client/Toolbar.tsx +449 -0
- package/src/editor/client/VideoUpload.tsx +251 -0
- package/src/editor/client/api.ts +417 -0
- package/src/editor/client/brand.ts +20 -0
- package/src/editor/client/connection.ts +98 -0
- package/src/editor/client/context.ts +19 -0
- package/src/editor/client/css.d.ts +2 -0
- package/src/editor/client/editor.css +4656 -0
- package/src/editor/client/fields.tsx +812 -0
- package/src/editor/client/icons.tsx +174 -0
- package/src/editor/client/keys.ts +15 -0
- package/src/editor/client/kinds.ts +289 -0
- package/src/editor/client/languages.ts +32 -0
- package/src/editor/client/main.tsx +100 -0
- package/src/editor/client/responses.ts +26 -0
- package/src/editor/client/rich-text.ts +326 -0
- package/src/editor/client/site-tree.ts +239 -0
- package/src/editor/client/theme.ts +55 -0
- package/src/editor/client/tsconfig.json +18 -0
- package/src/editor/client/tus.ts +137 -0
- package/src/editor/client/use-rect.ts +50 -0
- package/src/editor/client/widgets.tsx +1218 -0
- package/src/forms/captcha.ts +83 -0
- package/src/forms/define.ts +166 -0
- package/src/forms/fields.ts +197 -0
- package/src/forms/file-types.ts +108 -0
- package/src/forms/files.ts +216 -0
- package/src/forms/form-page.ts +9 -0
- package/src/forms/handler.ts +622 -0
- package/src/forms/spec.ts +89 -0
- package/src/forms/store.ts +96 -0
- package/src/forms/work.ts +50 -0
- package/src/icons/library.ts +144 -0
- package/src/icons/load.ts +63 -0
- package/src/index.ts +99 -0
- package/src/init.ts +257 -0
- package/src/integration.ts +469 -0
- package/src/mail/mailer.ts +197 -0
- package/src/mail/notify.ts +272 -0
- package/src/mail/settings.ts +173 -0
- package/src/mail/templates.ts +191 -0
- package/src/mcp/page-nodes.ts +209 -0
- package/src/mcp/server.ts +111 -0
- package/src/mcp/tools.ts +637 -0
- package/src/runtime/TemplatePage.astro +39 -0
- package/src/runtime/TemplatePageStatic.astro +13 -0
- package/src/runtime/admin-route.ts +80 -0
- package/src/runtime/connect.ts +53 -0
- package/src/runtime/content.ts +188 -0
- package/src/runtime/context.ts +131 -0
- package/src/runtime/form-files.ts +18 -0
- package/src/runtime/forms-route.ts +33 -0
- package/src/runtime/forms.ts +28 -0
- package/src/runtime/icons.ts +5 -0
- package/src/runtime/media.ts +175 -0
- package/src/runtime/middleware.ts +186 -0
- package/src/runtime/nav.ts +13 -0
- package/src/runtime/query-reads.ts +49 -0
- package/src/runtime/query-store.ts +16 -0
- package/src/runtime/request-state.ts +69 -0
- package/src/runtime/robots.ts +29 -0
- package/src/runtime/rss-static.ts +16 -0
- package/src/runtime/rss.ts +29 -0
- package/src/runtime/server.ts +29 -0
- package/src/runtime/sitemap.ts +34 -0
- package/src/runtime/template-paths.ts +34 -0
- package/src/runtime/uploads-route.ts +13 -0
- package/src/seed-cli.ts +253 -0
- package/src/server/auth.ts +31 -0
- package/src/server/fetch-image.ts +219 -0
- package/src/server/handler.ts +2106 -0
- package/src/server/http.ts +162 -0
- package/src/server/locales.ts +73 -0
- package/src/server/node.ts +57 -0
- package/src/server/query-params.ts +249 -0
- package/src/server/rate-limit.ts +72 -0
- package/src/server/video-host.ts +162 -0
- package/src/starter.ts +70 -0
- package/src/version.ts +4 -0
- package/src/virtual.d.ts +65 -0
|
@@ -0,0 +1,1299 @@
|
|
|
1
|
+
import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { formPage } from "../../forms/form-page.ts";
|
|
3
|
+
import { fileSize } from "../../forms/file-types.ts";
|
|
4
|
+
import { formsOf } from "./responses.ts";
|
|
5
|
+
import {
|
|
6
|
+
type ContentType,
|
|
7
|
+
type FormResponse,
|
|
8
|
+
type Item,
|
|
9
|
+
type ItemDraft,
|
|
10
|
+
type SitePage,
|
|
11
|
+
type Visibility,
|
|
12
|
+
api,
|
|
13
|
+
} from "./api.ts";
|
|
14
|
+
import {
|
|
15
|
+
ArrowLeftIcon,
|
|
16
|
+
DragIcon,
|
|
17
|
+
EyeIcon,
|
|
18
|
+
FolderIcon,
|
|
19
|
+
GridViewIcon,
|
|
20
|
+
HomeIcon,
|
|
21
|
+
PageIcon,
|
|
22
|
+
PencilIcon,
|
|
23
|
+
PlusIcon,
|
|
24
|
+
SearchIcon,
|
|
25
|
+
TableViewIcon,
|
|
26
|
+
TrashIcon,
|
|
27
|
+
} from "./icons.tsx";
|
|
28
|
+
import { ItemForm, type ItemLanguages } from "./ItemForm.tsx";
|
|
29
|
+
import { hasTag, rankTags, sameTag, tagsOf } from "../../content/tags.ts";
|
|
30
|
+
import { itemImage } from "../../content/item-image.ts";
|
|
31
|
+
import { itemPagePath } from "../../server/locales.ts";
|
|
32
|
+
import { itemLocale, translationsOf, useSiteLocales } from "./languages.ts";
|
|
33
|
+
import { Menu } from "./Menu.tsx";
|
|
34
|
+
import { pageTitle } from "./Toolbar.tsx";
|
|
35
|
+
import { isComposing } from "./keys.ts";
|
|
36
|
+
|
|
37
|
+
/** One row of the browser: a folder of a content type, a page of the site, or an item. */
|
|
38
|
+
interface Entry {
|
|
39
|
+
id: string;
|
|
40
|
+
kind: "folder" | "page" | "item" | "response";
|
|
41
|
+
title: string;
|
|
42
|
+
/** For a folder: how many items it holds. */
|
|
43
|
+
count?: number;
|
|
44
|
+
/** Name of the type, shown in the Type column and the filter chips. */
|
|
45
|
+
typeLabel: string;
|
|
46
|
+
/** The same name for many, on the filter chips: written out, since adding an "s" makes "Vocabularys". */
|
|
47
|
+
typePlural: string;
|
|
48
|
+
/** Public address, for View. Folders have none. */
|
|
49
|
+
path?: string;
|
|
50
|
+
visibility?: Visibility;
|
|
51
|
+
modified?: string;
|
|
52
|
+
item?: Item;
|
|
53
|
+
type?: ContentType;
|
|
54
|
+
response?: FormResponse;
|
|
55
|
+
page?: SitePage;
|
|
56
|
+
/** An item's tags: what its `tags` field holds. */
|
|
57
|
+
tags?: string[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The folder that holds what visitors sent through the site's forms. */
|
|
61
|
+
const RESPONSES = "form-responses";
|
|
62
|
+
|
|
63
|
+
type View = "table" | "grid";
|
|
64
|
+
|
|
65
|
+
const MINUTE = 60_000;
|
|
66
|
+
|
|
67
|
+
/** `Just now`, `5m ago`, `3h ago`, `2d ago`, then a date. */
|
|
68
|
+
function relativeDate(iso: string | undefined): string {
|
|
69
|
+
if (!iso) return "—";
|
|
70
|
+
const elapsed = Date.now() - new Date(iso).getTime();
|
|
71
|
+
if (elapsed < MINUTE) return "Just now";
|
|
72
|
+
if (elapsed < 60 * MINUTE) return `${Math.floor(elapsed / MINUTE)}m ago`;
|
|
73
|
+
if (elapsed < 24 * 60 * MINUTE) return `${Math.floor(elapsed / (60 * MINUTE))}h ago`;
|
|
74
|
+
if (elapsed < 7 * 24 * 60 * MINUTE) return `${Math.floor(elapsed / (24 * 60 * MINUTE))}d ago`;
|
|
75
|
+
return new Date(iso).toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric" });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Browses everything the owner can manage: the site's pages and the items of each content type.
|
|
80
|
+
* Content types are declared in the site's code, so none is created here; pages are added from the site map.
|
|
81
|
+
*/
|
|
82
|
+
export function ContentBrowser({
|
|
83
|
+
pages,
|
|
84
|
+
initialFolder = null,
|
|
85
|
+
initialCreate = false,
|
|
86
|
+
initialData,
|
|
87
|
+
initialForm,
|
|
88
|
+
initialResponse,
|
|
89
|
+
run,
|
|
90
|
+
onBack,
|
|
91
|
+
onEditPage,
|
|
92
|
+
onEditTemplate,
|
|
93
|
+
onPagesChanged,
|
|
94
|
+
}: {
|
|
95
|
+
pages: SitePage[];
|
|
96
|
+
/** Key of the content type to open on; all content when absent. */
|
|
97
|
+
initialFolder?: string | null;
|
|
98
|
+
/** Opens straight on the form of a new item of `initialFolder`. */
|
|
99
|
+
initialCreate?: boolean;
|
|
100
|
+
/** What that new item starts with, e.g. the field the listing it was added from filters on. */
|
|
101
|
+
initialData?: Record<string, unknown> | undefined;
|
|
102
|
+
/** In Form responses, the form whose responses to show first (its name); all of them when absent. */
|
|
103
|
+
initialForm?: string | undefined;
|
|
104
|
+
/** A response to open as soon as the responses are read. */
|
|
105
|
+
initialResponse?: string | undefined;
|
|
106
|
+
run: (work: Promise<unknown>) => Promise<boolean>;
|
|
107
|
+
onBack: () => void;
|
|
108
|
+
onEditPage: (path: string) => void;
|
|
109
|
+
/** Opens a content type's template on the page of one of its items, given as a sample. */
|
|
110
|
+
onEditTemplate: (samplePath: string) => void;
|
|
111
|
+
onPagesChanged: () => void;
|
|
112
|
+
}) {
|
|
113
|
+
const [types, setTypes] = useState<ContentType[]>([]);
|
|
114
|
+
const [itemsByType, setItemsByType] = useState<Record<string, Item[]>>({});
|
|
115
|
+
const [loading, setLoading] = useState(true);
|
|
116
|
+
/** Key of the content type being browsed; `null` at the root. */
|
|
117
|
+
const [folder, setFolder] = useState<string | null>(initialFolder);
|
|
118
|
+
const [view, setView] = useState<View>("table");
|
|
119
|
+
const [filter, setFilter] = useState<string | null>(null);
|
|
120
|
+
/** Only what has this tag, whatever its case; `null` shows everything. */
|
|
121
|
+
const [tag, setTag] = useState<string | null>(null);
|
|
122
|
+
const [query, setQuery] = useState("");
|
|
123
|
+
const [selected, setSelected] = useState<Set<string>>(new Set());
|
|
124
|
+
const [form, setForm] = useState<{ type: ContentType; item?: Item; data?: Record<string, unknown> } | null>(null);
|
|
125
|
+
/** Whether the open item form has changes not saved yet: leaving the browser or the folder asks first. */
|
|
126
|
+
const formDirty = useRef(false);
|
|
127
|
+
/** Why the item form could not be saved, e.g. a required field left empty. */
|
|
128
|
+
const [formError, setFormError] = useState<string>();
|
|
129
|
+
const [deleting, setDeleting] = useState<Entry[] | null>(null);
|
|
130
|
+
const [responses, setResponses] = useState<FormResponse[]>([]);
|
|
131
|
+
/** In Form responses, only the responses of this form (by its name); `null` shows them all. */
|
|
132
|
+
const [responseForm, setResponseForm] = useState<string | null>(initialForm ?? null);
|
|
133
|
+
const [reading, setReading] = useState<FormResponse | null>(null);
|
|
134
|
+
const offered = useOfferedChoices(reading);
|
|
135
|
+
const locales = useSiteLocales();
|
|
136
|
+
/** The item being dragged to a new place, and the row it would land before. */
|
|
137
|
+
const [dragging, setDragging] = useState<string | null>(null);
|
|
138
|
+
const [dropTarget, setDropTarget] = useState<string | null>(null);
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The types show at once and each type's items as they arrive: a type with a thousand items no longer holds back
|
|
142
|
+
* the list of types, nor the others' items. Resolves once everything is read.
|
|
143
|
+
*/
|
|
144
|
+
async function load(): Promise<ContentType[]> {
|
|
145
|
+
const { types: list } = await api.types();
|
|
146
|
+
setTypes(list);
|
|
147
|
+
setLoading(false);
|
|
148
|
+
await Promise.all([
|
|
149
|
+
api.responses().then(({ responses }) => {
|
|
150
|
+
setResponses(responses);
|
|
151
|
+
const opened = initialResponse && responses.find((response) => response.id === initialResponse);
|
|
152
|
+
if (opened) setReading(opened);
|
|
153
|
+
}),
|
|
154
|
+
...list.map(async (type) => {
|
|
155
|
+
const { items } = await api.items(type.key);
|
|
156
|
+
setItemsByType((current) => ({ ...current, [type.key]: items }));
|
|
157
|
+
}),
|
|
158
|
+
]);
|
|
159
|
+
return list;
|
|
160
|
+
}
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
void load().then((list) => {
|
|
163
|
+
const type = initialCreate ? list.find((candidate) => candidate.key === initialFolder) : undefined;
|
|
164
|
+
if (type) setForm({ type, ...(initialData && { data: initialData }) });
|
|
165
|
+
});
|
|
166
|
+
}, []);
|
|
167
|
+
|
|
168
|
+
const folderType = types.find((type) => type.key === folder);
|
|
169
|
+
// The newest item in the default language shows the template as a sample: the template in that language.
|
|
170
|
+
const listed = folderType ? (itemsByType[folderType.key] ?? []) : [];
|
|
171
|
+
const newest = listed.find((item) => item.locale === "") ?? listed[0];
|
|
172
|
+
const sample = folderType && newest ? itemPagePath(folderType, newest, locales ?? undefined) : undefined;
|
|
173
|
+
|
|
174
|
+
const entries = useMemo((): Entry[] => {
|
|
175
|
+
const itemEntries = (type: ContentType): Entry[] =>
|
|
176
|
+
(itemsByType[type.key] ?? []).map((item) => ({
|
|
177
|
+
id: item.id,
|
|
178
|
+
kind: "item",
|
|
179
|
+
title: item.title,
|
|
180
|
+
typeLabel: type.singular,
|
|
181
|
+
typePlural: type.label,
|
|
182
|
+
path: itemPagePath(type, item, locales ?? undefined),
|
|
183
|
+
visibility: item.visibility,
|
|
184
|
+
modified: item.updatedAt,
|
|
185
|
+
item,
|
|
186
|
+
type,
|
|
187
|
+
tags: tagsOf(item.data.tags),
|
|
188
|
+
}));
|
|
189
|
+
const pageEntries: Entry[] = pages.map((page) => ({
|
|
190
|
+
id: `page:${page.path}`,
|
|
191
|
+
kind: "page",
|
|
192
|
+
title: pageTitle(page.path, page.title),
|
|
193
|
+
typeLabel: "Page",
|
|
194
|
+
typePlural: "Pages",
|
|
195
|
+
path: page.path,
|
|
196
|
+
visibility: page.visibility,
|
|
197
|
+
page,
|
|
198
|
+
}));
|
|
199
|
+
const folders: Entry[] = [
|
|
200
|
+
// Vocabularies (lists of terms other types point at) come after the content they describe.
|
|
201
|
+
...[...types.filter((type) => !type.vocabulary), ...types.filter((type) => type.vocabulary)].map(
|
|
202
|
+
(type): Entry => ({
|
|
203
|
+
id: `folder:${type.key}`,
|
|
204
|
+
kind: "folder",
|
|
205
|
+
title: type.label,
|
|
206
|
+
typeLabel: type.vocabulary ? "Vocabulary" : "Content type",
|
|
207
|
+
typePlural: type.vocabulary ? "Vocabularies" : "Content types",
|
|
208
|
+
// No count until its items arrive.
|
|
209
|
+
...(itemsByType[type.key] && { count: itemsByType[type.key]!.length }),
|
|
210
|
+
type,
|
|
211
|
+
}),
|
|
212
|
+
),
|
|
213
|
+
{
|
|
214
|
+
id: `folder:${RESPONSES}`,
|
|
215
|
+
kind: "folder",
|
|
216
|
+
title: "Form responses",
|
|
217
|
+
typeLabel: "Folder",
|
|
218
|
+
typePlural: "Folders",
|
|
219
|
+
count: responses.length,
|
|
220
|
+
},
|
|
221
|
+
];
|
|
222
|
+
const responseEntries: Entry[] = responses
|
|
223
|
+
.filter((response) => responseForm === null || response.title === responseForm)
|
|
224
|
+
.map((response) => ({
|
|
225
|
+
id: response.id,
|
|
226
|
+
kind: "response",
|
|
227
|
+
// The first answer tells responses of the same form apart, and the page which item a template's form is about.
|
|
228
|
+
title: `${response.title}${formPage(response.form) ? ` (${formPage(response.form)})` : ""}: ${response.answers[0]?.value ?? "empty"}`,
|
|
229
|
+
typeLabel: "Response",
|
|
230
|
+
typePlural: "Responses",
|
|
231
|
+
modified: response.createdAt,
|
|
232
|
+
response,
|
|
233
|
+
}));
|
|
234
|
+
|
|
235
|
+
// Searching looks through everything; otherwise only the current folder is listed.
|
|
236
|
+
const searching = query.trim().length >= 2;
|
|
237
|
+
const all = searching
|
|
238
|
+
? [...pageEntries, ...types.flatMap(itemEntries)]
|
|
239
|
+
: folderType
|
|
240
|
+
? itemEntries(folderType)
|
|
241
|
+
: folder === RESPONSES
|
|
242
|
+
? responseEntries
|
|
243
|
+
: [...folders, ...pageEntries];
|
|
244
|
+
const needle = query.trim().toLowerCase();
|
|
245
|
+
// A search finds an entry by its title or by one of its tags.
|
|
246
|
+
const found = (entry: Entry): boolean =>
|
|
247
|
+
entry.title.toLowerCase().includes(needle) ||
|
|
248
|
+
(entry.tags ?? []).some((name) => name.toLowerCase().includes(needle));
|
|
249
|
+
return searching ? all.filter(found) : all;
|
|
250
|
+
}, [types, itemsByType, pages, folderType, folder, responses, responseForm, query, locales]);
|
|
251
|
+
|
|
252
|
+
/** The forms that have responses, for the filters of Form responses. */
|
|
253
|
+
const responseForms = useMemo(() => formsOf(responses), [responses]);
|
|
254
|
+
// Another folder forgets which form was being read.
|
|
255
|
+
useEffect(() => {
|
|
256
|
+
if (folder !== RESPONSES) setResponseForm(null);
|
|
257
|
+
}, [folder]);
|
|
258
|
+
|
|
259
|
+
const typeCounts = useMemo(() => {
|
|
260
|
+
const counts = new Map<string, { plural: string; count: number }>();
|
|
261
|
+
for (const entry of entries) {
|
|
262
|
+
const count = (counts.get(entry.typeLabel)?.count ?? 0) + 1;
|
|
263
|
+
counts.set(entry.typeLabel, { plural: entry.typePlural, count });
|
|
264
|
+
}
|
|
265
|
+
return [...counts];
|
|
266
|
+
}, [entries]);
|
|
267
|
+
|
|
268
|
+
const ofType = filter ? entries.filter((entry) => entry.typeLabel === filter) : entries;
|
|
269
|
+
// The tags of what is listed, most used first; the chosen one stays offered, to be cleared, even if none has it.
|
|
270
|
+
const tagCounts = useMemo(() => {
|
|
271
|
+
const counts = rankTags(ofType.map((entry) => entry.tags));
|
|
272
|
+
return tag && !counts.some((entry) => sameTag(entry.tag, tag)) ? [...counts, { tag, count: 0 }] : counts;
|
|
273
|
+
}, [ofType, tag]);
|
|
274
|
+
const shown = tag ? ofType.filter((entry) => hasTag(entry.tags ?? [], tag)) : ofType;
|
|
275
|
+
const selectable = shown.filter((entry) => entry.kind !== "folder");
|
|
276
|
+
const chosen = selectable.filter((entry) => selected.has(entry.id));
|
|
277
|
+
|
|
278
|
+
// The items of an `ordered` type are put in order by dragging them, while the whole folder is listed: in a
|
|
279
|
+
// search or a filtered list, a place among the rows shown is not a place among all of them.
|
|
280
|
+
const reorderable =
|
|
281
|
+
Boolean(folderType?.ordered) && query.trim().length < 2 && filter === null && tag === null && view === "table";
|
|
282
|
+
|
|
283
|
+
/** Moves an item (written first: its translations follow it) to `index` among the type's items written first. */
|
|
284
|
+
async function moveItem(id: string, index: number): Promise<void> {
|
|
285
|
+
if (!folderType) return;
|
|
286
|
+
const all = itemsByType[folderType.key] ?? [];
|
|
287
|
+
const ids = all.filter((item) => item.translationOf === null).map((item) => item.id);
|
|
288
|
+
const from = ids.indexOf(id);
|
|
289
|
+
if (from === -1) return;
|
|
290
|
+
ids.splice(from, 1);
|
|
291
|
+
ids.splice(Math.max(0, Math.min(index, ids.length)), 0, id);
|
|
292
|
+
if (
|
|
293
|
+
ids.join() ===
|
|
294
|
+
all
|
|
295
|
+
.filter((item) => item.translationOf === null)
|
|
296
|
+
.map((item) => item.id)
|
|
297
|
+
.join()
|
|
298
|
+
)
|
|
299
|
+
return;
|
|
300
|
+
const place = new Map(ids.map((itemId, position) => [itemId, position + 1]));
|
|
301
|
+
const placeOf = (item: Item): number => place.get(item.translationOf ?? item.id) ?? Number.MAX_SAFE_INTEGER;
|
|
302
|
+
// The list shows the new order at once; a refusal puts back the order the site has.
|
|
303
|
+
setItemsByType({
|
|
304
|
+
...itemsByType,
|
|
305
|
+
[folderType.key]: [...all]
|
|
306
|
+
.sort((a, b) => placeOf(a) - placeOf(b))
|
|
307
|
+
.map((item) => ({ ...item, position: placeOf(item) })),
|
|
308
|
+
});
|
|
309
|
+
if (!(await run(api.reorderItems(folderType.key, ids)))) await load();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The items written first, in their order: where up and down move an item. */
|
|
313
|
+
const ordered = (): string[] =>
|
|
314
|
+
(itemsByType[folderType?.key ?? ""] ?? []).filter((item) => item.translationOf === null).map((item) => item.id);
|
|
315
|
+
const orderIndex = (id: string): number => ordered().indexOf(id);
|
|
316
|
+
const orderCount = (): number => ordered().length;
|
|
317
|
+
|
|
318
|
+
/** The place among the items written first that dropping on the row of `targetId` gives. */
|
|
319
|
+
function placeBefore(targetId: string): number {
|
|
320
|
+
const target = (itemsByType[folderType?.key ?? ""] ?? []).find((item) => item.id === targetId);
|
|
321
|
+
const ids = (itemsByType[folderType?.key ?? ""] ?? [])
|
|
322
|
+
.filter((item) => item.translationOf === null)
|
|
323
|
+
.map((item) => item.id);
|
|
324
|
+
// Dropped on a row, the item takes that row's place: above it when it came from below, below it when it came
|
|
325
|
+
// from above (the rows between move up one).
|
|
326
|
+
return ids.indexOf(target?.translationOf ?? targetId);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Runs `leave` once the owner agrees to lose the item form's unsaved changes; at once when it has none. */
|
|
330
|
+
function leaveForm(leave: () => void): void {
|
|
331
|
+
if (form && formDirty.current && !window.confirm("Leave without saving? Your changes to this item will be lost.")) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
leave();
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function openFolder(key: string | null): void {
|
|
338
|
+
// Going somewhere leaves what is open: an item's form or a form message would otherwise stay over the folder.
|
|
339
|
+
setForm(null);
|
|
340
|
+
setFormError(undefined);
|
|
341
|
+
setReading(null);
|
|
342
|
+
setFolder(key);
|
|
343
|
+
setFilter(null);
|
|
344
|
+
setTag(null);
|
|
345
|
+
setQuery("");
|
|
346
|
+
setSelected(new Set());
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function toggle(id: string): void {
|
|
350
|
+
const next = new Set(selected);
|
|
351
|
+
if (!next.delete(id)) next.add(id);
|
|
352
|
+
setSelected(next);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function open(entry: Entry): void {
|
|
356
|
+
if (entry.kind === "folder") openFolder(entry.type?.key ?? RESPONSES);
|
|
357
|
+
else edit(entry);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function edit(entry: Entry): void {
|
|
361
|
+
if (entry.response) setReading(entry.response);
|
|
362
|
+
else if (entry.kind === "page" && entry.path) onEditPage(entry.path);
|
|
363
|
+
// An item with a page of its own is edited there; one without in its form.
|
|
364
|
+
else if (entry.item && entry.type?.hasPage && entry.path) onEditPage(entry.path);
|
|
365
|
+
else if (entry.item && entry.type) setForm({ type: entry.type, item: entry.item });
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async function setVisibility(targets: Entry[], visibility: Visibility): Promise<void> {
|
|
369
|
+
const work = Promise.all(
|
|
370
|
+
targets.map((entry) =>
|
|
371
|
+
entry.item
|
|
372
|
+
? api.setItemVisibility(entry.item.id, visibility)
|
|
373
|
+
: entry.path
|
|
374
|
+
? api.setVisibility(entry.path, visibility)
|
|
375
|
+
: undefined,
|
|
376
|
+
),
|
|
377
|
+
);
|
|
378
|
+
if (await run(work)) {
|
|
379
|
+
setSelected(new Set());
|
|
380
|
+
onPagesChanged();
|
|
381
|
+
await load();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async function confirmDelete(): Promise<void> {
|
|
386
|
+
if (!deleting) return;
|
|
387
|
+
const work = Promise.all(
|
|
388
|
+
deleting.map((entry) =>
|
|
389
|
+
entry.item
|
|
390
|
+
? api.deleteItem(entry.item.id)
|
|
391
|
+
: entry.response
|
|
392
|
+
? api.deleteResponse(entry.response.id)
|
|
393
|
+
: entry.page?.template
|
|
394
|
+
? api.deletePage(entry.page.path)
|
|
395
|
+
: undefined,
|
|
396
|
+
),
|
|
397
|
+
);
|
|
398
|
+
if (await run(work)) {
|
|
399
|
+
if (deleting.some((entry) => entry.page)) onPagesChanged();
|
|
400
|
+
setDeleting(null);
|
|
401
|
+
setReading(null);
|
|
402
|
+
setSelected(new Set());
|
|
403
|
+
await load();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** The open item in each of the site's languages, from the items already loaded. */
|
|
408
|
+
const formLanguages: ItemLanguages | undefined = (() => {
|
|
409
|
+
if (!locales || !form) return undefined;
|
|
410
|
+
const { type, item } = form;
|
|
411
|
+
const current = item ? itemLocale(item, locales) : locales.defaultLocale;
|
|
412
|
+
const family = item ? translationsOf(item, itemsByType[type.key] ?? []) : [];
|
|
413
|
+
return {
|
|
414
|
+
codes: locales.locales,
|
|
415
|
+
current,
|
|
416
|
+
items: Object.fromEntries(family.map((candidate) => [itemLocale(candidate, locales), candidate])),
|
|
417
|
+
onOpen: (next) => {
|
|
418
|
+
setFormError(undefined);
|
|
419
|
+
setForm({ type, item: next });
|
|
420
|
+
},
|
|
421
|
+
onTranslate: (code) => {
|
|
422
|
+
if (!item) return;
|
|
423
|
+
let made: Item | undefined;
|
|
424
|
+
void run(api.translateItem(item.id, code).then((answer) => (made = answer.item))).then(async (done) => {
|
|
425
|
+
if (!done || !made) return;
|
|
426
|
+
await load();
|
|
427
|
+
setForm({ type, item: made });
|
|
428
|
+
});
|
|
429
|
+
},
|
|
430
|
+
};
|
|
431
|
+
})();
|
|
432
|
+
|
|
433
|
+
async function save(draft: ItemDraft): Promise<void> {
|
|
434
|
+
if (!form) return;
|
|
435
|
+
const { type, item } = form;
|
|
436
|
+
let created: Item | undefined;
|
|
437
|
+
const work = item
|
|
438
|
+
? api.updateItem(item.id, draft)
|
|
439
|
+
: api.createItem(type.key, draft).then((answer) => (created = answer.item));
|
|
440
|
+
setFormError(undefined);
|
|
441
|
+
// The toolbar that reports failures is behind the browser, so the form says it itself.
|
|
442
|
+
work.catch((failure: unknown) => setFormError(failure instanceof Error ? failure.message : "Could not save."));
|
|
443
|
+
if (await run(work)) {
|
|
444
|
+
// A new item with a page opens there, to be filled in where it will be seen.
|
|
445
|
+
if (created && type.hasPage) return onEditPage(itemPagePath(type, created, locales ?? undefined));
|
|
446
|
+
// Otherwise it stays open, inside its type's folder: the owner sees it was made and where it is listed.
|
|
447
|
+
if (created) setFolder(type.key);
|
|
448
|
+
setForm(created ? { type, item: created } : null);
|
|
449
|
+
await load();
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
useEffect(() => {
|
|
454
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
455
|
+
// Shortcuts act on the list, never while someone is typing in a field.
|
|
456
|
+
if (form || (event.target as HTMLElement).closest("input, textarea, [contenteditable]")) return;
|
|
457
|
+
if (event.key === "Escape" && !isComposing(event)) setSelected(new Set());
|
|
458
|
+
if ((event.metaKey || event.ctrlKey) && event.key === "a") {
|
|
459
|
+
event.preventDefault();
|
|
460
|
+
setSelected(new Set(selectable.map((entry) => entry.id)));
|
|
461
|
+
}
|
|
462
|
+
const deletable = chosen.filter(isDeletable);
|
|
463
|
+
if ((event.key === "Delete" || event.key === "Backspace") && deletable.length > 0) setDeleting(deletable);
|
|
464
|
+
};
|
|
465
|
+
document.addEventListener("keydown", onKeyDown);
|
|
466
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
const deletable = chosen.filter(isDeletable);
|
|
470
|
+
const allSelected = selectable.length > 0 && chosen.length === selectable.length;
|
|
471
|
+
|
|
472
|
+
return (
|
|
473
|
+
<div className="cb-container">
|
|
474
|
+
<header className="cb-header">
|
|
475
|
+
<div className="cb-header-inner">
|
|
476
|
+
<div className="cb-header-left">
|
|
477
|
+
<button type="button" className="cb-back-btn" onClick={() => leaveForm(onBack)}>
|
|
478
|
+
<ArrowLeftIcon />
|
|
479
|
+
Back
|
|
480
|
+
</button>
|
|
481
|
+
<nav className="cb-breadcrumbs" aria-label="Location">
|
|
482
|
+
<button type="button" className="cb-breadcrumb" aria-label="All content" onClick={() => openFolder(null)}>
|
|
483
|
+
<HomeIcon />
|
|
484
|
+
</button>
|
|
485
|
+
{(folderType || folder === RESPONSES) && (
|
|
486
|
+
<>
|
|
487
|
+
<span className="cb-breadcrumb-separator">/</span>
|
|
488
|
+
<span className="cb-breadcrumb current">{folderType?.label ?? "Form responses"}</span>
|
|
489
|
+
</>
|
|
490
|
+
)}
|
|
491
|
+
</nav>
|
|
492
|
+
</div>
|
|
493
|
+
<label className="cb-search">
|
|
494
|
+
<SearchIcon />
|
|
495
|
+
<input
|
|
496
|
+
type="search"
|
|
497
|
+
placeholder="Search all content..."
|
|
498
|
+
aria-label="Search all content"
|
|
499
|
+
value={query}
|
|
500
|
+
onChange={(event) => setQuery(event.target.value)}
|
|
501
|
+
/>
|
|
502
|
+
</label>
|
|
503
|
+
<div className="cb-header-right">
|
|
504
|
+
<span className="cb-item-count">
|
|
505
|
+
{shown.length} {shown.length === 1 ? "item" : "items"}
|
|
506
|
+
</span>
|
|
507
|
+
{folderType?.hasPage && (
|
|
508
|
+
<button
|
|
509
|
+
type="button"
|
|
510
|
+
className="cb-toolbar-btn"
|
|
511
|
+
disabled={!sample}
|
|
512
|
+
title={
|
|
513
|
+
sample ? "Change what every item's page shows" : `Add a ${folderType.singular.toLowerCase()} first`
|
|
514
|
+
}
|
|
515
|
+
onClick={() => sample && onEditTemplate(sample)}
|
|
516
|
+
>
|
|
517
|
+
Edit template
|
|
518
|
+
</button>
|
|
519
|
+
)}
|
|
520
|
+
{/* Not while a new item is being written: the form's own buttons create it or leave it. */}
|
|
521
|
+
{!(form && !form.item) && (
|
|
522
|
+
<NewButton types={types} folderType={folderType} onNew={(type) => setForm({ type })} />
|
|
523
|
+
)}
|
|
524
|
+
</div>
|
|
525
|
+
</div>
|
|
526
|
+
</header>
|
|
527
|
+
|
|
528
|
+
{reading ? (
|
|
529
|
+
<div className="cb-table-container">
|
|
530
|
+
<article className="cb-item-form">
|
|
531
|
+
<header className="cb-item-form__header">
|
|
532
|
+
<h2>{reading.title}</h2>
|
|
533
|
+
<div className="cb-item-form__actions">
|
|
534
|
+
<button type="button" className="cb-toolbar-btn" onClick={() => setReading(null)}>
|
|
535
|
+
Back
|
|
536
|
+
</button>
|
|
537
|
+
<button
|
|
538
|
+
type="button"
|
|
539
|
+
className="cb-toolbar-btn danger"
|
|
540
|
+
onClick={() => setDeleting(entries.filter((entry) => entry.id === reading.id))}
|
|
541
|
+
>
|
|
542
|
+
Delete
|
|
543
|
+
</button>
|
|
544
|
+
</div>
|
|
545
|
+
</header>
|
|
546
|
+
<p className="field-description">
|
|
547
|
+
Sent {new Date(reading.createdAt).toLocaleString()}
|
|
548
|
+
{formPage(reading.form) ? (
|
|
549
|
+
<>
|
|
550
|
+
{" "}
|
|
551
|
+
from{" "}
|
|
552
|
+
<a href={formPage(reading.form)} target="_blank" rel="noreferrer">
|
|
553
|
+
{formPage(reading.form)}
|
|
554
|
+
</a>
|
|
555
|
+
</>
|
|
556
|
+
) : (
|
|
557
|
+
` from ${reading.form}`
|
|
558
|
+
)}
|
|
559
|
+
</p>
|
|
560
|
+
<dl className="cb-response">
|
|
561
|
+
{reading.answers.map(withOffered(offered)).map((answer) => (
|
|
562
|
+
<div key={answer.label}>
|
|
563
|
+
<dt>{answer.label}</dt>
|
|
564
|
+
{answer.choices?.length ? (
|
|
565
|
+
// As the form asked it: every choice it offered, the picked ones marked.
|
|
566
|
+
<dd className="cb-response-choices">
|
|
567
|
+
{answer.choices.map((choice) => {
|
|
568
|
+
const picked = wasPicked(answer, choice.label);
|
|
569
|
+
return (
|
|
570
|
+
<span
|
|
571
|
+
key={choice.label}
|
|
572
|
+
className="cb-response-choice"
|
|
573
|
+
data-picked={picked ? "" : undefined}
|
|
574
|
+
data-image={choice.image ? "" : undefined}
|
|
575
|
+
>
|
|
576
|
+
{choice.image && <img src={choice.image} alt="" loading="lazy" />}
|
|
577
|
+
{!choice.image && choice.icon && /^(\/|https?:)/.test(choice.icon) && (
|
|
578
|
+
<img className="cb-response-choice-icon" src={choice.icon} alt="" />
|
|
579
|
+
)}
|
|
580
|
+
{!choice.image && choice.icon && !/^(\/|https?:)/.test(choice.icon) && (
|
|
581
|
+
<span className="cb-response-choice-icon">{choice.icon}</span>
|
|
582
|
+
)}
|
|
583
|
+
<span>{choice.label}</span>
|
|
584
|
+
{picked && (
|
|
585
|
+
<span className="cb-response-tick" aria-label="picked">
|
|
586
|
+
✓
|
|
587
|
+
</span>
|
|
588
|
+
)}
|
|
589
|
+
</span>
|
|
590
|
+
);
|
|
591
|
+
})}
|
|
592
|
+
</dd>
|
|
593
|
+
) : (
|
|
594
|
+
<dd>{answer.value}</dd>
|
|
595
|
+
)}
|
|
596
|
+
</div>
|
|
597
|
+
))}
|
|
598
|
+
{/* A visitor's file is only ever downloaded, never opened in the editor's page. */}
|
|
599
|
+
{(reading.files ?? []).map((file) => (
|
|
600
|
+
<div key={file.id}>
|
|
601
|
+
<dt>{file.label}</dt>
|
|
602
|
+
<dd>
|
|
603
|
+
<a href={`/admin/api/responses/${reading.id}/files/${file.id}`} download={file.name}>
|
|
604
|
+
{file.name}
|
|
605
|
+
</a>{" "}
|
|
606
|
+
({fileSize(file.size)})
|
|
607
|
+
</dd>
|
|
608
|
+
</div>
|
|
609
|
+
))}
|
|
610
|
+
</dl>
|
|
611
|
+
</article>
|
|
612
|
+
</div>
|
|
613
|
+
) : form ? (
|
|
614
|
+
<div className="cb-table-container">
|
|
615
|
+
<ItemForm
|
|
616
|
+
key={form.item?.id ?? "new"}
|
|
617
|
+
type={form.type}
|
|
618
|
+
item={form.item}
|
|
619
|
+
initialData={form.data}
|
|
620
|
+
error={formError}
|
|
621
|
+
onSave={(draft) => void save(draft)}
|
|
622
|
+
onDelete={
|
|
623
|
+
form.item
|
|
624
|
+
? () => setDeleting([entries.find((entry) => entry.id === form.item?.id)].filter(isEntry))
|
|
625
|
+
: undefined
|
|
626
|
+
}
|
|
627
|
+
onCancel={() => {
|
|
628
|
+
setFormError(undefined);
|
|
629
|
+
setForm(null);
|
|
630
|
+
}}
|
|
631
|
+
onDirty={(dirty) => {
|
|
632
|
+
formDirty.current = dirty;
|
|
633
|
+
}}
|
|
634
|
+
languages={formLanguages}
|
|
635
|
+
/>
|
|
636
|
+
</div>
|
|
637
|
+
) : (
|
|
638
|
+
<>
|
|
639
|
+
<div className="cb-toolbar">
|
|
640
|
+
<div className="cb-toolbar-inner">
|
|
641
|
+
<label className="cb-select-all">
|
|
642
|
+
<input
|
|
643
|
+
type="checkbox"
|
|
644
|
+
checked={allSelected}
|
|
645
|
+
ref={(input) => {
|
|
646
|
+
if (input) input.indeterminate = chosen.length > 0 && !allSelected;
|
|
647
|
+
}}
|
|
648
|
+
onChange={() => setSelected(allSelected ? new Set() : new Set(selectable.map((entry) => entry.id)))}
|
|
649
|
+
/>
|
|
650
|
+
{chosen.length > 0 ? `${chosen.length} selected` : "Select all"}
|
|
651
|
+
</label>
|
|
652
|
+
<span className="cb-toolbar-divider" />
|
|
653
|
+
{folder === RESPONSES && query.trim().length < 2 ? (
|
|
654
|
+
// In Form responses the filters are the site's forms.
|
|
655
|
+
<div className="cb-type-filters" role="group" aria-label="Forms">
|
|
656
|
+
<button
|
|
657
|
+
type="button"
|
|
658
|
+
className={`cb-type-filter${responseForm === null ? " active" : ""}`}
|
|
659
|
+
onClick={() => setResponseForm(null)}
|
|
660
|
+
>
|
|
661
|
+
All <span>{responses.length}</span>
|
|
662
|
+
</button>
|
|
663
|
+
{responseForms.map(({ title, count }) => (
|
|
664
|
+
<button
|
|
665
|
+
key={title}
|
|
666
|
+
type="button"
|
|
667
|
+
className={`cb-type-filter${responseForm === title ? " active" : ""}`}
|
|
668
|
+
onClick={() => setResponseForm(responseForm === title ? null : title)}
|
|
669
|
+
>
|
|
670
|
+
{title} <span>{count}</span>
|
|
671
|
+
</button>
|
|
672
|
+
))}
|
|
673
|
+
</div>
|
|
674
|
+
) : (
|
|
675
|
+
<div className="cb-type-filters">
|
|
676
|
+
<button
|
|
677
|
+
type="button"
|
|
678
|
+
className={`cb-type-filter${filter === null ? " active" : ""}`}
|
|
679
|
+
onClick={() => setFilter(null)}
|
|
680
|
+
>
|
|
681
|
+
All <span>{entries.length}</span>
|
|
682
|
+
</button>
|
|
683
|
+
{typeCounts.map(([label, { plural, count }]) => (
|
|
684
|
+
<button
|
|
685
|
+
key={label}
|
|
686
|
+
type="button"
|
|
687
|
+
className={`cb-type-filter${filter === label ? " active" : ""}`}
|
|
688
|
+
data-type={typeKind(label)}
|
|
689
|
+
onClick={() => setFilter(filter === label ? null : label)}
|
|
690
|
+
>
|
|
691
|
+
{plural} <span>{count}</span>
|
|
692
|
+
</button>
|
|
693
|
+
))}
|
|
694
|
+
</div>
|
|
695
|
+
)}
|
|
696
|
+
<div className="cb-toolbar-right">
|
|
697
|
+
<div className="cb-view-toggle" role="group" aria-label="View">
|
|
698
|
+
<button
|
|
699
|
+
type="button"
|
|
700
|
+
className={`cb-view-btn${view === "table" ? " active" : ""}`}
|
|
701
|
+
aria-label="Table view"
|
|
702
|
+
title="Table view"
|
|
703
|
+
onClick={() => setView("table")}
|
|
704
|
+
>
|
|
705
|
+
<TableViewIcon />
|
|
706
|
+
</button>
|
|
707
|
+
<button
|
|
708
|
+
type="button"
|
|
709
|
+
className={`cb-view-btn${view === "grid" ? " active" : ""}`}
|
|
710
|
+
aria-label="Grid view"
|
|
711
|
+
title="Grid view"
|
|
712
|
+
onClick={() => setView("grid")}
|
|
713
|
+
>
|
|
714
|
+
<GridViewIcon />
|
|
715
|
+
</button>
|
|
716
|
+
</div>
|
|
717
|
+
<span className="cb-toolbar-divider" />
|
|
718
|
+
<button
|
|
719
|
+
type="button"
|
|
720
|
+
className="cb-toolbar-btn"
|
|
721
|
+
disabled={chosen.length === 0}
|
|
722
|
+
onClick={() => void setVisibility(chosen, "published")}
|
|
723
|
+
>
|
|
724
|
+
Publish
|
|
725
|
+
</button>
|
|
726
|
+
<button
|
|
727
|
+
type="button"
|
|
728
|
+
className="cb-toolbar-btn"
|
|
729
|
+
disabled={chosen.length === 0}
|
|
730
|
+
onClick={() => void setVisibility(chosen, "private")}
|
|
731
|
+
>
|
|
732
|
+
Retract
|
|
733
|
+
</button>
|
|
734
|
+
<span className="cb-toolbar-divider" />
|
|
735
|
+
<button
|
|
736
|
+
type="button"
|
|
737
|
+
className="cb-toolbar-btn danger"
|
|
738
|
+
disabled={deletable.length === 0}
|
|
739
|
+
onClick={() => setDeleting(deletable)}
|
|
740
|
+
>
|
|
741
|
+
<TrashIcon />
|
|
742
|
+
Delete
|
|
743
|
+
</button>
|
|
744
|
+
</div>
|
|
745
|
+
</div>
|
|
746
|
+
{tagCounts.length > 0 && (
|
|
747
|
+
<div className="cb-toolbar-inner cb-tag-filters" role="group" aria-label="Filter by tag">
|
|
748
|
+
<span className="cb-tag-filters__label">Tags</span>
|
|
749
|
+
{tagCounts.map(({ tag: name, count }) => {
|
|
750
|
+
const active = tag !== null && sameTag(name, tag);
|
|
751
|
+
return (
|
|
752
|
+
<button
|
|
753
|
+
key={name}
|
|
754
|
+
type="button"
|
|
755
|
+
className={`cb-type-filter${active ? " active" : ""}`}
|
|
756
|
+
data-type="tag"
|
|
757
|
+
aria-pressed={active}
|
|
758
|
+
onClick={() => setTag(active ? null : name)}
|
|
759
|
+
>
|
|
760
|
+
{name} <span>{count}</span>
|
|
761
|
+
</button>
|
|
762
|
+
);
|
|
763
|
+
})}
|
|
764
|
+
</div>
|
|
765
|
+
)}
|
|
766
|
+
</div>
|
|
767
|
+
|
|
768
|
+
{loading || (folderType && !(folderType.key in itemsByType)) ? (
|
|
769
|
+
<p className="cb-state">Loading...</p>
|
|
770
|
+
) : shown.length === 0 ? (
|
|
771
|
+
<EmptyState
|
|
772
|
+
searching={query.trim().length >= 2}
|
|
773
|
+
folderType={folderType}
|
|
774
|
+
onCreate={(type) => setForm({ type })}
|
|
775
|
+
/>
|
|
776
|
+
) : view === "table" ? (
|
|
777
|
+
<div className="cb-table-container">
|
|
778
|
+
<table className="cb-table">
|
|
779
|
+
<thead className="cb-table-header">
|
|
780
|
+
<tr>
|
|
781
|
+
<th className="cb-col-check" />
|
|
782
|
+
<th>Name</th>
|
|
783
|
+
<th className="cb-col-type">Type</th>
|
|
784
|
+
<th className="cb-col-status">Status</th>
|
|
785
|
+
<th className="cb-col-modified">Modified</th>
|
|
786
|
+
<th className="cb-col-actions" />
|
|
787
|
+
</tr>
|
|
788
|
+
</thead>
|
|
789
|
+
<tbody>
|
|
790
|
+
{shown.map((entry) => {
|
|
791
|
+
const movable = reorderable && entry.item?.translationOf === null;
|
|
792
|
+
return (
|
|
793
|
+
<tr
|
|
794
|
+
key={entry.id}
|
|
795
|
+
className={`cb-row${selected.has(entry.id) ? " selected" : ""}${
|
|
796
|
+
dropTarget === entry.id && dragging !== entry.id ? " cb-row-drop" : ""
|
|
797
|
+
}${dragging === entry.id ? " cb-row-dragging" : ""}`}
|
|
798
|
+
data-type={typeKind(entry.typeLabel)}
|
|
799
|
+
onClick={() => open(entry)}
|
|
800
|
+
draggable={movable}
|
|
801
|
+
onDragStart={
|
|
802
|
+
movable
|
|
803
|
+
? (event) => {
|
|
804
|
+
event.dataTransfer.effectAllowed = "move";
|
|
805
|
+
event.dataTransfer.setData("text/plain", entry.id);
|
|
806
|
+
setDragging(entry.id);
|
|
807
|
+
}
|
|
808
|
+
: undefined
|
|
809
|
+
}
|
|
810
|
+
onDragOver={
|
|
811
|
+
reorderable && dragging
|
|
812
|
+
? (event) => {
|
|
813
|
+
event.preventDefault();
|
|
814
|
+
event.dataTransfer.dropEffect = "move";
|
|
815
|
+
setDropTarget(entry.id);
|
|
816
|
+
}
|
|
817
|
+
: undefined
|
|
818
|
+
}
|
|
819
|
+
onDrop={
|
|
820
|
+
reorderable && dragging
|
|
821
|
+
? (event) => {
|
|
822
|
+
event.preventDefault();
|
|
823
|
+
const moved = dragging;
|
|
824
|
+
setDragging(null);
|
|
825
|
+
setDropTarget(null);
|
|
826
|
+
if (moved !== entry.id) void moveItem(moved, placeBefore(entry.id));
|
|
827
|
+
}
|
|
828
|
+
: undefined
|
|
829
|
+
}
|
|
830
|
+
onDragEnd={() => {
|
|
831
|
+
setDragging(null);
|
|
832
|
+
setDropTarget(null);
|
|
833
|
+
}}
|
|
834
|
+
>
|
|
835
|
+
<td className="cb-col-check" onClick={(event) => event.stopPropagation()}>
|
|
836
|
+
{entry.kind !== "folder" && (
|
|
837
|
+
<input
|
|
838
|
+
type="checkbox"
|
|
839
|
+
aria-label={`Select ${entry.title}`}
|
|
840
|
+
checked={selected.has(entry.id)}
|
|
841
|
+
onChange={() => toggle(entry.id)}
|
|
842
|
+
/>
|
|
843
|
+
)}
|
|
844
|
+
</td>
|
|
845
|
+
<td>
|
|
846
|
+
<div className="cb-name">
|
|
847
|
+
{reorderable && (
|
|
848
|
+
<button
|
|
849
|
+
type="button"
|
|
850
|
+
className="cb-drag-handle"
|
|
851
|
+
disabled={!movable}
|
|
852
|
+
aria-label={`Move ${entry.title || "Untitled"}: drag, or use the up and down arrows`}
|
|
853
|
+
title="Drag to change the order"
|
|
854
|
+
onClick={(event) => event.stopPropagation()}
|
|
855
|
+
onKeyDown={(event) => {
|
|
856
|
+
if (event.key !== "ArrowUp" && event.key !== "ArrowDown") return;
|
|
857
|
+
event.preventDefault();
|
|
858
|
+
const ids = (itemsByType[folderType?.key ?? ""] ?? [])
|
|
859
|
+
.filter((item) => item.translationOf === null)
|
|
860
|
+
.map((item) => item.id);
|
|
861
|
+
const index = ids.indexOf(entry.id);
|
|
862
|
+
void moveItem(entry.id, index + (event.key === "ArrowUp" ? -1 : 1));
|
|
863
|
+
}}
|
|
864
|
+
>
|
|
865
|
+
<DragIcon />
|
|
866
|
+
</button>
|
|
867
|
+
)}
|
|
868
|
+
{reorderable && movable && (
|
|
869
|
+
// The same move as dragging, one place at a time, for a click or a touch screen.
|
|
870
|
+
<span className="cb-move" onClick={(event) => event.stopPropagation()}>
|
|
871
|
+
<button
|
|
872
|
+
type="button"
|
|
873
|
+
className="cb-move-btn"
|
|
874
|
+
aria-label={`Move ${entry.title || "Untitled"} up`}
|
|
875
|
+
title="Move up"
|
|
876
|
+
disabled={orderIndex(entry.id) <= 0}
|
|
877
|
+
onClick={() => void moveItem(entry.id, orderIndex(entry.id) - 1)}
|
|
878
|
+
>
|
|
879
|
+
↑
|
|
880
|
+
</button>
|
|
881
|
+
<button
|
|
882
|
+
type="button"
|
|
883
|
+
className="cb-move-btn"
|
|
884
|
+
aria-label={`Move ${entry.title || "Untitled"} down`}
|
|
885
|
+
title="Move down"
|
|
886
|
+
disabled={orderIndex(entry.id) >= orderCount() - 1}
|
|
887
|
+
onClick={() => void moveItem(entry.id, orderIndex(entry.id) + 1)}
|
|
888
|
+
>
|
|
889
|
+
↓
|
|
890
|
+
</button>
|
|
891
|
+
</span>
|
|
892
|
+
)}
|
|
893
|
+
<TypeIcon entry={entry} />
|
|
894
|
+
<span className="cb-name-title">{entry.title || "Untitled"}</span>
|
|
895
|
+
{locales && entry.item && (
|
|
896
|
+
<span className="cb-badge cb-badge-language">
|
|
897
|
+
{itemLocale(entry.item, locales).toUpperCase()}
|
|
898
|
+
</span>
|
|
899
|
+
)}
|
|
900
|
+
{entry.path && <code>{entry.path}</code>}
|
|
901
|
+
{entry.tags && entry.tags.length > 0 && (
|
|
902
|
+
<EntryTags tags={entry.tags} active={tag} onPick={setTag} />
|
|
903
|
+
)}
|
|
904
|
+
{entry.count !== undefined && (
|
|
905
|
+
<code>
|
|
906
|
+
{entry.count} {entry.count === 1 ? "item" : "items"}
|
|
907
|
+
</code>
|
|
908
|
+
)}
|
|
909
|
+
</div>
|
|
910
|
+
</td>
|
|
911
|
+
<td className="cb-col-type">
|
|
912
|
+
<span className="cb-badge cb-badge-type">{entry.typeLabel}</span>
|
|
913
|
+
</td>
|
|
914
|
+
<td className="cb-col-status" onClick={(event) => event.stopPropagation()}>
|
|
915
|
+
{entry.visibility && (
|
|
916
|
+
<StatusDropdown
|
|
917
|
+
visibility={entry.visibility}
|
|
918
|
+
onChange={(next) => void setVisibility([entry], next)}
|
|
919
|
+
/>
|
|
920
|
+
)}
|
|
921
|
+
</td>
|
|
922
|
+
<td className="cb-col-modified">{relativeDate(entry.modified)}</td>
|
|
923
|
+
<td className="cb-col-actions" onClick={(event) => event.stopPropagation()}>
|
|
924
|
+
<EntryActions
|
|
925
|
+
entry={entry}
|
|
926
|
+
onEdit={() => edit(entry)}
|
|
927
|
+
onDelete={() => setDeleting([entry])}
|
|
928
|
+
onNew={(type) => setForm({ type })}
|
|
929
|
+
/>
|
|
930
|
+
</td>
|
|
931
|
+
</tr>
|
|
932
|
+
);
|
|
933
|
+
})}
|
|
934
|
+
</tbody>
|
|
935
|
+
</table>
|
|
936
|
+
</div>
|
|
937
|
+
) : (
|
|
938
|
+
<div className="cb-grid-container">
|
|
939
|
+
<div className="cb-grid">
|
|
940
|
+
{shown.map((entry) => (
|
|
941
|
+
<article
|
|
942
|
+
key={entry.id}
|
|
943
|
+
className={`cb-card${selected.has(entry.id) ? " selected" : ""}`}
|
|
944
|
+
data-type={typeKind(entry.typeLabel)}
|
|
945
|
+
onClick={(event) => (event.metaKey || event.ctrlKey ? toggle(entry.id) : open(entry))}
|
|
946
|
+
>
|
|
947
|
+
<div className="cb-card-preview">
|
|
948
|
+
{coverOf(entry) ? <img src={coverOf(entry)} alt="" /> : <TypeIcon entry={entry} />}
|
|
949
|
+
</div>
|
|
950
|
+
<div className="cb-card-info">
|
|
951
|
+
<span className="cb-card-title">{entry.title || "Untitled"}</span>
|
|
952
|
+
<span className="cb-card-type">
|
|
953
|
+
{entry.typeLabel}
|
|
954
|
+
{entry.count !== undefined && ` · ${entry.count} ${entry.count === 1 ? "item" : "items"}`}
|
|
955
|
+
</span>
|
|
956
|
+
{entry.tags && entry.tags.length > 0 && (
|
|
957
|
+
<EntryTags tags={entry.tags} active={tag} onPick={setTag} />
|
|
958
|
+
)}
|
|
959
|
+
</div>
|
|
960
|
+
<div className="cb-card-actions" onClick={(event) => event.stopPropagation()}>
|
|
961
|
+
<EntryActions
|
|
962
|
+
entry={entry}
|
|
963
|
+
onEdit={() => edit(entry)}
|
|
964
|
+
onDelete={() => setDeleting([entry])}
|
|
965
|
+
onNew={(type) => setForm({ type })}
|
|
966
|
+
/>
|
|
967
|
+
</div>
|
|
968
|
+
</article>
|
|
969
|
+
))}
|
|
970
|
+
</div>
|
|
971
|
+
</div>
|
|
972
|
+
)}
|
|
973
|
+
</>
|
|
974
|
+
)}
|
|
975
|
+
|
|
976
|
+
{deleting && (
|
|
977
|
+
<div className="cb-modal-overlay" onClick={() => setDeleting(null)}>
|
|
978
|
+
<div className="cb-modal" role="alertdialog" aria-label="Delete" onClick={(event) => event.stopPropagation()}>
|
|
979
|
+
<h3>Delete {deleting.length === 1 ? "Item" : "Items"}</h3>
|
|
980
|
+
<p>
|
|
981
|
+
Are you sure you want to delete{" "}
|
|
982
|
+
{deleting.length === 1 ? `"${deleting[0]?.title}"` : `${deleting.length} items`}? This action cannot be
|
|
983
|
+
undone.
|
|
984
|
+
</p>
|
|
985
|
+
{deleting.length > 1 && (
|
|
986
|
+
<ul>
|
|
987
|
+
{deleting.slice(0, 5).map((entry) => (
|
|
988
|
+
<li key={entry.id}>{entry.title}</li>
|
|
989
|
+
))}
|
|
990
|
+
{deleting.length > 5 && <li>...and {deleting.length - 5} more</li>}
|
|
991
|
+
</ul>
|
|
992
|
+
)}
|
|
993
|
+
<div className="cb-modal-actions">
|
|
994
|
+
<button type="button" className="cb-toolbar-btn" onClick={() => setDeleting(null)}>
|
|
995
|
+
Cancel
|
|
996
|
+
</button>
|
|
997
|
+
<button
|
|
998
|
+
type="button"
|
|
999
|
+
className="cb-new-btn danger"
|
|
1000
|
+
onClick={() => void confirmDelete().then(() => setForm(null))}
|
|
1001
|
+
>
|
|
1002
|
+
Delete
|
|
1003
|
+
</button>
|
|
1004
|
+
</div>
|
|
1005
|
+
</div>
|
|
1006
|
+
</div>
|
|
1007
|
+
)}
|
|
1008
|
+
</div>
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/** Folders and pages written in code stay; items, responses and pages added from a template can go. */
|
|
1013
|
+
function isDeletable(entry: Entry): boolean {
|
|
1014
|
+
return entry.kind === "item" || entry.kind === "response" || Boolean(entry.page?.template);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
function isEntry(entry: Entry | undefined): entry is Entry {
|
|
1018
|
+
return entry !== undefined;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
/** Pages, folders and items each have their own color; all item types share one. */
|
|
1022
|
+
function typeKind(typeLabel: string): "folder" | "page" | "custom" {
|
|
1023
|
+
if (typeLabel === "Folder" || typeLabel === "Content type") return "folder";
|
|
1024
|
+
return typeLabel === "Page" ? "page" : "custom";
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/** The item's own picture, for its card. */
|
|
1028
|
+
function coverOf(entry: Entry): string | undefined {
|
|
1029
|
+
return entry.type && entry.item ? itemImage(entry.type, entry.item) : undefined;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
function TypeIcon({ entry }: { entry: Entry }): ReactNode {
|
|
1033
|
+
return <span className="cb-type-icon">{entry.kind === "folder" ? <FolderIcon /> : <PageIcon />}</span>;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function NewButton({
|
|
1037
|
+
types,
|
|
1038
|
+
folderType,
|
|
1039
|
+
onNew,
|
|
1040
|
+
}: {
|
|
1041
|
+
types: ContentType[];
|
|
1042
|
+
folderType: ContentType | undefined;
|
|
1043
|
+
onNew: (type: ContentType) => void;
|
|
1044
|
+
}): ReactNode {
|
|
1045
|
+
const label = (
|
|
1046
|
+
<>
|
|
1047
|
+
<PlusIcon />
|
|
1048
|
+
New
|
|
1049
|
+
</>
|
|
1050
|
+
);
|
|
1051
|
+
if (folderType) {
|
|
1052
|
+
return (
|
|
1053
|
+
<button type="button" className="cb-new-btn" onClick={() => onNew(folderType)}>
|
|
1054
|
+
<PlusIcon />
|
|
1055
|
+
New {folderType.singular}
|
|
1056
|
+
</button>
|
|
1057
|
+
);
|
|
1058
|
+
}
|
|
1059
|
+
if (types.length === 0) return null;
|
|
1060
|
+
return (
|
|
1061
|
+
<Menu label="New content" className="cb-new-btn" align="end" trigger={label}>
|
|
1062
|
+
{(close) =>
|
|
1063
|
+
types.map((type) => (
|
|
1064
|
+
<button
|
|
1065
|
+
key={type.key}
|
|
1066
|
+
type="button"
|
|
1067
|
+
role="menuitem"
|
|
1068
|
+
className="brim-editor-menu__item"
|
|
1069
|
+
onClick={() => {
|
|
1070
|
+
close();
|
|
1071
|
+
onNew(type);
|
|
1072
|
+
}}
|
|
1073
|
+
>
|
|
1074
|
+
{type.singular}
|
|
1075
|
+
</button>
|
|
1076
|
+
))
|
|
1077
|
+
}
|
|
1078
|
+
</Menu>
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
function EntryActions({
|
|
1083
|
+
entry,
|
|
1084
|
+
onEdit,
|
|
1085
|
+
onDelete,
|
|
1086
|
+
onNew,
|
|
1087
|
+
}: {
|
|
1088
|
+
entry: Entry;
|
|
1089
|
+
onEdit: () => void;
|
|
1090
|
+
onDelete: () => void;
|
|
1091
|
+
onNew: (type: ContentType) => void;
|
|
1092
|
+
}): ReactNode {
|
|
1093
|
+
if (entry.kind === "folder") {
|
|
1094
|
+
const type = entry.type;
|
|
1095
|
+
if (!type) return null;
|
|
1096
|
+
return (
|
|
1097
|
+
<div className="cb-actions">
|
|
1098
|
+
<button type="button" className="cb-toolbar-btn cb-folder-new" onClick={() => onNew(type)}>
|
|
1099
|
+
<PlusIcon />
|
|
1100
|
+
New {type.singular}
|
|
1101
|
+
</button>
|
|
1102
|
+
</div>
|
|
1103
|
+
);
|
|
1104
|
+
}
|
|
1105
|
+
return (
|
|
1106
|
+
<div className="cb-actions">
|
|
1107
|
+
{entry.path && (
|
|
1108
|
+
<a
|
|
1109
|
+
className="cb-action-btn"
|
|
1110
|
+
href={entry.path}
|
|
1111
|
+
target="_blank"
|
|
1112
|
+
rel="noreferrer"
|
|
1113
|
+
title="View"
|
|
1114
|
+
aria-label={`View ${entry.title}`}
|
|
1115
|
+
>
|
|
1116
|
+
<EyeIcon />
|
|
1117
|
+
</a>
|
|
1118
|
+
)}
|
|
1119
|
+
<button type="button" className="cb-action-btn" title="Edit" aria-label={`Edit ${entry.title}`} onClick={onEdit}>
|
|
1120
|
+
<PencilIcon />
|
|
1121
|
+
</button>
|
|
1122
|
+
{entry.kind === "item" && (
|
|
1123
|
+
<button
|
|
1124
|
+
type="button"
|
|
1125
|
+
className="cb-action-btn danger"
|
|
1126
|
+
title="Delete"
|
|
1127
|
+
aria-label={`Delete ${entry.title}`}
|
|
1128
|
+
onClick={onDelete}
|
|
1129
|
+
>
|
|
1130
|
+
<TrashIcon />
|
|
1131
|
+
</button>
|
|
1132
|
+
)}
|
|
1133
|
+
</div>
|
|
1134
|
+
);
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
const PUBLICATION = [
|
|
1138
|
+
{ value: "published", label: "Published", description: "Visible on the public site" },
|
|
1139
|
+
{ value: "private", label: "Private", description: "Only visible in the editor" },
|
|
1140
|
+
] as const;
|
|
1141
|
+
|
|
1142
|
+
function StatusDropdown({
|
|
1143
|
+
visibility,
|
|
1144
|
+
onChange,
|
|
1145
|
+
}: {
|
|
1146
|
+
visibility: Visibility;
|
|
1147
|
+
onChange: (visibility: Visibility) => void;
|
|
1148
|
+
}) {
|
|
1149
|
+
return (
|
|
1150
|
+
<Menu label="Change status" className={`cb-badge cb-badge-status ${visibility}`} trigger={visibility}>
|
|
1151
|
+
{(close) => (
|
|
1152
|
+
<>
|
|
1153
|
+
<p className="cb-status-heading">Publication</p>
|
|
1154
|
+
{PUBLICATION.map((option) => (
|
|
1155
|
+
<button
|
|
1156
|
+
key={option.value}
|
|
1157
|
+
type="button"
|
|
1158
|
+
role="menuitemradio"
|
|
1159
|
+
aria-checked={option.value === visibility}
|
|
1160
|
+
className="cb-status-option"
|
|
1161
|
+
onClick={() => {
|
|
1162
|
+
close();
|
|
1163
|
+
if (option.value !== visibility) onChange(option.value);
|
|
1164
|
+
}}
|
|
1165
|
+
>
|
|
1166
|
+
<span className={`cb-status-dot ${option.value}`} />
|
|
1167
|
+
<span>
|
|
1168
|
+
<strong>{option.label}</strong>
|
|
1169
|
+
<small>{option.description}</small>
|
|
1170
|
+
</span>
|
|
1171
|
+
{option.value === visibility && <span className="cb-status-check">✓</span>}
|
|
1172
|
+
</button>
|
|
1173
|
+
))}
|
|
1174
|
+
</>
|
|
1175
|
+
)}
|
|
1176
|
+
</Menu>
|
|
1177
|
+
);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
function EmptyState({
|
|
1181
|
+
searching,
|
|
1182
|
+
folderType,
|
|
1183
|
+
onCreate,
|
|
1184
|
+
}: {
|
|
1185
|
+
searching: boolean;
|
|
1186
|
+
folderType: ContentType | undefined;
|
|
1187
|
+
onCreate: (type: ContentType) => void;
|
|
1188
|
+
}) {
|
|
1189
|
+
if (searching) return <p className="cb-state">No results found</p>;
|
|
1190
|
+
return (
|
|
1191
|
+
<div className="cb-empty">
|
|
1192
|
+
<FolderIcon />
|
|
1193
|
+
<h3>This folder is empty</h3>
|
|
1194
|
+
<p>Get started by creating your first piece of content</p>
|
|
1195
|
+
{folderType && (
|
|
1196
|
+
<button type="button" className="cb-new-btn" onClick={() => onCreate(folderType)}>
|
|
1197
|
+
Create {folderType.singular}
|
|
1198
|
+
</button>
|
|
1199
|
+
)}
|
|
1200
|
+
</div>
|
|
1201
|
+
);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
/** The first tags of an entry; one clicked lists only what has it. */
|
|
1205
|
+
function EntryTags({ tags, active, onPick }: { tags: string[]; active: string | null; onPick: (tag: string) => void }) {
|
|
1206
|
+
const SHOWN = 3;
|
|
1207
|
+
return (
|
|
1208
|
+
<span className="cb-tags">
|
|
1209
|
+
{tags.slice(0, SHOWN).map((name) => (
|
|
1210
|
+
<button
|
|
1211
|
+
key={name}
|
|
1212
|
+
type="button"
|
|
1213
|
+
className={`cb-tag${active !== null && sameTag(name, active) ? " active" : ""}`}
|
|
1214
|
+
title={`Only what is tagged ${name}`}
|
|
1215
|
+
onClick={(event) => {
|
|
1216
|
+
event.stopPropagation();
|
|
1217
|
+
onPick(name);
|
|
1218
|
+
}}
|
|
1219
|
+
>
|
|
1220
|
+
{name}
|
|
1221
|
+
</button>
|
|
1222
|
+
))}
|
|
1223
|
+
{tags.length > SHOWN && <span className="cb-tags__more">+{tags.length - SHOWN}</span>}
|
|
1224
|
+
</span>
|
|
1225
|
+
);
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
type Answer = FormResponse["answers"][number];
|
|
1229
|
+
type Offered = Map<string, { type: string; choices: { label: string; icon?: string; image?: string }[] }>;
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* The choices of the form a response came from, as its page offers them now: for responses sent before answers kept
|
|
1233
|
+
* their choices, read from the form's spec on its page. Empty when the page or the form is gone.
|
|
1234
|
+
*/
|
|
1235
|
+
function useOfferedChoices(response: FormResponse | null): Offered {
|
|
1236
|
+
const [offered, setOffered] = useState<Offered>(new Map());
|
|
1237
|
+
const page = response && response.answers.some((answer) => !answer.choices) ? formPage(response.form) : undefined;
|
|
1238
|
+
const form = response?.form;
|
|
1239
|
+
useEffect(() => {
|
|
1240
|
+
setOffered(new Map());
|
|
1241
|
+
if (!page || !form) return;
|
|
1242
|
+
let current = true;
|
|
1243
|
+
fetch(page)
|
|
1244
|
+
.then((reply) => (reply.ok ? reply.text() : ""))
|
|
1245
|
+
.then((html) => {
|
|
1246
|
+
const document = new DOMParser().parseFromString(html, "text/html");
|
|
1247
|
+
for (const element of document.querySelectorAll<HTMLElement>("brim-form[data-spec]")) {
|
|
1248
|
+
const spec = JSON.parse(
|
|
1249
|
+
new TextDecoder().decode(
|
|
1250
|
+
Uint8Array.from(atob((element.dataset.spec ?? "").replace(/-/g, "+").replace(/_/g, "/")), (c) =>
|
|
1251
|
+
c.charCodeAt(0),
|
|
1252
|
+
),
|
|
1253
|
+
),
|
|
1254
|
+
) as {
|
|
1255
|
+
form?: string;
|
|
1256
|
+
fields?: { label: string; type: string; options?: string[]; choices?: Answer["choices"] }[];
|
|
1257
|
+
};
|
|
1258
|
+
if (spec.form !== form) continue;
|
|
1259
|
+
const found: Offered = new Map();
|
|
1260
|
+
for (const field of spec.fields ?? []) {
|
|
1261
|
+
const choices = field.choices ?? field.options?.map((label) => ({ label }));
|
|
1262
|
+
if (choices?.length) found.set(field.label, { type: field.type, choices });
|
|
1263
|
+
}
|
|
1264
|
+
if (current) setOffered(found);
|
|
1265
|
+
}
|
|
1266
|
+
})
|
|
1267
|
+
.catch(() => {});
|
|
1268
|
+
return () => {
|
|
1269
|
+
current = false;
|
|
1270
|
+
};
|
|
1271
|
+
}, [page, form]);
|
|
1272
|
+
return offered;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
/** An answer without its choices takes them from the form as it is now, when the question is still asked. */
|
|
1276
|
+
function withOffered(offered: Offered): (answer: Answer) => Answer {
|
|
1277
|
+
return (answer) => {
|
|
1278
|
+
const known = answer.choices ? undefined : offered.get(answer.label);
|
|
1279
|
+
if (!known) return answer;
|
|
1280
|
+
return {
|
|
1281
|
+
...answer,
|
|
1282
|
+
choices: known.choices,
|
|
1283
|
+
picked: known.choices.map((choice) => choice.label).filter((label) => containsChoice(answer.value, label)),
|
|
1284
|
+
};
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Whether a choice was picked. The answer's text joins the picked choices with ", ", and a choice's own words may
|
|
1290
|
+
* hold a comma ("Cald, cu materiale naturale"), so the text is searched for each whole choice; `picked` lists that
|
|
1291
|
+
* were cut at such a comma are not trusted alone.
|
|
1292
|
+
*/
|
|
1293
|
+
function wasPicked(answer: Answer, label: string): boolean {
|
|
1294
|
+
return (answer.picked?.includes(label) ?? false) || containsChoice(answer.value, label);
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function containsChoice(value: string, label: string): boolean {
|
|
1298
|
+
return value === label || `, ${value}, `.includes(`, ${label}, `);
|
|
1299
|
+
}
|