@mgamadeus/ddd-design-studio 0.2.0 → 0.2.2

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 CHANGED
@@ -22,15 +22,24 @@ ESM only. Peer dependencies (installed by the application):
22
22
 
23
23
  | Package | Range |
24
24
  |---|---|
25
- | `react`, `react-dom` | `^19.3.1` |
25
+ | `react`, `react-dom` | `^19.0.0` |
26
26
  | `@reduxjs/toolkit` | `^2.13.0` |
27
27
  | `react-redux` | `^9.3.1` |
28
28
  | `@mgamadeus/ddd-web` | `^0.3.0` |
29
29
  | `@xyflow/react` | `^12.12.1` (loaded only by the `/canvas` entry) |
30
- | `lucide-react` | `^1.66.0` |
30
+ | `lucide-react` | `^0.550.0 \|\| ^1.0.0` |
31
31
 
32
32
  `clsx` and `tailwind-merge` are regular dependencies.
33
33
 
34
+ **pnpm `minimumReleaseAge`:** pnpm 10+ refuses versions younger than the configured age (24 h by default in many
35
+ setups), so `pnpm install --frozen-lockfile` fails in CI right after a release. Either wait, or exclude the package in
36
+ `pnpm-workspace.yaml` and commit that line together with the lockfile:
37
+
38
+ ```yaml
39
+ minimumReleaseAgeExclude:
40
+ - '@mgamadeus/ddd-design-studio'
41
+ ```
42
+
34
43
  ## Entries
35
44
 
36
45
  | Import | Content | Loads `@xyflow/react` |
