@terrafirmagreg-team/recipe 1.2.37 → 1.2.39

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 (28) hide show
  1. package/package.json +3 -3
  2. package/src/components/cards/card/text.tsx +3 -2
  3. package/src/components/cards/scroll-card/index.tsx +16 -3
  4. package/src/components/cards/slot-hitbox.tsx +7 -4
  5. package/src/components/cards/strip-card/member-cell.tsx +4 -2
  6. package/src/components/views/browser/card-grid.tsx +2 -1
  7. package/src/components/views/browser/frame.tsx +73 -33
  8. package/src/components/views/browser/item-view/index.tsx +12 -9
  9. package/src/components/views/browser/item-view/tags.tsx +3 -3
  10. package/src/components/views/browser/query-controls.tsx +1 -1
  11. package/src/components/views/browser/type-view.tsx +40 -1
  12. package/src/components/views/calc/calculator/actions.tsx +34 -5
  13. package/src/components/views/calc/calculator/index.tsx +22 -8
  14. package/src/components/views/calc/edge.tsx +11 -4
  15. package/src/components/views/calc/node-card/badges.tsx +17 -3
  16. package/src/components/views/calc/node-card/full.tsx +117 -37
  17. package/src/components/views/calc/node-card/menu.tsx +12 -1
  18. package/src/components/views/calc/node-card/port-cells.tsx +13 -8
  19. package/src/components/views/calc/node-card/ports.tsx +39 -13
  20. package/src/components/views/calc/recipe-picker/body.tsx +34 -1
  21. package/src/components/views/calc/sidebar.tsx +8 -2
  22. package/src/components/views/calc/trees/label.tsx +9 -5
  23. package/src/hooks/calc/use-actions.ts +26 -3
  24. package/src/hooks/calc/use-amounts.ts +32 -5
  25. package/src/hooks/calc/use-data.ts +2 -1
  26. package/src/hooks/calc/use-share.ts +15 -8
  27. package/src/hooks/calc/use-storage.ts +2 -1
  28. package/src/lib/calc/flow.ts +6 -1
@@ -19,6 +19,11 @@ import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
19
19
  import { StackCount } from '@terrafirmagreg-team/game/components/entry/stack-count';
20
20
  import { PagedCard } from '@terrafirmagreg-team/recipe/components/cards/paged-card';
21
21
  import { Skeleton } from '@terrafirmagreg-team/ui/components/ui/skeleton';
22
+ import { Hint } from '@terrafirmagreg-team/ui/components/blocks/tooltip/hint';
23
+ import {
24
+ formatCount,
25
+ formatFluidAmount,
26
+ } from '@terrafirmagreg-team/ui/lib/utils/format';
22
27
  import { useCalcSession } from '@terrafirmagreg-team/recipe/hooks/calc/use-calc-session';
23
28
  import { useCalcFar } from '@terrafirmagreg-team/recipe/hooks/calc/use-far-mode';
24
29
  import { useCalcTier } from '@terrafirmagreg-team/recipe/hooks/calc/use-tier';
@@ -38,6 +43,17 @@ import { NodeActions } from './actions';
38
43
  import { PortRailIn, PortRailOut } from './port-rails';
39
44
  import { NodeContextMenu, NodeHoverContent } from './menu';
40
45
 
