@principal-ai/subsystems-react 0.56.15 → 0.56.16

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 (44) hide show
  1. package/dist/index.d.ts +4 -3
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/subsystem/C4Graph.d.ts +17 -0
  6. package/dist/subsystem/C4Graph.d.ts.map +1 -1
  7. package/dist/subsystem/C4Graph.js +128 -61
  8. package/dist/subsystem/C4Graph.js.map +1 -1
  9. package/dist/subsystem/C4NodeCard.d.ts.map +1 -1
  10. package/dist/subsystem/C4NodeCard.js +7 -2
  11. package/dist/subsystem/C4NodeCard.js.map +1 -1
  12. package/dist/subsystem/DecorationPanel.d.ts +32 -3
  13. package/dist/subsystem/DecorationPanel.d.ts.map +1 -1
  14. package/dist/subsystem/DecorationPanel.js +162 -89
  15. package/dist/subsystem/DecorationPanel.js.map +1 -1
  16. package/dist/subsystem/SubsystemComponentGraph.d.ts +2 -0
  17. package/dist/subsystem/SubsystemComponentGraph.d.ts.map +1 -1
  18. package/dist/subsystem/SubsystemComponentGraph.js +1 -1
  19. package/dist/subsystem/SubsystemComponentGraph.js.map +1 -1
  20. package/dist/subsystem/c4.d.ts +2 -0
  21. package/dist/subsystem/c4.d.ts.map +1 -1
  22. package/dist/subsystem/c4.js.map +1 -1
  23. package/dist/subsystem/decoration.d.ts +19 -0
  24. package/dist/subsystem/decoration.d.ts.map +1 -1
  25. package/dist/subsystem/decoration.js.map +1 -1
  26. package/dist/subsystem/decorationC4.d.ts +9 -0
  27. package/dist/subsystem/decorationC4.d.ts.map +1 -0
  28. package/dist/subsystem/decorationC4.js +55 -0
  29. package/dist/subsystem/decorationC4.js.map +1 -0
  30. package/package.json +1 -1
  31. package/src/index.ts +6 -1
  32. package/src/stories/Components/DecorationPanel.stories.tsx +344 -36
  33. package/src/subsystem/C4Graph.groups.test.ts +46 -0
  34. package/src/subsystem/C4Graph.tsx +96 -16
  35. package/src/subsystem/C4NodeCard.test.ts +35 -1
  36. package/src/subsystem/C4NodeCard.tsx +7 -2
  37. package/src/subsystem/DecorationPanel.test.tsx +228 -4
  38. package/src/subsystem/DecorationPanel.tsx +248 -82
  39. package/src/subsystem/SubsystemComponentGraph.sidebar.test.tsx +11 -0
  40. package/src/subsystem/SubsystemComponentGraph.tsx +4 -1
  41. package/src/subsystem/c4.ts +2 -0
  42. package/src/subsystem/decoration.ts +25 -0
  43. package/src/subsystem/decorationC4.test.ts +134 -0
  44. package/src/subsystem/decorationC4.ts +60 -0
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Project a decoration's C4 additions without mutating the base architecture.
3
+ * Hosts resolve trail highlighting and construct membership separately.
4
+ * Invalid additions fail rather than replacing base elements or drawing dangling lines.
5
+ */
6
+ export function applyDecorationC4(base, decoration, active = true) {
7
+ const draft = decoration.draft?.c4;
8
+ if (!active || !draft)
9
+ return base;
10
+ const ids = new Set([base.system.id, ...base.nodes.map((node) => node.id)]);
11
+ const additions = (draft.elements ?? []).map((element) => {
12
+ if (ids.has(element.id))
13
+ throw new Error(`Duplicate C4 element id: ${element.id}`);
14
+ ids.add(element.id);
15
+ return {
16
+ ...element,
17
+ state: 'proposed',
18
+ ...(element.kind === 'container' ? { parentId: element.parentId ?? base.system.id } : {}),
19
+ };
20
+ });
21
+ const nodes = [...base.nodes, ...additions];
22
+ const byId = new Map(nodes.map((node) => [node.id, node]));
23
+ for (const element of additions) {
24
+ if (element.kind === 'component') {
25
+ if (byId.get(element.container)?.kind !== 'container') {
26
+ throw new Error(`Missing C4 container for ${element.id}: ${element.container}`);
27
+ }
28
+ if (element.parentId && element.parentId !== element.container) {
29
+ throw new Error(`Conflicting C4 parent for ${element.id}`);
30
+ }
31
+ }
32
+ else if (element.kind === 'container' && element.parentId !== base.system.id) {
33
+ throw new Error(`Unknown C4 system for ${element.id}: ${element.parentId}`);
34
+ }
35
+ else if ((element.kind === 'external-system' || element.kind === 'person') &&
36
+ element.parentId) {
37
+ throw new Error(`External C4 element cannot have a parent: ${element.id}`);
38
+ }
39
+ }
40
+ const edgeIds = new Set(base.edges.map((edge) => edge.id));
41
+ for (const edge of draft.edges ?? []) {
42
+ if (edgeIds.has(edge.id))
43
+ throw new Error(`Duplicate C4 edge id: ${edge.id}`);
44
+ edgeIds.add(edge.id);
45
+ if (!byId.has(edge.source) || !byId.has(edge.target)) {
46
+ throw new Error(`Missing C4 endpoint for edge: ${edge.id}`);
47
+ }
48
+ }
49
+ return {
50
+ ...base,
51
+ nodes,
52
+ edges: [...base.edges, ...(draft.edges ?? []).map((edge) => ({ ...edge, draft: true }))],
53
+ };
54
+ }
55
+ //# sourceMappingURL=decorationC4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decorationC4.js","sourceRoot":"","sources":["../../src/subsystem/decorationC4.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAAa,EACb,UAA+B,EAC/B,MAAM,GAAG,IAAI;IAEb,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,EAAE,EAAE,CAAC;IACnC,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IAEnC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC5E,MAAM,SAAS,GAAgB,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE;QACpE,IAAI,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;QACnF,G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",
4
4
  "description": "React UI for Subsystem Models \u2014 graph, Pierre code views, and session-event feeds",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -251,6 +251,8 @@ export {
251
251
  export type {
252
252
  DecorationBaseTrailRef,
253
253
  DecorationDraft,
254
+ DecorationDraftC4,
255
+ DecorationDraftC4Element,
254
256
  DecorationDraftComponent,
255
257
  DecorationDraftEdge,
256
258
  DecorationDraftTrailRef,
@@ -259,9 +261,12 @@ export type {
259
261
  DecorationTrailRef,
260
262
  SubsystemDecoration,
261
263
  } from './subsystem/decoration';
262
- export { DecorationPanel } from './subsystem/DecorationPanel';
264
+ export { applyDecorationC4 } from './subsystem/decorationC4';
265
+ export { DecorationHeader, DecorationPanel } from './subsystem/DecorationPanel';
263
266
  export type {
267
+ DecorationHeaderProps,
264
268
  DecorationPanelProps,
269
+ DecorationPanelSelection,
265
270
  DecorationPanelTrailMeta,
266
271
  } from './subsystem/DecorationPanel';
267
272
  export { GraphLayoutCover } from './subsystem/GraphLayoutCover';
@@ -1,17 +1,25 @@
1
- import React, { useState } from 'react';
1
+ import { useMemo, useState } from 'react';
2
+ import '@xyflow/react/dist/style.css';
2
3
  import type { Meta, StoryObj } from '@storybook/react';
3
4
  import { ThemeProvider, defaultEditorTheme } from '@principal-ade/industry-theme';
4
5
  import {
6
+ DecorationHeader,
5
7
  DecorationPanel,
6
8
  type DecorationPanelProps,
9
+ type DecorationPanelSelection,
7
10
  } from '../../subsystem/DecorationPanel';
8
11
  import {
9
12
  decorationTrailKey,
13
+ type DecorationDraftC4Element,
14
+ type DecorationTrailRef,
10
15
  type SubsystemDecoration,
11
16
  } from '../../subsystem/decoration';
17
+ import { applyDecorationC4 } from '../../subsystem/decorationC4';
18
+ import { C4Graph, type C4GraphProps } from '../../subsystem/C4Graph';
19
+ import type { C4Edge, C4Model } from '../../subsystem/c4';
12
20
 
13
21
  /**
14
- * PROTOTYPE: the decoration container as a reading surface.
22
+ * PROTOTYPE: a reading surface alongside an additive C4 projection.
15
23
  *
16
24
  * The fixture deliberately spans two models (desktop + sync service) and
17
25
  * includes one draft trail + draft structure, to show that a decoration is
@@ -43,7 +51,7 @@ const decoration: SubsystemDecoration = {
43
51
  '- The desktop stays the source of truth for the open tab.',
44
52
  '- The service is a projection; it never blocks the save.',
45
53
  '',
46
- '> This is a reading path, not a verified model claim.',
54
+ '> Dashed boxes and segmented relationship labels are proposals, not verified model claims.',
47
55
  ].join('\n'),
48
56
  trails: [
49
57
  {
@@ -70,12 +78,20 @@ const decoration: SubsystemDecoration = {
70
78
  },
71
79
  {
72
80
  from: 'sync-agent',
73
- to: 'sync-service',
81
+ to: 'sync-worker',
74
82
  mechanism: 'calls',
75
83
  file: 'src/sync/syncAgent.ts',
76
84
  purl: 'pkg:github/principal-ai/desktop-app#src/sync/syncAgent.ts',
77
85
  symbol: 'SyncAgent.push',
78
86
  },
87
+ {
88
+ from: 'sync-worker',
89
+ to: 'sync-service',
90
+ mechanism: 'calls',
91
+ file: 'src/sync/uploadWorker.ts',
92
+ purl: 'pkg:github/principal-ai/desktop-app#src/sync/uploadWorker.ts',
93
+ symbol: 'UploadWorker.upload',
94
+ },
79
95
  ],
80
96
  },
81
97
  },
@@ -90,16 +106,81 @@ const decoration: SubsystemDecoration = {
90
106
  process: 'draw-host',
91
107
  },
92
108
  {
93
- alias: 'sync-service',
94
- name: 'SyncService',
95
- construct: 'external',
96
- purpose: 'remote projection of saved drawings',
109
+ alias: 'sync-worker',
110
+ name: 'UploadWorker',
111
+ construct: 'class',
112
+ purpose: 'retries uploads without blocking local saves',
113
+ process: 'sync-worker',
97
114
  },
98
115
  ],
99
116
  edges: [
100
117
  { from: 'tab', to: 'sync-agent', mechanism: 'produces' },
101
- { from: 'sync-agent', to: 'sync-service', mechanism: 'calls' },
118
+ { from: 'sync-agent', to: 'sync-worker', mechanism: 'calls' },
119
+ { from: 'sync-worker', to: 'sync-service', mechanism: 'calls' },
102
120
  ],
121
+ c4: {
122
+ elements: [
123
+ {
124
+ kind: 'component',
125
+ id: 'component:sync-agent',
126
+ label: 'Sync coordinator',
127
+ container: 'container:desktop',
128
+ technology: 'TypeScript',
129
+ description: 'Queues saved drawings for background upload.',
130
+ members: ['sync-agent'],
131
+ },
132
+ {
133
+ kind: 'container',
134
+ id: 'container:sync-worker',
135
+ label: 'Background sync worker',
136
+ containerKind: 'application',
137
+ technology: 'Bun',
138
+ process: 'sync-worker',
139
+ description: 'Uploads independently of the open desktop tab.',
140
+ },
141
+ {
142
+ kind: 'component',
143
+ id: 'component:upload-worker',
144
+ label: 'Upload queue',
145
+ container: 'container:sync-worker',
146
+ technology: 'TypeScript',
147
+ description: 'Retries pending uploads to the sync service.',
148
+ members: ['sync-worker'],
149
+ },
150
+ ],
151
+ edges: [
152
+ {
153
+ id: 'draft:enqueue',
154
+ source: 'component:save',
155
+ target: 'component:sync-agent',
156
+ label: 'Queues saved drawing',
157
+ protocol: 'event',
158
+ mechanisms: ['produces'],
159
+ count: 1,
160
+ trailRefs: [{ trailId: 'sg-desktop:tl-planned-sync', stepIndices: [0] }],
161
+ },
162
+ {
163
+ id: 'draft:dispatch',
164
+ source: 'component:sync-agent',
165
+ target: 'component:upload-worker',
166
+ label: 'Dispatches upload',
167
+ protocol: 'RPC',
168
+ mechanisms: ['calls'],
169
+ count: 1,
170
+ trailRefs: [{ trailId: 'sg-desktop:tl-planned-sync', stepIndices: [1] }],
171
+ },
172
+ {
173
+ id: 'draft:upload',
174
+ source: 'component:upload-worker',
175
+ target: 'component:persist',
176
+ label: 'Uploads drawing',
177
+ protocol: 'HTTPS',
178
+ mechanisms: ['calls'],
179
+ count: 1,
180
+ trailRefs: [{ trailId: 'sg-desktop:tl-planned-sync', stepIndices: [2] }],
181
+ },
182
+ ],
183
+ },
103
184
  },
104
185
  provenance: {
105
186
  source: 'author',
@@ -111,8 +192,8 @@ const decoration: SubsystemDecoration = {
111
192
  const resolveTrail: NonNullable<DecorationPanelProps['resolveTrail']> = (ref) => {
112
193
  if (ref.kind === 'draft') return undefined;
113
194
  const key = decorationTrailKey(ref);
114
- if (key === 'sg-desktop:tl-save') return { title: 'Save drawing', stepCount: 4 };
115
- if (key === 'sg-server:tl-persist') return { title: 'Persist file', stepCount: 5 };
195
+ if (key === 'sg-desktop:tl-save') return { title: 'Save drawing', stepCount: 1 };
196
+ if (key === 'sg-server:tl-persist') return { title: 'Persist file', stepCount: 1 };
116
197
  return undefined;
117
198
  };
118
199
 
@@ -121,40 +202,267 @@ const modelLabels = new Map([
121
202
  ['sg-server', 'sync-service'],
122
203
  ]);
123
204
 
205
+ const baseC4: C4Model = {
206
+ view: 'container',
207
+ system: { id: 'system:drawings', label: 'Drawing save and sync' },
208
+ nodes: [
209
+ {
210
+ kind: 'container', id: 'container:desktop', label: 'Desktop app',
211
+ containerKind: 'application', technology: 'React', process: 'draw-host',
212
+ description: 'Owns the open drawing and saves locally.', state: 'accepted',
213
+ parentId: 'system:drawings',
214
+ },
215
+ {
216
+ kind: 'component', id: 'component:save', label: 'Drawing save',
217
+ container: 'container:desktop', technology: 'TypeScript',
218
+ members: ['tab'], description: 'Writes the current drawing to disk.', state: 'accepted',
219
+ },
220
+ {
221
+ kind: 'container', id: 'container:server', label: 'Sync service',
222
+ containerKind: 'application', technology: 'Bun', process: 'sync-service',
223
+ description: 'Persists remote drawing projections.', state: 'accepted',
224
+ parentId: 'system:drawings',
225
+ },
226
+ {
227
+ kind: 'component', id: 'component:persist', label: 'Drawing persistence',
228
+ container: 'container:server', technology: 'TypeScript',
229
+ members: ['sync-service'], description: 'Persists an uploaded drawing.', state: 'accepted',
230
+ },
231
+ {
232
+ kind: 'external-system', id: 'external:disk', label: 'File storage',
233
+ technology: 'File I/O', members: ['disk'],
234
+ description: 'Local and server-side drawing files.', state: 'accepted',
235
+ },
236
+ ],
237
+ edges: [
238
+ {
239
+ id: 'base:save', source: 'component:save', target: 'external:disk',
240
+ label: 'Saves locally', protocol: 'File I/O', mechanisms: ['writes'], count: 1,
241
+ trailRefs: [{ trailId: 'sg-desktop:tl-save', stepIndices: [0] }],
242
+ },
243
+ {
244
+ id: 'base:persist', source: 'component:persist', target: 'external:disk',
245
+ label: 'Persists projection', protocol: 'File I/O', mechanisms: ['writes'], count: 1,
246
+ trailRefs: [{ trailId: 'sg-server:tl-persist', stepIndices: [0] }],
247
+ },
248
+ ],
249
+ };
250
+
251
+ const c4ElementLabels = new Map(baseC4.nodes.map((element) => [element.id, element.label]));
252
+
124
253
  function Demo() {
125
254
  const [active, setActive] = useState(true);
126
- const [selected, setSelected] = useState<string>('none');
255
+ const [selected, setSelected] = useState<string | null>(null);
256
+ const [selectedElementId, setSelectedElementId] = useState<string | null>(null);
257
+ const [selectedEdgeId, setSelectedEdgeId] = useState<string | null>(null);
258
+ const [selectedDraftItem, setSelectedDraftItem] = useState<DecorationPanelSelection | null>(null);
259
+ const [focusedIds, setFocusedIds] = useState<ReadonlySet<string> | null>(null);
260
+ const [containerFocus, setContainerFocus] = useState<C4GraphProps['containerFocus']>(null);
261
+ const [openIds, setOpenIds] = useState<string[]>([]);
262
+ const projection = useMemo(() => applyDecorationC4(baseC4, decoration, active), [active]);
263
+ const model = useMemo<C4Model>(() => {
264
+ const opened = new Set(openIds);
265
+ const visibleId = (id: string) => {
266
+ const node = projection.nodes.find((element) => element.id === id);
267
+ return node?.kind === 'component' && !opened.has(node.container) ? node.container : id;
268
+ };
269
+ return {
270
+ ...projection,
271
+ openContainerIds: openIds,
272
+ // Fold component relationships onto their container while it is closed.
273
+ edges: projection.edges
274
+ .map((edge) => ({ ...edge, source: visibleId(edge.source), target: visibleId(edge.target) }))
275
+ .filter((edge) => edge.source !== edge.target),
276
+ };
277
+ }, [projection, openIds]);
278
+
279
+ const dimmedElementIds = focusedIds
280
+ ? new Set(model.nodes.filter((node) => !focusedIds.has(node.id)).map((node) => node.id))
281
+ : null;
282
+ const dimmedEdgeIds = selectedEdgeId
283
+ ? new Set(model.edges.filter((edge) => edge.id !== selectedEdgeId).map((edge) => edge.id))
284
+ : selected
285
+ ? new Set(model.edges
286
+ .filter((edge) => !edge.trailRefs?.some((ref) => ref.trailId === selected))
287
+ .map((edge) => edge.id))
288
+ : null;
289
+
290
+ function focusElements(ids: Iterable<string>, source = projection) {
291
+ const focus = new Set(ids);
292
+ const containers = new Set<string>();
293
+ for (const id of focus) {
294
+ const node = source.nodes.find((element) => element.id === id);
295
+ if (node?.kind === 'component') {
296
+ focus.add(node.container);
297
+ containers.add(node.container);
298
+ }
299
+ }
300
+ setFocusedIds(focus);
301
+ setOpenIds((previous) => [...new Set([...previous, ...containers])]);
302
+ }
303
+
304
+ function selectTrail(ref: DecorationTrailRef) {
305
+ setSelectedEdgeId(null);
306
+ setSelectedElementId(null);
307
+ setContainerFocus(null);
308
+ setSelectedDraftItem(null);
309
+ if (!active && ref.kind === 'draft') setActive(true);
310
+ // Use the active projection when selecting a draft while the overlay is off.
311
+ const all = applyDecorationC4(baseC4, decoration);
312
+ const key = decorationTrailKey(ref);
313
+ const edges = all.edges.filter((edge) => edge.trailRefs?.some((trail) => trail.trailId === key));
314
+ setSelected(key);
315
+ focusElements(edges.flatMap((edge) => [edge.source, edge.target]), all);
316
+ }
317
+
318
+ function selectC4Element(element: DecorationDraftC4Element) {
319
+ setSelectedEdgeId(null);
320
+ setSelectedElementId(element.id);
321
+ setSelectedDraftItem({ kind: 'c4', id: element.id });
322
+ setActive(true);
323
+ setSelected(null);
324
+ const containerId = element.kind === 'component' ? element.container : element.id;
325
+ focusContainer(containerId, applyDecorationC4(baseC4, decoration));
326
+ }
327
+
328
+ function focusContainer(id: string, source = projection) {
329
+ const container = source.nodes.find((node) => node.id === id && node.kind === 'container');
330
+ if (!container) {
331
+ setContainerFocus(null);
332
+ focusElements([id], source);
333
+ return;
334
+ }
335
+ setContainerFocus({ id });
336
+ setFocusedIds(new Set([
337
+ id,
338
+ ...source.nodes
339
+ .filter((node) => node.kind === 'component' && node.container === id)
340
+ .map((node) => node.id),
341
+ ]));
342
+ setOpenIds((previous) => [...new Set([...previous, id])]);
343
+ }
344
+
345
+ function toggleActive(next: boolean) {
346
+ setSelectedEdgeId(null);
347
+ setActive(next);
348
+ setSelected(null);
349
+ setFocusedIds(null);
350
+ setSelectedDraftItem(null);
351
+ setContainerFocus(null);
352
+ setSelectedElementId(null);
353
+ }
354
+
355
+ function clearFocus() {
356
+ setSelectedEdgeId(null);
357
+ setSelected(null);
358
+ setFocusedIds(null);
359
+ setSelectedDraftItem(null);
360
+ setContainerFocus(null);
361
+ setSelectedElementId(null);
362
+ }
363
+
364
+ function selectC4Edge(edge: C4Edge) {
365
+ const all = applyDecorationC4(baseC4, decoration);
366
+ const original = all.edges.find((candidate) => candidate.id === edge.id) ?? edge;
367
+ const isDraft = decoration.draft?.c4?.edges?.some((candidate) => candidate.id === edge.id);
368
+ if (isDraft) setActive(true);
369
+ setSelectedEdgeId(edge.id);
370
+ setSelectedElementId(null);
371
+ setSelected(null);
372
+ setContainerFocus({ ids: [original.source, original.target] });
373
+ setSelectedDraftItem(isDraft ? { kind: 'c4-edge', id: edge.id } : null);
374
+ focusElements([original.source, original.target], all);
375
+ }
127
376
 
128
377
  return (
129
- <div style={{ display: 'flex', height: '100vh', background: '#0f1115' }}>
130
- <div style={{ width: 420, height: '100%', borderRight: '1px solid #2a2f3a' }}>
131
- <DecorationPanel
132
- decoration={decoration}
133
- active={active}
134
- onToggleActive={setActive}
135
- resolveTrail={resolveTrail}
136
- modelLabels={modelLabels}
137
- onSelectTrail={(ref) => setSelected(decorationTrailKey(ref))}
138
- onSelectDraftComponent={(component) => setSelected(component.alias)}
139
- />
140
- </div>
141
- <div
142
- style={{
143
- flex: 1,
144
- padding: 24,
145
- color: '#9aa4b2',
146
- fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
147
- fontSize: 12,
148
- lineHeight: 1.6,
149
- }}
150
- >
151
- <div>active: {String(active)}</div>
152
- <div>selected: {selected}</div>
378
+ <div style={{ display: 'flex', flexDirection: 'column', height: '100vh', background: '#0f1115' }}>
379
+ <DecorationHeader
380
+ decoration={decoration}
381
+ active={active}
382
+ onToggleActive={toggleActive}
383
+ actions={
384
+ <div style={{ display: 'flex', alignItems: 'center', flexWrap: 'wrap', gap: 16, color: '#9aa4b2', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', fontSize: 12 }}>
385
+ <span>{active ? 'Base + decoration' : 'Base architecture only'}</span>
386
+ <span>Solid boxes: base · dashed boxes: draft</span>
387
+ {focusedIds && (
388
+ <button type="button" onClick={clearFocus}>
389
+ Clear focus
390
+ </button>
391
+ )}
392
+ </div>
393
+ }
394
+ />
395
+ <div style={{ display: 'flex', flex: 1, minHeight: 0 }}>
396
+ <div style={{ width: 360, flexShrink: 0, height: '100%', borderRight: '1px solid #2a2f3a' }}>
397
+ <DecorationPanel
398
+ decoration={decoration}
399
+ active={active}
400
+ onToggleActive={toggleActive}
401
+ hideHeader
402
+ resolveTrail={resolveTrail}
403
+ modelLabels={modelLabels}
404
+ c4ElementLabels={c4ElementLabels}
405
+ onSelectTrail={selectTrail}
406
+ onSelectDraftC4Element={selectC4Element}
407
+ onSelectDraftC4Edge={selectC4Edge}
408
+ selectedDraftItem={selectedDraftItem}
409
+ onSelectDraftComponent={(component) => {
410
+ const owner = decoration.draft?.c4?.elements?.find(
411
+ (element) => element.kind === 'component' && element.members?.includes(component.alias),
412
+ );
413
+ if (owner) selectC4Element(owner);
414
+ setSelectedDraftItem({ kind: 'component', alias: component.alias });
415
+ }}
416
+ />
417
+ </div>
418
+ <div style={{ flex: 1, minWidth: 0, minHeight: 0 }}>
419
+ <C4Graph
420
+ model={model}
421
+ containerFocus={containerFocus}
422
+ selectedNodeId={selectedElementId}
423
+ selectedEdgeId={selectedEdgeId}
424
+ dimmedElementIds={dimmedElementIds}
425
+ dimmedEdgeIds={dimmedEdgeIds}
426
+ onToggleContainer={(id) => setOpenIds((previous) =>
427
+ previous.includes(id) ? previous.filter((value) => value !== id) : [...previous, id])}
428
+ onSelectNode={(id) => {
429
+ setSelectedEdgeId(null);
430
+ setSelectedElementId(id);
431
+ setSelected(null);
432
+ setSelectedDraftItem(
433
+ id && decoration.draft?.c4?.elements?.some((element) => element.id === id)
434
+ ? { kind: 'c4', id }
435
+ : null,
436
+ );
437
+ const element = projection.nodes.find((node) => node.id === id);
438
+ if (element) {
439
+ focusContainer(element.kind === 'component' ? element.container : element.id);
440
+ } else {
441
+ setFocusedIds(null);
442
+ setContainerFocus(null);
443
+ }
444
+ }}
445
+ onSelectEdge={(edge) => {
446
+ if (!edge) {
447
+ setSelectedEdgeId(null);
448
+ setSelectedDraftItem((current) => current?.kind === 'c4-edge' ? null : current);
449
+ return;
450
+ }
451
+ selectC4Edge(edge);
452
+ }}
453
+ onTrailClick={(ref) => {
454
+ const trail = decoration.trails.find((candidate) => decorationTrailKey(candidate) === ref.trailId);
455
+ if (trail) selectTrail(trail);
456
+ }}
457
+ hideDrawer
458
+ />
459
+ </div>
153
460
  </div>
154
461
  </div>
155
462
  );
156
463
  }
157
464
 
158
465
  export const TwoModels: Story = {
466
+ args: { decoration, active: true, onToggleActive: () => {} },
159
467
  render: () => <Demo />,
160
468
  };
@@ -15,6 +15,7 @@ import { describe, expect, test } from 'bun:test';
15
15
  import {
16
16
  absoluteRectOf,
17
17
  c4GroupDefs,
18
+ c4CameraNodeIds,
18
19
  hidesComponentDuringMorph,
19
20
  rebaseGroupBoundsAbsolute,
20
21
  showDescriptionFor,
@@ -62,6 +63,51 @@ const base = model([
62
63
  const groupById = (defs: ReturnType<typeof c4GroupDefs>['groups']) =>
63
64
  new Map(defs.map((g) => [g.id, g]));
64
65
 
66
+ describe('C4 camera targets', () => {
67
+ const withOutside = model([
68
+ ...base.nodes,
69
+ { id: 'external:github', kind: 'external-system', label: 'GitHub' },
70
+ { id: 'person:dev', kind: 'person', label: 'Dev' },
71
+ ]);
72
+
73
+ test('focuses a container without fitting siblings or outside systems', () => {
74
+ expect(c4CameraNodeIds(withOutside, HOST)).toEqual([HOST]);
75
+ expect(c4CameraNodeIds({ ...withOutside, openContainerIds: [HOST] }, HOST)).toEqual([HOST]);
76
+ });
77
+
78
+ test('restores the system and outside elements for the overview', () => {
79
+ expect(c4CameraNodeIds(withOutside, null)).toEqual([SYSTEM, 'external:github', 'person:dev']);
80
+ expect(c4CameraNodeIds(withOutside)).toEqual([SYSTEM, 'external:github', 'person:dev']);
81
+ });
82
+
83
+ test('falls back to the overview for missing targets', () => {
84
+ const overview = [SYSTEM, 'external:github', 'person:dev'];
85
+ expect(c4CameraNodeIds(withOutside, 'missing')).toEqual(overview);
86
+ expect(c4CameraNodeIds(withOutside, ['missing', 'also-missing'])).toEqual(overview);
87
+ expect(c4CameraNodeIds(withOutside, [])).toEqual(overview);
88
+ });
89
+
90
+ test('frames both endpoint containers and excludes unrelated elements', () => {
91
+ expect(c4CameraNodeIds(withOutside, ['cmp:a', NOTES])).toEqual([HOST, NOTES]);
92
+ });
93
+
94
+ test('deduplicates endpoints in the same container', () => {
95
+ expect(c4CameraNodeIds(withOutside, ['cmp:a', 'cmp:b'])).toEqual([HOST]);
96
+ expect(c4CameraNodeIds(withOutside, ['cmp:a', HOST])).toEqual([HOST]);
97
+ });
98
+
99
+ test('keeps external endpoints in the relationship frame', () => {
100
+ expect(c4CameraNodeIds(withOutside, ['cmp:a', 'external:github']))
101
+ .toEqual([HOST, 'external:github']);
102
+ expect(c4CameraNodeIds(withOutside, ['person:dev', NOTES]))
103
+ .toEqual(['person:dev', NOTES]);
104
+ });
105
+
106
+ test('ignores missing endpoints when a valid target remains', () => {
107
+ expect(c4CameraNodeIds(withOutside, ['cmp:a', 'missing'])).toEqual([HOST]);
108
+ });
109
+ });
110
+
65
111
  describe('c4GroupDefs — the system boundary', () => {
66
112
  test('is a group of its own, even with nothing open', () => {
67
113
  const { groups } = c4GroupDefs(base, []);