@@ -189,6 +198,130 @@ export const designStudioApi = createDesignStudioApi({ baseApi, apiPrefix: '/api
189
198
  // src/design-studio/AppDesignStudioProvider.tsx: the environment built from the application's own pieces
190
199
  'use client';
191
200
 
201
+ import {
202
+ DesignStudioProvider,
203
+ type DesignStudioEnvironment,
204
+ type DesignStudioSearchParamUpdates,
205
+ type DesignStudioUrlStateOptions,
206
+ } from '@mgamadeus/ddd-design-studio';
207
+ import Link from 'next/link';
208
+ import { usePathname, useRouter, useSearchParams } from 'next/navigation';
209
+ import { type ReactNode, useCallback, useEffect, useRef, useState } from 'react';
210
+
211
+ import { designStudioApi } from './designStudioApi';
212
+
213
+ type AppRouter = ReturnType<typeof useRouter>;
214
+
215
+ // Writes of one tick are merged into one `router.replace` (the canvas writes several parameters at once).
216
+ let pendingUpdates: Record<string, string | null> | null = null;
217
+ const queueSearchParamsWrite = (pathname: string, router: AppRouter, updates: DesignStudioSearchParamUpdates) => {
218
+ if (!pendingUpdates) {
219
+ pendingUpdates = {};
220
+ queueMicrotask(() => {
221
+ const next = new URLSearchParams(window.location.search);
222
+ for (const [key, value] of Object.entries(pendingUpdates ?? {})) {
223
+ if (value) next.set(key, value);
224
+ else next.delete(key);
225
+ }
226
+ pendingUpdates = null;
227
+ const query = next.toString();
228
+ router.replace(query ? `${pathname}?${query}` : pathname, { scroll: false });
229
+ });
230
+ }
231
+ Object.assign(pendingUpdates, updates);
232
+ };
233
+
234
+ /** Replaces search parameters of the current page (no history entry); `null` / `''` removes one. */
235
+ const useSearchParamsWriter = () => {
236
+ const pathname = usePathname();
237
+ const router = useRouter();
238
+ return useCallback(
239
+ (updates: DesignStudioSearchParamUpdates) => queueSearchParamsWrite(pathname, router, updates),
240
+ [pathname, router]
241
+ );
242
+ };
243
+
244
+ /**
245
+ * The canvas state in the URL: read from the URL on mount, else restored from this tab's sessionStorage under
246
+ * `stateId`; written back (URL + storage) `debounceMs` after the last change. Defaults (`''`) are never written.
247
+ */
248
+ const useUrlState = <K extends string>(keys: readonly K[], { stateId, debounceMs }: DesignStudioUrlStateOptions) => {
249
+ const searchParams = useSearchParams();
250
+ const writeSearchParams = useSearchParamsWriter();
251
+ const storageKey = `app.${stateId}`;
252
+ const [values, setValues] = useState(() => {
253
+ const fromUrl = Object.fromEntries(keys.map(key => [key, searchParams.get(key) ?? ''])) as Record<K, string>;
254
+ if (keys.some(key => fromUrl[key]) || typeof window === 'undefined') return fromUrl;
255
+ try {
256
+ return { ...fromUrl, ...(JSON.parse(window.sessionStorage.getItem(storageKey) ?? '{}') as Partial<Record<K, string>>) };
257
+ } catch {
258
+ return fromUrl;
259
+ }
260
+ });
261
+ const [debounced, setDebounced] = useState(values);
262
+ const keysRef = useRef(keys);
263
+ useEffect(() => {
264
+ const timer = setTimeout(() => {
265
+ setDebounced(values);
266
+ writeSearchParams(Object.fromEntries(keysRef.current.map(key => [key, values[key] || null])));
267
+ try {
268
+ window.sessionStorage.setItem(storageKey, JSON.stringify(values));
269
+ } catch {
270
+ // storage blocked: the URL still holds the state
271
+ }
272
+ }, debounceMs);
273
+ return () => clearTimeout(timer);
274
+ }, [values, debounceMs, storageKey, writeSearchParams]);
275
+ const setValue = useCallback((key: K, value: string) => setValues(current => ({ ...current, [key]: value })), []);
276
+ return { values, debounced, setValue };
277
+ };
278
+
279
+ /** Client navigation (the overview opens a board code on its canvas). */
280
+ const useNavigate = () => {
281
+ const router = useRouter();
282
+ return useCallback((href: string) => router.push(href), [router]);
283
+ };
284
+
285
+ const ENVIRONMENT: DesignStudioEnvironment = {
286
+ // …
287
+ texts: { reload: 'Refresh (R)', boardsUpdated: count => `${count} screens changed` },
288
+ };
289
+ ```
290
+
291
+ The other texts of the studio are fixed English strings.
292
+
293
+ ## Routes the application creates
294
+
295
+ The package has no router. The application creates one page per screen and tells the studio their paths through
296
+ `environment.paths`:
297
+
298
+ | Page | Screen | `paths` entry |
299
+ |---|---|---|
300
+ | overview of the projects | `DesignStudioOverviewScreen` | `overview()` |
301
+ | one project and its canvases | `DesignStudioProjectScreen` (`projectKey`) | `project(projectKey)` |
302
+ | one canvas | `DesignStudioCanvasScreen` (`projectKey`, `canvasKey`) from `/canvas` | `canvas(projectKey, canvasKey)` |
303
+ | reviews (optional; an empty state in this version) | `DesignStudioReviewsScreen` | none: linked by the application's own navigation |
304
+
305
+ The canvas page keeps its state in search parameters: `board` (the focus view), `q`, `section`, `status`, `theme`,
306
+ `mode`, `vp`. `code` opens a board by its code (see Board codes) and is removed from the URL once handled. Project and canvas keys in a URL should be validated by the application before a screen is rendered.
307
+
308
+ ## Wiring with the Next.js App Router
309
+
310
+ A minimal example. `@/…` stands for the application's own modules.
311
+
312
+ ```ts
313
+ // src/design-studio/designStudioApi.ts: once, at module scope
314
+ import { createDesignStudioApi } from '@mgamadeus/ddd-design-studio';
315
+
316
+ import { baseApi } from '@/api/baseApi';
317
+
318
+ export const designStudioApi = createDesignStudioApi({ baseApi, apiPrefix: '/api/admin/designStudio' });
319
+ ```
320
+
321
+ ```tsx
322
+ // src/design-studio/AppDesignStudioProvider.tsx: the environment built from the application's own pieces
323
+ 'use client';
324
+
192
325
  import { DesignStudioProvider, type DesignStudioEnvironment } from '@mgamadeus/ddd-design-studio';
193
326
  import Link from 'next/link';
194
327
  import { usePathname, useRouter, useSearchParams } from 'next/navigation';
@@ -220,8 +353,7 @@ const useSearchParamsWriter = () => {
220
353
  const ENVIRONMENT: DesignStudioEnvironment = {
221
354
  api: designStudioApi,
222
355
  apiBaseUrl: '',
223
- // useUrlState: the application's hook for list state in the URL (see the contract of `router.useUrlState`)
224
- router: { Link, useSearchParams, useSearchParamsWriter, useUrlState: useAppUrlState },
356
+ router: { Link, useSearchParams, useSearchParamsWriter, useUrlState, useNavigate },
225
357
  paths: {
226
358
  overview: () => '/design',
227
359
  project: projectKey => `/design/${projectKey}`,
@@ -2243,7 +2243,9 @@ var DesignStudioCanvasScreen = ({ projectKey, canvasKey }) => {
2243
2243
  const [presentSectionId, setPresentSectionId] = useState8(null);
2244
2244
  const [fitRequest, setFitRequest] = useState8(null);
2245
2245
  const [tool, setTool] = useState8("hand");
2246
- const [isCommentsPanelOpen, setIsCommentsPanelOpen] = useState8(() => storage.read("commentsPanelOpen") !== "0");
2246
+ const [isCommentsPanelOpen, setIsCommentsPanelOpen] = useState8(
2247
+ () => Boolean(searchParams.get(CODE_PARAM)) || storage.read("commentsPanelOpen") !== "0"
2248
+ );
2247
2249
  const canvasState = router.useUrlState(CANVAS_STATE_KEYS, {
2248
2250
  stateId: `designCanvas.${projectKey}.${canvasKey}`,
2249
2251
  debounceMs: 250,
@@ -2592,9 +2594,21 @@ var DesignStudioCanvasScreen = ({ projectKey, canvasKey }) => {
2592
2594
  const nodeId = boardNodeId(board.boardKey);
2593
2595
  setSelectedBoardId(nodeId);
2594
2596
  setSelectedBoardIds(/* @__PURE__ */ new Set([nodeId]));
2595
- setIsCommentsPanelOpen(true);
2596
- if (!isFirstView) requestFit([nodeId]);
2597
- }, [codeParam, codeAtLoad, boards, writeSearchParams, notify, texts, requestFit]);
2597
+ if (isFirstView) return;
2598
+ if (isCommentsPanelOpen) {
2599
+ requestFit([nodeId]);
2600
+ } else {
2601
+ pendingCodeFitRef.current = nodeId;
2602
+ setIsCommentsPanelOpen(true);
2603
+ }
2604
+ }, [codeParam, codeAtLoad, boards, isCommentsPanelOpen, writeSearchParams, notify, texts, requestFit]);
2605
+ const pendingCodeFitRef = useRef3(null);
2606
+ useEffect6(() => {
2607
+ const nodeId = pendingCodeFitRef.current;
2608
+ if (!nodeId || layout.sections.length === 0) return;
2609
+ pendingCodeFitRef.current = null;
2610
+ requestFit([nodeId]);
2611
+ }, [layout, requestFit]);
2598
2612
  const stepFocus = useCallback2(
2599
2613
  (step) => {
2600
2614
  if (!focusedBoardId || navigationOrder.length === 0) return;