@kurbo/dkit 0.4.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/README.md +4 -0
- package/dist/bin.js +385 -139
- package/dist/bin.js.map +1 -1
- package/package.json +4 -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,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
export type DirectusFileRef = {
|
|
2
4
|
id: string;
|
|
3
5
|
};
|
|
@@ -17,11 +19,11 @@ export type TeamMember = {
|
|
|
17
19
|
/** Shared list/query options for team member fetches. */
|
|
18
20
|
export type GetTeamMembersOptions = {
|
|
19
21
|
/** Directus status filter. Default `"published"`. Pass `null` to skip. */
|
|
20
|
-
status?:
|
|
22
|
+
status?: TeamMember["status"] | null;
|
|
21
23
|
limit?: number;
|
|
22
|
-
sort?:
|
|
24
|
+
sort?: Array<DkitSortField<"sort" | "name" | "id" | "status" | "slug">>;
|
|
23
25
|
};
|
|
24
26
|
|
|
25
27
|
export type GetTeamMemberBySlugOptions = {
|
|
26
|
-
status?:
|
|
28
|
+
status?: TeamMember["status"] | null;
|
|
27
29
|
};
|
|
@@ -3,40 +3,58 @@ import {
|
|
|
3
3
|
directusFileId,
|
|
4
4
|
dkitQueryFailed,
|
|
5
5
|
getDirectus,
|
|
6
|
-
readItem,
|
|
7
6
|
readItems,
|
|
8
7
|
} from "@/lib/dkit/client";
|
|
9
8
|
import { collections } from "@/lib/dkit/config";
|
|
10
9
|
import { queryLimit, statusFilter } from "@/lib/dkit/helpers";
|
|
11
10
|
import {
|
|
12
|
-
isUsableTranslation,
|
|
13
|
-
pickTranslation,
|
|
14
11
|
requireLocaleTranslation,
|
|
15
12
|
resolveLocale,
|
|
16
13
|
translationsDeepFilter,
|
|
14
|
+
withLocale,
|
|
17
15
|
} from "@/lib/dkit/i18n";
|
|
18
|
-
import type {
|
|
16
|
+
import type {
|
|
17
|
+
GetTestimonialsOptions,
|
|
18
|
+
Testimonial,
|
|
19
|
+
TestimonialRow,
|
|
20
|
+
} from "./types.multilocale";
|
|
19
21
|
|
|
20
|
-
export type {
|
|
22
|
+
export type {
|
|
23
|
+
DirectusFileRef,
|
|
24
|
+
GetTestimonialsOptions,
|
|
25
|
+
Testimonial,
|
|
26
|
+
TestimonialRow,
|
|
27
|
+
TestimonialTranslation,
|
|
28
|
+
} from "./types.multilocale";
|
|
21
29
|
|
|
22
|
-
function flattenTestimonial(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
function flattenTestimonial(
|
|
31
|
+
row: TestimonialRow,
|
|
32
|
+
locale: string,
|
|
33
|
+
): Testimonial | null {
|
|
34
|
+
return withLocale(
|
|
35
|
+
row,
|
|
36
|
+
locale,
|
|
37
|
+
(r, t) => ({
|
|
38
|
+
id: r.id,
|
|
39
|
+
status: r.status,
|
|
40
|
+
quote: t.quote,
|
|
41
|
+
author_name: r.author_name,
|
|
42
|
+
author_role: t.author_role ?? null,
|
|
43
|
+
company: r.company ?? null,
|
|
44
|
+
avatar: r.avatar,
|
|
45
|
+
sort: r.sort ?? null,
|
|
46
|
+
}),
|
|
47
|
+
{ required: ["quote"] },
|
|
48
|
+
);
|
|
36
49
|
}
|
|
37
50
|
|
|
38
51
|
const TESTIMONIAL_FIELDS = [
|
|
39
|
-
"id",
|
|
52
|
+
"id",
|
|
53
|
+
"status",
|
|
54
|
+
"author_name",
|
|
55
|
+
"company",
|
|
56
|
+
"avatar",
|
|
57
|
+
"sort",
|
|
40
58
|
{ translations: ["id", "languages_code", "quote", "author_role"] },
|
|
41
59
|
] as const;
|
|
42
60
|
|
|
@@ -45,49 +63,63 @@ export function testimonialAvatarUrl(testimonial: Testimonial): string | null {
|
|
|
45
63
|
return id ? directusAssetUrl(id) : null;
|
|
46
64
|
}
|
|
47
65
|
|
|
48
|
-
export async function getTestimonials(
|
|
66
|
+
export async function getTestimonials(
|
|
67
|
+
options: GetTestimonialsOptions = {},
|
|
68
|
+
): Promise<Testimonial[]> {
|
|
49
69
|
const client = getDirectus();
|
|
50
70
|
if (!client) return [];
|
|
51
|
-
if (!collections.testimonials) {
|
|
71
|
+
if (!collections.testimonials) {
|
|
72
|
+
dkitQueryFailed(
|
|
73
|
+
"collections.testimonials missing — re-run `dkit add testimonials`",
|
|
74
|
+
);
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
52
77
|
const locale = await resolveLocale(options.locale);
|
|
53
78
|
const status = statusFilter(options.status);
|
|
54
|
-
const
|
|
55
|
-
requireLocaleTranslation(locale, ["quote"])
|
|
56
|
-
|
|
57
|
-
if (status) and.unshift(status);
|
|
79
|
+
const filter = status
|
|
80
|
+
? { _and: [status, requireLocaleTranslation(locale, ["quote"])] }
|
|
81
|
+
: requireLocaleTranslation(locale, ["quote"]);
|
|
58
82
|
const limit = queryLimit(options.limit);
|
|
59
83
|
try {
|
|
60
|
-
const rows = await client.request(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
84
|
+
const rows = await client.request(
|
|
85
|
+
readItems(collections.testimonials, {
|
|
86
|
+
filter,
|
|
87
|
+
deep: translationsDeepFilter(locale),
|
|
88
|
+
fields: [...TESTIMONIAL_FIELDS],
|
|
89
|
+
sort: options.sort ?? ["sort", "author_name"],
|
|
90
|
+
...(limit !== undefined ? { limit } : {}),
|
|
91
|
+
}),
|
|
92
|
+
);
|
|
93
|
+
return (rows as TestimonialRow[])
|
|
68
94
|
.map((r) => flattenTestimonial(r, locale))
|
|
69
95
|
.filter((t): t is Testimonial => t != null);
|
|
70
|
-
} catch (err) {
|
|
96
|
+
} catch (err) {
|
|
97
|
+
dkitQueryFailed(`getTestimonials failed`, err);
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
71
100
|
}
|
|
72
101
|
|
|
73
|
-
export async function getTestimonialById(
|
|
102
|
+
export async function getTestimonialById(
|
|
103
|
+
id: string,
|
|
104
|
+
options: { locale?: string } = {},
|
|
105
|
+
): Promise<Testimonial | null> {
|
|
74
106
|
const client = getDirectus();
|
|
75
107
|
if (!client) return null;
|
|
76
108
|
if (!collections.testimonials) return null;
|
|
77
109
|
const locale = await resolveLocale(options.locale);
|
|
78
110
|
try {
|
|
79
|
-
const rows = await client.request(
|
|
80
|
-
|
|
81
|
-
_and: [
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return
|
|
92
|
-
}
|
|
111
|
+
const rows = await client.request(
|
|
112
|
+
readItems(collections.testimonials, {
|
|
113
|
+
filter: { _and: [{ id: { _eq: id } }, requireLocaleTranslation(locale, ["quote"])] },
|
|
114
|
+
deep: translationsDeepFilter(locale),
|
|
115
|
+
fields: [...TESTIMONIAL_FIELDS],
|
|
116
|
+
limit: 1,
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
const row = (rows as TestimonialRow[])[0];
|
|
120
|
+
return row ? flattenTestimonial(row, locale) : null;
|
|
121
|
+
} catch (err) {
|
|
122
|
+
dkitQueryFailed(`getTestimonialById(${id}) failed`, err);
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
93
125
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"files": [
|
|
3
3
|
{
|
|
4
|
-
"id": "
|
|
4
|
+
"id": "b1000000-0000-4000-8000-000000000001",
|
|
5
5
|
"filename_download": "sample-author-avatar.jpg",
|
|
6
6
|
"title": "Sample avatar — Sample Author (Unsplash)",
|
|
7
7
|
"type": "image/jpeg",
|
|
8
8
|
"path": "assets/sample-author-avatar.jpg"
|
|
9
9
|
},
|
|
10
10
|
{
|
|
11
|
-
"id": "
|
|
11
|
+
"id": "b1000000-0000-4000-8000-000000000002",
|
|
12
12
|
"filename_download": "jane-doe-avatar.jpg",
|
|
13
13
|
"title": "Sample avatar — Jane Doe (Unsplash)",
|
|
14
14
|
"type": "image/jpeg",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"author_name": "Sample Author",
|
|
25
25
|
"author_role": "Head of Product",
|
|
26
26
|
"company": "Acme Corp",
|
|
27
|
-
"avatar": "
|
|
27
|
+
"avatar": "b1000000-0000-4000-8000-000000000001",
|
|
28
28
|
"sort": 1
|
|
29
29
|
},
|
|
30
30
|
{
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"author_name": "Jane Doe",
|
|
35
35
|
"author_role": "Engineering Lead",
|
|
36
36
|
"company": "Globex Inc",
|
|
37
|
-
"avatar": "
|
|
37
|
+
"avatar": "b1000000-0000-4000-8000-000000000002",
|
|
38
38
|
"sort": 2
|
|
39
39
|
},
|
|
40
40
|
{
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"files": [
|
|
3
3
|
{
|
|
4
|
-
"id": "
|
|
4
|
+
"id": "b1000000-0000-4000-8000-000000000001",
|
|
5
5
|
"filename_download": "sample-author-avatar.jpg",
|
|
6
6
|
"title": "Sample avatar — Sample Author (Unsplash)",
|
|
7
7
|
"type": "image/jpeg",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"status": "published",
|
|
16
16
|
"author_name": "Jane Doe",
|
|
17
17
|
"company": "Acme Inc.",
|
|
18
|
-
"avatar": "
|
|
18
|
+
"avatar": "b1000000-0000-4000-8000-000000000001",
|
|
19
19
|
"sort": 1,
|
|
20
20
|
"translations": [
|
|
21
21
|
{
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
export type DirectusFileRef = { id: string };
|
|
2
4
|
|
|
3
5
|
export type TestimonialTranslation = {
|
|
@@ -8,6 +10,16 @@ export type TestimonialTranslation = {
|
|
|
8
10
|
author_role?: string | null;
|
|
9
11
|
};
|
|
10
12
|
|
|
13
|
+
export type TestimonialRow = {
|
|
14
|
+
id: string;
|
|
15
|
+
status: "published" | "draft";
|
|
16
|
+
author_name: string;
|
|
17
|
+
company?: string | null;
|
|
18
|
+
avatar?: string | DirectusFileRef | null;
|
|
19
|
+
sort?: number | null;
|
|
20
|
+
translations?: TestimonialTranslation[] | null;
|
|
21
|
+
};
|
|
22
|
+
|
|
11
23
|
export type Testimonial = {
|
|
12
24
|
id: string;
|
|
13
25
|
status: "published" | "draft";
|
|
@@ -20,8 +32,8 @@ export type Testimonial = {
|
|
|
20
32
|
};
|
|
21
33
|
|
|
22
34
|
export type GetTestimonialsOptions = {
|
|
23
|
-
status?:
|
|
35
|
+
status?: Testimonial["status"] | null;
|
|
24
36
|
limit?: number;
|
|
25
|
-
sort?:
|
|
37
|
+
sort?: Array<DkitSortField<"sort" | "id" | "status" | "author_name">>;
|
|
26
38
|
locale?: string;
|
|
27
39
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
type DkitSortField<T extends string> = T | `-${T}`;
|
|
2
|
+
|
|
1
3
|
export type DirectusFileRef = {
|
|
2
4
|
id: string;
|
|
3
5
|
};
|
|
@@ -16,7 +18,7 @@ export type Testimonial = {
|
|
|
16
18
|
/** Shared list/query options for testimonial fetches. */
|
|
17
19
|
export type GetTestimonialsOptions = {
|
|
18
20
|
/** Directus status filter. Default `"published"`. Pass `null` to skip. */
|
|
19
|
-
status?:
|
|
21
|
+
status?: Testimonial["status"] | null;
|
|
20
22
|
limit?: number;
|
|
21
|
-
sort?:
|
|
23
|
+
sort?: Array<DkitSortField<"sort" | "id" | "status" | "author_name">>;
|
|
22
24
|
};
|
|
@@ -4,10 +4,16 @@
|
|
|
4
4
|
* Safe to edit.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Default status=published; pass `status: null` to skip the filter.
|
|
9
|
+
* Generic so `_eq` keeps the collection's status union (not widened to `string`).
|
|
10
|
+
*/
|
|
11
|
+
export function statusFilter<S extends string = "published">(
|
|
12
|
+
status?: S | null,
|
|
13
|
+
fallback: S = "published" as S,
|
|
14
|
+
): { status: { _eq: S } } | undefined {
|
|
9
15
|
if (status === null) return undefined;
|
|
10
|
-
return { status: { _eq: status ??
|
|
16
|
+
return { status: { _eq: status ?? fallback } };
|
|
11
17
|
}
|
|
12
18
|
|
|
13
19
|
/** Directus rejects `limit=undefined` — only send a finite number. */
|
|
@@ -6,17 +6,28 @@
|
|
|
6
6
|
import { defaultLocale } from "@/lib/dkit/config";
|
|
7
7
|
import { getRequestLocale } from "@/lib/dkit/locale";
|
|
8
8
|
|
|
9
|
+
export type TranslationCode = string | { code?: string };
|
|
10
|
+
|
|
11
|
+
export type TranslationLike = {
|
|
12
|
+
languages_code?: TranslationCode | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type WithTranslations<T extends TranslationLike> = {
|
|
16
|
+
translations?: T[] | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
9
19
|
export function translationCode(
|
|
10
|
-
languages_code:
|
|
20
|
+
languages_code: TranslationCode | null | undefined,
|
|
11
21
|
): string | undefined {
|
|
12
22
|
if (typeof languages_code === "string") return languages_code;
|
|
13
23
|
return languages_code?.code;
|
|
14
24
|
}
|
|
15
25
|
|
|
16
26
|
/** Exact locale only — never fall back to another language's row. */
|
|
17
|
-
export function pickTranslation<
|
|
18
|
-
T
|
|
19
|
-
|
|
27
|
+
export function pickTranslation<T extends TranslationLike>(
|
|
28
|
+
rows: T[] | null | undefined,
|
|
29
|
+
locale: string,
|
|
30
|
+
): T | undefined {
|
|
20
31
|
if (!Array.isArray(rows) || rows.length === 0) return undefined;
|
|
21
32
|
return rows.find((r) => translationCode(r.languages_code) === locale);
|
|
22
33
|
}
|
|
@@ -38,11 +49,15 @@ export async function resolveLocale(
|
|
|
38
49
|
/**
|
|
39
50
|
* Filter: item must have a translation for `locale` with every listed field
|
|
40
51
|
* non-empty. Defaults to title + slug (posts, pages, projects, services).
|
|
52
|
+
*
|
|
53
|
+
* Return is intentionally loose — dynamic translation field names do not fit
|
|
54
|
+
* `QueryFilter` literals from `@directus/sdk`.
|
|
41
55
|
*/
|
|
42
56
|
export function requireLocaleTranslation(
|
|
43
57
|
locale: string,
|
|
44
58
|
fields: string[] = ["title", "slug"],
|
|
45
|
-
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
): any {
|
|
46
61
|
return {
|
|
47
62
|
translations: {
|
|
48
63
|
_and: [
|
|
@@ -53,22 +68,79 @@ export function requireLocaleTranslation(
|
|
|
53
68
|
};
|
|
54
69
|
}
|
|
55
70
|
|
|
56
|
-
/**
|
|
57
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Deep query: only load translation rows for this locale.
|
|
73
|
+
* Use as `deep: translationsDeepFilter(locale)` on readItems.
|
|
74
|
+
*/
|
|
75
|
+
export function translationsDeepFilter(
|
|
76
|
+
locale: string,
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
+
): any {
|
|
58
79
|
return {
|
|
59
80
|
translations: { _filter: { languages_code: { _eq: locale } } },
|
|
60
|
-
}
|
|
81
|
+
};
|
|
61
82
|
}
|
|
62
83
|
|
|
63
84
|
/**
|
|
64
85
|
* True when every listed field is a non-empty string on the translation row.
|
|
65
86
|
* Defaults to title + slug.
|
|
66
87
|
*/
|
|
67
|
-
export function isUsableTranslation(
|
|
68
|
-
trans:
|
|
69
|
-
fields: string[] = ["title", "slug"],
|
|
70
|
-
):
|
|
88
|
+
export function isUsableTranslation<T extends object>(
|
|
89
|
+
trans: T | null | undefined,
|
|
90
|
+
fields: readonly string[] = ["title", "slug"],
|
|
91
|
+
): trans is T {
|
|
71
92
|
if (!trans) return false;
|
|
72
93
|
const row = trans as Record<string, unknown>;
|
|
73
94
|
return fields.every((f) => Boolean(String(row[f] ?? "").trim()));
|
|
74
95
|
}
|
|
96
|
+
|
|
97
|
+
type WithLocaleRequiredOptions = {
|
|
98
|
+
required: readonly string[];
|
|
99
|
+
fallbackLocale?: string;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
type WithLocaleOptionalOptions = {
|
|
103
|
+
required?: undefined;
|
|
104
|
+
fallbackLocale?: string;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
type TranslationOf<TRow> = TRow extends {
|
|
108
|
+
translations?: Array<infer T> | null;
|
|
109
|
+
}
|
|
110
|
+
? Extract<T, TranslationLike>
|
|
111
|
+
: TranslationLike;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Pick a translation for `locale` (optional fallback) and map to a public shape.
|
|
115
|
+
* When `required` is set, missing/empty fields → `null` and `map` receives a defined translation.
|
|
116
|
+
*/
|
|
117
|
+
export function withLocale<TRow extends WithTranslations<TranslationLike>, TOut>(
|
|
118
|
+
row: TRow,
|
|
119
|
+
locale: string,
|
|
120
|
+
map: (row: TRow, trans: TranslationOf<TRow>) => TOut | null,
|
|
121
|
+
options: WithLocaleRequiredOptions,
|
|
122
|
+
): TOut | null;
|
|
123
|
+
export function withLocale<TRow extends WithTranslations<TranslationLike>, TOut>(
|
|
124
|
+
row: TRow,
|
|
125
|
+
locale: string,
|
|
126
|
+
map: (row: TRow, trans: TranslationOf<TRow> | undefined) => TOut | null,
|
|
127
|
+
options?: WithLocaleOptionalOptions,
|
|
128
|
+
): TOut | null;
|
|
129
|
+
/** @internal overload impl — keep loose so required/optional map signatures both type-check */
|
|
130
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
131
|
+
export function withLocale(
|
|
132
|
+
row: any,
|
|
133
|
+
locale: string,
|
|
134
|
+
map: any,
|
|
135
|
+
options?: { required?: readonly string[]; fallbackLocale?: string },
|
|
136
|
+
): any {
|
|
137
|
+
let trans = pickTranslation(row.translations, locale);
|
|
138
|
+
if (!trans && options?.fallbackLocale) {
|
|
139
|
+
trans = pickTranslation(row.translations, options.fallbackLocale);
|
|
140
|
+
}
|
|
141
|
+
if (options?.required) {
|
|
142
|
+
if (!isUsableTranslation(trans, options.required)) return null;
|
|
143
|
+
return map(row, trans);
|
|
144
|
+
}
|
|
145
|
+
return map(row, trans);
|
|
146
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Shared rich-text styles for dkit content modules (blog, pages, page-builder, …).
|
|
3
3
|
* Import once in your app CSS. Override tokens below to match your brand.
|
|
4
4
|
*
|
|
5
|
-
* Uses shadcn
|
|
5
|
+
* Uses shadcn CSS variables when present
|
|
6
6
|
* (--foreground, --muted-foreground, --primary, --border, --muted, --radius)
|
|
7
7
|
* with sensible fallbacks for bare projects.
|
|
8
8
|
*
|