slice-machine-ui 2.20.5-alpha.jp-import-slices-2.1 → 2.20.5-alpha.lg-import-slices.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.
Files changed (53) hide show
  1. package/out/404.html +1 -1
  2. package/out/_next/static/GzPwmcuyHg0TUP7cX7YSA/_buildManifest.js +1 -0
  3. package/out/_next/static/chunks/{248-2902bda47821f37b.js → 248-84a5987f0499b074.js} +1 -1
  4. package/out/_next/static/chunks/422-c7a16d95b75c9e1c.js +1 -0
  5. package/out/_next/static/chunks/489-c9535ef34da63d1a.js +1 -0
  6. package/out/_next/static/chunks/585-c89bb2471e85b9f8.js +1 -0
  7. package/out/_next/static/chunks/{630-4fe1e982234bd925.js → 630-2bf927bca082a191.js} +1 -1
  8. package/out/_next/static/chunks/{647-a29bee23cb9ac7a2.js → 647-aa094286bc248d52.js} +1 -1
  9. package/out/_next/static/chunks/{882-8ab59c0c72307c87.js → 882-48d61b2fabee28d8.js} +1 -1
  10. package/out/_next/static/chunks/954-bedaaabf664584a0.js +1 -0
  11. package/out/_next/static/chunks/a6495ab1.00190ac98e794d8f.js +28 -0
  12. package/out/_next/static/chunks/pages/{_app-a60aecdbaa4efedd.js → _app-0668d41463a0a907.js} +217 -217
  13. package/out/_next/static/chunks/pages/{changelog-bc83e25e8d316ca9.js → changelog-8514e0696e90a1b2.js} +1 -1
  14. package/out/_next/static/chunks/pages/{changes-6f371ebe52dbe76a.js → changes-e66094f57453cf9c.js} +1 -1
  15. package/out/_next/static/chunks/pages/custom-types/{[customTypeId]-80defa14b6ea36f7.js → [customTypeId]-273e9a82c085b596.js} +1 -1
  16. package/out/_next/static/chunks/pages/{labs-16d118a80936d583.js → labs-56fd818a63553497.js} +1 -1
  17. package/out/_next/static/chunks/pages/page-types/{[pageTypeId]-f4ff9ad74b44bf09.js → [pageTypeId]-3fa7667de1a790d9.js} +1 -1
  18. package/out/_next/static/chunks/pages/slices/[lib]/[sliceName]/[variation]/{simulator-873c0c89aaf50b3c.js → simulator-8c70298caf51bed0.js} +1 -1
  19. package/out/_next/static/chunks/pages/slices/[lib]/[sliceName]/{[variation]-0e9aa745ed9fe1ec.js → [variation]-22ffa2561ac66557.js} +1 -1
  20. package/out/_next/static/chunks/pages/slices-76679cf064761d2b.js +1 -0
  21. package/out/_next/static/chunks/{webpack-e986dd216b883a74.js → webpack-24ddb02bea621501.js} +1 -1
  22. package/out/_next/static/css/bc89f2cd4e4781f5.css +1 -0
  23. package/out/changelog.html +1 -1
  24. package/out/changes.html +1 -1
  25. package/out/custom-types/[customTypeId].html +1 -1
  26. package/out/custom-types.html +1 -1
  27. package/out/index.html +1 -1
  28. package/out/labs.html +1 -1
  29. package/out/page-types/[pageTypeId].html +1 -1
  30. package/out/slices/[lib]/[sliceName]/[variation]/simulator.html +1 -1
  31. package/out/slices/[lib]/[sliceName]/[variation].html +1 -1
  32. package/out/slices.html +1 -1
  33. package/package.json +6 -6
  34. package/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/ImportSlicesFromLibraryModal.tsx +285 -104
  35. package/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/hooks/useImportSlicesFromGithub.ts +4 -7
  36. package/src/features/customTypes/customTypesBuilder/SliceZoneBlankSlate.tsx +18 -4
  37. package/src/features/customTypes/customTypesBuilder/shared/getSubmitButtonLabel.ts +5 -3
  38. package/src/features/customTypes/customTypesBuilder/sliceCreationOptions.tsx +14 -0
  39. package/src/legacy/lib/builders/CustomTypeBuilder/SliceZone/UpdateSliceZoneModal.tsx +72 -0
  40. package/src/legacy/lib/builders/CustomTypeBuilder/SliceZone/UpdateSliceZoneModalList.tsx +3 -1
  41. package/src/legacy/lib/builders/CustomTypeBuilder/SliceZone/index.tsx +79 -31
  42. package/src/pages/slices.tsx +30 -0
  43. package/out/_next/static/60BSwFmsh1-aNIyMe29hH/_buildManifest.js +0 -1
  44. package/out/_next/static/chunks/21-f3962d02eda0a46e.js +0 -1
  45. package/out/_next/static/chunks/633-2d20c3e815c79c7f.js +0 -1
  46. package/out/_next/static/chunks/660-a3ec593dcfbb44c9.js +0 -1
  47. package/out/_next/static/chunks/6611d287.1714222015f895ea.js +0 -28
  48. package/out/_next/static/chunks/pages/slices-93fc784c443a996c.js +0 -1
  49. package/out/_next/static/css/b30fbbc3ed00a725.css +0 -1
  50. package/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/LibrarySlicesTab.tsx +0 -125
  51. package/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/LocalSlicesTab.tsx +0 -67
  52. package/src/features/customTypes/customTypesBuilder/ImportSlicesFromLibraryModal/ReuseExistingSlicesContext.tsx +0 -279
  53. /package/out/_next/static/{60BSwFmsh1-aNIyMe29hH → GzPwmcuyHg0TUP7cX7YSA}/_ssgManifest.js +0 -0
