@bieberkit/plugin-pages 0.0.0-stage → 0.6.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.
@@ -0,0 +1,151 @@
1
+ import { DraftForm } from "../components/DraftForm.js";
2
+ import { draftsApi, listPublications, newPublication, publicationsKey } from "../lib/publications.js";
3
+ import { useAdminClient, useCan, useLanguage, useText } from "@bieberkit/admin-antd";
4
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
5
+ import { Alert, Button, Form, Input, Skeleton, Space, Table } from "antd";
6
+ import { useState } from "react";
7
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
+ // src/admin/pages/PublicationsPage.tsx
9
+ /**
10
+ * BieberKit
11
+ *
12
+ * @file plugins/pages/src/admin/pages/PublicationsPage.tsx
13
+ * @description Lists editorial records and opens a locale-specific draft.
14
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
15
+ * @copyright Copyright (c) 2026 Bieber Kieu
16
+ * @license Apache-2.0
17
+ *
18
+ * Licensed under the Apache License, Version 2.0 (the "License");
19
+ * you may not use this file except in compliance with the License.
20
+ * You may obtain a copy of the License at
21
+ *
22
+ * http://www.apache.org/licenses/LICENSE-2.0
23
+ *
24
+ * Unless required by applicable law or agreed to in writing, software
25
+ * distributed under the License is distributed on an "AS IS" BASIS,
26
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ * See the License for the specific language governing permissions and
28
+ * limitations under the License.
29
+ *
30
+ * SPDX-License-Identifier: Apache-2.0
31
+ */
32
+ function PublicationsPage() {
33
+ const client = useAdminClient();
34
+ const cache = useQueryClient();
35
+ const text = useText();
36
+ const { locale: language } = useLanguage();
37
+ const canManage = useCan("pages.manage");
38
+ const [page, setPage] = useState(1);
39
+ const [selected, select] = useState();
40
+ const id = selected?.id;
41
+ const [locale, setLocale] = useState(language);
42
+ const list = useQuery({
43
+ queryKey: [...publicationsKey, page],
44
+ queryFn: () => listPublications(client, page)
45
+ });
46
+ const create = useMutation({
47
+ mutationFn: (kind) => newPublication(client, kind),
48
+ onSuccess: async (created, kind) => {
49
+ if (created !== void 0) select({
50
+ id: created,
51
+ kind
52
+ });
53
+ await cache.invalidateQueries({ queryKey: publicationsKey });
54
+ }
55
+ });
56
+ const draft = useQuery({
57
+ queryKey: [
58
+ ...publicationsKey,
59
+ "draft",
60
+ id,
61
+ locale
62
+ ],
63
+ queryFn: () => id === void 0 ? Promise.resolve(null) : draftsApi(client, id, locale).get(),
64
+ enabled: id !== void 0
65
+ });
66
+ const error = create.error ?? draft.error ?? list.error;
67
+ return /* @__PURE__ */ jsxs(Space, {
68
+ orientation: "vertical",
69
+ size: "large",
70
+ style: { width: "100%" },
71
+ children: [
72
+ error && /* @__PURE__ */ jsx(Alert, {
73
+ title: error.message,
74
+ type: "error",
75
+ showIcon: true,
76
+ action: /* @__PURE__ */ jsx(Button, {
77
+ type: "link",
78
+ onClick: () => {
79
+ if (list.isError) list.refetch();
80
+ if (draft.isError) draft.refetch();
81
+ create.reset();
82
+ },
83
+ children: text("pages.retry")
84
+ })
85
+ }),
86
+ canManage && /* @__PURE__ */ jsxs(Space, { children: [/* @__PURE__ */ jsx(Button, {
87
+ loading: create.isPending,
88
+ onClick: () => create.mutate("page"),
89
+ children: text("pages.createPage")
90
+ }), /* @__PURE__ */ jsx(Button, {
91
+ loading: create.isPending,
92
+ onClick: () => create.mutate("article"),
93
+ children: text("pages.createArticle")
94
+ })] }),
95
+ /* @__PURE__ */ jsx(Table, {
96
+ rowKey: "id",
97
+ loading: list.isPending,
98
+ dataSource: list.data?.data ?? [],
99
+ locale: { emptyText: text("pages.empty") },
100
+ pagination: {
101
+ current: page,
102
+ total: list.data?.total ?? 0,
103
+ pageSize: 50,
104
+ showSizeChanger: false,
105
+ onChange: setPage
106
+ },
107
+ columns: [
108
+ {
109
+ key: "title",
110
+ title: text("pages.name"),
111
+ render: (_, row) => row.title ?? text("pages.untitled")
112
+ },
113
+ {
114
+ key: "kind",
115
+ title: text("pages.content"),
116
+ render: (_, row) => text("pages." + row.kind)
117
+ },
118
+ {
119
+ key: "edit",
120
+ render: (_, row) => /* @__PURE__ */ jsx(Button, {
121
+ onClick: () => select(row),
122
+ children: text("pages.edit")
123
+ })
124
+ }
125
+ ]
126
+ }),
127
+ id === void 0 ? /* @__PURE__ */ jsx("p", { children: text("pages.choose") }) : /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(Form, {
128
+ layout: "inline",
129
+ initialValues: { locale },
130
+ onFinish: (value) => {
131
+ if (value.locale.trim() !== "") setLocale(value.locale.trim());
132
+ },
133
+ children: [/* @__PURE__ */ jsx(Form.Item, {
134
+ name: "locale",
135
+ label: text("pages.locale"),
136
+ rules: [{ required: true }],
137
+ children: /* @__PURE__ */ jsx(Input, { maxLength: 32 })
138
+ }), /* @__PURE__ */ jsx(Button, {
139
+ htmlType: "submit",
140
+ children: text("pages.load")
141
+ })]
142
+ }), draft.isPending ? /* @__PURE__ */ jsx(Skeleton, { active: true }) : draft.data !== void 0 && /* @__PURE__ */ jsx(DraftForm, {
143
+ id,
144
+ locale,
145
+ initial: draft.data,
146
+ article: selected?.kind === "article"
147
+ }, id + "/" + locale)] })
148
+ ]
149
+ });
150
+ }
151
+ export { PublicationsPage as default };
@@ -0,0 +1,216 @@
1
+ import { PublicationPicker } from "../components/PublicationPicker.js";
2
+ import { readTerm, readTerms } from "../lib/taxonomy.js";
3
+ import { useAdminClient, useCan, useText } from "@bieberkit/admin-antd";
4
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
5
+ import { Alert, Button, Form, Input, Modal, Popconfirm, Skeleton, Space, Table } from "antd";
6
+ import { useState } from "react";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ // src/admin/pages/TaxonomyPage.tsx
9
+ /**
10
+ * BieberKit
11
+ *
12
+ * @file plugins/pages/src/admin/pages/TaxonomyPage.tsx
13
+ * @description Manages taxonomy records and their publication assignments.
14
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
15
+ * @copyright Copyright (c) 2026 Bieber Kieu
16
+ * @license Apache-2.0
17
+ *
18
+ * Licensed under the Apache License, Version 2.0 (the "License");
19
+ * you may not use this file except in compliance with the License.
20
+ * You may obtain a copy of the License at
21
+ *
22
+ * http://www.apache.org/licenses/LICENSE-2.0
23
+ *
24
+ * Unless required by applicable law or agreed to in writing, software
25
+ * distributed under the License is distributed on an "AS IS" BASIS,
26
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27
+ * See the License for the specific language governing permissions and
28
+ * limitations under the License.
29
+ *
30
+ * SPDX-License-Identifier: Apache-2.0
31
+ */
32
+ function TermForm({ row, api, onDone }) {
33
+ const client = useAdminClient();
34
+ const text = useText();
35
+ const canAssign = useCan("pages.read");
36
+ const save = useMutation({
37
+ mutationFn: ({ name, slug, publications }) => {
38
+ const body = {
39
+ name,
40
+ slug,
41
+ ...canAssign ? { publications: (publications ?? []).map((publicationId) => ({ publicationId })) } : {}
42
+ };
43
+ return row === void 0 ? client.request("POST", api, { body }) : client.request("PATCH", api + "/" + row.id, {
44
+ body,
45
+ headers: { "if-match": String(row.version) }
46
+ });
47
+ },
48
+ onSuccess: onDone
49
+ });
50
+ return /* @__PURE__ */ jsxs(Form, {
51
+ layout: "vertical",
52
+ initialValues: {
53
+ name: row?.name,
54
+ slug: row?.slug,
55
+ publications: row?.publications.map(({ id }) => id) ?? []
56
+ },
57
+ onFinish: (fields) => save.mutate(fields),
58
+ children: [
59
+ save.error && /* @__PURE__ */ jsx(Alert, {
60
+ type: "error",
61
+ title: save.error.message,
62
+ showIcon: true
63
+ }),
64
+ /* @__PURE__ */ jsx(Form.Item, {
65
+ name: "name",
66
+ label: text("pages.taxonomyName"),
67
+ rules: [{
68
+ required: true,
69
+ whitespace: true
70
+ }],
71
+ children: /* @__PURE__ */ jsx(Input, { maxLength: 120 })
72
+ }),
73
+ /* @__PURE__ */ jsx(Form.Item, {
74
+ name: "slug",
75
+ label: text("pages.slug"),
76
+ rules: [{
77
+ required: true,
78
+ pattern: /^[a-z0-9]+(?:-[a-z0-9]+)*$/
79
+ }],
80
+ children: /* @__PURE__ */ jsx(Input, { maxLength: 80 })
81
+ }),
82
+ canAssign && /* @__PURE__ */ jsx(Form.Item, {
83
+ name: "publications",
84
+ label: text("pages.title"),
85
+ children: /* @__PURE__ */ jsx(PublicationPicker, { initial: row?.publications ?? [] })
86
+ }),
87
+ /* @__PURE__ */ jsxs(Space, { children: [/* @__PURE__ */ jsx(Button, {
88
+ htmlType: "submit",
89
+ type: "primary",
90
+ loading: save.isPending,
91
+ children: text("form.save")
92
+ }), /* @__PURE__ */ jsx(Button, {
93
+ disabled: save.isPending,
94
+ onClick: onDone,
95
+ children: text("common.cancel")
96
+ })] })
97
+ ]
98
+ });
99
+ }
100
+ function taxonomyPage(module) {
101
+ const api = "/api/v1/" + module + "/" + module;
102
+ const queryKey = ["site-taxonomy", module];
103
+ return function TaxonomyPage() {
104
+ const client = useAdminClient();
105
+ const text = useText();
106
+ const cache = useQueryClient();
107
+ const canManage = useCan(module + ".manage");
108
+ const canReadPublications = useCan("pages.read");
109
+ const [page, setPage] = useState(1);
110
+ const [editing, setEditing] = useState();
111
+ const list = useQuery({
112
+ queryKey: [...queryKey, page],
113
+ queryFn: async () => readTerms(await client.request("GET", api + "?page=" + String(page)))
114
+ });
115
+ const detail = useQuery({
116
+ queryKey: [
117
+ ...queryKey,
118
+ "term",
119
+ editing
120
+ ],
121
+ queryFn: async () => readTerm(await client.request("GET", api + "/" + String(editing))),
122
+ enabled: editing !== void 0 && editing !== "new"
123
+ });
124
+ const remove = useMutation({
125
+ mutationFn: ({ id, version }) => client.request("DELETE", api + "/" + id, { headers: { "if-match": String(version) } }),
126
+ onSuccess: async () => {
127
+ if ((list.data?.data.length ?? 0) === 1 && page > 1) setPage(page - 1);
128
+ await cache.invalidateQueries({ queryKey });
129
+ }
130
+ });
131
+ const close = () => setEditing(void 0);
132
+ const saved = () => {
133
+ close();
134
+ cache.invalidateQueries({ queryKey });
135
+ };
136
+ const error = list.error ?? remove.error;
137
+ return /* @__PURE__ */ jsxs(Space, {
138
+ orientation: "vertical",
139
+ style: { width: "100%" },
140
+ children: [
141
+ error && /* @__PURE__ */ jsx(Alert, {
142
+ type: "error",
143
+ title: error.message,
144
+ showIcon: true
145
+ }),
146
+ canManage && /* @__PURE__ */ jsx(Button, {
147
+ type: "primary",
148
+ onClick: () => setEditing("new"),
149
+ children: text("form.new", { name: text(module + ".term") })
150
+ }),
151
+ /* @__PURE__ */ jsx(Table, {
152
+ rowKey: "id",
153
+ loading: list.isPending,
154
+ dataSource: list.data?.data ?? [],
155
+ pagination: {
156
+ current: page,
157
+ pageSize: 50,
158
+ total: list.data?.total ?? 0,
159
+ showSizeChanger: false,
160
+ onChange: setPage
161
+ },
162
+ columns: [
163
+ {
164
+ dataIndex: "name",
165
+ title: text("pages.taxonomyName")
166
+ },
167
+ {
168
+ dataIndex: "slug",
169
+ title: text("pages.slug")
170
+ },
171
+ {
172
+ key: "actions",
173
+ render: (_, row) => /* @__PURE__ */ jsxs(Space, { children: [(canManage || canReadPublications) && /* @__PURE__ */ jsx(Button, {
174
+ onClick: () => setEditing(row.id),
175
+ children: text(canManage ? "pages.edit" : "pages.taxonomyView")
176
+ }), canManage && /* @__PURE__ */ jsx(Popconfirm, {
177
+ title: text("list.confirmDeleteNamed", { name: row.name }),
178
+ onConfirm: () => remove.mutateAsync(row),
179
+ children: /* @__PURE__ */ jsx(Button, {
180
+ danger: true,
181
+ loading: remove.isPending,
182
+ children: text("common.delete")
183
+ })
184
+ })] })
185
+ }
186
+ ]
187
+ }),
188
+ /* @__PURE__ */ jsxs(Modal, {
189
+ open: editing !== void 0,
190
+ title: editing === "new" ? text("form.new", { name: text(module + ".term") }) : detail.data?.name ?? text(module + ".term"),
191
+ onCancel: close,
192
+ footer: null,
193
+ destroyOnHidden: true,
194
+ children: [
195
+ detail.error && editing !== "new" && /* @__PURE__ */ jsx(Alert, {
196
+ type: "error",
197
+ title: detail.error.message,
198
+ showIcon: true
199
+ }),
200
+ editing === "new" && /* @__PURE__ */ jsx(TermForm, {
201
+ api,
202
+ onDone: saved
203
+ }, "new"),
204
+ editing !== void 0 && editing !== "new" && detail.isPending && /* @__PURE__ */ jsx(Skeleton, { active: true }),
205
+ detail.data !== void 0 && editing !== "new" && (canManage ? /* @__PURE__ */ jsx(TermForm, {
206
+ row: detail.data,
207
+ api,
208
+ onDone: saved
209
+ }, detail.data.id) : detail.data.publications.length > 0 ? /* @__PURE__ */ jsx("ul", { children: detail.data.publications.map((publication) => /* @__PURE__ */ jsx("li", { children: publication.title ?? text("pages.untitled") }, publication.id)) }) : /* @__PURE__ */ jsx("p", { children: text("pages.taxonomyEmpty") }))
210
+ ]
211
+ })
212
+ ]
213
+ });
214
+ };
215
+ }
216
+ export { taxonomyPage };
@@ -0,0 +1,48 @@
1
+ /**
2
+ * BieberKit
3
+ *
4
+ * @file plugins/pages/src/index.ts
5
+ * @description Declares the pages plugin with editorial and public composition routes.
6
+ * @author Bieber Kieu <kieutrongthien@gmail.com>
7
+ * @copyright Copyright (c) 2026 Bieber Kieu
8
+ * @license Apache-2.0
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ *
22
+ * SPDX-License-Identifier: Apache-2.0
23
+ */
24
+ import { PluginDef, ServiceRegistry } from "@bieberkit/core";
25
+ import { EntityDef } from "@bieberkit/data";
26
+ import { ContentDocument } from "@bieberkit/content";
27
+ import { RouteDef } from "@bieberkit/http";
28
+ import "@bieberkit/data/orm";
29
+ import "@bieberkit/content/plugin";
30
+ import "@bieberkit/plugin-redirects";
31
+ interface Draft {
32
+ readonly title: string;
33
+ readonly slug: string;
34
+ readonly excerpt?: string;
35
+ readonly document: ContentDocument;
36
+ }
37
+ interface PublicationSeed extends Draft {
38
+ readonly kind: "page" | "article";
39
+ readonly locale: string;
40
+ }
41
+ /** Installs a starter publication without overwriting a draft, published page or reserved path. */
42
+ export declare function seedPublication(services: Pick<ServiceRegistry, "database" | "content" | "publicationRedirects">, input: PublicationSeed): Promise<void>;
43
+ export declare const Publication: EntityDef<"pages.Publication">;
44
+ type TaxonomyRoutes = readonly RouteDef[];
45
+ /** Builds the first-party taxonomy API with the editorial site's scope and independent plugin permissions. */
46
+ export declare function taxonomyRoutes(module: "categories" | "tags", term: EntityDef<string>, assignmentEntity: EntityDef<string>): TaxonomyRoutes;
47
+ declare const plugin: PluginDef;
48
+ export { type PublicationSeed, type TaxonomyRoutes, plugin as default };