@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
@@ -18,11 +18,16 @@ export function formatPortCount(value: number, kind: CalcKind): string {
18
18
  return formatCount(value, 1_000);
19
19
  }
20
20
 
21
- export function portAmountLine(value: number, kind: CalcKind): ReactNode {
21
+ export function portAmountLine(
22
+ value: number,
23
+ kind: CalcKind,
24
+ chance?: number,
25
+ ): ReactNode {
22
26
  return (
23
27
  <p className="text-muted-foreground">
24
28
  {value.toLocaleString('en-US')}
25
29
  {kind === 'fluid' ? ' mB' : ''}
30
+ {chance != null ? ` · ${Math.round(chance * 100)}%` : ''}
26
31
  </p>
27
32
  );
28
33
  }
@@ -11,12 +11,16 @@ import {
11
11
  DialogTitle,
12
12
  } from '@terrafirmagreg-team/ui/components/ui/dialog';
13
13
  import { Button } from '@terrafirmagreg-team/ui/components/ui/button';
14
- import { PagerArrow } from '@terrafirmagreg-team/ui/components/blocks/page/pager';
15
14
  import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
16
15
  import { RecipeFrame } from '@terrafirmagreg-team/recipe/components/views/browser/frame';
17
16
  import { RecipeCardGrid } from '@terrafirmagreg-team/recipe/components/views/browser/card-grid';
18
17
  import { recipeCardFitDim } from '@terrafirmagreg-team/recipe/lib/browser/card-dim';
19
18
  import { RecipeTypeTabs } from '@terrafirmagreg-team/recipe/components/views/browser/type-tabs';
19
+ import {
20
+ rowMatchesTerms,
21
+ rowSearchTerms,
22
+ } from '@terrafirmagreg-team/recipe/lib/browser/row-search';
23
+ import { recipeItemName } from '@terrafirmagreg-team/recipe/lib/client-index';
20
24
  import { PlaceHeader } from '@terrafirmagreg-team/recipe/components/views/browser/place-header';
21
25
  import { WorkstationRail } from '@terrafirmagreg-team/recipe/components/views/browser/workstation-rail';
22
26
  import type { CalcFlowData } from '@terrafirmagreg-team/recipe/lib/calc/flow';
@@ -29,7 +33,6 @@ export function PickerBody({
29
33
  onSelectRecipe,
30
34
  onSelectTagMember,
31
35
  recipeDefaults,
32
- onSetRecipeDefault,
33
36
  actions,
34
37
  close,
35
38
  }: {
@@ -53,6 +56,7 @@ export function PickerBody({
53
56
  const [tab, setTab] = useState<string | null>(null);
54
57
  const [page, setPage] = useState(0);
55
58
  const [pinned, setPinned] = useState(true);
59
+ const [query, setQuery] = useState('');
56
60
  const recipeKey = `${node.materialId}${node.recipe?.recipeId ?? ''}`;
57
61
  const [seenRecipeKey, setSeenRecipeKey] = useState(recipeKey);
58
62
  if (seenRecipeKey !== recipeKey) {
@@ -60,6 +64,7 @@ export function PickerBody({
60
64
  setTab(null);
61
65
  setPinned(true);
62
66
  setPage(0);
67
+ setQuery('');
63
68
  }
64
69
  const defaultRecipeId = recipeDefaults[node.materialId];
65
70
  const active =
@@ -70,16 +75,24 @@ export function PickerBody({
70
75
  recipes.find((row) => row.recipeId === defaultRecipeId)?.category ??
71
76
  categories[0]);
72
77
  const categoriesById = useCategoriesById(index);
78
+ const terms = useMemo(() => rowSearchTerms(query), [query]);
73
79
  const rows = useMemo(
74
80
  () =>
75
81
  recipes
76
- .filter((row) => row.category === active)
82
+ .filter(
83
+ (row) =>
84
+ row.category === active &&
85
+ (!terms.length ||
86
+ rowMatchesTerms(row.row, terms, (id) =>
87
+ recipeItemName(session.names, lang, id),
88
+ )),
89
+ )
77
90
  .sort((a, b) =>
78
91
  (b.recipeId === defaultRecipeId ? 1 : 0) -
79
92
  (a.recipeId === defaultRecipeId ? 1 : 0),
80
93
  )
81
94
  .map((row) => row.row),
82
- [recipes, active, defaultRecipeId],
95
+ [recipes, active, terms, defaultRecipeId, session.names, lang],
83
96
  );
84
97
  const boxRef = useRef<HTMLDivElement | null>(null);
85
98
  const focusIndex = rows.findIndex(
@@ -100,14 +113,6 @@ export function PickerBody({
100
113
  setPinned(false);
101
114
  setPage(next);
102
115
  };
103
- const goCategory = (dir: -1 | 1) => {
104
- const cur = categories.indexOf(active ?? '');
105
- const next =
106
- categories[(cur + dir + categories.length) % categories.length];
107
- if (!next || next === active) return;
108
- setTab(next);
109
- goPage(0);
110
- };
111
116
  const custom =
112
117
  !!node.recipe &&
113
118
  node.recipe.recipeId !== (defaultRecipeId ?? recipes[0]?.recipeId);
@@ -118,6 +123,8 @@ export function PickerBody({
118
123
  const frameSession = {
119
124
  ...session,
120
125
  place: active ? { kind: 'type' as const, id: active } : session.place,
126
+ rquery: query,
127
+ setRquery: setQuery,
121
128
  };
122
129
  const gridSession = { ...frameSession, recipeId: node.recipe?.recipeId };
123
130
 
@@ -170,25 +177,8 @@ export function PickerBody({
170
177
  }}
171
178
  />
172
179
  }
173
- navPrev={
174
- <PagerArrow
175
- dir={-1}
176
- label={t('Previous category', { note: 'recipe.axis.prev' })}
177
- disabled={categories.length < 2}
178
- onClick={() => goCategory(-1)}
179
- />
180
- }
181
- navNext={
182
- <PagerArrow
183
- dir={1}
184
- label={t('Next category', { note: 'recipe.axis.next' })}
185
- disabled={categories.length < 2}
186
- onClick={() => goCategory(1)}
187
- />
188
- }
189
- onNavWheel={goCategory}
190
180
  header={
191
- <div className="flex min-w-0 justify-center">
181
+ <div className="mx-auto w-full max-w-90">
192
182
  <PlaceHeader
193
183
  session={frameSession}
194
184
  overline={t('Categories', { note: 'recipe.axis_type' })}
@@ -204,6 +194,8 @@ export function PickerBody({
204
194
  />
205
195
  }
206
196
  onPageChange={goPage}
197
+ search
198
+ searchPersist={false}
207
199
  >
208
200
  <RecipeCardGrid
209
201
  session={gridSession}
@@ -230,8 +222,6 @@ export function PickerBody({
230
222
  dataset={dataset}
231
223
  lang={lang}
232
224
  langMap={langMap}
233
- recipeDefaults={recipeDefaults}
234
- onSetRecipeDefault={onSetRecipeDefault}
235
225
  onSelectTagMember={onSelectTagMember}
236
226
  />
237
227
  </>
@@ -6,8 +6,6 @@ import { ListFilter } from 'lucide-react';
6
6
  import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
7
7
  import { Button } from '@terrafirmagreg-team/ui/components/ui/button';
8
8
  import { Hint } from '@terrafirmagreg-team/ui/components/blocks/tooltip/hint';
9
- import { Checkbox } from '@terrafirmagreg-team/ui/components/ui/checkbox';
10
- import { cn } from '@terrafirmagreg-team/ui/lib/utils/cn';
11
9
  import { formatCount } from '@terrafirmagreg-team/ui/lib/utils/format';
12
10
  import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
13
11
  import { CalcMemberList } from '@terrafirmagreg-team/recipe/components/views/calc/member-picker';
@@ -20,8 +18,6 @@ export function PickerFooter({
20
18
  dataset,
21
19
  lang,
22
20
  langMap,
23
- recipeDefaults,
24
- onSetRecipeDefault,
25
21
  onSelectTagMember,
26
22
  }: {
27
23
  node: CalcFlowData['node'];
@@ -29,15 +25,13 @@ export function PickerFooter({
29
25
  dataset: DatasetRef;
30
26
  lang: Lang;
31
27
  langMap?: Record<string, string>;
32
- recipeDefaults: CalcFlowData['recipeDefaults'];
33
- onSetRecipeDefault: CalcFlowData['onSetRecipeDefault'];
34
28
  onSelectTagMember: CalcFlowData['onSelectTagMember'];
35
29
  }) {
36
30
  const t = useTranslations();
37
31
  const [memberPickerOpen, setMemberPickerOpen] = useState(false);
38
32
  return (
39
33
  <>
40
- {members || node.recipe ? (
34
+ {members ? (
41
35
  <div className="flex shrink-0 items-center gap-2 rounded-md border border-border bg-card px-2 py-1.5">
42
36
  {members ? (
43
37
  <>
@@ -72,27 +66,6 @@ export function PickerFooter({
72
66
  </Button>
73
67
  </>
74
68
  ) : null}
75
- {node.recipe ? (
76
- <label
77
- className={cn(
78
- 'flex shrink-0 cursor-pointer select-none items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground',
79
- members ? 'ms-auto' : 'w-full',
80
- )}
81
- >
82
- <Checkbox
83
- checked={
84
- recipeDefaults[node.materialId] === node.recipe.recipeId
85
- }
86
- onCheckedChange={(checked) =>
87
- onSetRecipeDefault(
88
- node.materialId,
89
- checked ? node.recipe!.recipeId : null,
90
- )
91
- }
92
- />
93
- {t('Pin as default recipe', { note: 'recipe.calc.set_default' })}
94
- </label>
95
- ) : null}
96
69
  </div>
97
70
  ) : null}
98
71
  {memberPickerOpen && members ? (
@@ -27,6 +27,8 @@ export function useFlowHandlers({
27
27
  applyEngaged,
28
28
  centerOnNode,
29
29
  setPickerNode,
30
+ openPipeEdit,
31
+ closePipeEdit,
30
32
  }: {
31
33
  data: Omit<
32
34
  CalcFlowData,
@@ -36,6 +38,8 @@ export function useFlowHandlers({
36
38
  applyEngaged: (id: string | null) => void;
37
39
  centerOnNode: (node: AnyCalcFlowNode) => void;
38
40
  setPickerNode: (node: CalcNode | null) => void;
41
+ openPipeEdit: (edgeId: string) => void;
42
+ closePipeEdit: () => void;
39
43
  }) {
40
44
  const { screenToFlowPosition, getNode } = useReactFlow();
41
45
  const slotAccepts = useCallback(
@@ -81,10 +85,10 @@ export function useFlowHandlers({
81
85
  },
82
86
  [setPickerNode],
83
87
  );
84
- const handlePaneClick = useCallback(
85
- () => applyFocus(null),
86
- [applyFocus],
87
- );
88
+ const handlePaneClick = useCallback(() => {
89
+ applyFocus(null);
90
+ closePipeEdit();
91
+ }, [applyFocus, closePipeEdit]);
88
92
  const handleDragOver = useCallback((event: DragEvent) => {
89
93
  if (event.dataTransfer.types.includes('application/x-tfg-calc')) {
90
94
  event.preventDefault();
@@ -128,19 +132,19 @@ export function useFlowHandlers({
128
132
  const slot = tNode?.inputs[input];
129
133
  const srcOut = sNode?.outputs[out];
130
134
  if (!sNode || !tNode || !slotAccepts(slot, srcOut)) return;
131
- data.onWire(source, target, input, out);
135
+ data.onConnect(source, target, input, out);
132
136
  },
133
137
  [data, getNode, slotAccepts],
134
138
  );
135
139
 
136
140
  const handleReconnect = useCallback(
137
141
  (oldEdge: Edge, conn: Connection) => {
138
- const wire = oldEdge.data?.wire as
142
+ const pipe = oldEdge.data?.pipe as
139
143
  | { from: string; to: string; input: number; out?: number }
140
144
  | undefined;
141
145
  const input = Number(/^in-(\d+)$/.exec(conn.targetHandle ?? '')?.[1] ?? -1);
142
146
  const outMatch = /^out(?:-(\d+))?$/.exec(conn.sourceHandle ?? '');
143
- if (!wire || !conn.source || !conn.target || input < 0 || !outMatch)
147
+ if (!pipe || !conn.source || !conn.target || input < 0 || !outMatch)
144
148
  return;
145
149
  const out = Number(outMatch[1] ?? 0);
146
150
  if (conn.source === conn.target) return;
@@ -149,7 +153,7 @@ export function useFlowHandlers({
149
153
  const slot = tNode?.inputs[input];
150
154
  const srcOut = sNode?.outputs[out];
151
155
  if (!sNode || !tNode || !slotAccepts(slot, srcOut)) return;
152
- data.onRewire(wire.from, wire.to, wire.input, {
156
+ data.onReroute(pipe.from, pipe.to, pipe.input, {
153
157
  from: conn.source,
154
158
  to: conn.target,
155
159
  input,
@@ -162,10 +166,10 @@ export function useFlowHandlers({
162
166
  const handleEdgesDelete = useCallback(
163
167
  (edges: Edge[]) => {
164
168
  for (const edge of edges) {
165
- const wire = edge.data?.wire as
169
+ const pipe = edge.data?.pipe as
166
170
  | { from: string; to: string; input: number; out?: number }
167
171
  | undefined;
168
- if (wire) data.onUnwire(wire.from, wire.to, wire.input, wire.out);
172
+ if (pipe) data.onDisconnect(pipe.from, pipe.to, pipe.input, pipe.out);
169
173
  }
170
174
  },
171
175
  [data],
@@ -173,13 +177,16 @@ export function useFlowHandlers({
173
177
 
174
178
  const handleEdgeClick = useCallback(
175
179
  (event: MouseEvent, edge: Edge) => {
176
- if (!event.shiftKey) return;
177
- const wire = edge.data?.wire as
180
+ const pipe = edge.data?.pipe as
178
181
  | { from: string; to: string; input: number; out?: number }
179
182
  | undefined;
180
- if (wire) data.onUnwire(wire.from, wire.to, wire.input, wire.out);
183
+ if (event.shiftKey) {
184
+ if (pipe) data.onDisconnect(pipe.from, pipe.to, pipe.input, pipe.out);
185
+ return;
186
+ }
187
+ if (pipe) openPipeEdit(edge.id);
181
188
  },
182
- [data],
189
+ [data, openPipeEdit],
183
190
  );
184
191
 
185
192
  return {
@@ -13,12 +13,12 @@ import { layoutCards } from '@terrafirmagreg-team/recipe/lib/calc/pack';
13
13
  import type {
14
14
  CalcCard,
15
15
  CalcNode,
16
- CalcWire,
16
+ CalcPipe,
17
17
  } from '@terrafirmagreg-team/recipe/types/calc';
18
18
 
19
19
  export function useFlowLayout({
20
20
  nodes,
21
- wires,
21
+ pipes,
22
22
  flows,
23
23
  cards,
24
24
  data,
@@ -29,7 +29,7 @@ export function useFlowLayout({
29
29
  onRequestDemand,
30
30
  }: {
31
31
  nodes: CalcNode[];
32
- wires: CalcWire[];
32
+ pipes: CalcPipe[];
33
33
  flows?: readonly (number | undefined)[];
34
34
  cards: CalcCard[];
35
35
  data: Omit<
@@ -68,8 +68,8 @@ export function useFlowLayout({
68
68
  ],
69
69
  );
70
70
  const rawFlow = useMemo(
71
- () => cardsToFlow(nodes, wires, flowData, flows),
72
- [nodes, wires, flowData, flows],
71
+ () => cardsToFlow(nodes, pipes, flowData, flows),
72
+ [nodes, pipes, flowData, flows],
73
73
  );
74
74
 
75
75
  const storedPos = useMemo(() => {
@@ -97,12 +97,12 @@ export function useFlowLayout({
97
97
  }, [rawFlow, storedPos]);
98
98
 
99
99
  const relayout = useCallback(
100
- () => layoutCards(nodes, wires, {
100
+ () => layoutCards(nodes, pipes, {
101
101
  mode,
102
102
  gaps: GRID_GAPS,
103
103
  recipeBody: data.recipeBody,
104
104
  }),
105
- [nodes, wires, mode, data.recipeBody],
105
+ [nodes, pipes, mode, data.recipeBody],
106
106
  );
107
107
 
108
108
  return { rawFlow, positioned, relayout };
@@ -0,0 +1,25 @@
1
+ 'use client';
2
+
3
+ import { useEffect, useState } from 'react';
4
+
5
+ export function useShiftHeld() {
6
+ const [held, setHeld] = useState(false);
7
+ useEffect(() => {
8
+ const down = (e: KeyboardEvent) => {
9
+ if (e.key === 'Shift') setHeld(true);
10
+ };
11
+ const up = (e: KeyboardEvent) => {
12
+ if (e.key === 'Shift') setHeld(false);
13
+ };
14
+ const blur = () => setHeld(false);
15
+ window.addEventListener('keydown', down);
16
+ window.addEventListener('keyup', up);
17
+ window.addEventListener('blur', blur);
18
+ return () => {
19
+ window.removeEventListener('keydown', down);
20
+ window.removeEventListener('keyup', up);
21
+ window.removeEventListener('blur', blur);
22
+ };
23
+ }, []);
24
+ return held;
25
+ }