@terrafirmagreg-team/recipe 1.2.13 → 1.2.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrafirmagreg-team/recipe",
3
- "version": "1.2.13",
3
+ "version": "1.2.14",
4
4
  "type": "module",
5
5
  "description": "Recipe viewer and calculator",
6
6
  "license": "LGPL-3.0-only",
@@ -24,14 +24,14 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@terrafirmagreg-team/data": "^1.2.3",
27
- "@terrafirmagreg-team/dump": "^1.2.9",
27
+ "@terrafirmagreg-team/dump": "^1.2.11",
28
28
  "@terrafirmagreg-team/game": "^1.2.6",
29
- "@terrafirmagreg-team/kit": "^1.2.14",
29
+ "@terrafirmagreg-team/kit": "^1.2.15",
30
30
  "@terrafirmagreg-team/mdx": "^1.2.7",
31
31
  "@terrafirmagreg-team/preview": "^1.2.3",
32
32
  "@terrafirmagreg-team/sites": "^1.2.2",
33
33
  "@terrafirmagreg-team/translate": "^1.2.8",
34
- "@terrafirmagreg-team/ui": "^1.2.9",
34
+ "@terrafirmagreg-team/ui": "^1.2.10",
35
35
  "@types/pathfinding": "^0.1.0",
36
36
  "comlink": "^4.4.2",
37
37
  "fflate": "0.8.3",
