mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Store exported data files with a hash in the filename to allow the new caching stategy for static files to work with it and provide a manifest for the web interface to find the exported files with.
45 lines
1.2 KiB
React
45 lines
1.2 KiB
React
import { useEffect, useState } from "react";
|
|
|
|
import { useExportManifest } from "./export_manifest";
|
|
|
|
|
|
let itemMetadataCache = null;
|
|
export function useItemMetadata() {
|
|
let exportManifest = useExportManifest();
|
|
let [itemMetadata, setItemMetadata] = useState(itemMetadataCache || new Map());
|
|
useEffect(() => {
|
|
async function load() {
|
|
if (!exportManifest["item-metadata"] || !exportManifest["item-spritesheet"]) {
|
|
return;
|
|
}
|
|
let response = await fetch(`${staticRoot}${exportManifest["item-metadata"]}`);
|
|
if (response.ok) {
|
|
let data = await response.json();
|
|
itemMetadataCache = new Map(data);
|
|
let style = document.createElement("style");
|
|
style.type = "text/css";
|
|
document.head.appendChild(style);
|
|
for (let [name, meta] of itemMetadataCache) {
|
|
style.sheet.insertRule(
|
|
`.item-${CSS.escape(name)} {
|
|
background-image: url("${staticRoot}${exportManifest["item-spritesheet"]}");
|
|
background-repeat: no-repeat;
|
|
background-position: -${meta.x}px -${meta.y}px;
|
|
height: ${meta.size}px;
|
|
width: ${meta.size}px;
|
|
}`
|
|
);
|
|
}
|
|
setItemMetadata(itemMetadataCache);
|
|
}
|
|
}
|
|
|
|
if (!itemMetadataCache) {
|
|
load();
|
|
}
|
|
}, [exportManifest]);
|
|
|
|
return itemMetadata;
|
|
}
|
|
|