@carlonicora/nextjs-jsonapi 1.16.0 → 1.18.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/README.md +1 -1
- package/dist/ApiData-DPKNfY-9.d.mts +10 -0
- package/dist/ApiData-DPKNfY-9.d.ts +10 -0
- package/dist/ApiRequestDataTypeInterface-DIEOFn9s.d.mts +40 -0
- package/dist/ApiRequestDataTypeInterface-DIEOFn9s.d.ts +40 -0
- package/dist/{ApiResponseInterface-BvWIeLkq.d.ts → ApiResponseInterface-BKyod24U.d.ts} +2 -11
- package/dist/{ApiResponseInterface-CAbw0sv7.d.mts → ApiResponseInterface-Dqvu09tz.d.mts} +2 -11
- package/dist/{BlockNoteEditor-HFX7Z5BQ.mjs → BlockNoteEditor-6TWTNHNZ.mjs} +7 -6
- package/dist/{BlockNoteEditor-HFX7Z5BQ.mjs.map → BlockNoteEditor-6TWTNHNZ.mjs.map} +1 -1
- package/dist/{BlockNoteEditor-MBFDWP7X.js → BlockNoteEditor-C3WWGGT6.js} +17 -16
- package/dist/BlockNoteEditor-C3WWGGT6.js.map +1 -0
- package/dist/JsonApiContext-Bsm_Q2oe.d.mts +41 -0
- package/dist/JsonApiContext-Bsm_Q2oe.d.ts +41 -0
- package/dist/JsonApiRequest-54ZBO7WQ.js +24 -0
- package/dist/{JsonApiRequest-45CLE65I.js.map → JsonApiRequest-54ZBO7WQ.js.map} +1 -1
- package/dist/{JsonApiRequest-6IPS3DZJ.mjs → JsonApiRequest-XWQWTFEQ.mjs} +2 -2
- package/dist/chunk-3EPNHTMH.js +26 -0
- package/dist/chunk-3EPNHTMH.js.map +1 -0
- package/dist/{chunk-BCKYJQ3K.mjs → chunk-3VM3WAOV.mjs} +1 -1
- package/dist/{chunk-ONB2DAIV.js → chunk-6U6QCSJK.js} +4224 -2775
- package/dist/chunk-6U6QCSJK.js.map +1 -0
- package/dist/{chunk-R5QSSISB.js → chunk-7DTKRMYW.js} +21 -14
- package/dist/chunk-7DTKRMYW.js.map +1 -0
- package/dist/{chunk-BCQSE3EU.mjs → chunk-KUFWHMMY.mjs} +8 -8
- package/dist/{chunk-POKIJ56Q.mjs → chunk-KX7YG6LY.mjs} +22 -15
- package/dist/chunk-KX7YG6LY.mjs.map +1 -0
- package/dist/{chunk-GPGJNTHP.js → chunk-LI6CPNJI.js} +1 -1
- package/dist/{chunk-GPGJNTHP.js.map → chunk-LI6CPNJI.js.map} +1 -1
- package/dist/{chunk-2AZLCF6D.js → chunk-UYY34W7R.js} +28 -28
- package/dist/{chunk-2AZLCF6D.js.map → chunk-UYY34W7R.js.map} +1 -1
- package/dist/{chunk-5RAUCUAA.mjs → chunk-UZDAPWJG.mjs} +5645 -4196
- package/dist/chunk-UZDAPWJG.mjs.map +1 -0
- package/dist/chunk-VOXD3ZLY.mjs +26 -0
- package/dist/chunk-VOXD3ZLY.mjs.map +1 -0
- package/dist/client/index.d.mts +11 -45
- package/dist/client/index.d.ts +11 -45
- package/dist/client/index.js +9 -7
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +11 -9
- package/dist/components/index.d.mts +302 -388
- package/dist/components/index.d.ts +302 -388
- package/dist/components/index.js +31 -6
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +40 -15
- package/dist/{config-DEaUbBqR.d.ts → config--nwiW74Z.d.ts} +1 -1
- package/dist/{config-CWsTwnsK.d.mts → config-BKSQmUWU.d.mts} +1 -1
- package/dist/{content.interface-D_4b4RQt.d.ts → content.interface-4VICFRA0.d.ts} +2 -1
- package/dist/{content.interface-Dk4UZcJM.d.mts → content.interface-CFc97-Cj.d.mts} +2 -1
- package/dist/contexts/index.d.mts +3 -2
- package/dist/contexts/index.d.ts +3 -2
- package/dist/contexts/index.js +7 -6
- package/dist/contexts/index.js.map +1 -1
- package/dist/contexts/index.mjs +6 -5
- package/dist/core/index.d.mts +11 -8
- package/dist/core/index.d.ts +11 -8
- package/dist/core/index.js +4 -4
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +3 -3
- package/dist/index.d.mts +15 -11
- package/dist/index.d.ts +15 -11
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -6
- package/dist/{notification.interface-BllkURRm.d.ts → notification.interface-BGaPiCUM.d.mts} +2 -40
- package/dist/{notification.interface-BllkURRm.d.mts → notification.interface-CqwaOIgM.d.ts} +2 -40
- package/dist/{s3.service-BEfGqho0.d.ts → s3.service-BYs88XEE.d.ts} +3 -2
- package/dist/{s3.service-DIQRYe93.d.mts → s3.service-C0BjOdvn.d.mts} +3 -2
- package/dist/scripts/generate-web-module/templates/components/editor.template.d.ts.map +1 -1
- package/dist/scripts/generate-web-module/templates/components/editor.template.js +20 -6
- package/dist/scripts/generate-web-module/templates/components/editor.template.js.map +1 -1
- package/dist/scripts/generate-web-module/templates/components/selector.template.d.ts.map +1 -1
- package/dist/scripts/generate-web-module/templates/components/selector.template.js +45 -48
- package/dist/scripts/generate-web-module/templates/components/selector.template.js.map +1 -1
- package/dist/server/index.d.mts +6 -4
- package/dist/server/index.d.ts +6 -4
- package/dist/server/index.js +13 -13
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +3 -3
- package/dist/{stripe-subscription.interface-C63L6hVg.d.mts → stripe-subscription.interface-B-TM40Io.d.ts} +1 -1
- package/dist/{stripe-subscription.interface-CUvNDvw5.d.ts → stripe-subscription.interface-DDxnpj0F.d.mts} +1 -1
- package/dist/testing/index.d.mts +338 -0
- package/dist/testing/index.d.ts +338 -0
- package/dist/testing/index.js +323 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/index.mjs +323 -0
- package/dist/testing/index.mjs.map +1 -0
- package/dist/{useSocket-BpenBR2z.d.mts → useSocket-BNj9PrRw.d.mts} +1 -1
- package/dist/{useSocket-D-QYA0Sr.d.ts → useSocket-Dwt8cz1x.d.ts} +1 -1
- package/package.json +26 -27
- package/scripts/generate-web-module/templates/components/editor.template.ts +20 -6
- package/scripts/generate-web-module/templates/components/selector.template.ts +45 -48
- package/src/client/hooks/__tests__/useJsonApiGet.test.tsx +229 -0
- package/src/client/hooks/__tests__/useJsonApiMutation.test.tsx +348 -0
- package/src/client/hooks/__tests__/useRehydration.test.ts +188 -0
- package/src/components/forms/CommonDeleter.tsx +2 -2
- package/src/components/forms/CommonEditorTrigger.tsx +3 -3
- package/src/components/forms/DatePickerPopover.tsx +3 -1
- package/src/components/forms/DateRangeSelector.tsx +1 -1
- package/src/components/forms/FormCheckbox.tsx +1 -1
- package/src/components/forms/FormDate.tsx +3 -1
- package/src/components/forms/FormDateTime.tsx +5 -3
- package/src/components/forms/FormSelect.tsx +1 -1
- package/src/components/forms/FormSlider.tsx +4 -1
- package/src/components/forms/__tests__/FormCheckbox.test.tsx +242 -0
- package/src/components/forms/__tests__/FormDate.test.tsx +216 -0
- package/src/components/forms/__tests__/FormInput.test.tsx +292 -0
- package/src/components/forms/__tests__/FormSelect.test.tsx +177 -0
- package/src/components/navigations/RecentPagesNavigator.tsx +2 -2
- package/src/components/tables/ContentListTable.tsx +3 -3
- package/src/components/tables/__tests__/ContentListTable.test.tsx +411 -0
- package/src/core/endpoint/__tests__/EndpointCreator.test.ts +168 -0
- package/src/core/factories/__tests__/JsonApiDataFactory.test.ts +109 -0
- package/src/core/factories/__tests__/RehydrationFactory.test.ts +151 -0
- package/src/core/registry/__tests__/DataClassRegistry.test.ts +136 -0
- package/src/core/registry/__tests__/ModuleRegistrar.test.ts +159 -0
- package/src/features/auth/components/details/LandingComponent.tsx +14 -12
- package/src/features/billing/stripe-customer/components/details/PaymentMethodCard.tsx +2 -2
- package/src/features/company/components/forms/CompanyConfigurationEditor.tsx +2 -2
- package/src/features/company/components/forms/CompanyDeleter.tsx +1 -1
- package/src/features/content/components/lists/ContentsList.tsx +1 -1
- package/src/features/notification/components/lists/NotificationsList.tsx +1 -1
- package/src/features/notification/components/modals/NotificationModal.tsx +2 -2
- package/src/features/role/components/forms/FormRoles.tsx +1 -1
- package/src/features/user/components/forms/UserEditor.tsx +2 -2
- package/src/features/user/components/forms/UserReactivator.tsx +1 -1
- package/src/features/user/components/forms/UserResentInvitationEmail.tsx +2 -2
- package/src/features/user/components/widgets/UserAvatar.tsx +37 -31
- package/src/features/user/components/widgets/UserSearchPopover.tsx +1 -1
- package/src/hooks/__tests__/useDataListRetriever.test.ts +321 -0
- package/src/hooks/__tests__/useDebounce.test.ts +170 -0
- package/src/hooks/use-mobile.ts +1 -0
- package/src/index.ts +4 -1
- package/src/lib/utils.ts +2 -0
- package/src/login/config.ts +27 -0
- package/src/login/index.ts +2 -0
- package/src/shadcnui/custom/multi-select.tsx +10 -21
- package/src/shadcnui/ui/accordion.tsx +64 -42
- package/src/shadcnui/ui/alert-dialog.tsx +142 -108
- package/src/shadcnui/ui/alert.tsx +64 -35
- package/src/shadcnui/ui/avatar.tsx +106 -50
- package/src/shadcnui/ui/badge.tsx +34 -26
- package/src/shadcnui/ui/breadcrumb.tsx +103 -92
- package/src/shadcnui/ui/button.tsx +30 -30
- package/src/shadcnui/ui/calendar.tsx +192 -50
- package/src/shadcnui/ui/card.tsx +94 -43
- package/src/shadcnui/ui/carousel.tsx +220 -201
- package/src/shadcnui/ui/chart.tsx +244 -190
- package/src/shadcnui/ui/checkbox.tsx +25 -25
- package/src/shadcnui/ui/collapsible.tsx +10 -4
- package/src/shadcnui/ui/combobox.tsx +292 -0
- package/src/shadcnui/ui/command.tsx +158 -126
- package/src/shadcnui/ui/context-menu.tsx +242 -164
- package/src/shadcnui/ui/dialog.tsx +125 -70
- package/src/shadcnui/ui/drawer.tsx +106 -70
- package/src/shadcnui/ui/dropdown-menu.tsx +231 -182
- package/src/shadcnui/ui/field.tsx +227 -0
- package/src/shadcnui/ui/hover-card.tsx +45 -23
- package/src/shadcnui/ui/input-group.tsx +149 -0
- package/src/shadcnui/ui/input-otp.tsx +19 -9
- package/src/shadcnui/ui/input.tsx +4 -5
- package/src/shadcnui/ui/label.tsx +16 -22
- package/src/shadcnui/ui/navigation-menu.tsx +44 -49
- package/src/shadcnui/ui/popover.tsx +81 -24
- package/src/shadcnui/ui/progress.tsx +77 -22
- package/src/shadcnui/ui/radio-group.tsx +30 -28
- package/src/shadcnui/ui/resizable.tsx +23 -17
- package/src/shadcnui/ui/scroll-area.tsx +50 -35
- package/src/shadcnui/ui/select.tsx +163 -135
- package/src/shadcnui/ui/separator.tsx +5 -8
- package/src/shadcnui/ui/sheet.tsx +40 -50
- package/src/shadcnui/ui/sidebar.tsx +317 -271
- package/src/shadcnui/ui/skeleton.tsx +2 -2
- package/src/shadcnui/ui/slider.tsx +60 -21
- package/src/shadcnui/ui/sonner.tsx +25 -1
- package/src/shadcnui/ui/switch.tsx +31 -24
- package/src/shadcnui/ui/table.tsx +84 -103
- package/src/shadcnui/ui/tabs.tsx +82 -55
- package/src/shadcnui/ui/textarea.tsx +15 -21
- package/src/shadcnui/ui/toggle.tsx +26 -21
- package/src/shadcnui/ui/tooltip.tsx +33 -24
- package/src/testing/factories/createMockApiData.ts +143 -0
- package/src/testing/factories/createMockModule.ts +32 -0
- package/src/testing/factories/createMockResponse.ts +88 -0
- package/src/testing/factories/createMockService.ts +76 -0
- package/src/testing/index.ts +56 -0
- package/src/testing/matchers/jsonApiMatchers.ts +172 -0
- package/src/testing/providers/MockJsonApiProvider.tsx +58 -0
- package/src/testing/utils/renderWithProviders.tsx +76 -0
- package/src/utils/__tests__/date-formatter.test.ts +161 -0
- package/src/utils/__tests__/exists.test.ts +100 -0
- package/src/utils/cn.test.ts +44 -0
- package/dist/BlockNoteEditor-MBFDWP7X.js.map +0 -1
- package/dist/JsonApiRequest-45CLE65I.js +0 -24
- package/dist/chunk-5RAUCUAA.mjs.map +0 -1
- package/dist/chunk-ONB2DAIV.js.map +0 -1
- package/dist/chunk-POKIJ56Q.mjs.map +0 -1
- package/dist/chunk-R5QSSISB.js.map +0 -1
- package/src/discord/config.ts +0 -15
- package/src/discord/index.ts +0 -1
- /package/dist/{JsonApiRequest-6IPS3DZJ.mjs.map → JsonApiRequest-XWQWTFEQ.mjs.map} +0 -0
- /package/dist/{chunk-BCKYJQ3K.mjs.map → chunk-3VM3WAOV.mjs.map} +0 -0
- /package/dist/{chunk-BCQSE3EU.mjs.map → chunk-KUFWHMMY.mjs.map} +0 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { expect } from "vitest";
|
|
2
|
+
|
|
3
|
+
interface JsonApiResponse {
|
|
4
|
+
data?:
|
|
5
|
+
| {
|
|
6
|
+
type?: string;
|
|
7
|
+
id?: string;
|
|
8
|
+
attributes?: Record<string, any>;
|
|
9
|
+
relationships?: Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
| Array<{
|
|
12
|
+
type?: string;
|
|
13
|
+
id?: string;
|
|
14
|
+
attributes?: Record<string, any>;
|
|
15
|
+
relationships?: Record<string, any>;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom Vitest matchers for JSON:API assertions.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { jsonApiMatchers } from '@carlonicora/nextjs-jsonapi/testing';
|
|
25
|
+
* import { expect } from 'vitest';
|
|
26
|
+
*
|
|
27
|
+
* expect.extend(jsonApiMatchers);
|
|
28
|
+
*
|
|
29
|
+
* // Then use in tests:
|
|
30
|
+
* expect(response).toBeValidJsonApi();
|
|
31
|
+
* expect(response).toHaveJsonApiType('articles');
|
|
32
|
+
* expect(response).toHaveJsonApiAttribute('title', 'My Article');
|
|
33
|
+
* expect(response).toHaveJsonApiRelationship('author');
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const jsonApiMatchers = {
|
|
37
|
+
/**
|
|
38
|
+
* Asserts that the response has a valid JSON:API structure with type and id.
|
|
39
|
+
*/
|
|
40
|
+
toBeValidJsonApi(received: JsonApiResponse) {
|
|
41
|
+
const data = Array.isArray(received?.data) ? received.data[0] : received?.data;
|
|
42
|
+
const hasType = typeof data?.type === "string" && data.type.length > 0;
|
|
43
|
+
const hasId = typeof data?.id === "string" && data.id.length > 0;
|
|
44
|
+
const isValid = hasType && hasId;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
pass: isValid,
|
|
48
|
+
message: () =>
|
|
49
|
+
isValid
|
|
50
|
+
? `Expected response not to be valid JSON:API, but it has type "${data?.type}" and id "${data?.id}"`
|
|
51
|
+
: `Expected response to be valid JSON:API with type and id, but got type: ${JSON.stringify(data?.type)}, id: ${JSON.stringify(data?.id)}`,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Asserts that the response data has the expected JSON:API type.
|
|
57
|
+
*/
|
|
58
|
+
toHaveJsonApiType(received: JsonApiResponse, expectedType: string) {
|
|
59
|
+
const data = Array.isArray(received?.data) ? received.data[0] : received?.data;
|
|
60
|
+
const actualType = data?.type;
|
|
61
|
+
const pass = actualType === expectedType;
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
pass,
|
|
65
|
+
message: () =>
|
|
66
|
+
pass
|
|
67
|
+
? `Expected response not to have JSON:API type "${expectedType}"`
|
|
68
|
+
: `Expected response to have JSON:API type "${expectedType}", but got "${actualType}"`,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Asserts that the response data has an attribute with the expected value.
|
|
74
|
+
*/
|
|
75
|
+
toHaveJsonApiAttribute(received: JsonApiResponse, attributeName: string, expectedValue?: any) {
|
|
76
|
+
const data = Array.isArray(received?.data) ? received.data[0] : received?.data;
|
|
77
|
+
const attributes = data?.attributes ?? {};
|
|
78
|
+
const hasAttribute = attributeName in attributes;
|
|
79
|
+
const actualValue = attributes[attributeName];
|
|
80
|
+
|
|
81
|
+
if (expectedValue === undefined) {
|
|
82
|
+
// Just check existence
|
|
83
|
+
return {
|
|
84
|
+
pass: hasAttribute,
|
|
85
|
+
message: () =>
|
|
86
|
+
hasAttribute
|
|
87
|
+
? `Expected response not to have JSON:API attribute "${attributeName}"`
|
|
88
|
+
: `Expected response to have JSON:API attribute "${attributeName}", but it was not found. Available attributes: ${Object.keys(attributes).join(", ") || "none"}`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const valuesMatch = actualValue === expectedValue;
|
|
93
|
+
return {
|
|
94
|
+
pass: hasAttribute && valuesMatch,
|
|
95
|
+
message: () =>
|
|
96
|
+
hasAttribute && valuesMatch
|
|
97
|
+
? `Expected response not to have JSON:API attribute "${attributeName}" with value "${expectedValue}"`
|
|
98
|
+
: !hasAttribute
|
|
99
|
+
? `Expected response to have JSON:API attribute "${attributeName}", but it was not found`
|
|
100
|
+
: `Expected JSON:API attribute "${attributeName}" to be "${expectedValue}", but got "${actualValue}"`,
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Asserts that the response data has the specified relationship.
|
|
106
|
+
*/
|
|
107
|
+
toHaveJsonApiRelationship(received: JsonApiResponse, relationshipName: string) {
|
|
108
|
+
const data = Array.isArray(received?.data) ? received.data[0] : received?.data;
|
|
109
|
+
const relationships = data?.relationships ?? {};
|
|
110
|
+
const hasRelationship = relationshipName in relationships;
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
pass: hasRelationship,
|
|
114
|
+
message: () =>
|
|
115
|
+
hasRelationship
|
|
116
|
+
? `Expected response not to have JSON:API relationship "${relationshipName}"`
|
|
117
|
+
: `Expected response to have JSON:API relationship "${relationshipName}", but it was not found. Available relationships: ${Object.keys(relationships).join(", ") || "none"}`,
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Asserts that the response data array has the expected length.
|
|
123
|
+
*/
|
|
124
|
+
toHaveJsonApiLength(received: JsonApiResponse, expectedLength: number) {
|
|
125
|
+
const data = received?.data;
|
|
126
|
+
const isArray = Array.isArray(data);
|
|
127
|
+
const actualLength = isArray ? data.length : data ? 1 : 0;
|
|
128
|
+
const pass = actualLength === expectedLength;
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
pass,
|
|
132
|
+
message: () =>
|
|
133
|
+
pass
|
|
134
|
+
? `Expected response not to have ${expectedLength} items`
|
|
135
|
+
: `Expected response to have ${expectedLength} items, but got ${actualLength}`,
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Type declarations for the custom matchers
|
|
141
|
+
declare module "vitest" {
|
|
142
|
+
interface Assertion<T = any> {
|
|
143
|
+
toBeValidJsonApi(): T;
|
|
144
|
+
toHaveJsonApiType(expectedType: string): T;
|
|
145
|
+
toHaveJsonApiAttribute(attributeName: string, expectedValue?: any): T;
|
|
146
|
+
toHaveJsonApiRelationship(relationshipName: string): T;
|
|
147
|
+
toHaveJsonApiLength(expectedLength: number): T;
|
|
148
|
+
}
|
|
149
|
+
interface AsymmetricMatchersContaining {
|
|
150
|
+
toBeValidJsonApi(): any;
|
|
151
|
+
toHaveJsonApiType(expectedType: string): any;
|
|
152
|
+
toHaveJsonApiAttribute(attributeName: string, expectedValue?: any): any;
|
|
153
|
+
toHaveJsonApiRelationship(relationshipName: string): any;
|
|
154
|
+
toHaveJsonApiLength(expectedLength: number): any;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Extends Vitest's expect with JSON:API matchers.
|
|
160
|
+
* Call this in your test setup file.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* // vitest.setup.ts
|
|
165
|
+
* import { extendExpectWithJsonApiMatchers } from '@carlonicora/nextjs-jsonapi/testing';
|
|
166
|
+
*
|
|
167
|
+
* extendExpectWithJsonApiMatchers();
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export function extendExpectWithJsonApiMatchers(): void {
|
|
171
|
+
expect.extend(jsonApiMatchers);
|
|
172
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { JsonApiConfig, JsonApiContext } from "../../client/context/JsonApiContext";
|
|
5
|
+
|
|
6
|
+
export interface MockJsonApiProviderProps {
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
config?: Partial<JsonApiConfig>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const defaultMockConfig: JsonApiConfig = {
|
|
12
|
+
apiUrl: "https://api.test.com",
|
|
13
|
+
tokenGetter: async () => "mock-token-for-testing",
|
|
14
|
+
languageGetter: async () => "en",
|
|
15
|
+
defaultHeaders: {},
|
|
16
|
+
onError: () => {},
|
|
17
|
+
cacheConfig: {
|
|
18
|
+
defaultProfile: "default",
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A test-friendly provider that wraps components with mock JSON:API context.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* import { MockJsonApiProvider } from '@carlonicora/nextjs-jsonapi/testing';
|
|
28
|
+
*
|
|
29
|
+
* render(
|
|
30
|
+
* <MockJsonApiProvider>
|
|
31
|
+
* <MyComponent />
|
|
32
|
+
* </MockJsonApiProvider>
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example With custom config
|
|
37
|
+
* ```tsx
|
|
38
|
+
* render(
|
|
39
|
+
* <MockJsonApiProvider config={{ apiUrl: 'https://custom.api.com' }}>
|
|
40
|
+
* <MyComponent />
|
|
41
|
+
* </MockJsonApiProvider>
|
|
42
|
+
* );
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function MockJsonApiProvider({ children, config }: MockJsonApiProviderProps) {
|
|
46
|
+
const mergedConfig: JsonApiConfig = {
|
|
47
|
+
...defaultMockConfig,
|
|
48
|
+
...config,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<JsonApiContext.Provider value={mergedConfig}>
|
|
53
|
+
{children}
|
|
54
|
+
</JsonApiContext.Provider>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { defaultMockConfig };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import React, { ReactElement } from "react";
|
|
4
|
+
import { render, RenderOptions, RenderResult } from "@testing-library/react";
|
|
5
|
+
import { MockJsonApiProvider, MockJsonApiProviderProps } from "../providers/MockJsonApiProvider";
|
|
6
|
+
import { JsonApiConfig } from "../../client/context/JsonApiContext";
|
|
7
|
+
|
|
8
|
+
export interface RenderWithProvidersOptions extends Omit<RenderOptions, "wrapper"> {
|
|
9
|
+
/**
|
|
10
|
+
* Custom JSON:API configuration to pass to the mock provider.
|
|
11
|
+
*/
|
|
12
|
+
jsonApiConfig?: Partial<JsonApiConfig>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Additional wrapper component to wrap around the providers.
|
|
16
|
+
*/
|
|
17
|
+
wrapper?: React.ComponentType<{ children: React.ReactNode }>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Renders a component wrapped with all necessary providers for testing.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* import { renderWithProviders } from '@carlonicora/nextjs-jsonapi/testing';
|
|
26
|
+
*
|
|
27
|
+
* const { getByText } = renderWithProviders(<MyComponent />);
|
|
28
|
+
* expect(getByText('Hello')).toBeInTheDocument();
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example With custom config
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const { getByText } = renderWithProviders(<MyComponent />, {
|
|
34
|
+
* jsonApiConfig: { apiUrl: 'https://custom.api.com' },
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @example With additional wrapper
|
|
39
|
+
* ```tsx
|
|
40
|
+
* const CustomWrapper = ({ children }) => (
|
|
41
|
+
* <ThemeProvider>{children}</ThemeProvider>
|
|
42
|
+
* );
|
|
43
|
+
*
|
|
44
|
+
* const { getByText } = renderWithProviders(<MyComponent />, {
|
|
45
|
+
* wrapper: CustomWrapper,
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export function renderWithProviders(
|
|
50
|
+
ui: ReactElement,
|
|
51
|
+
options: RenderWithProvidersOptions = {}
|
|
52
|
+
): RenderResult {
|
|
53
|
+
const { jsonApiConfig, wrapper: AdditionalWrapper, ...renderOptions } = options;
|
|
54
|
+
|
|
55
|
+
function AllProviders({ children }: { children: React.ReactNode }) {
|
|
56
|
+
const content = (
|
|
57
|
+
<MockJsonApiProvider config={jsonApiConfig}>
|
|
58
|
+
{children}
|
|
59
|
+
</MockJsonApiProvider>
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if (AdditionalWrapper) {
|
|
63
|
+
return <AdditionalWrapper>{content}</AdditionalWrapper>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return content;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return render(ui, { wrapper: AllProviders, ...renderOptions });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Re-export render utilities from Testing Library for convenience.
|
|
74
|
+
*/
|
|
75
|
+
export { render, screen, waitFor, fireEvent, within } from "@testing-library/react";
|
|
76
|
+
export { userEvent } from "@testing-library/user-event";
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { formatDate, FormatOption } from "../date-formatter";
|
|
3
|
+
|
|
4
|
+
describe("formatDate", () => {
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
// Mock Date.now to have consistent test results
|
|
7
|
+
vi.useFakeTimers();
|
|
8
|
+
vi.setSystemTime(new Date("2024-06-15T12:00:00Z"));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
vi.useRealTimers();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("date format", () => {
|
|
16
|
+
it("should format date only", () => {
|
|
17
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
18
|
+
const result = formatDate(date, "date", "en-GB");
|
|
19
|
+
|
|
20
|
+
// en-GB format: DD/MM/YYYY
|
|
21
|
+
expect(result).toBe("15/06/2024");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should format date with US locale", () => {
|
|
25
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
26
|
+
const result = formatDate(date, "date", "en-US");
|
|
27
|
+
|
|
28
|
+
// en-US format: MM/DD/YYYY
|
|
29
|
+
expect(result).toBe("06/15/2024");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should use en-GB as default locale", () => {
|
|
33
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
34
|
+
const result = formatDate(date, "date");
|
|
35
|
+
|
|
36
|
+
expect(result).toBe("15/06/2024");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("time format", () => {
|
|
41
|
+
it("should format time only", () => {
|
|
42
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
43
|
+
const result = formatDate(date, "time", "en-GB");
|
|
44
|
+
|
|
45
|
+
// Should include hours and minutes
|
|
46
|
+
expect(result).toMatch(/\d{2}:\d{2}/);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("dateTime format", () => {
|
|
51
|
+
it("should format both date and time", () => {
|
|
52
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
53
|
+
const result = formatDate(date, "dateTime", "en-GB");
|
|
54
|
+
|
|
55
|
+
// Should contain date and time
|
|
56
|
+
expect(result).toContain("15/06/2024");
|
|
57
|
+
expect(result).toMatch(/\d{2}:\d{2}/);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("timeSince format", () => {
|
|
62
|
+
it("should format seconds ago", () => {
|
|
63
|
+
const date = new Date("2024-06-15T11:59:30Z"); // 30 seconds ago
|
|
64
|
+
const result = formatDate(date, "timeSince");
|
|
65
|
+
|
|
66
|
+
expect(result).toBe("30 seconds ago");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should format minutes ago", () => {
|
|
70
|
+
const date = new Date("2024-06-15T11:55:00Z"); // 5 minutes ago
|
|
71
|
+
const result = formatDate(date, "timeSince");
|
|
72
|
+
|
|
73
|
+
expect(result).toBe("5 minutes ago");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should format hours ago", () => {
|
|
77
|
+
const date = new Date("2024-06-15T09:00:00Z"); // 3 hours ago
|
|
78
|
+
const result = formatDate(date, "timeSince");
|
|
79
|
+
|
|
80
|
+
expect(result).toBe("3 hours ago");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should format days ago", () => {
|
|
84
|
+
const date = new Date("2024-06-13T12:00:00Z"); // 2 days ago
|
|
85
|
+
const result = formatDate(date, "timeSince");
|
|
86
|
+
|
|
87
|
+
expect(result).toBe("2 days ago");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("default format", () => {
|
|
92
|
+
it("should use timeSince for dates within 24 hours", () => {
|
|
93
|
+
const date = new Date("2024-06-15T10:00:00Z"); // 2 hours ago
|
|
94
|
+
const result = formatDate(date, "default");
|
|
95
|
+
|
|
96
|
+
expect(result).toBe("2 hours ago");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should use dateTime for dates older than 24 hours", () => {
|
|
100
|
+
const date = new Date("2024-06-13T10:00:00Z"); // 2 days + 2 hours ago
|
|
101
|
+
const result = formatDate(date, "default", "en-GB");
|
|
102
|
+
|
|
103
|
+
// Should contain date format for older dates
|
|
104
|
+
expect(result).toContain("13/06/2024");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("edge cases", () => {
|
|
109
|
+
it("should handle zero seconds ago", () => {
|
|
110
|
+
const date = new Date("2024-06-15T12:00:00Z"); // exactly now
|
|
111
|
+
const result = formatDate(date, "timeSince");
|
|
112
|
+
|
|
113
|
+
expect(result).toBe("0 seconds ago");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should handle 1 minute ago", () => {
|
|
117
|
+
const date = new Date("2024-06-15T11:59:00Z");
|
|
118
|
+
const result = formatDate(date, "timeSince");
|
|
119
|
+
|
|
120
|
+
expect(result).toBe("1 minutes ago");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should handle 1 hour ago", () => {
|
|
124
|
+
const date = new Date("2024-06-15T11:00:00Z");
|
|
125
|
+
const result = formatDate(date, "timeSince");
|
|
126
|
+
|
|
127
|
+
expect(result).toBe("1 hours ago");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("should handle 1 day ago", () => {
|
|
131
|
+
const date = new Date("2024-06-14T12:00:00Z");
|
|
132
|
+
const result = formatDate(date, "timeSince");
|
|
133
|
+
|
|
134
|
+
expect(result).toBe("1 days ago");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should throw for invalid format option", () => {
|
|
138
|
+
const date = new Date("2024-06-15T12:00:00Z");
|
|
139
|
+
// @ts-expect-error Testing invalid input
|
|
140
|
+
expect(() => formatDate(date, "invalid")).toThrow("Invalid format option");
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("different locales", () => {
|
|
145
|
+
it("should format with German locale", () => {
|
|
146
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
147
|
+
const result = formatDate(date, "date", "de-DE");
|
|
148
|
+
|
|
149
|
+
// German format: DD.MM.YYYY
|
|
150
|
+
expect(result).toBe("15.06.2024");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should format with Italian locale", () => {
|
|
154
|
+
const date = new Date("2024-06-15T10:30:00Z");
|
|
155
|
+
const result = formatDate(date, "date", "it-IT");
|
|
156
|
+
|
|
157
|
+
// Italian format: DD/MM/YYYY
|
|
158
|
+
expect(result).toBe("15/06/2024");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { exists } from "../exists";
|
|
3
|
+
|
|
4
|
+
describe("exists", () => {
|
|
5
|
+
describe("with null and undefined", () => {
|
|
6
|
+
it("should return false for null", () => {
|
|
7
|
+
expect(exists(null)).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should return false for undefined", () => {
|
|
11
|
+
expect(exists(undefined)).toBe(false);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("with arrays", () => {
|
|
16
|
+
it("should return true for non-empty array", () => {
|
|
17
|
+
expect(exists([1, 2, 3])).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should return false for empty array", () => {
|
|
21
|
+
expect(exists([])).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should return true for array with one element", () => {
|
|
25
|
+
expect(exists([1])).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should return true for array of objects", () => {
|
|
29
|
+
expect(exists([{ id: 1 }, { id: 2 }])).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return true for array with null elements", () => {
|
|
33
|
+
// Array exists even if it contains null values
|
|
34
|
+
expect(exists([null, null])).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return true for array with undefined elements", () => {
|
|
38
|
+
// Array exists even if it contains undefined values
|
|
39
|
+
expect(exists([undefined, undefined])).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("with single values", () => {
|
|
44
|
+
it("should return true for non-zero number", () => {
|
|
45
|
+
expect(exists(42)).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should return false for zero", () => {
|
|
49
|
+
// Note: 0 is falsy so exists returns false
|
|
50
|
+
expect(exists(0)).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should return true for non-empty string", () => {
|
|
54
|
+
expect(exists("hello")).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should return false for empty string", () => {
|
|
58
|
+
expect(exists("")).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should return true for object", () => {
|
|
62
|
+
expect(exists({ id: 1 })).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should return true for empty object", () => {
|
|
66
|
+
expect(exists({})).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should return true for true boolean", () => {
|
|
70
|
+
expect(exists(true)).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should return false for false boolean", () => {
|
|
74
|
+
expect(exists(false)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("type inference", () => {
|
|
79
|
+
it("should work with string type", () => {
|
|
80
|
+
const value: string | null = "test";
|
|
81
|
+
expect(exists(value)).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should work with number type", () => {
|
|
85
|
+
const value: number | undefined = 10;
|
|
86
|
+
expect(exists(value)).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should work with object type", () => {
|
|
90
|
+
type User = { name: string };
|
|
91
|
+
const value: User | null = { name: "John" };
|
|
92
|
+
expect(exists(value)).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should work with array type", () => {
|
|
96
|
+
const value: string[] | null = ["a", "b"];
|
|
97
|
+
expect(exists(value)).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { cn } from "./cn";
|
|
3
|
+
|
|
4
|
+
describe("cn utility", () => {
|
|
5
|
+
it("should merge class names", () => {
|
|
6
|
+
const result = cn("foo", "bar");
|
|
7
|
+
expect(result).toBe("foo bar");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("should handle conditional classes", () => {
|
|
11
|
+
const result = cn("base", true && "included", false && "excluded");
|
|
12
|
+
expect(result).toBe("base included");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("should merge tailwind classes correctly", () => {
|
|
16
|
+
const result = cn("px-2 py-1", "px-4");
|
|
17
|
+
expect(result).toBe("py-1 px-4");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should handle arrays of classes", () => {
|
|
21
|
+
const result = cn(["foo", "bar"], "baz");
|
|
22
|
+
expect(result).toBe("foo bar baz");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should handle undefined and null values", () => {
|
|
26
|
+
const result = cn("foo", undefined, null, "bar");
|
|
27
|
+
expect(result).toBe("foo bar");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should handle object syntax", () => {
|
|
31
|
+
const result = cn({ foo: true, bar: false, baz: true });
|
|
32
|
+
expect(result).toBe("foo baz");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("should handle empty input", () => {
|
|
36
|
+
const result = cn();
|
|
37
|
+
expect(result).toBe("");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should deduplicate conflicting tailwind utilities", () => {
|
|
41
|
+
const result = cn("text-red-500", "text-blue-500");
|
|
42
|
+
expect(result).toBe("text-blue-500");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/nextjs-jsonapi/nextjs-jsonapi/dist/BlockNoteEditor-MBFDWP7X.js","../src/components/editors/BlockNoteEditor.tsx","../src/components/editors/BlockNoteEditorFormattingToolbar.tsx"],"names":["jsxs","jsx"],"mappings":"AAAA,ylBAAY;AACZ;AACE;AACA;AACA;AACF,sDAA4B;AAC5B,+BAA4B;AAC5B;AACE;AACA;AACA;AACA;AACF,sDAA4B;AAC5B,+BAA4B;AAC5B,+BAA4B;AAC5B,+BAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACA;AClBA,uCAAyE;AACzE,yCAAiE;AACjE,2CAA8B;AAC9B,uCAAO;AACP,2CAAiC;AACjC,qCAAgC;AAChC,+BAAyE;ADoBzE;AACA;AE3BA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAAA;AAOM,+CAAA;AAJD,SAAS,gCAAA,CAAA,EAAmC;AACjD,EAAA,uBACE,6BAAA;AAAA,IAAC,kCAAA;AAAA,IAAA;AAAA,MACC,iBAAA,EAAmB,CAAA,EAAA,mBACjB,8BAAA,wBAAC,EAAA,EACC,QAAA,EAAA;AAAA,wBAAA,6BAAA,sBAAC,EAAA,CAAA,CAAA,EAAqB,iBAAmB,CAAA;AAAA,wBAEzC,6BAAA,wBAAC,EAAA,CAAA,CAAA,EAAuB,mBAAqB,CAAA;AAAA,wBAC7C,6BAAA,wBAAC,EAAA,CAAA,CAAA,EAAuB,mBAAqB,CAAA;AAAA,wBAE7C,6BAAA,2BAAC,EAAA,EAAqB,cAAA,EAAgB,OAAA,CAAA,EAAa,iBAAmB,CAAA;AAAA,wBACtE,6BAAA,2BAAC,EAAA,EAAqB,cAAA,EAAgB,SAAA,CAAA,EAAe,mBAAqB,CAAA;AAAA,wBAC1E,6BAAA,2BAAC,EAAA,EAAqB,cAAA,EAAgB,YAAA,CAAA,EAAkB,sBAAwB,CAAA;AAAA,wBAChF,6BAAA,2BAAC,EAAA,EAAqB,cAAA,EAAgB,SAAA,CAAA,EAAe,mBAAqB,CAAA;AAAA,wBAE1E,6BAAA,sBAAC,EAA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;AACrD,IAAA;AAEiE,IAAA;AACR,EAAA;AAGH,EAAA;AACT,IAAA;AADxB,EAAA;AAKsC,EAAA;AAC7C,EAAA;AACoC,IAAA;AACa,MAAA;AACf,MAAA;AACA,QAAA;AAC9C,MAAA;AAJoB,IAAA;AAOwC,IAAA;AACvB,MAAA;AACmB,MAAA;AAC1D,IAAA;AAC0B,EAAA;AAI6B,EAAA;AACzC,EAAA;AACoB,IAAA;AACU,IAAA;AACC,IAAA;AACK,IAAA;AACxB,IAAA;AACS,MAAA;AACjC,MAAA;AACF,IAAA;AACwE,IAAA;AACvC,IAAA;AACN,EAAA;AAGD,EAAA;AACL,IAAA;AACf,MAAA;AAEqD,QAAA;AACP,QAAA;AAC1B,UAAA;AACtB,QAAA;AAGkC,QAAA;AACpB,MAAA;AAC0C,QAAA;AAEpD,QAAA;AACoB,UAAA;AACC,UAAA;AACqB,YAAA;AACM,YAAA;AACd,YAAA;AACpC,UAAA;AACsB,QAAA;AAC2B,UAAA;AACnD,QAAA;AACF,MAAA;AACF,IAAA;AACO,IAAA;AACT,EAAA;AAGqC,EAAA;AAChC,IAAA;AAAA,IAAA;AACC,MAAA;AACU,MAAA;AACa,MAAA;AACJ,MAAA;AACb,MAAA;AACuD,MAAA;AAE5D,MAAA;AAAiC,IAAA;AAEtC,EAAA;AAEJ;AAtUwB;AD4S6C;AACA;AACA","file":"/home/runner/work/nextjs-jsonapi/nextjs-jsonapi/dist/BlockNoteEditor-MBFDWP7X.js","sourcesContent":[null,"\"use client\";\n\nimport { BlockNoteSchema, defaultInlineContentSpecs, PartialBlock } from \"@blocknote/core\";\nimport { createReactInlineContentSpec, useCreateBlockNote } from \"@blocknote/react\";\nimport { BlockNoteView } from \"@blocknote/shadcn\";\nimport \"@blocknote/shadcn/style.css\";\nimport { CheckIcon, XIcon } from \"lucide-react\";\nimport { useTranslations } from \"next-intl\";\nimport React, { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport { useCurrentUserContext } from \"../../contexts\";\nimport { S3Interface } from \"../../features/s3/data\";\nimport { UserInterface } from \"../../features/user/data\";\nimport { S3Service } from \"../../features/s3/data/s3.service\";\nimport { Button } from \"../../shadcnui\";\nimport { BlockNoteDiffUtil, BlockNoteWordDiffRendererUtil, cn } from \"../../utils\";\nimport { errorToast } from \"../errors\";\nimport { BlockNoteEditorFormattingToolbar } from \"./BlockNoteEditorFormattingToolbar\";\n\nexport type BlockNoteEditorProps = {\n id: string;\n type: string;\n initialContent?: PartialBlock[];\n onChange?: (content: any, isEmpty: boolean, hasUnresolvedDiff: boolean) => void;\n size?: \"sm\" | \"md\";\n className?: string;\n markdownContent?: string;\n diffContent?: PartialBlock[];\n placeholder?: string;\n bordered?: boolean;\n};\n\nconst createDiffActionsInlineContentSpec = (\n handleAcceptChange: (diffId: string) => void,\n handleRejectChange: (diffId: string) => void,\n) => {\n return createReactInlineContentSpec(\n {\n type: \"diffActions\",\n propSchema: {\n diffIds: {\n default: \"\",\n },\n },\n content: \"none\",\n },\n {\n render: (props) => {\n const diffIds = props.inlineContent.props.diffIds;\n\n return (\n <span className=\"diff-actions-container mx-2 inline-flex items-center gap-1 align-middle\">\n <Button\n title=\"Accept change\"\n onClick={(e) => {\n e.preventDefault();\n e.stopPropagation();\n diffIds.split(\",\").forEach((id: string) => handleAcceptChange(id.trim()));\n }}\n >\n <CheckIcon className=\"h-3 w-3 text-green-600\" />\n </Button>\n <Button\n title=\"Reject change\"\n className=\"mx-2 p-0\"\n onClick={(e) => {\n e.preventDefault();\n e.stopPropagation();\n diffIds.split(\",\").forEach((id: string) => handleRejectChange(id.trim()));\n }}\n >\n <XIcon className=\"h-3 w-3 text-red-600\" />\n </Button>\n </span>\n );\n },\n },\n );\n};\n\nexport default function BlockNoteEditor({\n id,\n type,\n initialContent,\n onChange,\n size,\n className,\n markdownContent,\n diffContent,\n placeholder,\n bordered,\n}: BlockNoteEditorProps): React.JSX.Element {\n const t = useTranslations();\n const { company } = useCurrentUserContext<UserInterface>();\n\n const [acceptedChanges, setAcceptedChanges] = useState<Set<string>>(new Set());\n const [rejectedChanges, setRejectedChanges] = useState<Set<string>>(new Set());\n\n const editorRef = useRef<HTMLDivElement>(null);\n\n const handleAcceptChange = useCallback((diffId: string) => {\n setAcceptedChanges((prev) => new Set([...prev, diffId]));\n setRejectedChanges((prev) => {\n const newSet = new Set(prev);\n newSet.delete(diffId);\n return newSet;\n });\n }, []);\n\n const handleRejectChange = useCallback((diffId: string) => {\n setRejectedChanges((prev) => new Set([...prev, diffId]));\n setAcceptedChanges((prev) => {\n const newSet = new Set(prev);\n newSet.delete(diffId);\n return newSet;\n });\n }, []);\n\n const DiffActionsInlineContent = useMemo(\n () => createDiffActionsInlineContentSpec(handleAcceptChange, handleRejectChange),\n [handleAcceptChange, handleRejectChange],\n );\n\n const schema = useMemo(\n () =>\n BlockNoteSchema.create({\n inlineContentSpecs: {\n ...defaultInlineContentSpecs,\n diffActions: DiffActionsInlineContent,\n },\n } as any),\n [DiffActionsInlineContent],\n );\n\n const uploadImage = useCallback(\n async (file: File): Promise<string> => {\n if (!company) {\n errorToast({\n title: t(`generic.errors.upload`),\n error: t(`generic.errors.upload_description`),\n });\n throw new Error(t(`generic.errors.upload`));\n }\n\n const fileType = file.type;\n const key = `companies/${company.id}/${type}/${id}/${file.name}`;\n\n const s3: S3Interface = await S3Service.getPreSignedUrl({\n key: key,\n contentType: fileType,\n isPublic: true,\n });\n\n await fetch(s3.url, {\n method: \"PUT\",\n headers: s3.headers,\n body: file,\n });\n\n const signedImage: S3Interface = await S3Service.getSignedUrl({\n key: key,\n isPublic: true,\n });\n\n return signedImage.url;\n },\n [company, id, t],\n );\n\n // Utility: Remove trailing empty blocks for read-only display\n const removeTrailingEmptyBlocks = useCallback(\n (blocks: PartialBlock[]): PartialBlock[] => {\n if (!blocks || blocks.length === 0) return blocks;\n\n // Only remove trailing empty blocks in read-only mode\n if (onChange !== undefined) return blocks;\n\n const result = [...blocks];\n\n // Remove trailing empty paragraph blocks, but keep at least one block\n while (result.length > 1) {\n const lastBlock = result[result.length - 1];\n\n // Check if it's an empty paragraph\n const isEmptyParagraph =\n lastBlock.type === \"paragraph\" &&\n (!lastBlock.content ||\n lastBlock.content.length === 0 ||\n (Array.isArray(lastBlock.content) && lastBlock.content.every((c: any) => !c.text || c.text.trim() === \"\")));\n\n if (isEmptyParagraph) {\n result.pop();\n } else {\n break;\n }\n }\n\n return result;\n },\n [onChange],\n );\n\n const processedContent = useMemo(() => {\n if (diffContent && initialContent) {\n try {\n const diffResult = BlockNoteDiffUtil.diff(initialContent, diffContent);\n const renderedDiff = BlockNoteWordDiffRendererUtil.renderWordDiffs(\n diffResult.blocks,\n handleAcceptChange,\n handleRejectChange,\n acceptedChanges,\n rejectedChanges,\n );\n return removeTrailingEmptyBlocks(renderedDiff);\n } catch (error) {\n return initialContent && Array.isArray(initialContent) && initialContent.length > 0\n ? removeTrailingEmptyBlocks(initialContent)\n : [];\n }\n }\n\n if (!initialContent) {\n return [];\n }\n\n if (!Array.isArray(initialContent)) {\n return [];\n }\n\n return initialContent.length > 0 ? removeTrailingEmptyBlocks(initialContent) : [];\n }, [\n initialContent,\n diffContent,\n handleAcceptChange,\n handleRejectChange,\n acceptedChanges,\n rejectedChanges,\n removeTrailingEmptyBlocks,\n ]);\n\n const validatedInitialContent = useMemo(() => {\n if (processedContent && Array.isArray(processedContent) && processedContent.length > 0) {\n const validatedContent = processedContent.filter((block) => {\n if (!block || typeof block !== \"object\") return false;\n if (!(block as any).type) return false;\n return true;\n });\n return validatedContent.length > 0 ? (validatedContent as PartialBlock[]) : undefined;\n }\n return undefined;\n }, [processedContent]);\n\n const editor = useCreateBlockNote(\n useMemo(\n () => ({\n placeholders: {\n emptyDocument: placeholder || t(`generic.blocknote.placeholder`),\n },\n schema,\n initialContent: validatedInitialContent,\n uploadFile: uploadImage,\n }),\n [placeholder, t, schema, validatedInitialContent, uploadImage],\n ),\n );\n\n const handleChange = useCallback(async () => {\n if (!onChange) return;\n const newBlocks = editor.document;\n\n const markdownFromBlocks = (await editor.blocksToMarkdownLossy(editor.document)).trim();\n\n function hasUnresolvedDiffsRecursive(block: any): boolean {\n if (!block || typeof block !== \"object\") return false;\n let diffId = undefined;\n if (block.props && block.props.diffId) diffId = block.props.diffId;\n if (!diffId && block.attrs && block.attrs.diffId) diffId = block.attrs.diffId;\n if (diffId && !acceptedChanges.has(diffId) && !rejectedChanges.has(diffId)) {\n return true;\n }\n\n if (block.content) {\n const contentArr = Array.isArray(block.content) ? block.content : [block.content];\n for (const inline of contentArr) {\n if (inline && typeof inline === \"object\") {\n if (inline.type === \"diffActions\" && inline.props && inline.props.diffIds) {\n const ids =\n typeof inline.props.diffIds === \"string\" ? inline.props.diffIds.split(\",\") : inline.props.diffIds;\n for (const id of ids) {\n const trimmed = (id || \"\").toString().trim();\n if (trimmed && !acceptedChanges.has(trimmed) && !rejectedChanges.has(trimmed)) {\n return true;\n }\n }\n }\n if (inline.props && inline.props.diffId) {\n const diffIdInline = inline.props.diffId;\n if (diffIdInline && !acceptedChanges.has(diffIdInline) && !rejectedChanges.has(diffIdInline)) {\n return true;\n }\n }\n if (inline.children && Array.isArray(inline.children)) {\n for (const child of inline.children) {\n if (hasUnresolvedDiffsRecursive(child)) return true;\n }\n }\n }\n }\n }\n if (Array.isArray(block.children)) {\n for (const child of block.children) {\n if (hasUnresolvedDiffsRecursive(child)) return true;\n }\n }\n return false;\n }\n\n let hasUnresolvedDiff = false;\n if (Array.isArray(newBlocks)) {\n hasUnresolvedDiff = newBlocks.some((block: any) => hasUnresolvedDiffsRecursive(block));\n }\n\n onChange(newBlocks, !markdownFromBlocks.length, hasUnresolvedDiff);\n }, [editor, onChange, id, acceptedChanges, rejectedChanges]);\n\n // Utility: deep equality for arrays of blocks\n const areBlocksEqual = (a: any[], b: any[]): boolean => {\n return JSON.stringify(a) === JSON.stringify(b);\n };\n\n // Only initialize from markdownContent once per value, and only if different\n const hasInitializedFromMarkdown = useRef<string | null>(null);\n useEffect(() => {\n const updateContent = async (markdown: string) => {\n const blocks = await editor.tryParseMarkdownToBlocks(markdown);\n if (!areBlocksEqual(blocks, editor.document)) {\n editor.replaceBlocks(editor.document, blocks);\n }\n };\n\n if (markdownContent && hasInitializedFromMarkdown.current !== markdownContent) {\n hasInitializedFromMarkdown.current = markdownContent;\n updateContent(markdownContent).then(() => handleChange());\n }\n }, [markdownContent, editor]);\n\n // Update editor content when diff content changes, but only if different\n // Prevent unnecessary replaceBlocks calls that reset scroll/cursor.\n const previousContentHashRef = useRef<string | null>(null);\n useEffect(() => {\n if (!processedContent || !editor) return;\n const hash = JSON.stringify(processedContent);\n if (previousContentHashRef.current === hash) return; // no changes\n const currentHash = JSON.stringify(editor.document);\n if (currentHash === hash) {\n previousContentHashRef.current = hash;\n return; // already in sync\n }\n editor.replaceBlocks(editor.document, processedContent as PartialBlock[]);\n previousContentHashRef.current = hash;\n }, [processedContent, editor]);\n\n // Handle audio received from whisper transcription\n const handleAudioReceived = useCallback(\n (message: string) => {\n try {\n // Ensure the editor has focus\n const editorElement = editorRef.current?.querySelector('[contenteditable=\"true\"]') as HTMLElement;\n if (editorElement && document.activeElement !== editorElement) {\n editorElement.focus();\n }\n\n // Insert the transcribed text at the current cursor position\n editor.insertInlineContent(message);\n } catch (error) {\n console.error(\"Error inserting transcribed text:\", error);\n // Fallback: try to insert at the end of the document\n try {\n const blocks = editor.document;\n if (blocks.length > 0) {\n const lastBlock = blocks[blocks.length - 1];\n editor.setTextCursorPosition(lastBlock.id, \"end\");\n editor.insertInlineContent(message);\n }\n } catch (fallbackError) {\n console.error(\"Fallback insertion also failed:\", fallbackError);\n }\n }\n },\n [editor],\n );\n\n return (\n <div ref={editorRef} className={cn(bordered ? \"rounded-md border\" : \"\", \"w-full\")}>\n <BlockNoteView\n editor={editor}\n onChange={handleChange}\n editable={onChange !== undefined}\n formattingToolbar={false}\n theme=\"light\"\n className={cn(`BlockNoteView ${onChange ? \"min-h-96 p-4\" : \"\"}`, className, size === \"sm\" && \"small\")}\n >\n <BlockNoteEditorFormattingToolbar />\n </BlockNoteView>\n </div>\n );\n}\n","\"use client\";\n\nimport {\n BasicTextStyleButton,\n BlockTypeSelect,\n CreateLinkButton,\n FileCaptionButton,\n FileReplaceButton,\n FormattingToolbar,\n FormattingToolbarController,\n TextAlignButton,\n} from \"@blocknote/react\";\n\nexport function BlockNoteEditorFormattingToolbar() {\n return (\n <FormattingToolbarController\n formattingToolbar={() => (\n <FormattingToolbar>\n <BlockTypeSelect key={\"blockTypeSelect\"} />\n\n <FileCaptionButton key={\"fileCaptionButton\"} />\n <FileReplaceButton key={\"replaceFileButton\"} />\n\n <BasicTextStyleButton basicTextStyle={\"bold\"} key={\"boldStyleButton\"} />\n <BasicTextStyleButton basicTextStyle={\"italic\"} key={\"italicStyleButton\"} />\n <BasicTextStyleButton basicTextStyle={\"underline\"} key={\"underlineStyleButton\"} />\n <BasicTextStyleButton basicTextStyle={\"strike\"} key={\"strikeStyleButton\"} />\n\n <TextAlignButton textAlignment={\"left\"} key={\"textAlignLeftButton\"} />\n <TextAlignButton textAlignment={\"center\"} key={\"textAlignCenterButton\"} />\n <TextAlignButton textAlignment={\"right\"} key={\"textAlignRightButton\"} />\n\n <CreateLinkButton key={\"createLinkButton\"} />\n </FormattingToolbar>\n )}\n />\n );\n}\n"]}
|