@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
@@ -6,154 +6,238 @@ import {
6
6
  type ChevronEdgePathContext,
7
7
  } from '@terrafirmagreg-team/ui/components/blocks/canvas/chevron-edge';
8
8
  import { getSmoothStepPath } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
9
- import {
10
- roundedPolylinePath,
11
- svgPathToPoints,
12
- } from '@terrafirmagreg-team/ui/lib/canvas/edge';
9
+ import { svgPathToPoints } from '@terrafirmagreg-team/ui/lib/canvas/edge';
10
+ import { orthoPoints } from '@terrafirmagreg-team/recipe/lib/calc/route';
13
11
  import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
12
+ import {
13
+ Tooltip,
14
+ TooltipContent,
15
+ TooltipTrigger,
16
+ } from '@terrafirmagreg-team/ui/components/ui/tooltip';
14
17
  import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
18
+ import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
15
19
  import { useCalcSession } from '@terrafirmagreg-team/recipe/hooks/calc/use-calc-session';
20
+ import { useCalcPipeEdit } from '@terrafirmagreg-team/recipe/hooks/calc/use-pipe-edit';
21
+ import { useShiftHeld } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-shift-held';
16
22
  import { recipeItemName } from '@terrafirmagreg-team/recipe/lib/client-index';
17
- import {
18
- formatCount,
19
- formatFluidAmount,
20
- } from '@terrafirmagreg-team/ui/lib/utils/format';
23
+ import { Scissors } from 'lucide-react';
21
24
  import type { FlowPoint } from '@terrafirmagreg-team/ui/types/canvas';
22
- import type { CalcWire } from '@terrafirmagreg-team/recipe/types/calc';
25
+ import type { CalcPipe } from '@terrafirmagreg-team/recipe/types/calc';
23
26
 
