diff --git a/ivette/src/dome/misc/system.ts b/ivette/src/dome/misc/system.ts index 82e0aefc1c0e8a10f2752df1fdf9b26c6d171451..ca044d7402321f8bf66222e3ae5703ef42b32db6 100644 --- a/ivette/src/dome/misc/system.ts +++ b/ivette/src/dome/misc/system.ts @@ -658,3 +658,27 @@ export default { }; // -------------------------------------------------------------------------- +// --- Get filecontent +// -------------------------------------------------------------------------- + +export function getFileContent( + path: string, + callback: (val: string) => void +): void { + fetch(path) + .then((response) => { + if (!response.ok) { + // Statut HTTP 404 ou autre erreur + throw new Error(`HTTP error! status: ${response.status}`); + } + return response.text(); + }) + .then(callback) + .catch((error) => { + // eslint-disable-next-line no-console + console.error('Error while loading the file :', error); + } + ); +} + +// -------------------------------------------------------------------------- diff --git a/ivette/src/dome/renderer/dome.tsx b/ivette/src/dome/renderer/dome.tsx index 19b04a7f0c4f49920a00ad4171d3bccf715cc13d..1e03e03aba57821328ca78f0609d2de0e931998e 100644 --- a/ivette/src/dome/renderer/dome.tsx +++ b/ivette/src/dome/renderer/dome.tsx @@ -821,6 +821,19 @@ export function useProtected<A>(fn: Callback<A> | undefined): Callback<A> { return trigger; } +/** + * A hook to retrieve text from a file. + * The update is protected by the useProtected() hook. + */ +export function useFileContent( + path: string, +): string { + const [ fileContent, setFileContent ] = React.useState(""); + const callback = useProtected(setFileContent); + System.getFileContent(path, callback); + return fileContent; +} + /** Debounced callback (waiting time in milliseconds). The returned callback will be only fired when the component is mounted.