@terrafirmagreg-team/recipe 1.2.42 → 1.2.44

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 (57) hide show
  1. package/package.json +6 -5
  2. package/src/components/cards/calc-add-item.tsx +2 -1
  3. package/src/components/views/browser/frame.tsx +46 -42
  4. package/src/components/views/browser/query-controls.tsx +6 -1
  5. package/src/components/views/browser/type-tabs.tsx +5 -0
  6. package/src/components/views/browser/type-view.tsx +22 -39
  7. package/src/components/views/calc/calculator/help.tsx +6 -6
  8. package/src/components/views/calc/calculator/index.tsx +29 -6
  9. package/src/components/views/calc/edge.tsx +197 -113
  10. package/src/components/views/calc/flow/index.tsx +120 -39
  11. package/src/components/views/calc/flow/notes.tsx +282 -0
  12. package/src/components/views/calc/flow/toolbar.tsx +46 -1
  13. package/src/components/views/calc/flow/view-menu.tsx +11 -0
  14. package/src/components/views/calc/material-picker.tsx +112 -0
  15. package/src/components/views/calc/node-card/actions.tsx +1 -1
  16. package/src/components/views/calc/node-card/badges.tsx +3 -3
  17. package/src/components/views/calc/node-card/far.tsx +1 -1
  18. package/src/components/views/calc/node-card/full.tsx +14 -3
  19. package/src/components/views/calc/node-card/menu.tsx +0 -17
  20. package/src/components/views/calc/node-card/port-cells.tsx +4 -4
  21. package/src/components/views/calc/node-card/ports.tsx +6 -1
  22. package/src/components/views/calc/recipe-picker/body.tsx +22 -32
  23. package/src/components/views/calc/recipe-picker/footer.tsx +1 -28
  24. package/src/hooks/calc/flow/use-handlers.ts +21 -14
  25. package/src/hooks/calc/flow/use-layout.ts +7 -7
  26. package/src/hooks/calc/flow/use-shift-held.ts +25 -0
  27. package/src/hooks/calc/use-actions.ts +126 -61
  28. package/src/hooks/calc/use-amounts.ts +8 -5
  29. package/src/hooks/calc/use-data.ts +11 -8
  30. package/src/hooks/calc/use-history.ts +98 -0
  31. package/src/hooks/calc/use-pipe-edit.ts +21 -0
  32. package/src/hooks/calc/use-snap.ts +17 -0
  33. package/src/hooks/calc/use-storage.ts +2 -1
  34. package/src/hooks/use-add-to-calc.ts +2 -1
  35. package/src/hooks/use-item-press.ts +4 -2
  36. package/src/hooks/use-query.ts +22 -1
  37. package/src/hooks/use-session.ts +2 -0
  38. package/src/lib/browser/row-search.test.ts +55 -0
  39. package/src/lib/browser/row-search.ts +24 -0
  40. package/src/lib/calc/flow.ts +31 -26
  41. package/src/lib/calc/member.test.ts +5 -5
  42. package/src/lib/calc/pack.ts +6 -6
  43. package/src/lib/calc/{wire.test.ts → pipe.test.ts} +5 -5
  44. package/src/lib/calc/{wire.ts → pipe.ts} +9 -9
  45. package/src/lib/calc/route/index.ts +1 -0
  46. package/src/lib/calc/scheme.test.ts +7 -7
  47. package/src/lib/calc/scheme.ts +16 -15
  48. package/src/lib/calc/storage/entry.test.ts +66 -0
  49. package/src/lib/calc/storage/entry.ts +29 -7
  50. package/src/lib/calc/storage/store.ts +3 -1
  51. package/src/lib/calc/tree/share.test.ts +102 -0
  52. package/src/lib/calc/tree/share.ts +43 -13
  53. package/src/lib/links.ts +3 -0
  54. package/src/types/calc-flow-props.ts +13 -2
  55. package/src/types/calc.ts +23 -3
  56. package/src/types/index.ts +3 -0
  57. package/styles/calc.css +65 -39
@@ -22,8 +22,9 @@ import type {
22
22
  CalcCard,
23
23
  CalcKind,
24
24
  CalcNode,
25
+ CalcNote,
25
26
  CalculatorState,
26
- CalcWire,
27
+ CalcPipe,
27
28
  TagResolution,
28
29
  } from '@terrafirmagreg-team/recipe/types/calc';