@@ -22,7 +22,10 @@ function categoryIcon(
22
22
  if (!category.icon) return undefined;
23
23
  return createElement(
24
24
  'span',
25
- { className: 'inline-flex size-4 shrink-0 items-center justify-center' },
25
+ {
26
+ key: 'icon',
27
+ className: 'inline-flex size-4 shrink-0 items-center justify-center',
28
+ },
26
29
  createElement(Entry, {
27
30
  dataset,
28
31
  reference: category.icon,
package/src/lib/og.ts ADDED
@@ -0,0 +1,127 @@
1
+ import type { DatasetRef } from '@terrafirmagreg-team/sites/location';
2
+ import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
3
+ import { clientDumpLang } from '@terrafirmagreg-team/dump/lib/dump/client/dump';
4
+ import { loadClientTagMembers } from '@terrafirmagreg-team/dump/lib/dump/client/tags';
5
+ import type { ReferenceKind } from '@terrafirmagreg-team/dump/lib/dump/contract/ref';
6
+ import { stripMcFormat } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/names';
7
+ import {
8
+ ogLocalized,
9
+ ogRefSprite,
10
+ ogStackName,
11
+ } from '@terrafirmagreg-team/dump/lib/dump/assets/og';
12
+ import {
13
+ ogName,
14
+ registerOgResolver,
15
+ type OgContext,
16
+ } from '@terrafirmagreg-team/preview/lib';
17
+ import { loadDatasetRecipeCategories } from './data/category/load';
18
+ import { loadClientRecipe } from './client/recipe';
19
+ import { recipeToolName } from './tool-names';
20
+
21
+ async function recipeOgContext(
22
+ dataset: DatasetRef,
23
+ search: URLSearchParams,
24
+ lang: Lang,
25
+ ): Promise<OgContext> {
26
+ const tool = search.get('tool');
27
+ if (tool) {
28
+ return {
29
+ entity: { kind: 'tool', name: recipeToolName(tool, lang) },
30
+ imageQuery: `tool=${encodeURIComponent(tool)}`,
31
+ };
32
+ }
33
+ const item = search.get('item');
34
+ const fluid = search.get('fluid');
35
+ if (item || fluid) {
36
+ const kind = fluid ? 'fluid' : 'item';
37
+ const id = (item ?? fluid)!;
38
+ return {
39
+ entity: {
40
+ kind,
41
+ name: await ogStackName(dataset, id, kind, lang),
42
+ id,
43
+ },
44
+ icon: await ogRefSprite(dataset, { kind, id }),
45
+ description: id,
46
+ imageQuery: `${kind}=${encodeURIComponent(id)}`,
47
+ };
48
+ }
49
+ const type = search.get('type');
50
+ const id = search.get('id');
51
+ if (type === 'emi:tag' && id) {
52
+ const [, tagKind = 'item', ns = '', ...rest] = id.split('/');
53
+ const path = rest.join('/');
54
+ const tagPath = path.startsWith(`${tagKind}s/`)
55
+ ? path.slice(tagKind.length + 2)
56
+ : path;
57
+ const tagId = rest.length ? `${ns}:${tagPath}` : ns;
58
+ const members = await loadClientTagMembers(dataset, tagKind);
59
+ const list = tagId ? members?.[`#${tagId}`] : undefined;
60
+ const first = list?.[0];
61
+ return {
62
+ entity: { kind: 'tag', name: `#${tagId || id}`, id },
63
+ icon: first
64
+ ? await ogRefSprite(dataset, {
65
+ kind: tagKind as ReferenceKind,
66
+ id: first,
67
+ })
68
+ : undefined,
69
+ description:
70
+ list != null
71
+ ? `${list.length} member${list.length === 1 ? '' : 's'}`
72
+ : `${tagKind} tag`,
73
+ imageQuery: `type=emi:tag&id=${encodeURIComponent(id)}`,
74
+ };
75
+ }
76
+ if (id) {
77
+ const recipe = await loadClientRecipe(dataset, id);
78
+ if (!recipe) return {};
79
+ const out = recipe.outputs[0];
80
+ const [categories, map] = await Promise.all([
81
+ loadDatasetRecipeCategories(dataset),
82
+ clientDumpLang(dataset, lang),
83
+ ]);
84
+ const category = categories.find((row) => row.id === recipe.type);
85
+ const catName = category
86
+ ? stripMcFormat(
87
+ ogLocalized(map, category.display?.name) ?? category.id,
88
+ ).trim()
89
+ : undefined;
90
+ const name = out
91
+ ? await ogStackName(
92
+ dataset,
93
+ out.id,
94
+ out.kind === 'fluid' ? 'fluid' : 'item',
95
+ lang,
96
+ ).catch(() => recipe.id)
97
+ : recipe.id;
98
+ return {
99
+ entity: { kind: 'recipe', name, id: recipe.id },
100
+ icon: out
101
+ ? await ogRefSprite(dataset, {
102
+ kind: out.kind === 'tag' ? 'itemTag' : out.kind,
103
+ id: out.id,
104
+ })
105
+ : undefined,
106
+ description: catName ?? recipe.id,
107
+ imageQuery: `id=${encodeURIComponent(id)}`,
108
+ };
109
+ }
110
+ if (!type || type === 'all') return {};
111
+ const [categories, map] = await Promise.all([
112
+ loadDatasetRecipeCategories(dataset),
113
+ clientDumpLang(dataset, lang),
114
+ ]);
115
+ const category = categories.find((row) => row.id === type);
116
+ if (!category) return {};
117
+ const localized =
118
+ ogLocalized(map, category.display?.name) ?? category.id;
119
+ return {
120
+ entity: { kind: 'category', ...ogName(localized), id: category.id },
121
+ icon: await ogRefSprite(dataset, category.icon),
122
+ description: category.id,
123
+ imageQuery: `type=${encodeURIComponent(type)}`,
124
+ };
125
+ }
126
+
127
+ registerOgResolver('recipe', recipeOgContext);
@@ -0,0 +1,20 @@
1
+ import { humanizeKey } from '@terrafirmagreg-team/dump/lib/dump/assets/lang/names';
2
+ import { loadLocaleRegistry } from '@terrafirmagreg-team/translate/server';
3
+ import type { Lang } from '@terrafirmagreg-team/translate/types/locale';
4
+
5
+ const TOOL_KEYS: Record<string, [string, string]> = {
6
+ calc: ['Calculator', 'recipe.calculator'],
7
+ forge: ['Forging', 'recipe.forge.title'],
8
+ alloy: ['Alloys', 'recipe.alloy.title'],
9
+ };
10
+
11
+ export function recipeToolName(tool: string, lang: Lang): string {
12
+ const [source, note] = TOOL_KEYS[tool] ?? [];
13
+ if (!source || !note) return humanizeKey(tool);
14
+ const registry = loadLocaleRegistry();
15
+ const messages =
16
+ registry.messages[lang] ??
17
+ registry.messages[registry.defaultLanguage] ??
18
+ {};
19
+ return messages[`${source}(${note})`] ?? source;
20
+ }