46
+ const CRAFTS_SUFFIX: Record<string, number> = { k: 1e3, m: 1e6, b: 1e9 };
47
+
48
+ function parseCraftsInput(raw: string): number | null {
49
+ const match = /^(\d+(?:\.\d+)?)\s*([kmb])?$/i.exec(raw.trim());
50
+ if (!match) return null;
51
+ const value = Math.round(
52
+ Number(match[1]) * (CRAFTS_SUFFIX[match[2]?.toLowerCase() ?? ''] ?? 1),
53
+ );
54
+ return Number.isFinite(value) ? Math.max(1, value) : null;
55
+ }
56
+
41
57
  export const CalcNodeCardFull = memo(function CalcNodeCardFull({
42
58
  data,
43
59
  id,
@@ -112,6 +128,25 @@ export const CalcNodeCardFull = memo(function CalcNodeCardFull({
112
128
  [id],
113
129
  );
114
130
 
131
+ const crafts = data.pendingCrafts?.get(id) ?? node.crafts;
132
+ const fmtAmount =
133
+ node.kind === 'fluid'
134
+ ? formatFluidAmount
135
+ : (n: number) => formatCount(n, 1_000);
136
+ const producedTip = t('Surplus: {produced} produced, {allocated} wired, {remaining} left', {
137
+ note: 'recipe.calc.hint.produced',
138
+ })
139
+ .replaceAll('{produced}', fmtAmount(node.produced))
140
+ .replaceAll('{allocated}', fmtAmount(node.allocated))
141
+ .replaceAll('{remaining}', fmtAmount(Math.max(0, node.remaining)));
142
+ const producedBadge = (
143
+ <Hint label={producedTip} side="top">
144
+ <span className="pointer-events-auto">
145
+ <StackCount count={node.produced} format={fmtAmount} />
146
+ </span>
147
+ </Hint>
148
+ );
149
+
115
150
  const skeleton =
116
151
  mode === 'detailed' && node.recipe != null && !cardOn;
117
152
  const stateCls = cn(
@@ -143,18 +178,22 @@ export const CalcNodeCardFull = memo(function CalcNodeCardFull({
143
178
  >
144
179
  {mode === 'mini' ? (
145
180
  <div className="mx-3 flex w-auto flex-col items-center gap-0.5">
146
- <Entry
147
- id={node.materialId}
148
- kind={node.kind}
149
- dataset={dataset}
150
- lang={lang}
151
- langMap={langMap}
152
- name={name}
153
- size="lg"
154
- href={false}
155
- tooltip="adaptive"
156
- badge={<StackCount count={node.produced} />}
157
- />
181
+ <span className="relative inline-flex">
182
+ <Entry
183
+ id={node.materialId}
184
+ kind={node.kind}
185
+ dataset={dataset}
186
+ lang={lang}
187
+ langMap={langMap}
188
+ name={name}
189
+ size="lg"
190
+ href={false}
191
+ tooltip="adaptive"
192
+ />
193
+ <span className="pointer-events-none absolute -bottom-0.5 left-[34px] z-10 -translate-x-full">
194
+ {producedBadge}
195
+ </span>
196
+ </span>
158
197
  <span className="w-full truncate px-0.5 text-center text-[9px] leading-tight text-muted-foreground">
159
198
  {name}
160
199
  </span>
@@ -174,19 +213,23 @@ export const CalcNodeCardFull = memo(function CalcNodeCardFull({
174
213
  ) : (
175
214
  <>
176
215
  <div className="mx-1 flex min-w-0 items-center gap-1.5">
177
- <Entry
178
- id={node.materialId}
179
- kind={node.kind}
180
- dataset={dataset}
181
- lang={lang}
182
- langMap={langMap}
183
- size="lg"
184
- labeled
185
- href={false}
186
- tooltip="adaptive"
187
- badge={<StackCount count={node.produced} />}
188
- className="min-w-0 flex-1 justify-start truncate"
189
- />
216
+ <span className="relative min-w-0 flex-1">
217
+ <Entry
218
+ id={node.materialId}
219
+ kind={node.kind}
220
+ dataset={dataset}
221
+ lang={lang}
222
+ langMap={langMap}
223
+ size="lg"
224
+ labeled
225
+ href={false}
226
+ tooltip="adaptive"
227
+ className="min-w-0 flex-1 justify-start truncate"
228
+ />
229
+ <span className="pointer-events-none absolute -bottom-0.5 left-[34px] z-10 -translate-x-full">
230
+ {producedBadge}
231
+ </span>
232
+ </span>
190
233
  <NodeChips
191
234
  node={node}
192
235
  mini={false}
@@ -203,36 +246,55 @@ export const CalcNodeCardFull = memo(function CalcNodeCardFull({
203
246
  <div className="mx-1 flex min-w-0 items-center gap-1">
204
247
  {interactive ? (
205
248
  <span className="nodrag ml-auto flex shrink-0 items-center gap-0.5">
249
+ {[2, 4].map((div) => (
250
+ <button
251
+ key={div}
252
+ type="button"
253
+ aria-label={
254
+ div === 2
255
+ ? t('Halve crafts', { note: 'recipe.calc.crafts.d2' })
256
+ : t('Quarter crafts', { note: 'recipe.calc.crafts.d4' })
257
+ }
258
+ className="grid h-4 min-w-4 place-items-center rounded border border-border bg-background px-px text-[8px] font-medium leading-none text-muted-foreground hover:text-foreground"
259
+ onClick={(e) => {
260
+ e.stopPropagation();
261
+ onCrafts(id, (crafts) => Math.max(1, Math.round(crafts / div)));
262
+ }}
263
+ >
264
+ ÷{div}
265
+ </button>
266
+ ))}
206
267
  <button
207
268
  type="button"
208
269
  aria-label={t('Fewer crafts', { note: 'recipe.calc.crafts.down' })}
209
270
  className="grid size-4 place-items-center rounded border border-border bg-background text-muted-foreground hover:text-foreground"
210
271
  onClick={(e) => {
211
272
  e.stopPropagation();
212
- onCrafts(id, Math.max(1, node.crafts - 1));
273
+ onCrafts(id, (crafts) => Math.max(1, crafts - 1));
213
274
  }}
214
275
  >
215
276
  <Minus className="size-2.5" />
216
277
  </button>
217
278
  <input
218
- key={node.crafts}
279
+ key={crafts}
219
280
  type="text"
220
281
  inputMode="numeric"
221
- defaultValue={node.crafts}
222
- title={t('Crafts', { note: 'recipe.calc.crafts.title' })}
282
+ defaultValue={crafts}
283
+ size={Math.min(9, Math.max(3, String(crafts).length))}
284
+ title={`${t('Crafts', { note: 'recipe.calc.crafts.title' })}: ${String(crafts)}`}
223
285
  aria-label={t('Crafts', { note: 'recipe.calc.crafts.title' })}
224
- className="w-9 rounded border border-transparent bg-transparent text-center tabular-nums hover:border-border focus:border-border focus:outline-none"
286
+ className="w-auto min-w-9 rounded border border-border bg-transparent text-center tabular-nums focus:outline-none"
225
287
  onClick={(e) => e.stopPropagation()}
226
288
  onKeyDown={(e) => {
227
289
  if (e.key === 'Enter') e.currentTarget.blur();
228
290
  }}
229
291
  onBlur={(e) => {
230
- const next = Math.max(
231
- 1,
232
- Math.round(Number(e.target.value) || 1),
233
- );
234
- if (next !== node.crafts) onCrafts(id, next);
235
- else e.target.value = String(node.crafts);
292
+ const value = parseCraftsInput(e.target.value);
293
+ if (value == null) {
294
+ e.target.value = String(crafts);
295
+ return;
296
+ }
297
+ onCrafts(id, value);
236
298
  }}
237
299
  />
238
300
  <button
@@ -241,11 +303,29 @@ export const CalcNodeCardFull = memo(function CalcNodeCardFull({
241
303
  className="grid size-4 place-items-center rounded border border-border bg-background text-muted-foreground hover:text-foreground"
242
304
  onClick={(e) => {
243
305
  e.stopPropagation();
244
- onCrafts(id, node.crafts + 1);
306
+ onCrafts(id, (crafts) => crafts + 1);
245
307
  }}
246
308
  >
247
309
  <Plus className="size-2.5" />
248
310
  </button>
311
+ {[2, 4].map((mult) => (
312
+ <button
313
+ key={mult}
314
+ type="button"
315
+ aria-label={
316
+ mult === 2
317
+ ? t('Double crafts', { note: 'recipe.calc.crafts.x2' })
318
+ : t('Quadruple crafts', { note: 'recipe.calc.crafts.x4' })
319
+ }
320
+ className="grid h-4 min-w-4 place-items-center rounded border border-border bg-background px-px text-[8px] font-medium leading-none text-muted-foreground hover:text-foreground"
321
+ onClick={(e) => {
322
+ e.stopPropagation();
323
+ onCrafts(id, (crafts) => crafts * mult);
324
+ }}
325
+ >
326
+ ×{mult}
327
+ </button>
328
+ ))}
249
329
  </span>
250
330
  ) : null}
251
331
  </div>
@@ -4,6 +4,7 @@ import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
4
4
  import {
5
5
  ExternalLink,
6
6
  ListFilter,
7
+ Mountain,
7
8
  MoveRight,
8
9
  Pin,
9
10
  PinOff,
@@ -166,6 +167,7 @@ export function PortContextMenu({
166
167
  onRequestPicker,
167
168
  onRequestSupply,
168
169
  onRequestDemand,
170
+ onSupplyInput,
169
171
  } = data;
170
172
  const t = useTranslations();
171
173
  return (
@@ -186,13 +188,22 @@ export function PortContextMenu({
186
188
  </ContextMenuLabel>
187
189
  </ContextMenuGroup>
188
190
  <ContextMenuSeparator />
191
+ {input != null ? (
192
+ <ContextMenuItem
193
+ className="whitespace-nowrap"
194
+ onClick={() => onSupplyInput(node.id, input, null)}
195
+ >
196
+ <Mountain />
197
+ {t('Keep as external source', { note: 'recipe.calc.supply.source' })}
198
+ </ContextMenuItem>
199
+ ) : null}
189
200
  {input != null ? (
190
201
  <ContextMenuItem
191
202
  className="whitespace-nowrap"
192
203
  onClick={() => onRequestSupply(node, input)}
193
204
  >
194
205
  <Plus />
195
- {t('Keep as external source', { note: 'recipe.calc.supply.source' })}
206
+ {t('Supply via recipe…', { note: 'recipe.calc.supply.recipe' })}
196
207
  </ContextMenuItem>
197
208
  ) : null}
198
209
  {out != null ? (
@@ -21,7 +21,7 @@ import type {
21
21
  } from '@terrafirmagreg-team/recipe/types/calc';
22
22
  import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
23
23
  import { PortContextMenu } from './menu';
24
- import { formatPortCount, PortCount } from './ports';
24
+ import { PortCount, portAmountLine } from './ports';
25
25
 
26
26
  interface CellProps {
27
27
  index: number;
@@ -49,7 +49,6 @@ export function OutputCell({
49
49
  }) {
50
50
  const name = recipeItemName(names, lang, out.id);
51
51
  const count = out.remaining;
52
- const qty = formatPortCount(count, out.kind);
53
52
  return (
54
53
  <ContextMenu>
55
54
  <ContextMenuTrigger
@@ -83,12 +82,13 @@ export function OutputCell({
83
82
  dataset={dataset}
84
83
  lang={lang}
85
84
  langMap={langMap}
86
- name={`${name} ×${qty}`}
85
+ name={name}
86
+ subheader={portAmountLine(count, out.kind)}
87
87
  size="sm"
88
88
  href={false}
89
89
  tooltip="adaptive"
90
90
  />
91
- <PortCount value={count} kind={out.kind} />
91
+ <PortCount value={count} kind={out.kind} name={name} />
92
92
  </span>
93
93
  </Handle>
94
94
  }
@@ -123,7 +123,6 @@ export function InputCell({
123
123
  const id = inp.id;
124
124
  const name = id ? recipeItemName(names, lang, id) : (inp.tagId ?? '?');
125
125
  const uncovered = Math.max(0, inp.amount - inp.covered);
126
- const qty = formatPortCount(uncovered, inp.kind);
127
126
  const state = inp.wired
128
127
  ? uncovered <= 0
129
128
  ? 'full'
@@ -160,7 +159,8 @@ export function InputCell({
160
159
  dataset={dataset}
161
160
  lang={lang}
162
161
  langMap={langMap}
163
- name={`${name} ×${qty}`}
162
+ name={name}
163
+ subheader={portAmountLine(uncovered, inp.kind)}
164
164
  size="sm"
165
165
  href={false}
166
166
  tooltip="adaptive"
@@ -172,7 +172,8 @@ export function InputCell({
172
172
  dataset={dataset}
173
173
  lang={lang}
174
174
  langMap={langMap}
175
- name={`${name} ×${qty}`}
175
+ name={name}
176
+ subheader={portAmountLine(uncovered, inp.kind)}
176
177
  size="sm"
177
178
  href={false}
178
179
  tooltip="adaptive"
@@ -180,7 +181,11 @@ export function InputCell({
180
181
  ) : (
181
182
  <span className="text-[9px]">?</span>
182
183
  )}
183
- <PortCount value={uncovered} kind={inp.kind} />
184
+ <PortCount
185
+ value={uncovered}
186
+ kind={inp.kind}
187
+ name={id || inp.tagId ? name : undefined}
188
+ />
184
189
  </span>
185
190
  </Handle>
186
191
  }
@@ -1,7 +1,12 @@
1
1
  'use client';
2
2
 
3
+ import type { ReactNode } from 'react';
3
4
  import { Handle, Position } from '@terrafirmagreg-team/ui/lib/canvas/xyflow';
4
- import { formatCount } from '@terrafirmagreg-team/ui/lib/utils/format';
5
+ import {
6
+ formatCount,
7
+ formatFluidAmount,
8
+ } from '@terrafirmagreg-team/ui/lib/utils/format';
9
+ import { Hint } from '@terrafirmagreg-team/ui/components/blocks/tooltip/hint';
5
10
  import { PORT_OUT } from '@terrafirmagreg-team/recipe/lib/calc/ports';
6
11
  import type {
7
12
  CalcKind,
@@ -9,24 +14,45 @@ import type {
9
14
  } from '@terrafirmagreg-team/recipe/types/calc';
10
15
 
11
16
  export function formatPortCount(value: number, kind: CalcKind): string {
12
- if (kind === 'fluid') {
13
- if (Math.abs(value) >= 1_000)
14
- return `${Math.round((value / 1000) * 100) / 100}`;
15
- return `${Math.round(value)}`;
16
- }
17
+ if (kind === 'fluid') return formatFluidAmount(value);
17
18
  return formatCount(value, 1_000);
18
19
  }
19
20
 
20
- export function PortCount({ value, kind }: { value: number; kind: CalcKind }) {
21
+ export function portAmountLine(value: number, kind: CalcKind): ReactNode {
22
+ return (
23
+ <p className="text-muted-foreground">
24
+ {value.toLocaleString('en-US')}
25
+ {kind === 'fluid' ? ' mB' : ''}
26
+ </p>
27
+ );
28
+ }
29
+
30
+ export function PortCount({
31
+ value,
32
+ kind,
33
+ name,
34
+ }: {
35
+ value: number;
36
+ kind: CalcKind;
37
+ name?: string;
38
+ }) {
21
39
  const text = formatPortCount(value, kind);
22
40
  return (
23
- <span
24
- className="calc-port-count"
25
- data-len={text.length >= 5 ? 'xl' : text.length >= 4 ? 'l' : undefined}
26
- title={String(value)}
41
+ <Hint
42
+ label={
43
+ <>
44
+ <p>{name}</p>
45
+ {portAmountLine(value, kind)}
46
+ </>
47
+ }
27
48
  >
28
- {text}
29
- </span>
49
+ <span
50
+ className="calc-port-count"
51
+ data-len={text.length >= 5 ? 'xl' : text.length >= 4 ? 'l' : undefined}
52
+ >
53
+ {text}
54
+ </span>
55
+ </Hint>
30
56
  );
31
57
  }
32
58
 
@@ -10,6 +10,7 @@ import {
10
10
  DialogTitle,
11
11
  } from '@terrafirmagreg-team/ui/components/ui/dialog';
12
12
  import { Button } from '@terrafirmagreg-team/ui/components/ui/button';
13
+ import { PagerArrow } from '@terrafirmagreg-team/ui/components/blocks/page/pager';
13
14
  import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
14
15
  import { RecipeFrame } from '@terrafirmagreg-team/recipe/components/views/browser/frame';
15
16
  import {
@@ -101,6 +102,14 @@ export function PickerBody({
101
102
  setPinned(false);
102
103
  setPage(next);
103
104
  };
105
+ const goCategory = (dir: -1 | 1) => {
106
+ const cur = categories.indexOf(active ?? '');
107
+ const next =
108
+ categories[(cur + dir + categories.length) % categories.length];
109
+ if (!next || next === active) return;
110
+ setTab(next);
111
+ goPage(0);
112
+ };
104
113
  const custom =
105
114
  !!node.recipe &&
106
115
  node.recipe.recipeId !== (defaultRecipeId ?? recipes[0]?.recipeId);
@@ -163,7 +172,31 @@ export function PickerBody({
163
172
  }}
164
173
  />
165
174
  }
166
- header={<PlaceHeader session={frameSession} />}
175
+ navPrev={
176
+ <PagerArrow
177
+ dir={-1}
178
+ label={t('Previous category', { note: 'recipe.axis.prev' })}
179
+ disabled={categories.length < 2}
180
+ onClick={() => goCategory(-1)}
181
+ />
182
+ }
183
+ navNext={
184
+ <PagerArrow
185
+ dir={1}
186
+ label={t('Next category', { note: 'recipe.axis.next' })}
187
+ disabled={categories.length < 2}
188
+ onClick={() => goCategory(1)}
189
+ />
190
+ }
191
+ onNavWheel={goCategory}
192
+ header={
193
+ <div className="flex min-w-0 justify-center">
194
+ <PlaceHeader
195
+ session={frameSession}
196
+ overline={t('Categories', { note: 'recipe.axis_type' })}
197
+ />
198
+ </div>
199
+ }
167
200
  rail={
168
201
  <WorkstationRail
169
202
  session={session}
@@ -11,7 +11,10 @@ import {
11
11
  LeftSidebarTree,
12
12
  } from '@terrafirmagreg-team/ui/components/layouts/left-sidebar';
13
13
  import { recipeStackHref } from '@terrafirmagreg-team/recipe/lib/links';
14
- import { formatCount } from '@terrafirmagreg-team/ui/lib/utils/format';
14
+ import {
15
+ formatCount,
16
+ formatFluidAmount,
17
+ } from '@terrafirmagreg-team/ui/lib/utils/format';
15
18
  import { CalcTrees } from '@terrafirmagreg-team/recipe/components/views/calc/trees';
16
19
  import type { CalcMaterial } from '@terrafirmagreg-team/recipe/types/calc';
17
20
  import type { CalcTreeEntry } from '@terrafirmagreg-team/recipe/lib/calc/storage';
@@ -109,7 +112,10 @@ export function CalcSidebar({
109
112
  className="ms-auto shrink-0 tabular-nums text-xs text-muted-foreground"
110
113
  title={row.amount.toLocaleString()}
111
114
  >
112
- ×{formatCount(row.amount)}
115
+ ×
116
+ {row.kind === 'fluid'
117
+ ? formatFluidAmount(row.amount)
118
+ : formatCount(row.amount, 1_000)}
113
119
  </span>
114
120
  </>
115
121
  ),
@@ -3,6 +3,8 @@
3
3
  import { Workflow } from 'lucide-react';
4
4
  import { useTranslations } from '@terrafirmagreg-team/translate/hooks/use-translations';
5
5
  import { Entry } from '@terrafirmagreg-team/game/components/entry/entry';
6
+ import { Hint } from '@terrafirmagreg-team/ui/components/blocks/tooltip/hint';
7
+ import { stripMcFormat } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/names';
6
8
  import type { CalcTreeEntry } from '@terrafirmagreg-team/recipe/lib/calc/storage';
7
9
  import type { EntryMeta } from '@terrafirmagreg-team/recipe/lib/calc/meta';
8
10
 
@@ -36,14 +38,16 @@ export function TreeLabel({
36
38
  }: { entry: CalcTreeEntry } & EntryMeta) {
37
39
  const t = useTranslations();
38
40
  const card = entry.state.cards?.[0];
41
+ const derived = card
42
+ ? langMap?.[card.materialId] || card.materialId
43
+ : undefined;
39
44
  const text =
40
- entry.name ??
41
- (card ? langMap?.[card.materialId] || card.materialId : undefined) ??
45
+ stripMcFormat(entry.name ?? derived ?? '') ||
42
46
  t('Untitled scheme', { note: 'recipe.calc.tab.untitled' });
43
47
  return (
44
- <span className="min-w-0 flex-1 truncate text-start">
45
- {text}
46
- </span>
48
+ <Hint label={text} side="top">
49
+ <span className="min-w-0 flex-1 truncate text-start">{text}</span>
50
+ </Hint>
47
51
  );
48
52
  }
49
53
 
@@ -33,6 +33,22 @@ let cardSeq = 0;
33
33
  const newCardId = () =>
34
34
  `c${Date.now().toString(36)}${(cardSeq += 1).toString(36)}`;
35
35
 
36
+ async function copyText(text: string) {
37
+ try {
38
+ await navigator.clipboard.writeText(text);
39
+ return;
40
+ } catch {
41
+ const area = document.createElement('textarea');
42
+ area.value = text;
43
+ area.style.position = 'fixed';
44
+ area.style.opacity = '0';
45
+ document.body.appendChild(area);
46
+ area.select();
47
+ document.execCommand('copy');
48
+ area.remove();
49
+ }
50
+ }
51
+
36
52
  export function useCalcActions({
37
53
  dataset,
38
54
  lang,
@@ -68,7 +84,12 @@ export function useCalcActions({
68
84
  tagMembers?: (tagId: string) => TagResolution | undefined;
69
85
  ensureMaterial?: (materialId: string) => void;
70
86
  }) {
71
- const { checked: shared, onClick: onShare } = useCopyButton(async () => {
87
+ const {
88
+ checked: shared,
89
+ isLoading: sharing,
90
+ error: shareError,
91
+ onClick: onShare,
92
+ } = useCopyButton(async () => {
72
93
  const tree = encodeShare();
73
94
  const { id: datasetId, version } = dataset;
74
95
  try {
@@ -84,14 +105,14 @@ export function useCalcActions({
84
105
  dataset?: DatasetRef;
85
106
  } | null;
86
107
  if (res?.id) {
87
- await navigator.clipboard.writeText(
108
+ await copyText(
88
109
  `${window.location.origin}${recipeCalcHref(res.dataset ?? dataset, undefined, `s${res.id}`, linkVersion)}`,
89
110
  );
90
111
  return;
91
112
  }
92
113
  } catch {
93
114
  }
94
- await navigator.clipboard.writeText(
115
+ await copyText(
95
116
  `${window.location.origin}${recipeCalcHref(dataset, undefined, tree, linkVersion)}`,
96
117
  );
97
118
  });
@@ -493,6 +514,8 @@ export function useCalcActions({
493
514
 
494
515
  return {
495
516
  shared,
517
+ sharing,
518
+ shareError,
496
519
  onShare,
497
520
  flowData,
498
521
  onAddCard,
@@ -1,8 +1,9 @@
1
1
  'use client';
2
2
 
3
- import { useCallback, useEffect, useRef, useState } from 'react';
3
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
4
4
  import type {
5
5
  CalcCard,
6
+ CalcNode,
6
7
  CalcWire,
7
8
  CalculatorState,
8
9
  } from '@terrafirmagreg-team/recipe/types/calc';
@@ -11,11 +12,13 @@ export function useCalcAmounts({
11
12
  cards,
12
13
  wires,
13
14
  tagSelections,
15
+ resolved,
14
16
  persist,
15
17
  }: {
16
18
  cards: CalcCard[];
17
19
  wires: CalcWire[];
18
20
  tagSelections: Record<string, string>;
21
+ resolved: CalcNode[];
19
22
  persist: (next: CalculatorState) => void;
20
23
  }) {
21
24
  const [pending, setPending] = useState<CalcCard[] | null>(null);
@@ -28,35 +31,59 @@ export function useCalcAmounts({
28
31
  [],
29
32
  );
30
33
 
34
+ if (pending) {
35
+ const settled = pending.every(
36
+ (card) =>
37
+ (resolved.find((node) => node.id === card.id)?.crafts ??
38
+ card.crafts) === card.crafts,
39
+ );
40
+ if (settled) setPending(null);
41
+ }
42
+
31
43
  const flushAmounts = useCallback((): CalcCard[] => {
32
44
  if (amountTimer.current) {
33
45
  clearTimeout(amountTimer.current);
34
46
  amountTimer.current = undefined;
35
47
  }
36
48
  if (!pending) return cards;
37
- setPending(null);
38
49
  return pending;
39
50
  }, [pending, cards]);
40
51
 
41
52
  const onCrafts = useCallback(
42
- (cardId: string, crafts: number) => {
53
+ (cardId: string, crafts: number | ((current: number) => number)) => {
43
54
  const base = pending ?? cards;
44
55
  const next = base.map((card) =>
45
- card.id === cardId ? { ...card, crafts: Math.max(1, crafts) } : card,
56
+ card.id === cardId
57
+ ? {
58
+ ...card,
59
+ crafts: Math.max(
60
+ 1,
61
+ typeof crafts === 'function' ? crafts(card.crafts) : crafts,
62
+ ),
63
+ }
64
+ : card,
46
65
  );
47
66
  setPending(next);
48
67
  if (amountTimer.current) clearTimeout(amountTimer.current);
49
68
  amountTimer.current = setTimeout(() => {
50
69
  amountTimer.current = undefined;
51
- setPending(null);
52
70
  persist({ cards: next, wires, tagSelections });
53
71
  }, 300);
54
72
  },
55
73
  [pending, cards, persist, wires, tagSelections],
56
74
  );
57
75
 
76
+ const pendingCrafts = useMemo(
77
+ () =>
78
+ pending
79
+ ? new Map(pending.map((card) => [card.id, card.crafts]))
80
+ : null,
81
+ [pending],
82
+ );
83
+
58
84
  return {
59
85
  displayCards: pending ?? cards,
86
+ pendingCrafts,
60
87
  flushAmounts,
61
88
  onCrafts,
62
89
  };