Moved maps and tokens to a data source model

This will allow for easier custom token support as well as changing default tokens
This commit is contained in:
Mitchell McCaffrey
2020-04-24 15:50:05 +10:00
parent 98798235c9
commit ed8f3bd283
12 changed files with 194 additions and 113 deletions

View File

@@ -0,0 +1,29 @@
import { useEffect, useState } from "react";
// Helper function to load either file or default data
// into a URL and ensure that it is revoked if needed
function useDataSource(data, defaultSources) {
const [dataSource, setDataSource] = useState(null);
useEffect(() => {
if (!data) {
return;
}
let url = null;
if (data.type === "file") {
url = URL.createObjectURL(data.file);
} else if (data.type === "default") {
url = defaultSources[data.name];
}
setDataSource(url);
return () => {
if (data.type === "file" && url) {
URL.revokeObjectURL(url);
}
};
}, [data, defaultSources]);
return dataSource;
}
export default useDataSource;