Digitale Ausgänge ausgelagert von Ein- und Asgänge
This commit is contained in:
52
hooks/useLoadScript.ts
Normal file
52
hooks/useLoadScript.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
// hooks/useLoadScript.ts:
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useLoadScript() {
|
||||
const [mockData, setMockData] = useState();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const isDevelopment = process.env.NODE_ENV === "development";
|
||||
const script = document.createElement("script");
|
||||
|
||||
script.src = isDevelopment
|
||||
? "/CPLmockData/SERVICE/de.js"
|
||||
: "/CPL/SERVICE/de.js";
|
||||
script.async = true;
|
||||
|
||||
script.onload = () => {
|
||||
try {
|
||||
if (
|
||||
typeof win_de !== "undefined" &&
|
||||
typeof win_counter !== "undefined" &&
|
||||
typeof win_flutter !== "undefined"
|
||||
) {
|
||||
setMockData({
|
||||
win_de,
|
||||
win_counter,
|
||||
win_flutter,
|
||||
});
|
||||
} else {
|
||||
console.error("Mock-Daten konnten nicht geladen werden.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Fehler beim Zugriff auf die globalen Daten:", error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
script.onerror = () => {
|
||||
console.error("Fehler beim Laden der Skript-Datei:", script.src);
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
document.body.appendChild(script);
|
||||
|
||||
return () => {
|
||||
document.body.removeChild(script);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return { mockData, isLoading };
|
||||
}
|
||||
Reference in New Issue
Block a user