@@ -1,279 +0,0 @@
1
- import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes";
2
- import {
3
- createContext,
4
- type ReactNode,
5
- useCallback,
6
- useContext,
7
- useRef,
8
- useState,
9
- } from "react";
10
- import { toast } from "react-toastify";
11
-
12
- import { getState, telemetry } from "@/apiClient";
13
- import { useOnboarding } from "@/features/onboarding/useOnboarding";
14
- import { useAutoSync } from "@/features/sync/AutoSyncProvider";
15
- import { ComponentUI } from "@/legacy/lib/models/common/ComponentUI";
16
- import useSliceMachineActions from "@/modules/useSliceMachineActions";
17
-
18
- import type { SliceImport } from "./types";
19
- import { NewSlice } from "./types";
20
- import { addSlices } from "./utils/addSlices";
21
- import { sliceWithoutConflicts } from "./utils/sliceWithoutConflicts";
22
-
23
- interface ReuseExistingSlicesContextValue {
24
- selectedLocalSlices: ComponentUI[];
25
- selectedLibrarySlices: SliceImport[];
26
- toggleLocalSlice: (slice: ComponentUI) => void;
27
- toggleLibrarySlice: (slice: SliceImport) => void;
28
- setAllLibrarySlices: (slices: SliceImport[]) => void;
29
- reset: () => void;
30
- submit: (args: {
31
- existingSlices: React.MutableRefObject<SharedSlice[]>;
32
- location: "custom_type" | "page_type" | "slices";
33
- resetSlices: () => void;
34
- }) => Promise<{ slices: SharedSlice[]; library?: string }>;
35
- isSubmitting: boolean;
36
- totalSelected: number;
37
- }
38
-
39
- const ReuseExistingSlicesContext = createContext<
40
- ReuseExistingSlicesContextValue | undefined
41
- >(undefined);
42
-
43
- interface ReuseExistingSlicesProviderProps {
44
- children: ReactNode;
45
- }
46
-
47
- export function ReuseExistingSlicesProvider({
48
- children,
49
- }: ReuseExistingSlicesProviderProps) {
50
- const [localSlices, setSelectedLocalSlices] = useState<ComponentUI[]>([]);
51
- const [librarySlices, setSelectedLibrarySlices] = useState<SliceImport[]>([]);
52
- const [isSubmitting, setIsSubmitting] = useState(false);
53
-
54
- const { syncChanges } = useAutoSync();
55
- const { createSliceSuccess, updateSliceMockSuccess } =
56
- useSliceMachineActions();
57
- const { completeStep } = useOnboarding();
58
-
59
- /**
60
- * Keeps track of the current instance id.
61
- * When the modal is closed, the id is reset.
62
- */
63
- const id = useRef(crypto.randomUUID());
64
-
65
- const toggleLocalSlice = useCallback((slice: ComponentUI) => {
66
- setSelectedLocalSlices((prev) => {
67
- const isSelected = prev.some((s) => s.model.id === slice.model.id);
68
- if (isSelected) {
69
- return prev.filter((s) => s.model.id !== slice.model.id);
70
- }
71
- return [...prev, slice];
72
- });
73
- }, []);
74
-
75
- const toggleLibrarySlice = useCallback((slice: SliceImport) => {
76
- setSelectedLibrarySlices((prev) => {
77
- const isSelected = prev.some((s) => s.model.id === slice.model.id);
78
- if (isSelected) {
79
- return prev.filter((s) => s.model.id !== slice.model.id);
80
- }
81
- return [...prev, slice];
82
- });
83
- }, []);
84
-
85
- const setAllLibrarySlices = useCallback((slices: SliceImport[]) => {
86
- setSelectedLibrarySlices(slices);
87
- }, []);
88
-
89
- const reset = useCallback(() => {
90
- setSelectedLocalSlices([]);
91
- setSelectedLibrarySlices([]);
92
- id.current = crypto.randomUUID();
93
- }, []);
94
-
95
- const submit = useCallback(
96
- async (args: {
97
- existingSlices: React.MutableRefObject<SharedSlice[]>;
98
- location: "custom_type" | "page_type" | "slices";
99
- resetSlices: () => void;
100
- }): Promise<{ slices: SharedSlice[]; library?: string }> => {
101
- const { existingSlices, location, resetSlices } = args;
102
- const totalSelected = localSlices.length + librarySlices.length;
103
-
104
- if (totalSelected === 0) {
105
- toast.error("Please select at least one slice");
106
- throw new Error("No slices selected");
107
- }
108
-
109
- // Prepare local slices (just extract models)
110
- const localSliceModels = localSlices.map((slice) => slice.model);
111
-
112
- // Prepare library slices for import
113
- const librarySlicesToImport: NewSlice[] = librarySlices.map((slice) => ({
114
- image: slice.image,
115
- model: slice.model,
116
- files: slice.files,
117
- componentContents: slice.componentContents,
118
- mocks: slice.mocks,
119
- screenshots: slice.screenshots,
120
- }));
121
-
122
- // If there are library slices, create them first
123
- if (librarySlicesToImport.length > 0) {
124
- // Ensure ids and names are conflict-free against existing and newly-added slices
125
- const conflictFreeSlices: NewSlice[] = [];
126
- const allExistingSlices: SharedSlice[] = [
127
- ...existingSlices.current,
128
- ...localSliceModels,
129
- ] as SharedSlice[];
130
-
131
- for (const s of librarySlicesToImport) {
132
- const adjustedModel = sliceWithoutConflicts({
133
- existingSlices: allExistingSlices,
134
- newSlices: conflictFreeSlices,
135
- slice: s.model,
136
- });
137
- conflictFreeSlices.push({ ...s, model: adjustedModel });
138
- }
139
-
140
- const currentId = id.current;
141
- setIsSubmitting(true);
142
-
143
- try {
144
- const { slices: createdSlices, library } =
145
- await addSlices(conflictFreeSlices);
146
-
147
- if (currentId !== id.current) {
148
- throw new Error("Modal instance changed");
149
- }
150
-
151
- // Wait a moment to ensure all file writes are complete
152
- await new Promise((resolve) => setTimeout(resolve, 100));
153
-
154
- const serverState = await getState();
155
-
156
- // Ensure mocks are included in the libraries data before updating store
157
- const librariesWithMocks = serverState.libraries.map((lib) => {
158
- if (lib.name !== library) return lib;
159
-
160
- return {
161
- ...lib,
162
- components: lib.components.map((component) => {
163
- // Find the corresponding slice from librarySlicesToImport to get its mocks
164
- const importedSlice = conflictFreeSlices.find(
165
- (s) => s.model.id === component.model.id,
166
- );
167
-
168
- // If mocks are already in component, use them; otherwise use imported mocks
169
- const mocks =
170
- component.mocks && component.mocks.length > 0
171
- ? component.mocks
172
- : importedSlice?.mocks;
173
-
174
- return {
175
- ...component,
176
- mocks: mocks ?? component.mocks,
177
- };
178
- }),
179
- };
180
- });
181
-
182
- createSliceSuccess(librariesWithMocks);
183
-
184
- // Also update mocks individually to ensure they're in the store
185
- for (const slice of librarySlicesToImport) {
186
- if (
187
- slice.mocks &&
188
- Array.isArray(slice.mocks) &&
189
- slice.mocks.length > 0
190
- ) {
191
- updateSliceMockSuccess({
192
- libraryID: library,
193
- sliceID: slice.model.id,
194
- mocks: slice.mocks,
195
- });
196
- }
197
- }
198
-
199
- syncChanges();
200
-
201
- // Combine local slices with created library slices
202
- const allSlices: SharedSlice[] = [
203
- ...localSliceModels,
204
- ...createdSlices.map((s) => s.model),
205
- ] as SharedSlice[];
206
-
207
- setIsSubmitting(false);
208
- reset();
209
- resetSlices();
210
-
211
- void completeStep("createSlice");
212
-
213
- for (const { model } of createdSlices) {
214
- void telemetry.track({
215
- event: "slice:created",
216
- id: model.id,
217
- name: model.name,
218
- library,
219
- location,
220
- mode: "import",
221
- });
222
- }
223
-
224
- return { slices: allSlices, library };
225
- } catch (error) {
226
- if (currentId !== id.current) {
227
- throw error;
228
- }
229
- setIsSubmitting(false);
230
- toast.error("An unexpected error happened while adding slices.");
231
- throw error;
232
- }
233
- } else {
234
- // Only local slices selected, no need to create anything
235
- reset();
236
- return { slices: localSliceModels as SharedSlice[] };
237
- }
238
- },
239
- [
240
- localSlices,
241
- librarySlices,
242
- syncChanges,
243
- createSliceSuccess,
244
- updateSliceMockSuccess,
245
- completeStep,
246
- reset,
247
- ],
248
- );
249
-
250
- const totalSelected = localSlices.length + librarySlices.length;
251
-
252
- return (
253
- <ReuseExistingSlicesContext.Provider
254
- value={{
255
- selectedLocalSlices: localSlices,
256
- selectedLibrarySlices: librarySlices,
257
- toggleLocalSlice,
258
- toggleLibrarySlice,
259
- setAllLibrarySlices,
260
- reset,
261
- submit,
262
- isSubmitting,
263
- totalSelected,
264
- }}
265
- >
266
- {children}
267
- </ReuseExistingSlicesContext.Provider>
268
- );
269
- }
270
-
271
- export function useReuseExistingSlicesContext() {
272
- const context = useContext(ReuseExistingSlicesContext);
273
- if (context === undefined) {
274
- throw new Error(
275
- "useReuseExistingSlicesContext must be used within ReuseExistingSlicesProvider",
276
- );
277
- }
278
- return context;
279
- }