24
- function EdgeAmountLabel({ ctx }: { ctx: ChevronEdgePathContext }) {
25
- const wire = ctx.data?.wire as CalcWire | undefined;
26
- const flow = ctx.data?.flow as number | undefined;
27
- const matId = ctx.data?.matId as string | undefined;
28
- const matKind = ctx.data?.matKind as 'item' | 'fluid' | undefined;
29
- const dataset = ctx.data?.dataset as DatasetRef | undefined;
30
- const onWireAmount = ctx.data?.onWireAmount as
31
- | ((from: string, to: string, input: number, amount: number | null) => void)
32
- | undefined;
33
- const onUnwire = ctx.data?.onUnwire as
34
- | ((from: string, to: string, input: number, out?: number) => void)
35
- | undefined;
36
- const session = useCalcSession();
37
- const [edit, setEdit] = useState<string | null>(null);
38
- if (!wire || flow == null) return null;
39
- const cap = wire.amount;
27
+ type PipeEdgeData = {
28
+ pipe: CalcPipe;
29
+ flow: number;
30
+ matId?: string;
31
+ matKind?: 'item' | 'fluid';
32
+ dataset?: DatasetRef;
33
+ onPipeLimit?: (
34
+ from: string,
35
+ to: string,
36
+ input: number,
37
+ amount: number | null,
38
+ ) => void;
39
+ };
40
+
41
+ function fullAmount(n: number, lang?: string, kind?: 'item' | 'fluid') {
42
+ const text = (v: number) => {
43
+ try {
44
+ return v.toLocaleString(lang?.replace('_', '-'), {
45
+ maximumFractionDigits: 3,
46
+ });
47
+ } catch {
48
+ return String(v);
49
+ }
50
+ };
51
+ if (kind !== 'fluid') return text(n);
52
+ return Math.abs(n) >= 1000 ? `${text(n / 1000)} B` : `${text(n)} mB`;
53
+ }
40
54
 
55
+ function EdgeLimitEditor({
56
+ pipe,
57
+ cap,
58
+ matId,
59
+ matKind,
60
+ dataset,
61
+ name,
62
+ onPipeLimit,
63
+ }: PipeEdgeData & { cap: number | null | undefined; name?: string }) {
64
+ const t = useTranslations();
65
+ const { close } = useCalcPipeEdit();
66
+ const [edit, setEdit] = useState(cap != null ? String(cap) : '');
41
67
  const commit = () => {
42
- if (edit == null) return;
43
68
  const raw = edit.trim();
44
69
  const value = raw === '' ? null : Math.max(0, Number(raw));
45
- onWireAmount?.(
46
- wire.from,
47
- wire.to,
48
- wire.input,
70
+ onPipeLimit?.(
71
+ pipe.from,
72
+ pipe.to,
73
+ pipe.input,
49
74
  value != null && Number.isFinite(value) ? value : null,
50
75
  );
51
- setEdit(null);
76
+ close();
52
77
  };
53
78
  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {
54
79
  if (e.key === 'Enter') commit();
55
- if (e.key === 'Escape') setEdit(null);
80
+ if (e.key === 'Escape') close();
56
81
  };
82
+ return (
83
+ <div className="calc-edge-edit" onClick={(e) => e.stopPropagation()}>
84
+ <div className="calc-edge-edit__head">
85
+ {matId && matKind && dataset ? (
86
+ <Entry
87
+ id={matId}
88
+ kind={matKind}
89
+ dataset={dataset}
90
+ size="sm"
91
+ href={false}
92
+ tooltip={false}
93
+ />
94
+ ) : null}
95
+ <span className="calc-edge-edit__name">{name}</span>
96
+ </div>
97
+ <label className="calc-edge-edit__row">
98
+ <span className="calc-edge-edit__label">
99
+ {t('Limit', { note: 'recipe.calc.pipe.limit' })}
100
+ </span>
101
+ <input
102
+ autoFocus
103
+ className="calc-edge-edit__input"
104
+ value={edit}
105
+ onChange={(e) => setEdit(e.target.value)}
106
+ onBlur={commit}
107
+ onKeyDown={onKey}
108
+ />
109
+ {matKind === 'fluid' ? (
110
+ <span className="calc-edge-edit__unit">mB</span>
111
+ ) : null}
112
+ </label>
113
+ </div>
114
+ );
115
+ }
57
116
 
58
- if (edit != null) {
117
+ function EdgeAmountLabel({ ctx }: { ctx: ChevronEdgePathContext }) {
118
+ const session = useCalcSession();
119
+ const { edgeId } = useCalcPipeEdit();
120
+ const shiftHeld = useShiftHeld();
121
+ const t = useTranslations();
122
+ const pipe = ctx.data?.pipe as CalcPipe | undefined;
123
+ const flow = ctx.data?.flow as number | undefined;
124
+ const matId = ctx.data?.matId as string | undefined;
125
+ const matKind = ctx.data?.matKind as 'item' | 'fluid' | undefined;
126
+ const dataset = ctx.data?.dataset as DatasetRef | undefined;
127
+ const onPipeLimit = ctx.data?.onPipeLimit as
128
+ | PipeEdgeData['onPipeLimit']
129
+ | undefined;
130
+ const editing = edgeId === ctx.id;
131
+ if (!editing && shiftHeld) {
59
132
  return (
60
- <input
61
- autoFocus
62
- className="calc-edge-label__input"
63
- value={edit}
64
- onChange={(e) => setEdit(e.target.value)}
65
- onBlur={commit}
66
- onKeyDown={onKey}
67
- onClick={(e) => e.stopPropagation()}
68
- />
133
+ <div className="calc-edge-del">
134
+ <Scissors size={8} strokeWidth={2.5} />
135
+ </div>
69
136
  );
70
137
  }
138
+ if (!pipe || flow == null) return null;
139
+ const cap = pipe.amount;
71
140
  const matName =
72
141
  matId && session?.names
73
142
  ? recipeItemName(session.names, session.lang, matId)
74
143
  : matId;
75
- const fmt =
76
- matKind === 'fluid'
77
- ? formatFluidAmount
78
- : (n: number) => formatCount(n, 1_000);
144
+ const full = (n: number) => fullAmount(n, session?.lang, matKind);
145
+ const vertical =
146
+ Math.abs(ctx.targetY - ctx.sourceY) >
147
+ Math.abs(ctx.targetX - ctx.sourceX);
148
+ if (editing) {
149
+ return (
150
+ <EdgeLimitEditor
151
+ pipe={pipe}
152
+ flow={flow}
153
+ cap={cap}
154
+ matId={matId}
155
+ matKind={matKind}
156
+ dataset={dataset}
157
+ name={matName}
158
+ onPipeLimit={onPipeLimit}
159
+ />
160
+ );
161
+ }
79
162
  return (
80
- <button
81
- type="button"
82
- className="calc-edge-label"
83
- title={`${matName ?? ''} ${fmt(flow)}${cap != null ? ` / ${fmt(cap)}` : ''}`}
84
- onClick={(e) => {
85
- e.stopPropagation();
86
- if (e.shiftKey) {
87
- onUnwire?.(wire.from, wire.to, wire.input, wire.out);
88
- return;
89
- }
90
- setEdit(cap != null ? String(cap) : '');
91
- }}
92
- >
93
- {matId && matKind && dataset ? (
94
- <Entry
95
- id={matId}
96
- kind={matKind}
97
- dataset={dataset}
98
- size="sm"
99
- href={false}
100
- tooltip={false}
101
- />
102
- ) : null}
103
- {matName ? (
104
- <span className="calc-edge-label__name">{matName}</span>
105
- ) : null}
106
- <span className="calc-edge-label__amount">
107
- ×{fmt(flow)}
108
- {cap != null ? `/${fmt(cap)}` : ''}
109
- </span>
110
- <span className="calc-edge-label__cut" aria-hidden>
111
- ×
112
- </span>
113
- </button>
163
+ <Tooltip>
164
+ <TooltipTrigger render={<div className="calc-edge-hotspot" />} />
165
+ <TooltipContent
166
+ side={vertical ? 'right' : 'top'}
167
+ className="flex-col items-start gap-1"
168
+ >
169
+ <span className="flex items-center gap-1.5">
170
+ {matId && matKind && dataset ? (
171
+ <Entry
172
+ id={matId}
173
+ kind={matKind}
174
+ dataset={dataset}
175
+ size="sm"
176
+ href={false}
177
+ tooltip={false}
178
+ />
179
+ ) : null}
180
+ {matName ? <span>{matName}</span> : null}
181
+ <span className="tabular-nums">×{full(flow)}</span>
182
+ </span>
183
+ {cap != null ? (
184
+ <span className="tabular-nums opacity-70">
185
+ {t('Limit', { note: 'recipe.calc.pipe.limit' })}: {full(cap)}
186
+ </span>
187
+ ) : null}
188
+ </TooltipContent>
189
+ </Tooltip>
114
190
  );
115
191
  }
116
192
 
193
+ function pinOrthoEnds(
194
+ points: readonly FlowPoint[],
195
+ s: FlowPoint,
196
+ t: FlowPoint,
197
+ ): FlowPoint[] {
198
+ const pts = points.slice();
199
+ pts[0] = s;
200
+ pts[pts.length - 1] = t;
201
+ if (pts.length === 2) {
202
+ if (s.x !== t.x && s.y !== t.y) {
203
+ const mid = (s.x + t.x) / 2;
204
+ pts.splice(1, 0, { x: mid, y: s.y }, { x: mid, y: t.y });
205
+ }
206
+ } else if (pts.length === 3) {
207
+ const m = pts[1]!;
208
+ pts.splice(1, 1, { x: m.x, y: s.y }, { x: m.x, y: t.y });
209
+ } else {
210
+ pts[1] = { x: pts[1]!.x, y: s.y };
211
+ pts[pts.length - 2] = { x: pts[pts.length - 2]!.x, y: t.y };
212
+ }
213
+ return orthoPoints(pts);
214
+ }
215
+
117
216
  export const CalcEdge = createChevronEdge({
217
+ casing: true,
118
218
  renderLabel: (ctx) => <EdgeAmountLabel ctx={ctx} />,
119
219
  labelVisibility: 'hover',
120
220
  resolvePath: (ctx) => {
121
221
  const points = ctx.data?.points;
122
222
  if (Array.isArray(points) && points.length >= 2) {
123
- const pts = (points as FlowPoint[]).slice();
124
- const oldA = pts[0]!;
125
- const oldB = pts[pts.length - 1]!;
126
- pts[0] = { x: ctx.sourceX, y: ctx.sourceY };
127
- pts[pts.length - 1] = { x: ctx.targetX, y: ctx.targetY };
128
- if (pts.length > 2) {
129
- const s2 = pts[1]!;
130
- const sL = pts[pts.length - 2]!;
131
- if (s2.x === oldA.x) s2.x = ctx.sourceX;
132
- if (s2.y === oldA.y) s2.y = ctx.sourceY;
133
- if (sL.x === oldB.x) sL.x = ctx.targetX;
134
- if (sL.y === oldB.y) sL.y = ctx.targetY;
135
- }
136
- if (
137
- pts.length === 2 &&
138
- pts[0].x !== pts[1].x &&
139
- pts[0].y !== pts[1].y
140
- ) {
141
- const mid = (pts[0].x + pts[1].x) / 2;
142
- pts.splice(1, 0, { x: mid, y: pts[0].y }, { x: mid, y: pts[1].y });
143
- }
144
- return svgPathToPoints(roundedPolylinePath(pts, 12), 8);
223
+ return pinOrthoEnds(
224
+ points as FlowPoint[],
225
+ { x: ctx.sourceX, y: ctx.sourceY },
226
+ { x: ctx.targetX, y: ctx.targetY },
227
+ );
145
228
  }
146
- return svgPathToPoints(
147
- getSmoothStepPath({
148
- sourceX: ctx.sourceX,
149
- sourceY: ctx.sourceY,
150
- sourcePosition: ctx.sourcePosition,
151
- targetX: ctx.targetX,
152
- targetY: ctx.targetY,
153
- targetPosition: ctx.targetPosition,
154
- borderRadius: 10,
155
- })[0],
229
+ return orthoPoints(
230
+ svgPathToPoints(
231
+ getSmoothStepPath({
232
+ sourceX: ctx.sourceX,
233
+ sourceY: ctx.sourceY,
234
+ sourcePosition: ctx.sourcePosition,
235
+ targetX: ctx.targetX,
236
+ targetY: ctx.targetY,
237
+ targetPosition: ctx.targetPosition,
238
+ borderRadius: 0,
239
+ })[0],
240
+ ),
156
241
  );
157
242
  },
158
243
  });
159
-
@@ -3,11 +3,11 @@
3
3
  import {
4
4
  memo,
5
5
  useCallback,
6
- useEffect,
7
6
  useMemo,
8
7
  useRef,
9
8
  useState,
10
9
  } from 'react';
10
+ import { createPortal } from 'react-dom';
11
11
  import {
12
12
  ReactFlowProvider,
13
13
  useReactFlow,
@@ -16,6 +16,12 @@ import {
16
16
  } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
17
17
  import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
18
18
  import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
19
+ import {
20
+ ContextMenu,
21
+ ContextMenuContent,
22
+ ContextMenuItem,
23
+ ContextMenuTrigger,
24
+ } from '@terrafirmagreg-team/ui/components/ui/context-menu';
19
25
  import { FlowCanvas } from '@terrafirmagreg-team/ui/components/blocks/canvas/flow-canvas';
20
26
  import {
21
27
  CALC_LOD_DOT,
@@ -27,6 +33,9 @@ import { CalcDotOverlay } from '@terrafirmagreg-team/recipe/components/views/cal
27
33
  import { EmptyState } from '@terrafirmagreg-team/ui/components/blocks/page/empty-state';
28
34
  import { CalcEdge } from '@terrafirmagreg-team/recipe/components/views/calc/edge';
29
35
  import { CalcSessionProvider } from '@terrafirmagreg-team/recipe/hooks/calc/use-calc-session';
36
+ import { CalcPipeEditProvider } from '@terrafirmagreg-team/recipe/hooks/calc/use-pipe-edit';
37
+ import { CalcMaterialPicker } from '@terrafirmagreg-team/recipe/components/views/calc/material-picker';
38
+ import { FlaskConical, Target } from 'lucide-react';
30
39
  import { CalcFarProvider } from '@terrafirmagreg-team/recipe/hooks/calc/use-far-mode';
31
40
  import { CalcRecipeIdsProvider } from '@terrafirmagreg-team/recipe/hooks/calc/use-recipe-ids';
32
41
  import { CalcTierProvider } from '@terrafirmagreg-team/recipe/hooks/calc/use-tier';
@@ -34,9 +43,11 @@ import { useFlowPickers } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-
34
43
  import { useFlowLayout } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-layout';
35
44
  import { useFlowNodes } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-nodes';
36
45
  import { useFlowViewport } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-viewport';
46
+ import { CalcNotes } from '@terrafirmagreg-team/recipe/components/views/calc/flow/notes';
37
47
  import { CalcToolbar } from '@terrafirmagreg-team/recipe/components/views/calc/flow/toolbar';
38
48
  import { CalcPickers } from '@terrafirmagreg-team/recipe/components/views/calc/flow/picker-dialogs';
39
49
  import { useFlowHandlers } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-handlers';
50
+ import { useShiftHeld } from '@terrafirmagreg-team/recipe/hooks/calc/flow/use-shift-held';
40
51
  import type { CalcFlowProps } from '@terrafirmagreg-team/recipe/types/calc-flow-props';
41
52
 
42
53
  const nodeTypes = { calcMaterial: CalcNodeCard };
@@ -44,31 +55,9 @@ const edgeTypes = { calcEdge: CalcEdge };
44
55
  const EMPTY_FLOW: AnyCalcFlowNode[] = [];
45
56
  const EMPTY_EDGES: Edge[] = [];
46
57
 
47
- function useShiftHeld() {
48
- const [held, setHeld] = useState(false);
49
- useEffect(() => {
50
- const down = (e: KeyboardEvent) => {
51
- if (e.key === 'Shift') setHeld(true);
52
- };
53
- const up = (e: KeyboardEvent) => {
54
- if (e.key === 'Shift') setHeld(false);
55
- };
56
- const blur = () => setHeld(false);
57
- window.addEventListener('keydown', down);
58
- window.addEventListener('keyup', up);
59
- window.addEventListener('blur', blur);
60
- return () => {
61
- window.removeEventListener('keydown', down);
62
- window.removeEventListener('keyup', up);
63
- window.removeEventListener('blur', blur);
64
- };
65
- }, []);
66
- return held;
67
- }
68
-
69
58
  function CalcFlowInner({
70
59
  nodes: schemeNodes,
71
- wires,
60
+ pipes,
72
61
  flows,
73
62
  cards,
74
63
  dataset,
@@ -80,6 +69,11 @@ function CalcFlowInner({
80
69
  onFarCardsChange,
81
70
  recipeIds,
82
71
  onRecipeIdsChange,
72
+ snap,
73
+ onSnapChange,
74
+ notes,
75
+ onAddNote,
76
+ history,
83
77
  treeReady = true,
84
78
  resetGen = 0,
85
79
  canvasId,
@@ -91,8 +85,34 @@ function CalcFlowInner({
91
85
  onRelayout,
92
86
  }: CalcFlowProps) {
93
87
  const t = useTranslations();
94
- const { zoomIn, zoomOut } = useReactFlow();
88
+ const { zoomIn, zoomOut, screenToFlowPosition } = useReactFlow();
89
+ const domNode = useStore((s) => s.domNode);
90
+ const shell = domNode?.parentElement ?? null;
91
+ const addNoteCenter = useCallback(() => {
92
+ if (!onAddNote) return;
93
+ const rect = domNode?.getBoundingClientRect();
94
+ onAddNote(
95
+ screenToFlowPosition({
96
+ x: rect ? rect.left + rect.width / 2 : window.innerWidth / 2,
97
+ y: rect ? rect.top + rect.height / 2 : window.innerHeight / 2,
98
+ }),
99
+ );
100
+ }, [onAddNote, domNode, screenToFlowPosition]);
95
101
  const shiftHeld = useShiftHeld();
102
+ const [pipeEditId, setPipeEditId] = useState<string | null>(null);
103
+ const pipeEdit = useMemo(
104
+ () => ({
105
+ edgeId: pipeEditId,
106
+ open: setPipeEditId,
107
+ close: () => setPipeEditId(null),
108
+ }),
109
+ [pipeEditId],
110
+ );
111
+ const addPosRef = useRef<{ x: number; y: number } | null>(null);
112
+ const [matPick, setMatPick] = useState<{
113
+ x: number;
114
+ y: number;
115
+ } | null>(null);
96
116
  const {
97
117
  pickerNode,
98
118
  setPickerNode,
@@ -122,7 +142,7 @@ function CalcFlowInner({
122
142
  );
123
143
  const { rawFlow, positioned, relayout } = useFlowLayout({
124
144
  nodes: schemeNodes,
125
- wires,
145
+ pipes,
126
146
  flows,
127
147
  cards,
128
148
  data,
@@ -221,6 +241,8 @@ function CalcFlowInner({
221
241
  setMemberNode(null);
222
242
  setSupply(null);
223
243
  setDemand(null);
244
+ setPipeEditId(null);
245
+ setMatPick(null);
224
246
  }
225
247
 
226
248
  const {
@@ -242,6 +264,8 @@ function CalcFlowInner({
242
264
  applyEngaged,
243
265
  centerOnNode,
244
266
  setPickerNode,
267
+ openPipeEdit: pipeEdit.open,
268
+ closePipeEdit: pipeEdit.close,
245
269
  });
246
270
  const closePicker = useCallback(() => setPickerNode(null), [setPickerNode]);
247
271
  const closeMember = useCallback(() => setMemberNode(null), [setMemberNode]);
@@ -256,6 +280,14 @@ function CalcFlowInner({
256
280
  <CalcFarProvider value={farCards}>
257
281
  <CalcRecipeIdsProvider value={recipeIds}>
258
282
  <CalcTierProvider value={tier}>
283
+ <ContextMenu>
284
+ <ContextMenuTrigger
285
+ className="contents"
286
+ onContextMenu={(e) => {
287
+ addPosRef.current = { x: e.clientX, y: e.clientY };
288
+ }}
289
+ >
290
+ <CalcPipeEditProvider value={pipeEdit}>
259
291
  <FlowCanvas<AnyCalcFlowNode>
260
292
  shellProps={shellProps}
261
293
  className={cn(
@@ -286,6 +318,8 @@ function CalcFlowInner({
286
318
  onEdgeClick={handleEdgeClick}
287
319
  onEdgesDelete={handleEdgesDelete}
288
320
  deleteKeyCode={['Backspace', 'Delete']}
321
+ snapToGrid={snap}
322
+ snapGrid={[16, 16]}
289
323
  nodesDraggable={tier !== 'dot'}
290
324
  nodesConnectable={tier === 'near'}
291
325
  selectionKeyCode="Shift"
@@ -299,19 +333,35 @@ function CalcFlowInner({
299
333
  {tier === 'dot' ? (
300
334
  <CalcDotOverlay nodes={mergedFlow.nodes} focusId={focusId} />
301
335
  ) : null}
302
- <CalcToolbar
303
- onFit={onFit}
304
- onRelayout={runRelayout}
305
- onZoomIn={() => void zoomIn({ duration: 150 })}
306
- onZoomOut={() => void zoomOut({ duration: 150 })}
307
- actions={actions}
308
- mode={mode}
309
- onModeChange={onModeChange}
310
- farCards={farCards}
311
- onFarCardsChange={onFarCardsChange}
312
- recipeIds={recipeIds}
313
- onRecipeIdsChange={onRecipeIdsChange}
314
- />
336
+ <CalcNotes notes={notes} snap={snap} data={flowData} />
337
+ {shell
338
+ ? createPortal(
339
+ <>
340
+ <CalcToolbar
341
+ onFit={onFit}
342
+ onRelayout={runRelayout}
343
+ onZoomIn={() => void zoomIn({ duration: 150 })}
344
+ onZoomOut={() => void zoomOut({ duration: 150 })}
345
+ onAddNote={onAddNote ? addNoteCenter : undefined}
346
+ actions={actions}
347
+ mode={mode}
348
+ onModeChange={onModeChange}
349
+ farCards={farCards}
350
+ onFarCardsChange={onFarCardsChange}
351
+ recipeIds={recipeIds}
352
+ onRecipeIdsChange={onRecipeIdsChange}
353
+ snap={snap}
354
+ onSnapChange={onSnapChange}
355
+ history={history}
356
+ />
357
+ <div className="pointer-events-auto absolute left-2 top-2 z-10 flex items-center gap-1 rounded-md border border-border bg-background/80 px-1.5 py-0.5 text-[10px] font-medium text-muted-foreground shadow-sm backdrop-blur-sm">
358
+ <FlaskConical className="size-3" />
359
+ {t('Experimental', { note: 'recipe.calc.experimental' })}
360
+ </div>
361
+ </>,
362
+ shell,
363
+ )
364
+ : null}
315
365
  {schemeNodes.length ? (
316
366
  ready ? null : (
317
367
  <div className="pointer-events-none absolute inset-x-0 bottom-4 z-10 flex justify-center">
@@ -324,6 +374,37 @@ function CalcFlowInner({
324
374
  </>
325
375
  )}
326
376
  </FlowCanvas>
377
+ </CalcPipeEditProvider>
378
+ </ContextMenuTrigger>
379
+ <ContextMenuContent>
380
+ <ContextMenuItem
381
+ className="whitespace-nowrap"
382
+ onClick={() => {
383
+ const pos = addPosRef.current;
384
+ setMatPick(pos ? screenToFlowPosition(pos) : null);
385
+ }}
386
+ >
387
+ <Target />
388
+ {t('Add card', { note: 'recipe.calc.add.goal' })}
389
+ </ContextMenuItem>
390
+ </ContextMenuContent>
391
+ </ContextMenu>
392
+ {matPick ? (
393
+ <CalcMaterialPicker
394
+ open
395
+ onOpenChange={(open) => {
396
+ if (!open) setMatPick(null);
397
+ }}
398
+ index={session.index}
399
+ dataset={dataset}
400
+ lang={session.lang}
401
+ langMap={session.langMap}
402
+ onSelect={(id, kind) => {
403
+ data.onAddCard(id, kind, matPick);
404
+ setMatPick(null);
405
+ }}
406
+ />
407
+ ) : null}
327
408
  <CalcPickers
328
409
  pickerNode={pickerNode}
329
410
  memberNode={memberNode}