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:
29
src/helpers/useDataSource.js
Normal file
29
src/helpers/useDataSource.js
Normal 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;
|
||||
Reference in New Issue
Block a user