@kurbo/dkit 0.5.0 → 0.5.1
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/dist/bin.js +119 -26
- package/dist/bin.js.map +1 -1
- package/package.json +1 -1
- package/vendor/registry/blocks/blog-detail/ui/minimal/next/blog-detail.tsx +0 -1
- package/vendor/registry/blocks/blog-detail/ui/shadcn/next/blog-detail.tsx +0 -1
- package/vendor/registry/blocks/blog-list/ui/minimal/next/blog-list.tsx +0 -1
- package/vendor/registry/blocks/blog-list/ui/shadcn/next/blog-list.tsx +0 -1
- package/vendor/registry/blocks/blog-list-cards/ui/minimal/next/blog-list-cards.tsx +0 -1
- package/vendor/registry/blocks/blog-list-cards/ui/shadcn/next/blog-list-cards.tsx +0 -1
- package/vendor/registry/blocks/booking-form-ui/ui/minimal/next/booking-form.tsx +0 -1
- package/vendor/registry/blocks/booking-form-ui/ui/shadcn/next/booking-form.tsx +0 -1
- package/vendor/registry/blocks/contact-form-ui/ui/minimal/next/contact-form.tsx +0 -1
- package/vendor/registry/blocks/contact-form-ui/ui/shadcn/next/contact-form.tsx +0 -1
- package/vendor/registry/blocks/faq-list/ui/minimal/next/faq-list.tsx +0 -1
- package/vendor/registry/blocks/faq-list/ui/shadcn/next/faq-list.tsx +0 -1
- package/vendor/registry/blocks/page-detail/ui/minimal/next/page-detail.tsx +0 -1
- package/vendor/registry/blocks/page-detail/ui/shadcn/next/page-detail.tsx +0 -1
- package/vendor/registry/blocks/project-detail/ui/minimal/next/project-detail.tsx +0 -1
- package/vendor/registry/blocks/project-detail/ui/shadcn/next/project-detail.tsx +0 -1
- package/vendor/registry/blocks/project-list/ui/minimal/next/project-list.tsx +0 -1
- package/vendor/registry/blocks/project-list/ui/shadcn/next/project-list.tsx +0 -1
- package/vendor/registry/blocks/project-list-cards/ui/minimal/next/project-list-cards.tsx +0 -1
- package/vendor/registry/blocks/project-list-cards/ui/shadcn/next/project-list-cards.tsx +0 -1
- package/vendor/registry/blocks/projects-carousel/ui/minimal/next/projects-carousel-client.tsx +0 -4
- package/vendor/registry/blocks/projects-carousel/ui/minimal/next/projects-carousel.tsx +0 -1
- package/vendor/registry/blocks/projects-carousel/ui/shadcn/next/projects-carousel-client.tsx +0 -1
- package/vendor/registry/blocks/projects-carousel/ui/shadcn/next/projects-carousel.tsx +0 -1
- package/vendor/registry/blocks/service-detail/ui/minimal/next/service-detail.tsx +0 -1
- package/vendor/registry/blocks/service-detail/ui/shadcn/next/service-detail.tsx +0 -1
- package/vendor/registry/blocks/service-list/ui/minimal/next/service-list.tsx +0 -1
- package/vendor/registry/blocks/service-list/ui/shadcn/next/service-list.tsx +0 -1
- package/vendor/registry/blocks/service-list-cards/ui/minimal/next/service-list-cards.tsx +0 -1
- package/vendor/registry/blocks/service-list-cards/ui/shadcn/next/service-list-cards.tsx +0 -1
- package/vendor/registry/blocks/social-links/ui/minimal/next/social-links.tsx +0 -1
- package/vendor/registry/blocks/social-links/ui/shadcn/next/social-links.tsx +0 -1
- package/vendor/registry/blocks/team-detail/ui/minimal/next/team-detail.tsx +0 -1
- package/vendor/registry/blocks/team-detail/ui/shadcn/next/team-detail.tsx +0 -1
- package/vendor/registry/blocks/team-list/ui/minimal/next/team-list.tsx +0 -1
- package/vendor/registry/blocks/team-list/ui/shadcn/next/team-list.tsx +0 -1
- package/vendor/registry/blocks/team-list-cards/ui/minimal/next/team-list-cards.tsx +0 -1
- package/vendor/registry/blocks/team-list-cards/ui/shadcn/next/team-list-cards.tsx +0 -1
- package/vendor/registry/blocks/testimonials-carousel/ui/minimal/next/testimonials-carousel-client.tsx +0 -4
- package/vendor/registry/blocks/testimonials-carousel/ui/minimal/next/testimonials-carousel.tsx +0 -1
- package/vendor/registry/blocks/testimonials-carousel/ui/shadcn/next/testimonials-carousel-client.tsx +0 -1
- package/vendor/registry/blocks/testimonials-carousel/ui/shadcn/next/testimonials-carousel.tsx +0 -1
- package/vendor/registry/blocks/testimonials-list/ui/minimal/next/testimonials-list.tsx +0 -1
- package/vendor/registry/blocks/testimonials-list/ui/shadcn/next/testimonials-list.tsx +0 -1
- package/vendor/registry/blocks/testimonials-list-cards/ui/minimal/next/testimonials-list-cards.tsx +0 -1
- package/vendor/registry/blocks/testimonials-list-cards/ui/shadcn/next/testimonials-list-cards.tsx +0 -1
- package/vendor/registry/modules/blog/lib.multilocale.ts +124 -129
- package/vendor/registry/modules/blog/lib.ts +12 -9
- package/vendor/registry/modules/blog/services/seed.json +1 -1
- package/vendor/registry/modules/blog/services/seed.multilocale.json +1 -1
- package/vendor/registry/modules/blog/types.multilocale.ts +41 -12
- package/vendor/registry/modules/blog/types.ts +8 -6
- package/vendor/registry/modules/faq/lib.multilocale.ts +30 -30
- package/vendor/registry/modules/faq/lib.ts +4 -5
- package/vendor/registry/modules/faq/types.multilocale.ts +13 -2
- package/vendor/registry/modules/faq/types.ts +4 -2
- package/vendor/registry/modules/navigation/lib.multilocale.ts +207 -76
- package/vendor/registry/modules/navigation/lib.ts +18 -17
- package/vendor/registry/modules/navigation/types.multilocale.ts +24 -4
- package/vendor/registry/modules/navigation/types.ts +6 -4
- package/vendor/registry/modules/pages/lib.multilocale.ts +34 -39
- package/vendor/registry/modules/pages/lib.ts +4 -3
- package/vendor/registry/modules/pages/types.multilocale.ts +14 -3
- package/vendor/registry/modules/pages/types.ts +5 -3
- package/vendor/registry/modules/projects/lib.multilocale.ts +149 -65
- package/vendor/registry/modules/projects/lib.ts +6 -6
- package/vendor/registry/modules/projects/services/seed.json +2 -2
- package/vendor/registry/modules/projects/services/seed.multilocale.json +2 -2
- package/vendor/registry/modules/projects/types.multilocale.ts +15 -3
- package/vendor/registry/modules/projects/types.ts +5 -3
- package/vendor/registry/modules/services/lib.multilocale.ts +143 -63
- package/vendor/registry/modules/services/lib.ts +6 -6
- package/vendor/registry/modules/services/services/seed.json +4 -4
- package/vendor/registry/modules/services/services/seed.multilocale.json +2 -2
- package/vendor/registry/modules/services/types.multilocale.ts +13 -3
- package/vendor/registry/modules/services/types.ts +5 -3
- package/vendor/registry/modules/settings/lib.multilocale.ts +159 -57
- package/vendor/registry/modules/settings/types.multilocale.ts +15 -0
- package/vendor/registry/modules/settings/types.ts +2 -2
- package/vendor/registry/modules/team/lib.multilocale.ts +116 -49
- package/vendor/registry/modules/team/lib.ts +4 -3
- package/vendor/registry/modules/team/services/seed.json +4 -4
- package/vendor/registry/modules/team/services/seed.multilocale.json +2 -2
- package/vendor/registry/modules/team/types.multilocale.ts +16 -3
- package/vendor/registry/modules/team/types.ts +5 -3
- package/vendor/registry/modules/testimonials/lib.multilocale.ts +82 -50
- package/vendor/registry/modules/testimonials/services/seed.json +4 -4
- package/vendor/registry/modules/testimonials/services/seed.multilocale.json +2 -2
- package/vendor/registry/modules/testimonials/types.multilocale.ts +14 -2
- package/vendor/registry/modules/testimonials/types.ts +4 -2
- package/vendor/registry/shared/helpers.ts +9 -3
- package/vendor/registry/shared/i18n.ts +84 -12
- package/vendor/registry/shared/prose.css +1 -1
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
import {
|
|
2
2
|
dkitQueryFailed,
|
|
3
3
|
getDirectus,
|
|
4
|
-
readItem,
|
|
5
4
|
readItems,
|
|
6
5
|
} from "@/lib/dkit/client";
|
|
7
6
|
import { collections } from "@/lib/dkit/config";
|
|
8
7
|
import { queryLimit, statusFilter } from "@/lib/dkit/helpers";
|
|
9
8
|
import {
|
|
10
|
-
isUsableTranslation,
|
|
11
|
-
pickTranslation,
|
|
12
9
|
requireLocaleTranslation,
|
|
13
10
|
resolveLocale,
|
|
14
11
|
translationsDeepFilter,
|
|
12
|
+
withLocale,
|
|
15
13
|
} from "@/lib/dkit/i18n";
|
|
16
|
-
import type {
|
|
14
|
+
import type {
|
|
15
|
+
FaqItem,
|
|
16
|
+
FaqItemRow,
|
|
17
|
+
GetFaqItemsOptions,
|
|
18
|
+
} from "./types.multilocale";
|
|
17
19
|
|
|
18
|
-
export type {
|
|
20
|
+
export type {
|
|
21
|
+
FaqItem,
|
|
22
|
+
FaqItemRow,
|
|
23
|
+
FaqItemTranslation,
|
|
24
|
+
GetFaqItemsOptions,
|
|
25
|
+
} from "./types.multilocale";
|
|
19
26
|
|
|
20
|
-
function flattenFaqItem(row:
|
|
21
|
-
|
|
22
|
-
row
|
|
27
|
+
function flattenFaqItem(row: FaqItemRow, locale: string): FaqItem | null {
|
|
28
|
+
return withLocale(
|
|
29
|
+
row,
|
|
23
30
|
locale,
|
|
31
|
+
(r, t) => ({
|
|
32
|
+
id: r.id,
|
|
33
|
+
status: r.status,
|
|
34
|
+
sort: r.sort ?? null,
|
|
35
|
+
question: t.question,
|
|
36
|
+
answer: t.answer,
|
|
37
|
+
category: t.category ?? null,
|
|
38
|
+
}),
|
|
39
|
+
{ required: ["question", "answer"] },
|
|
24
40
|
);
|
|
25
|
-
if (!isUsableTranslation(trans, ["question", "answer"])) return null;
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
id: row.id as string,
|
|
29
|
-
status: row.status as FaqItem["status"],
|
|
30
|
-
sort: (row.sort as number | null) ?? null,
|
|
31
|
-
question: trans!.question!,
|
|
32
|
-
answer: trans!.answer!,
|
|
33
|
-
category: trans?.category ?? null,
|
|
34
|
-
};
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
const FAQ_FIELDS = [
|
|
@@ -55,9 +61,7 @@ export async function getFaqItems(
|
|
|
55
61
|
}
|
|
56
62
|
const locale = await resolveLocale(options.locale);
|
|
57
63
|
const status = statusFilter(options.status);
|
|
58
|
-
const and
|
|
59
|
-
requireLocaleTranslation(locale, ["question", "answer"]),
|
|
60
|
-
];
|
|
64
|
+
const and = [requireLocaleTranslation(locale, ["question", "answer"])];
|
|
61
65
|
if (status) and.unshift(status);
|
|
62
66
|
if (options.category) {
|
|
63
67
|
and.push({
|
|
@@ -69,18 +73,19 @@ export async function getFaqItems(
|
|
|
69
73
|
},
|
|
70
74
|
});
|
|
71
75
|
}
|
|
76
|
+
const filter = { _and: and };
|
|
72
77
|
const limit = queryLimit(options.limit);
|
|
73
78
|
try {
|
|
74
79
|
const rows = await client.request(
|
|
75
80
|
readItems(collections.faq_items, {
|
|
76
|
-
filter
|
|
81
|
+
filter,
|
|
77
82
|
deep: translationsDeepFilter(locale),
|
|
78
83
|
fields: [...FAQ_FIELDS],
|
|
79
84
|
sort: options.sort ?? ["sort"],
|
|
80
85
|
...(limit !== undefined ? { limit } : {}),
|
|
81
86
|
}),
|
|
82
87
|
);
|
|
83
|
-
return (rows as
|
|
88
|
+
return (rows as FaqItemRow[])
|
|
84
89
|
.map((r) => flattenFaqItem(r, locale))
|
|
85
90
|
.filter((f): f is FaqItem => f != null);
|
|
86
91
|
} catch (err) {
|
|
@@ -100,18 +105,13 @@ export async function getFaqItemById(
|
|
|
100
105
|
try {
|
|
101
106
|
const rows = await client.request(
|
|
102
107
|
readItems(collections.faq_items, {
|
|
103
|
-
filter: {
|
|
104
|
-
_and: [
|
|
105
|
-
{ id: { _eq: id } },
|
|
106
|
-
requireLocaleTranslation(locale, ["question", "answer"]),
|
|
107
|
-
],
|
|
108
|
-
},
|
|
108
|
+
filter: { _and: [{ id: { _eq: id } }, requireLocaleTranslation(locale, ["question", "answer"])] },
|
|
109
109
|
deep: translationsDeepFilter(locale),
|
|
110
110
|
fields: [...FAQ_FIELDS],
|
|
111
111
|
limit: 1,
|
|
112
112
|
}),
|
|
113
113
|
);
|
|
114
|
-
const row = (rows as
|
|
114
|
+
const row = (rows as FaqItemRow[])[0];
|
|
115
115
|
return row ? flattenFaqItem(row, locale) : null;
|
|
116
116
|
} catch (err) {
|
|
117
117
|
dkitQueryFailed(`getFaqItemById(${id}) failed`, err);
|
|
@@ -32,16 +32,15 @@ export async function getFaqItems(
|
|
|
32
32
|
return [];
|
|
33
33
|
}
|
|
34
34
|
const status = statusFilter(options.status);
|
|
35
|
-
const and
|
|
35
|
+
const and = [];
|
|
36
36
|
if (status) and.push(status);
|
|
37
|
-
if (options.category) {
|
|
38
|
-
|
|
39
|
-
}
|
|
37
|
+
if (options.category) and.push({ category: { _eq: options.category } });
|
|
38
|
+
const filter = and.length ? { _and: and } : undefined;
|
|
40
39
|
const limit = queryLimit(options.limit);
|
|
41
40
|
try {
|
|
42
41
|
return await client.request(
|
|
43
42
|
readItems(collections.faq_items, {
|
|
44
|
-
filter
|
|
43
|
+
filter,
|
|
45
44
|
fields: [...FAQ_FIELDS],
|
|
46
45
|
sort: options.sort ?? ["sort", "question"],
|
|
47
46
|
...(limit !== undefined ? { limit } : {}),
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
/* ── Translation row type ── */
|
|
2
4
|
|
|
3
5
|
export type FaqItemTranslation = {
|
|
@@ -9,6 +11,15 @@ export type FaqItemTranslation = {
|
|
|
9
11
|
category?: string | null;
|
|
10
12
|
};
|
|
11
13
|
|
|
14
|
+
/* ── Wire type ── */
|
|
15
|
+
|
|
16
|
+
export type FaqItemRow = {
|
|
17
|
+
id: string;
|
|
18
|
+
status: "published" | "draft";
|
|
19
|
+
sort?: number | null;
|
|
20
|
+
translations?: FaqItemTranslation[] | null;
|
|
21
|
+
};
|
|
22
|
+
|
|
12
23
|
/* ── Public API type (flat) ── */
|
|
13
24
|
|
|
14
25
|
export type FaqItem = {
|
|
@@ -22,10 +33,10 @@ export type FaqItem = {
|
|
|
22
33
|
|
|
23
34
|
export type GetFaqItemsOptions = {
|
|
24
35
|
/** Directus status filter. Default `"published"`. Pass `null` to skip. */
|
|
25
|
-
status?:
|
|
36
|
+
status?: FaqItem["status"] | null;
|
|
26
37
|
limit?: number;
|
|
27
38
|
/** Filter by category label. */
|
|
28
39
|
category?: string;
|
|
29
|
-
sort?:
|
|
40
|
+
sort?: Array<DkitSortField<"sort" | "id" | "status">>;
|
|
30
41
|
locale?: string;
|
|
31
42
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
export type FaqItem = {
|
|
2
4
|
id: string;
|
|
3
5
|
status: "published" | "draft";
|
|
@@ -9,9 +11,9 @@ export type FaqItem = {
|
|
|
9
11
|
|
|
10
12
|
export type GetFaqItemsOptions = {
|
|
11
13
|
/** Directus status filter. Default `"published"`. Pass `null` to skip. */
|
|
12
|
-
status?:
|
|
14
|
+
status?: FaqItem["status"] | null;
|
|
13
15
|
limit?: number;
|
|
14
16
|
/** Filter by category label. */
|
|
15
17
|
category?: string;
|
|
16
|
-
sort?:
|
|
18
|
+
sort?: Array<DkitSortField<"sort" | "id" | "status">>;
|
|
17
19
|
};
|
|
@@ -7,19 +7,19 @@ import {
|
|
|
7
7
|
import { collections } from "@/lib/dkit/config";
|
|
8
8
|
import { queryLimit, statusFilter } from "@/lib/dkit/helpers";
|
|
9
9
|
import {
|
|
10
|
-
pickTranslation,
|
|
11
10
|
requireLocaleTranslation,
|
|
12
11
|
resolveLocale,
|
|
13
12
|
translationsDeepFilter,
|
|
13
|
+
withLocale,
|
|
14
14
|
} from "@/lib/dkit/i18n";
|
|
15
15
|
import type {
|
|
16
16
|
GetNavItemsOptions,
|
|
17
17
|
GetNavMenuByKeyOptions,
|
|
18
18
|
GetNavMenusOptions,
|
|
19
19
|
NavItem,
|
|
20
|
-
|
|
20
|
+
NavItemRow,
|
|
21
21
|
NavMenu,
|
|
22
|
-
|
|
22
|
+
NavMenuRow,
|
|
23
23
|
NavMenuTree,
|
|
24
24
|
} from "./types.multilocale";
|
|
25
25
|
|
|
@@ -28,47 +28,83 @@ export type {
|
|
|
28
28
|
GetNavMenuByKeyOptions,
|
|
29
29
|
GetNavMenusOptions,
|
|
30
30
|
NavItem,
|
|
31
|
+
NavItemRow,
|
|
31
32
|
NavItemTranslation,
|
|
32
33
|
NavMenu,
|
|
34
|
+
NavMenuRow,
|
|
33
35
|
NavMenuTranslation,
|
|
34
36
|
NavMenuTree,
|
|
35
37
|
} from "./types.multilocale";
|
|
36
38
|
|
|
37
|
-
function flattenMenu(row:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
function flattenMenu(row: NavMenuRow, locale: string): NavMenu {
|
|
40
|
+
return (
|
|
41
|
+
withLocale(row, locale, (r, t) => ({
|
|
42
|
+
id: r.id,
|
|
43
|
+
status: r.status,
|
|
44
|
+
name: t?.name ?? "",
|
|
45
|
+
key: r.key,
|
|
46
|
+
description: t?.description ?? null,
|
|
47
|
+
})) ?? {
|
|
48
|
+
id: row.id,
|
|
49
|
+
status: row.status,
|
|
50
|
+
name: "",
|
|
51
|
+
key: row.key,
|
|
52
|
+
description: null,
|
|
53
|
+
}
|
|
54
|
+
);
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
function flattenItem(row:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
function flattenItem(row: NavItemRow, locale: string): NavItem | null {
|
|
58
|
+
return withLocale(
|
|
59
|
+
row,
|
|
60
|
+
locale,
|
|
61
|
+
(r, t) => {
|
|
62
|
+
if (!t.label?.trim()) return null;
|
|
63
|
+
let menu: NavItem["menu"] = null;
|
|
64
|
+
if (typeof r.menu === "string") menu = r.menu;
|
|
65
|
+
else if (r.menu && typeof r.menu === "object") {
|
|
66
|
+
menu = {
|
|
67
|
+
id: r.menu.id,
|
|
68
|
+
status: r.menu.status,
|
|
69
|
+
key: r.menu.key,
|
|
70
|
+
name: "",
|
|
71
|
+
description: null,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
let parent: NavItem["parent"] = null;
|
|
75
|
+
if (typeof r.parent === "string") parent = r.parent;
|
|
76
|
+
else if (r.parent && typeof r.parent === "object") {
|
|
77
|
+
parent = { id: r.parent.id } as NavItem;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
id: r.id,
|
|
81
|
+
status: r.status,
|
|
82
|
+
menu,
|
|
83
|
+
parent,
|
|
84
|
+
label: t.label,
|
|
85
|
+
href: t.href ?? "#",
|
|
86
|
+
open_in_new_tab: r.open_in_new_tab ?? null,
|
|
87
|
+
sort: r.sort ?? null,
|
|
88
|
+
children: undefined,
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
{ required: ["label"] },
|
|
92
|
+
);
|
|
63
93
|
}
|
|
64
94
|
|
|
65
95
|
const MENU_FIELDS = [
|
|
66
|
-
"id",
|
|
96
|
+
"id",
|
|
97
|
+
"status",
|
|
98
|
+
"key",
|
|
67
99
|
{ translations: ["id", "languages_code", "name", "description"] },
|
|
68
100
|
] as const;
|
|
69
101
|
|
|
70
102
|
const ITEM_FIELDS = [
|
|
71
|
-
"id",
|
|
103
|
+
"id",
|
|
104
|
+
"status",
|
|
105
|
+
"open_in_new_tab",
|
|
106
|
+
"sort",
|
|
107
|
+
"parent",
|
|
72
108
|
{ menu: ["id", "key", "status"] },
|
|
73
109
|
{ translations: ["id", "languages_code", "label", "href"] },
|
|
74
110
|
] as const;
|
|
@@ -101,30 +137,53 @@ export function isExternalHref(href: string): boolean {
|
|
|
101
137
|
}
|
|
102
138
|
|
|
103
139
|
export function navLinkProps(item: Pick<NavItem, "href" | "open_in_new_tab">): {
|
|
104
|
-
href: string;
|
|
140
|
+
href: string;
|
|
141
|
+
target?: "_blank";
|
|
142
|
+
rel?: string;
|
|
105
143
|
} {
|
|
106
144
|
const external = isExternalHref(item.href) || Boolean(item.open_in_new_tab);
|
|
107
|
-
return {
|
|
145
|
+
return {
|
|
146
|
+
href: item.href,
|
|
147
|
+
...(external
|
|
148
|
+
? { target: "_blank" as const, rel: "noopener noreferrer" }
|
|
149
|
+
: {}),
|
|
150
|
+
};
|
|
108
151
|
}
|
|
109
152
|
|
|
110
153
|
async function fetchNavDescendants(
|
|
111
|
-
roots: NavItem[],
|
|
154
|
+
roots: NavItem[],
|
|
155
|
+
status: ReturnType<typeof statusFilter<NavItemRow["status"]>>,
|
|
156
|
+
locale: string,
|
|
112
157
|
): Promise<NavItem[]> {
|
|
113
158
|
const client = getDirectus();
|
|
114
159
|
if (!client || !collections.nav_items || roots.length === 0) return roots;
|
|
115
160
|
const all = [...roots];
|
|
116
161
|
let frontier = roots.map((i) => i.id);
|
|
117
162
|
while (frontier.length > 0) {
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
163
|
+
const kids = await client.request(
|
|
164
|
+
readItems(collections.nav_items, {
|
|
165
|
+
filter: status
|
|
166
|
+
? {
|
|
167
|
+
_and: [
|
|
168
|
+
status,
|
|
169
|
+
{ parent: { _in: frontier } },
|
|
170
|
+
requireLocaleTranslation(locale, ["label"]),
|
|
171
|
+
],
|
|
172
|
+
}
|
|
173
|
+
: {
|
|
174
|
+
_and: [
|
|
175
|
+
{ parent: { _in: frontier } },
|
|
176
|
+
requireLocaleTranslation(locale, ["label"]),
|
|
177
|
+
],
|
|
178
|
+
},
|
|
179
|
+
deep: translationsDeepFilter(locale),
|
|
180
|
+
fields: [...ITEM_FIELDS],
|
|
181
|
+
sort: ["sort"],
|
|
182
|
+
limit: -1,
|
|
183
|
+
}),
|
|
184
|
+
);
|
|
126
185
|
if (!kids.length) break;
|
|
127
|
-
const flat = (kids as
|
|
186
|
+
const flat = (kids as NavItemRow[])
|
|
128
187
|
.map((r) => flattenItem(r, locale))
|
|
129
188
|
.filter((i): i is NavItem => i != null);
|
|
130
189
|
all.push(...flat);
|
|
@@ -133,64 +192,136 @@ async function fetchNavDescendants(
|
|
|
133
192
|
return all;
|
|
134
193
|
}
|
|
135
194
|
|
|
136
|
-
export async function getNavMenus(
|
|
137
|
-
|
|
138
|
-
|
|
195
|
+
export async function getNavMenus(
|
|
196
|
+
options: GetNavMenusOptions = {},
|
|
197
|
+
): Promise<NavMenu[]> {
|
|
198
|
+
const client = getDirectus();
|
|
199
|
+
if (!client) return [];
|
|
200
|
+
if (!collections.nav_menus) {
|
|
201
|
+
dkitQueryFailed(
|
|
202
|
+
"collections.nav_menus missing — re-run `dkit add navigation`",
|
|
203
|
+
);
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
139
206
|
const locale = await resolveLocale(options.locale);
|
|
140
207
|
const limit = queryLimit(options.limit);
|
|
141
208
|
try {
|
|
142
|
-
const rows = await client.request(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
209
|
+
const rows = await client.request(
|
|
210
|
+
readItems(collections.nav_menus, {
|
|
211
|
+
filter: statusFilter(options.status),
|
|
212
|
+
deep: translationsDeepFilter(locale),
|
|
213
|
+
fields: [...MENU_FIELDS],
|
|
214
|
+
sort: options.sort ?? ["key"],
|
|
215
|
+
...(limit !== undefined ? { limit } : {}),
|
|
216
|
+
}),
|
|
217
|
+
);
|
|
218
|
+
return (rows as NavMenuRow[]).map((r) => flattenMenu(r, locale));
|
|
219
|
+
} catch (err) {
|
|
220
|
+
dkitQueryFailed(`getNavMenus failed`, err);
|
|
221
|
+
return [];
|
|
222
|
+
}
|
|
150
223
|
}
|
|
151
224
|
|
|
152
|
-
export async function getNavMenuById(
|
|
153
|
-
|
|
225
|
+
export async function getNavMenuById(
|
|
226
|
+
id: string,
|
|
227
|
+
options: { locale?: string } = {},
|
|
228
|
+
): Promise<NavMenu | null> {
|
|
229
|
+
const client = getDirectus();
|
|
230
|
+
if (!client) return null;
|
|
231
|
+
if (!collections.nav_menus) return null;
|
|
154
232
|
const locale = await resolveLocale(options.locale);
|
|
155
233
|
try {
|
|
156
|
-
const row = await client.request(
|
|
157
|
-
|
|
158
|
-
|
|
234
|
+
const row = await client.request(
|
|
235
|
+
readItem(collections.nav_menus, id, {
|
|
236
|
+
deep: translationsDeepFilter(locale),
|
|
237
|
+
fields: [...MENU_FIELDS],
|
|
238
|
+
}),
|
|
239
|
+
);
|
|
240
|
+
return row ? flattenMenu(row as NavMenuRow, locale) : null;
|
|
241
|
+
} catch (err) {
|
|
242
|
+
dkitQueryFailed(`getNavMenuById(${id}) failed`, err);
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
159
245
|
}
|
|
160
246
|
|
|
161
|
-
export async function getNavMenuByKey(
|
|
162
|
-
|
|
163
|
-
|
|
247
|
+
export async function getNavMenuByKey(
|
|
248
|
+
key: string,
|
|
249
|
+
options: GetNavMenuByKeyOptions = {},
|
|
250
|
+
): Promise<NavMenuTree | null> {
|
|
251
|
+
const client = getDirectus();
|
|
252
|
+
if (!client) return null;
|
|
253
|
+
if (!collections.nav_menus || !collections.nav_items) {
|
|
254
|
+
dkitQueryFailed("collections.nav_menus / nav_items missing");
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
164
257
|
const locale = await resolveLocale(options.locale);
|
|
165
258
|
const status = statusFilter(options.status);
|
|
166
|
-
const menuAnd: Record<string, unknown>[] = [{ key: { _eq: key } }];
|
|
167
|
-
if (status) menuAnd.unshift(status);
|
|
168
259
|
try {
|
|
169
|
-
const menus = await client.request(
|
|
170
|
-
|
|
260
|
+
const menus = await client.request(
|
|
261
|
+
readItems(collections.nav_menus, {
|
|
262
|
+
filter: status
|
|
263
|
+
? { _and: [status, { key: { _eq: key } }] }
|
|
264
|
+
: { key: { _eq: key } },
|
|
265
|
+
deep: translationsDeepFilter(locale),
|
|
266
|
+
fields: [...MENU_FIELDS],
|
|
267
|
+
limit: 1,
|
|
268
|
+
}),
|
|
269
|
+
);
|
|
270
|
+
const menu = (menus as NavMenuRow[])[0];
|
|
171
271
|
if (!menu) return null;
|
|
172
272
|
const fm = flattenMenu(menu, locale);
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
273
|
+
const roots = await client.request(
|
|
274
|
+
readItems(collections.nav_items, {
|
|
275
|
+
filter: status
|
|
276
|
+
? {
|
|
277
|
+
_and: [
|
|
278
|
+
status,
|
|
279
|
+
{ menu: { _eq: fm.id } },
|
|
280
|
+
{ parent: { _null: true } },
|
|
281
|
+
requireLocaleTranslation(locale, ["label"]),
|
|
282
|
+
],
|
|
283
|
+
}
|
|
284
|
+
: {
|
|
285
|
+
_and: [
|
|
286
|
+
{ menu: { _eq: fm.id } },
|
|
287
|
+
{ parent: { _null: true } },
|
|
288
|
+
requireLocaleTranslation(locale, ["label"]),
|
|
289
|
+
],
|
|
290
|
+
},
|
|
291
|
+
deep: translationsDeepFilter(locale),
|
|
292
|
+
fields: [...ITEM_FIELDS],
|
|
293
|
+
sort: ["sort"],
|
|
294
|
+
limit: -1,
|
|
295
|
+
}),
|
|
296
|
+
);
|
|
297
|
+
const flatRoots = (roots as NavItemRow[])
|
|
181
298
|
.map((r) => flattenItem(r, locale))
|
|
182
299
|
.filter((i): i is NavItem => i != null);
|
|
183
300
|
const items = await fetchNavDescendants(flatRoots, status, locale);
|
|
184
301
|
return { id: fm.id, key: fm.key, name: fm.name, items: buildNavTree(items) };
|
|
185
|
-
} catch (err) {
|
|
302
|
+
} catch (err) {
|
|
303
|
+
dkitQueryFailed(`getNavMenuByKey(${key}) failed`, err);
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
186
306
|
}
|
|
187
307
|
|
|
188
|
-
export async function getNavItems(
|
|
189
|
-
|
|
308
|
+
export async function getNavItems(
|
|
309
|
+
key: string,
|
|
310
|
+
options: GetNavItemsOptions = {},
|
|
311
|
+
): Promise<NavItem[]> {
|
|
312
|
+
const tree = await getNavMenuByKey(key, {
|
|
313
|
+
status: options.status,
|
|
314
|
+
locale: options.locale,
|
|
315
|
+
});
|
|
190
316
|
if (!tree) return [];
|
|
191
317
|
if (options.tree === false) {
|
|
192
318
|
const flat: NavItem[] = [];
|
|
193
|
-
const walk = (nodes: NavItem[]) => {
|
|
319
|
+
const walk = (nodes: NavItem[]) => {
|
|
320
|
+
for (const n of nodes) {
|
|
321
|
+
flat.push(n);
|
|
322
|
+
if (n.children?.length) walk(n.children);
|
|
323
|
+
}
|
|
324
|
+
};
|
|
194
325
|
walk(tree.items);
|
|
195
326
|
return flat;
|
|
196
327
|
}
|
|
@@ -56,7 +56,7 @@ function sortItems(a: NavItem, b: NavItem): number {
|
|
|
56
56
|
/** Walk parent → children until the branch is exhausted. */
|
|
57
57
|
async function fetchNavDescendants(
|
|
58
58
|
roots: NavItem[],
|
|
59
|
-
status:
|
|
59
|
+
status: ReturnType<typeof statusFilter<NavItem["status"]>>,
|
|
60
60
|
): Promise<NavItem[]> {
|
|
61
61
|
const client = getDirectus();
|
|
62
62
|
if (!client || !collections.nav_items || roots.length === 0) return roots;
|
|
@@ -65,14 +65,11 @@ async function fetchNavDescendants(
|
|
|
65
65
|
let frontier = roots.map((item) => item.id);
|
|
66
66
|
|
|
67
67
|
while (frontier.length > 0) {
|
|
68
|
-
const childAnd: Record<string, unknown>[] = [
|
|
69
|
-
{ parent: { _in: frontier } },
|
|
70
|
-
];
|
|
71
|
-
if (status) childAnd.unshift(status);
|
|
72
|
-
|
|
73
68
|
const kids = (await client.request(
|
|
74
69
|
readItems(collections.nav_items, {
|
|
75
|
-
filter:
|
|
70
|
+
filter: status
|
|
71
|
+
? { _and: [status, { parent: { _in: frontier } }] }
|
|
72
|
+
: { parent: { _in: frontier } },
|
|
76
73
|
fields: [...ITEM_FIELDS],
|
|
77
74
|
sort: ["sort"],
|
|
78
75
|
limit: -1,
|
|
@@ -184,13 +181,13 @@ export async function getNavMenuByKey(
|
|
|
184
181
|
return null;
|
|
185
182
|
}
|
|
186
183
|
const status = statusFilter(options.status);
|
|
187
|
-
const menuAnd: Record<string, unknown>[] = [{ key: { _eq: key } }];
|
|
188
|
-
if (status) menuAnd.unshift(status);
|
|
189
184
|
|
|
190
185
|
try {
|
|
191
186
|
const menus = await client.request(
|
|
192
187
|
readItems(collections.nav_menus, {
|
|
193
|
-
filter:
|
|
188
|
+
filter: status
|
|
189
|
+
? { _and: [status, { key: { _eq: key } }] }
|
|
190
|
+
: { key: { _eq: key } },
|
|
194
191
|
fields: [...MENU_FIELDS],
|
|
195
192
|
limit: 1,
|
|
196
193
|
}),
|
|
@@ -199,15 +196,19 @@ export async function getNavMenuByKey(
|
|
|
199
196
|
if (!menu) return null;
|
|
200
197
|
|
|
201
198
|
// Top-level rows own the menu FK; nested Sub-items only set `parent`.
|
|
202
|
-
const rootAnd: Record<string, unknown>[] = [
|
|
203
|
-
{ menu: { _eq: menu.id } },
|
|
204
|
-
{ parent: { _null: true } },
|
|
205
|
-
];
|
|
206
|
-
if (status) rootAnd.unshift(status);
|
|
207
|
-
|
|
208
199
|
const roots = await client.request(
|
|
209
200
|
readItems(collections.nav_items, {
|
|
210
|
-
filter:
|
|
201
|
+
filter: status
|
|
202
|
+
? {
|
|
203
|
+
_and: [
|
|
204
|
+
status,
|
|
205
|
+
{ menu: { _eq: menu.id } },
|
|
206
|
+
{ parent: { _null: true } },
|
|
207
|
+
],
|
|
208
|
+
}
|
|
209
|
+
: {
|
|
210
|
+
_and: [{ menu: { _eq: menu.id } }, { parent: { _null: true } }],
|
|
211
|
+
},
|
|
211
212
|
fields: [...ITEM_FIELDS],
|
|
212
213
|
sort: ["sort"],
|
|
213
214
|
limit: -1,
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
export type NavMenuTranslation = {
|
|
2
4
|
id: number;
|
|
3
5
|
nav_menus_id?: string;
|
|
@@ -14,6 +16,24 @@ export type NavItemTranslation = {
|
|
|
14
16
|
href: string;
|
|
15
17
|
};
|
|
16
18
|
|
|
19
|
+
export type NavMenuRow = {
|
|
20
|
+
id: string;
|
|
21
|
+
status: "published" | "draft" | "archived";
|
|
22
|
+
key: string;
|
|
23
|
+
translations?: NavMenuTranslation[] | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type NavItemRow = {
|
|
27
|
+
id: string;
|
|
28
|
+
status: "published" | "draft" | "archived";
|
|
29
|
+
menu?: string | NavMenuRow | null;
|
|
30
|
+
parent?: string | NavItemRow | null;
|
|
31
|
+
open_in_new_tab?: boolean | null;
|
|
32
|
+
sort?: number | null;
|
|
33
|
+
children?: NavItemRow[];
|
|
34
|
+
translations?: NavItemTranslation[] | null;
|
|
35
|
+
};
|
|
36
|
+
|
|
17
37
|
export type NavMenu = {
|
|
18
38
|
id: string;
|
|
19
39
|
status: "published" | "draft" | "archived";
|
|
@@ -42,19 +62,19 @@ export type NavMenuTree = {
|
|
|
42
62
|
};
|
|
43
63
|
|
|
44
64
|
export type GetNavMenusOptions = {
|
|
45
|
-
status?:
|
|
65
|
+
status?: NavMenu["status"] | null;
|
|
46
66
|
limit?: number;
|
|
47
|
-
sort?:
|
|
67
|
+
sort?: Array<DkitSortField<"key" | "id" | "status">>;
|
|
48
68
|
locale?: string;
|
|
49
69
|
};
|
|
50
70
|
|
|
51
71
|
export type GetNavMenuByKeyOptions = {
|
|
52
|
-
status?:
|
|
72
|
+
status?: NavMenu["status"] | null;
|
|
53
73
|
locale?: string;
|
|
54
74
|
};
|
|
55
75
|
|
|
56
76
|
export type GetNavItemsOptions = {
|
|
57
|
-
status?:
|
|
77
|
+
status?: NavItem["status"] | null;
|
|
58
78
|
tree?: boolean;
|
|
59
79
|
locale?: string;
|
|
60
80
|
};
|