mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 15:33:12 +09:00
Refactor web_ui handling of exports
The handling of exported data such as locales and icons in web_ui was based on a model where there is only one set of data exported per cluster. This model changed to have one set of exports per mod pack when mod packs were introduced, but the existing web_ui code was bodged to use exports from a "default mod pack" on the controller. Refactor the code loading exported data to take the mod pack as an argument for selecting which exported data to load. The different kinds of prototype metadata that can be loaded is consolidated into one `useExportPrototypeMetadata` hook. The logic for displaying icons is put into a `FactorioIcon` component. And the obsolete `useExportManifest` hook is removed. These changes allow the `/icon-reference` page to display exports on a per mod pack basis. Which has now been linked on the bottom of the mod pack view page.
This commit is contained in:
1 parent
4b85367d4e
commit
0d28a3ac3c
18 files changed
+391
-300
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import type { ModPack } from "@clusterio/lib";
|
||||
import { useCallback, useSyncExternalStore } from "react";
|
||||
import { exportLocaleStore } from "../store/export_locale_stores";
|
||||
|
||||
|
||||
export function useExportLocale(modPack: ModPack | undefined) {
|
||||
const localePath = modPack?.exportManifest?.assets["locale"];
|
||||
const subscribe = useCallback(
|
||||
(callback: () => void) => exportLocaleStore.subscribe(modPack?.id, localePath, callback),
|
||||
[modPack?.id, localePath],
|
||||
);
|
||||
return useSyncExternalStore(subscribe, () => exportLocaleStore.getSnapshot(modPack?.id));
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import { useEffect, useContext, useState } from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
let exportManifestCache: lib.ExportManifest|null = null;
|
||||
let loaded = false;
|
||||
export function useExportManifest(): lib.ExportManifest|null {
|
||||
const control = useContext(ControlContext);
|
||||
let [exportManifest, setExportManifest] = useState<lib.ExportManifest|null>(exportManifestCache);
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
let modPack = await control.send(new lib.ModPackGetDefaultRequest());
|
||||
if (modPack.exportManifest) {
|
||||
exportManifestCache = modPack.exportManifest;
|
||||
setExportManifest(exportManifestCache);
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
if (!loaded) {
|
||||
load();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return exportManifest;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { useCallback, useSyncExternalStore } from "react";
|
||||
|
||||
import { ModPack } from "@clusterio/lib";
|
||||
import { exportPrototypeMetadataStore } from "../store/export_prototype_metadata_store";
|
||||
|
||||
|
||||
/**
|
||||
* Load the unified spritesheet metadata and inject CSS rules for every icon.
|
||||
* Required for {@link FactorioIcon}.
|
||||
*/
|
||||
export function useExportPrototypeMetadata(modPack: ModPack | undefined) {
|
||||
const subscribe = useCallback(
|
||||
(callback: () => void) => exportPrototypeMetadataStore.subscribe(modPack, callback),
|
||||
[modPack]
|
||||
);
|
||||
return useSyncExternalStore(subscribe, () => exportPrototypeMetadataStore.getSnapshot(modPack?.id));
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
import { useExportManifest } from "./export_manifest";
|
||||
import { ExportMetadata, ExportMetadataEntry } from "@clusterio/lib";
|
||||
|
||||
|
||||
const metadataCaches = new Map<string, Map<string, ExportMetadataEntry>>();
|
||||
let cssInjected = false;
|
||||
|
||||
/**
|
||||
* Load the unified spritesheet metadata and inject CSS rules for every icon.
|
||||
* CSS class name: `.{category}-{CSS.escape(name)}`
|
||||
* e.g. `.item-iron-plate`, `.recipe-iron-plate`, `.signal-signal-red`, `.planet-nauvis`
|
||||
*/
|
||||
function useSpriteMetadata(category: string): Map<string, ExportMetadataEntry> {
|
||||
const exportManifest = useExportManifest();
|
||||
const [metadata, setMetadata] = useState<Map<string, ExportMetadataEntry>>(
|
||||
metadataCaches.get(category) ?? new Map()
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
if (
|
||||
!exportManifest
|
||||
|| !exportManifest.assets["metadata"]
|
||||
|| !exportManifest.assets["spritesheet"]
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetch(`${staticRoot}static/${exportManifest.assets["metadata"]}`);
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data: ExportMetadata = await response.json();
|
||||
|
||||
if (!cssInjected) {
|
||||
const sheetUrl = `${staticRoot}static/${exportManifest.assets["spritesheet"]}`;
|
||||
const style = document.createElement("style");
|
||||
document.head.appendChild(style);
|
||||
for (const [baseName, prototypes] of Object.entries(data)) {
|
||||
for (const prototype of prototypes) {
|
||||
const icon = prototype.icon;
|
||||
if (!icon) {
|
||||
continue;
|
||||
}
|
||||
style.sheet!.insertRule(`\
|
||||
.${baseName}-${CSS.escape(prototype.name)} {
|
||||
background-image: url("${sheetUrl}");
|
||||
background-repeat: no-repeat;
|
||||
background-position: -${icon.x}px -${icon.y}px;
|
||||
height: ${icon.size}px;
|
||||
width: ${icon.size}px;
|
||||
}`
|
||||
);
|
||||
}
|
||||
}
|
||||
cssInjected = true;
|
||||
}
|
||||
|
||||
const cache = new Map<string, ExportMetadataEntry>();
|
||||
for (const prototype of data[category] ?? []) {
|
||||
cache.set(prototype.name, prototype);
|
||||
}
|
||||
metadataCaches.set(category, cache);
|
||||
setMetadata(cache);
|
||||
}
|
||||
|
||||
if (!metadataCaches.has(category)) {
|
||||
load();
|
||||
}
|
||||
}, [exportManifest]);
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
export function useItemMetadata() { return useSpriteMetadata("item"); }
|
||||
export function useFluidMetadata() { return useSpriteMetadata("fluid"); }
|
||||
export function useRecipeMetadata() { return useSpriteMetadata("recipe"); }
|
||||
export function useVirtualSignalMetadata() { return useSpriteMetadata("virtual-signal"); }
|
||||
export function useTechnologyMetadata() { return useSpriteMetadata("technology"); }
|
||||
export function useSpaceLocationMetadata() { return useSpriteMetadata("space-location"); }
|
||||
export function useQualityMetadata() { return useSpriteMetadata("quality"); }
|
||||
export function useEntityMetadata() { return useSpriteMetadata("entity"); }
|
||||
@@ -1,29 +0,0 @@
|
||||
import React, { useEffect, useState } from "react";
|
||||
|
||||
import { useExportManifest } from "./export_manifest";
|
||||
|
||||
|
||||
let localeCache: Map<string, string>|null = null;
|
||||
export function useLocale(): Map<string, string> {
|
||||
let exportManifest = useExportManifest();
|
||||
let [locale, setLocale] = useState<Map<string, string>>(localeCache || new Map());
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
if (!exportManifest?.assets["locale"]) {
|
||||
return;
|
||||
}
|
||||
let response = await fetch(`${staticRoot}static/${exportManifest.assets["locale"]}`);
|
||||
if (response.ok) {
|
||||
let data = await response.json();
|
||||
localeCache = new Map(data);
|
||||
setLocale(localeCache);
|
||||
}
|
||||
}
|
||||
|
||||
if (!localeCache) {
|
||||
load();
|
||||
}
|
||||
}, [exportManifest]);
|
||||
|
||||
return locale;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import { useCallback, useContext, useSyncExternalStore } from "react";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
import { defaultModPackStore } from "../store/default_mod_pack_store";
|
||||
|
||||
|
||||
export function useModPack(id?: number) {
|
||||
const [modPacks, synced] = useModPacks();
|
||||
@@ -11,3 +13,11 @@ export function useModPacks() {
|
||||
const subscribe = useCallback((callback: () => void) => control.modPacks.subscribe(callback), [control]);
|
||||
return useSyncExternalStore(subscribe, () => control.modPacks.getSnapshot());
|
||||
}
|
||||
|
||||
export function useDefaultModPack() {
|
||||
const control = useContext(ControlContext);
|
||||
const subscribe = useCallback(
|
||||
(callback: () => void) => defaultModPackStore.subscribe(control, callback), [control],
|
||||
);
|
||||
return useSyncExternalStore(subscribe, () => defaultModPackStore.getSnapshot());
|
||||
}
|
||||
Reference in new issue
Block a user