29
30
  import type { RecipeRelation } from '@terrafirmagreg-team/recipe/types';
@@ -33,6 +34,10 @@ let cardSeq = 0;
33
34
  const newCardId = () =>
34
35
  `c${Date.now().toString(36)}${(cardSeq += 1).toString(36)}`;
35
36
 
37
+ export const NOTE_ID_PREFIX = 'n_';
38
+ const newNoteId = () =>
39
+ `${NOTE_ID_PREFIX}${Date.now().toString(36)}${(cardSeq += 1).toString(36)}`;
40
+
36
41
  function selectTagMember(
37
42
  prev: Record<string, string>,
38
43
  tagId: string,
@@ -65,8 +70,9 @@ export function useCalcActions({
65
70
  lang,
66
71
  linkVersion,
67
72
  cards,
68
- wires,
73
+ pipes,
69
74
  tagSelections,
75
+ notes,
70
76
  persist,
71
77
  recipeDefaults,
72
78
  setRecipeDefault,
@@ -82,8 +88,9 @@ export function useCalcActions({
82
88
  lang: Lang;
83
89
  linkVersion?: string;
84
90
  cards: CalcCard[];
85
- wires: CalcWire[];
91
+ pipes: CalcPipe[];
86
92
  tagSelections: Record<string, string>;
93
+ notes: CalcNote[];
87
94
  persist: (next: CalculatorState) => void;
88
95
  recipeDefaults: Record<string, string>;
89
96
  setRecipeDefault: (materialId: string, recipeId: string | null) => void;
@@ -129,14 +136,19 @@ export function useCalcActions({
129
136
  });
130
137
 
131
138
  const write = useCallback(
132
- (next: Partial<Pick<CalculatorState, 'cards' | 'wires' | 'tagSelections'>>) => {
139
+ (
140
+ next: Partial<
141
+ Pick<CalculatorState, 'cards' | 'pipes' | 'tagSelections' | 'notes'>
142
+ >,
143
+ ) => {
133
144
  persist({
134
145
  cards: next.cards ?? flushAmounts(),
135
- wires: next.wires ?? wires,
146
+ pipes: next.pipes ?? pipes,
136
147
  tagSelections: next.tagSelections ?? tagSelections,
148
+ notes: next.notes ?? notes,
137
149
  });
138
150
  },
139
- [flushAmounts, persist, wires, tagSelections],
151
+ [flushAmounts, persist, pipes, tagSelections, notes],
140
152
  );
141
153
 
142
154
  const onSelectRecipe = useCallback(
@@ -145,12 +157,12 @@ export function useCalcActions({
145
157
  const nextCards = cards.map((row) =>
146
158
  row.id === cardId ? { ...row, recipeId } : row,
147
159
  );
148
- const nextWires = recipeId
149
- ? wires
150
- : wires.filter((wire) => wire.to !== cardId);
151
- write({ cards: nextCards, wires: nextWires });
160
+ const nextPipes = recipeId
161
+ ? pipes
162
+ : pipes.filter((pipe) => pipe.to !== cardId);
163
+ write({ cards: nextCards, pipes: nextPipes });
152
164
  },
153
- [cards, wires, write],
165
+ [cards, pipes, write],
154
166
  );
155
167
 
156
168
  const onSelectTagMember = useCallback(
@@ -168,13 +180,14 @@ export function useCalcActions({
168
180
  ...flushAmounts(),
169
181
  { id, materialId, kind, recipeId: null, crafts: 1, pos },
170
182
  ],
171
- wires,
183
+ pipes,
172
184
  tagSelections,
185
+ notes,
173
186
  });
174
187
  setFocusNodeId(id);
175
188
  return id;
176
189
  },
177
- [flushAmounts, persist, wires, tagSelections, setFocusNodeId],
190
+ [flushAmounts, persist, pipes, tagSelections, notes, setFocusNodeId],
178
191
  );
179
192
 
180
193
  const onSupplyInput = useCallback(
@@ -188,9 +201,9 @@ export function useCalcActions({
188
201
  const need = Math.max(1, input.amount - input.covered);
189
202
  const base = consumerCard.pos;
190
203
  const id = newCardId();
191
- const nextWires = [
192
- ...wires.filter(
193
- (wire) => !(wire.to === cardId && wire.input === inputIndex),
204
+ const nextPipes = [
205
+ ...pipes.filter(
206
+ (pipe) => !(pipe.to === cardId && pipe.input === inputIndex),
194
207
  ),
195
208
  { from: id, to: cardId, input: inputIndex },
196
209
  ];
@@ -223,13 +236,14 @@ export function useCalcActions({
223
236
  : undefined,
224
237
  },
225
238
  ],
226
- wires: nextWires,
239
+ pipes: nextPipes,
227
240
  tagSelections,
241
+ notes,
228
242
  });
229
243
  })();
230
244
  return id;
231
245
  },
232
- [nodes, cards, wires, flushAmounts, persist, tagSelections, recipeBody, dataset],
246
+ [nodes, cards, pipes, flushAmounts, persist, tagSelections, notes, recipeBody, dataset],
233
247
  );
234
248
 
235
249
  const onDemandOutput = useCallback(
@@ -293,10 +307,10 @@ export function useCalcActions({
293
307
  };
294
308
  persist({
295
309
  cards: [...flushAmounts(), card],
296
- wires:
310
+ pipes:
297
311
  inputIndex >= 0
298
312
  ? [
299
- ...wires,
313
+ ...pipes,
300
314
  {
301
315
  from: cardId,
302
316
  to: id,
@@ -304,10 +318,11 @@ export function useCalcActions({
304
318
  out: outputIndex,
305
319
  },
306
320
  ]
307
- : wires,
321
+ : pipes,
308
322
  tagSelections: pickedTag
309
323
  ? selectTagMember(tagSelections, pickedTag, out.id)
310
324
  : tagSelections,
325
+ notes,
311
326
  });
312
327
  ensureMaterial?.(materialId);
313
328
  setFocusNodeId(id);
@@ -316,10 +331,11 @@ export function useCalcActions({
316
331
  [
317
332
  nodes,
318
333
  cards,
319
- wires,
334
+ pipes,
320
335
  flushAmounts,
321
336
  persist,
322
337
  tagSelections,
338
+ notes,
323
339
  dataset,
324
340
  tagMembers,
325
341
  ensureMaterial,
@@ -340,25 +356,37 @@ export function useCalcActions({
340
356
 
341
357
  const onRemoveCard = useCallback(
342
358
  (cardId: string) => {
359
+ if (cardId.startsWith(NOTE_ID_PREFIX)) {
360
+ write({ notes: notes.filter((note) => note.id !== cardId) });
361
+ return;
362
+ }
343
363
  write({
344
364
  cards: flushAmounts().filter((row) => row.id !== cardId),
345
- wires: wires.filter(
346
- (wire) => wire.from !== cardId && wire.to !== cardId,
365
+ pipes: pipes.filter(
366
+ (pipe) => pipe.from !== cardId && pipe.to !== cardId,
347
367
  ),
348
368
  });
349
369
  },
350
- [flushAmounts, write, wires],
370
+ [flushAmounts, write, pipes, notes],
351
371
  );
352
372
 
353
373
  const onMoveCard = useCallback(
354
374
  (cardId: string, pos: { x: number; y: number }) => {
375
+ if (cardId.startsWith(NOTE_ID_PREFIX)) {
376
+ write({
377
+ notes: notes.map((note) =>
378
+ note.id === cardId ? { ...note, pos } : note,
379
+ ),
380
+ });
381
+ return;
382
+ }
355
383
  write({
356
384
  cards: flushAmounts().map((row) =>
357
385
  row.id === cardId ? { ...row, pos } : row,
358
386
  ),
359
387
  });
360
388
  },
361
- [flushAmounts, write],
389
+ [flushAmounts, write, notes],
362
390
  );
363
391
 
364
392
  const onMoveCards = useCallback(
@@ -389,7 +417,7 @@ export function useCalcActions({
389
417
  [nodes],
390
418
  );
391
419
 
392
- const onWire = useCallback(
420
+ const onConnect = useCallback(
393
421
  (from: string, to: string, input: number, out?: number) => {
394
422
  if (from === to) return;
395
423
  const pinTag = tagPinFor(to, input, from, out);
@@ -397,9 +425,9 @@ export function useCalcActions({
397
425
  .find((n) => n.id === from)
398
426
  ?.outputs[out ?? 0];
399
427
  write({
400
- wires: [
401
- ...wires.filter(
402
- (wire) => !(wire.to === to && wire.input === input),
428
+ pipes: [
429
+ ...pipes.filter(
430
+ (pipe) => !(pipe.to === to && pipe.input === input),
403
431
  ),
404
432
  { from, to, input, out: out || undefined },
405
433
  ],
@@ -409,27 +437,27 @@ export function useCalcActions({
409
437
  : tagSelections,
410
438
  });
411
439
  },
412
- [wires, write, nodes, tagSelections, tagPinFor],
440
+ [pipes, write, nodes, tagSelections, tagPinFor],
413
441
  );
414
442
 
415
- const onUnwire = useCallback(
443
+ const onDisconnect = useCallback(
416
444
  (from: string, to: string, input: number, out?: number) => {
417
445
  write({
418
- wires: wires.filter(
419
- (wire) =>
446
+ pipes: pipes.filter(
447
+ (pipe) =>
420
448
  !(
421
- wire.from === from &&
422
- wire.to === to &&
423
- wire.input === input &&
424
- (wire.out ?? 0) === (out ?? 0)
449
+ pipe.from === from &&
450
+ pipe.to === to &&
451
+ pipe.input === input &&
452
+ (pipe.out ?? 0) === (out ?? 0)
425
453
  ),
426
454
  ),
427
455
  });
428
456
  },
429
- [wires, write],
457
+ [pipes, write],
430
458
  );
431
459
 
432
- const onRewire = useCallback(
460
+ const onReroute = useCallback(
433
461
  (
434
462
  from: string,
435
463
  to: string,
@@ -442,11 +470,11 @@ export function useCalcActions({
442
470
  .find((n) => n.id === next.from)
443
471
  ?.outputs[next.out ?? 0];
444
472
  write({
445
- wires: [
446
- ...wires.filter(
447
- (wire) =>
448
- !(wire.from === from && wire.to === to && wire.input === input) &&
449
- !(wire.to === next.to && wire.input === next.input),
473
+ pipes: [
474
+ ...pipes.filter(
475
+ (pipe) =>
476
+ !(pipe.from === from && pipe.to === to && pipe.input === input) &&
477
+ !(pipe.to === next.to && pipe.input === next.input),
450
478
  ),
451
479
  next,
452
480
  ],
@@ -456,30 +484,62 @@ export function useCalcActions({
456
484
  : tagSelections,
457
485
  });
458
486
  },
459
- [wires, write, nodes, tagSelections, tagPinFor],
487
+ [pipes, write, nodes, tagSelections, tagPinFor],
460
488
  );
461
489
 
462
- const onWireAmount = useCallback(
490
+ const onPipeLimit = useCallback(
463
491
  (from: string, to: string, input: number, amount: number | null) => {
464
492
  write({
465
- wires: wires.map((wire) =>
466
- wire.from === from && wire.to === to && wire.input === input
467
- ? { ...wire, amount: amount ?? undefined }
468
- : wire,
493
+ pipes: pipes.map((pipe) =>
494
+ pipe.from === from && pipe.to === to && pipe.input === input
495
+ ? { ...pipe, amount: amount ?? undefined }
496
+ : pipe,
469
497
  ),
470
498
  });
471
499
  },
472
- [wires, write],
500
+ [pipes, write],
473
501
  );
474
502
 
475
503
  const onClear = useCallback(() => {
476
504
  persist({
477
505
  cards: [],
478
- wires: [],
506
+ pipes: [],
479
507
  tagSelections: {},
508
+ notes: [],
480
509
  });
481
510
  }, [persist]);
482
511
 
512
+ const onAddNote = useCallback(
513
+ (pos: { x: number; y: number }) => {
514
+ const id = newNoteId();
515
+ write({ notes: [...notes, { id, text: '', pos }] });
516
+ return id;
517
+ },
518
+ [write, notes],
519
+ );
520
+
521
+ const onEditNote = useCallback(
522
+ (id: string, text: string) => {
523
+ write({
524
+ notes: notes.map((note) =>
525
+ note.id === id ? { ...note, text } : note,
526
+ ),
527
+ });
528
+ },
529
+ [write, notes],
530
+ );
531
+
532
+ const onNoteColor = useCallback(
533
+ (id: string, color: CalcNote['color']) => {
534
+ write({
535
+ notes: notes.map((note) =>
536
+ note.id === id ? { ...note, color } : note,
537
+ ),
538
+ });
539
+ },
540
+ [write, notes],
541
+ );
542
+
483
543
  const flowData = useMemo<
484
544
  Omit<
485
545
  CalcFlowData,
@@ -506,10 +566,12 @@ export function useCalcActions({
506
566
  onOpenItem,
507
567
  onRemoveCard,
508
568
  onMoveCard,
509
- onWire,
510
- onUnwire,
511
- onRewire,
512
- onWireAmount,
569
+ onConnect,
570
+ onDisconnect,
571
+ onReroute,
572
+ onPipeLimit,
573
+ onEditNote,
574
+ onNoteColor,
513
575
  recipeDefaults,
514
576
  onSetRecipeDefault: setRecipeDefault,
515
577
  tagSelections,
@@ -526,10 +588,12 @@ export function useCalcActions({
526
588
  onOpenItem,
527
589
  onRemoveCard,
528
590
  onMoveCard,
529
- onWire,
530
- onUnwire,
531
- onRewire,
532
- onWireAmount,
591
+ onConnect,
592
+ onDisconnect,
593
+ onReroute,
594
+ onPipeLimit,
595
+ onEditNote,
596
+ onNoteColor,
533
597
  recipeDefaults,
534
598
  setRecipeDefault,
535
599
  ],
@@ -545,6 +609,7 @@ export function useCalcActions({
545
609
  onRemoveCard,
546
610
  onMoveCards,
547
611
  onSupplyInput,
612
+ onAddNote,
548
613
  onClear,
549
614
  };
550
615
  }
@@ -4,20 +4,23 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
4
4
  import type {
5
5
  CalcCard,
6
6
  CalcNode,
7
- CalcWire,
7
+ CalcNote,
8
+ CalcPipe,
8
9
  CalculatorState,
9
10
  } from '@terrafirmagreg-team/recipe/types/calc';
10
11
 
11
12
  export function useCalcAmounts({
12
13
  cards,
13
- wires,
14
+ pipes,
14
15
  tagSelections,
16
+ notes,
15
17
  resolved,
16
18
  persist,
17
19
  }: {
18
20
  cards: CalcCard[];
19
- wires: CalcWire[];
21
+ pipes: CalcPipe[];
20
22
  tagSelections: Record<string, string>;
23
+ notes: CalcNote[];
21
24
  resolved: CalcNode[];
22
25
  persist: (next: CalculatorState) => void;
23
26
  }) {
@@ -67,10 +70,10 @@ export function useCalcAmounts({
67
70
  if (amountTimer.current) clearTimeout(amountTimer.current);
68
71
  amountTimer.current = setTimeout(() => {
69
72
  amountTimer.current = undefined;
70
- persist({ cards: next, wires, tagSelections });
73
+ persist({ cards: next, pipes, tagSelections, notes });
71
74
  }, 300);
72
75
  },
73
- [pending, cards, persist, wires, tagSelections],
76
+ [pending, cards, persist, pipes, tagSelections, notes],
74
77
  );
75
78
 
76
79
  const pendingCrafts = useMemo(
@@ -55,11 +55,12 @@ export function useCalcData(session: RecipeSession) {
55
55
  } = data;
56
56
 
57
57
  const cards = useMemo(() => calcState?.cards ?? [], [calcState]);
58
- const wires = useMemo(() => calcState?.wires ?? [], [calcState]);
58
+ const pipes = useMemo(() => calcState?.pipes ?? [], [calcState]);
59
59
  const tagSelections = useMemo(
60
60
  () => calcState?.tagSelections ?? {},
61
61
  [calcState],
62
62
  );
63
+ const notes = useMemo(() => calcState?.notes ?? [], [calcState]);
63
64
 
64
65
  const getRecipesForItem = useCallback(
65
66
  (materialId: string, kind: CalcKind): CalcRecipeSummary[] => {
@@ -85,7 +86,7 @@ export function useCalcData(session: RecipeSession) {
85
86
  () =>
86
87
  buildScheme({
87
88
  cards,
88
- wires,
89
+ pipes,
89
90
  ctx: {
90
91
  recipeMap: dataRecipeMap,
91
92
  getRecipesForItem,
@@ -97,7 +98,7 @@ export function useCalcData(session: RecipeSession) {
97
98
  }),
98
99
  [
99
100
  cards,
100
- wires,
101
+ pipes,
101
102
  dataRecipeMap,
102
103
  getRecipesForItem,
103
104
  tagMembers,
@@ -110,8 +111,8 @@ export function useCalcData(session: RecipeSession) {
110
111
  const summary = useMemo(() => schemeSummary(nodes), [nodes]);
111
112
 
112
113
  const inputKey = useMemo(
113
- () => JSON.stringify([cards, wires, tagSelections]),
114
- [cards, wires, tagSelections],
114
+ () => JSON.stringify([cards, pipes, tagSelections]),
115
+ [cards, pipes, tagSelections],
115
116
  );
116
117
 
117
118
  const worker = useCalcWorker({
@@ -149,10 +150,11 @@ export function useCalcData(session: RecipeSession) {
149
150
  () =>
150
151
  encodeCalcState({
151
152
  cards,
152
- wires,
153
+ pipes,
153
154
  tagSelections,
155
+ notes,
154
156
  }),
155
- [cards, wires, tagSelections],
157
+ [cards, pipes, tagSelections, notes],
156
158
  );
157
159
 
158
160
  const recipeBody = useCallback(
@@ -172,8 +174,9 @@ export function useCalcData(session: RecipeSession) {
172
174
 
173
175
  return {
174
176
  cards,
175
- wires,
177
+ pipes,
176
178
  tagSelections,
179
+ notes,
177
180
  tabs,
178
181
  activeTabId,
179
182
  history,
@@ -0,0 +1,98 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useEffect, useRef, useState } from 'react';
4
+ import type { CalculatorState } from '@terrafirmagreg-team/recipe/types/calc';
5
+
6
+ const HISTORY_LIMIT = 50;
7
+
8
+ export function useCalcHistory(
9
+ state: CalculatorState,
10
+ persist: (next: CalculatorState) => void,
11
+ canvasId?: string,
12
+ ) {
13
+ const [stacks, setStacks] = useState<{
14
+ past: CalculatorState[];
15
+ future: CalculatorState[];
16
+ }>({ past: [], future: [] });
17
+ const stateRef = useRef(state);
18
+ const pendingRef = useRef<CalculatorState | null>(null);
19
+
20
+ useEffect(() => {
21
+ stateRef.current = state;
22
+ }, [state]);
23
+
24
+ const [prevCanvas, setPrevCanvas] = useState(canvasId);
25
+ if (prevCanvas !== canvasId) {
26
+ setPrevCanvas(canvasId);
27
+ setStacks({ past: [], future: [] });
28
+ }
29
+
30
+ useEffect(() => {
31
+ const pending = pendingRef.current;
32
+ if (!pending) return;
33
+ pendingRef.current = null;
34
+ persist(pending);
35
+ });
36
+
37
+ const write = useCallback(
38
+ (next: CalculatorState) => {
39
+ setStacks((s) => ({
40
+ past: [...s.past.slice(-(HISTORY_LIMIT - 1)), stateRef.current],
41
+ future: [],
42
+ }));
43
+ persist(next);
44
+ },
45
+ [persist],
46
+ );
47
+
48
+ const undo = useCallback(() => {
49
+ setStacks((s) => {
50
+ const prev = s.past[s.past.length - 1];
51
+ if (!prev) return s;
52
+ pendingRef.current = prev;
53
+ return {
54
+ past: s.past.slice(0, -1),
55
+ future: [...s.future, stateRef.current],
56
+ };
57
+ });
58
+ }, []);
59
+
60
+ const redo = useCallback(() => {
61
+ setStacks((s) => {
62
+ const next = s.future[s.future.length - 1];
63
+ if (!next) return s;
64
+ pendingRef.current = next;
65
+ return {
66
+ past: [...s.past, stateRef.current],
67
+ future: s.future.slice(0, -1),
68
+ };
69
+ });
70
+ }, []);
71
+
72
+ useEffect(() => {
73
+ const onKey = (e: KeyboardEvent) => {
74
+ if (!(e.ctrlKey || e.metaKey) || e.altKey) return;
75
+ const target = e.target as HTMLElement | null;
76
+ if (target?.closest('input, textarea, [contenteditable]')) return;
77
+ const key = e.key.toLowerCase();
78
+ if (key === 'z') {
79
+ e.preventDefault();
80
+ if (e.shiftKey) redo();
81
+ else undo();
82
+ } else if (key === 'y') {
83
+ e.preventDefault();
84
+ redo();
85
+ }
86
+ };
87
+ window.addEventListener('keydown', onKey);
88
+ return () => window.removeEventListener('keydown', onKey);
89
+ }, [undo, redo]);
90
+
91
+ return {
92
+ write,
93
+ undo,
94
+ redo,
95
+ canUndo: stacks.past.length > 0,
96
+ canRedo: stacks.future.length > 0,
97
+ };
98
+ }
@@ -0,0 +1,21 @@
1
+ 'use client';
2
+
3
+ import { createContext, useContext } from 'react';
4
+
5
+ export type CalcPipeEdit = {
6
+ edgeId: string | null;
7
+ open: (edgeId: string) => void;
8
+ close: () => void;
9
+ };
10
+
11
+ const CalcPipeEditContext = createContext<CalcPipeEdit>({
12
+ edgeId: null,
13
+ open: () => {},
14
+ close: () => {},
15
+ });
16
+
17
+ export const CalcPipeEditProvider = CalcPipeEditContext.Provider;
18
+
19
+ export function useCalcPipeEdit() {
20
+ return useContext(CalcPipeEditContext);
21
+ }
@@ -0,0 +1,17 @@
1
+ 'use client';
2
+
3
+ import { startTransition, useCallback } from 'react';
4
+ import {
5
+ boolCodec,
6
+ createStoredValue,
7
+ } from '@terrafirmagreg-team/ui/lib/utils/stored-value';
8
+
9
+ const useStoredSnap = createStoredValue('calc:snap', boolCodec(true));
10
+
11
+ export function useCalcSnap() {
12
+ const [snap, setStored] = useStoredSnap();
13
+ const setSnap = useCallback((next: boolean) => {
14
+ startTransition(() => setStored(next));
15
+ }, [setStored]);
16
+ return [snap, setSnap] as const;
17
+ }
@@ -23,8 +23,9 @@ import { stripMcFormat } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/na
23
23
  const EMPTY_TABS: CalcTreeEntry[] = [];
24
24
  const EMPTY_CALC: CalculatorState = {
25
25
  cards: [],
26
- wires: [],
26
+ pipes: [],
27
27
  tagSelections: {},
28
+ notes: [],
28
29
  };
29
30
 
30
31
  export function useCalcStorage(storageKey: string) {
@@ -17,8 +17,9 @@ export function useAddToCalc(session: RecipeSession) {
17
17
  (item: string, kind: 'item' | 'fluid') => {
18
18
  const state = decodeCalcState(tree ?? '') ?? {
19
19
  cards: [],
20
- wires: [],
20
+ pipes: [],
21
21
  tagSelections: {},
22
+ notes: [],
22
23
  };
23
24
  const existing = state.cards.find(
24
25
  (card) => card.materialId === item && card.kind === kind,
@@ -33,8 +33,9 @@ export function useItemPress({
33
33
  const key = calcStorageKey('recipe', dataset);
34
34
  const parsed = readCalcStorage(key) ?? {
35
35
  cards: [],
36
- wires: [],
36
+ pipes: [],
37
37
  tagSelections: {},
38
+ notes: [],
38
39
  };
39
40
  const kind = ref === 'fluid' ? ('fluid' as const) : ('item' as const);
40
41
  const same = parsed.cards.findIndex(
@@ -57,8 +58,9 @@ export function useItemPress({
57
58
  ];
58
59
  writeCalcStorage(key, {
59
60
  cards,
60
- wires: parsed.wires,
61
+ pipes: parsed.pipes,
61
62
  tagSelections: parsed.tagSelections,
63
+ notes: parsed.notes,
62
64
  });
63
65
  requestCalcFocus(id);
64
66
  return;