mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 07:33:12 +09:00
Add models for accessing and using the icons and locale that's exported when an instance export is runned.
39 lines
984 B
React
39 lines
984 B
React
import React, { useEffect, useState } from "react";
|
|
|
|
|
|
let itemMetadataCache = null;
|
|
export function useItemMetadata() {
|
|
let [itemMetadata, setItemMetadata] = useState(itemMetadataCache || new Map());
|
|
useEffect(() => {
|
|
async function load() {
|
|
let response = await fetch(`${webRoot}export/item-metadata.json`);
|
|
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("${webRoot}export/item-spritesheet.png");
|
|
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();
|
|
}
|
|
}, []);
|
|
|
|
return itemMetadata;
|
|
}
|
|
|