import { useCallback, useEffect, useState } from "react"; import { IAppListProps } from "../components/appList"; import { IBookmarkListProps } from "../components/bookmarks"; import { ISearchProps } from "../components/searchBar"; import { IThemeDataProps } from "./useTheme"; import { IImprintProps } from "../components/imprint"; import { IGreeterDataProps } from "../components/greeter"; export interface IDataProps { response?: I; error?: string | boolean; } const inProduction = process.env.NODE_ENV === "production"; /** * Handles the response from the fetch requests * @param {Response} response - The response given by the fetch request * @returns - The response in JSON * @throws - Error with given error message if request failed */ export const handleResponse = async (response: Response) => { if (!response.ok) throw new Error(response.statusText); return response.json(); }; const fetchFile = (f: string) => { if (!inProduction) return require(`../data/${f}.json`); return fetch(`/data/${f}.json`) .then(handleResponse) .catch((error: Error) => { return { error: error.message }; }); }; interface IFetchProps { appData: IDataProps; bookmarkData: IDataProps; searchData: IDataProps; themeData: IDataProps; imprintData: IDataProps; greeterData: IDataProps; callback?: () => void; } /** * Fetches app, bookmark, search, theme and imprint data and returns it. */ export const useFetcher = (): IFetchProps => { const defaults: IDataProps = { error: true }; const [appData, setAppData] = useState>(defaults); const [bookmarkData, setBookmarkData] = useState>(defaults); const [searchData, setSearchData] = useState>(defaults); const [themeData, setThemeData] = useState>(defaults); const [imprintData, setImprintData] = useState>(defaults); const [greeterData, setGreeterData] = useState>(defaults); const callback = useCallback(() => { const files = [ "apps", "bookmarks", "search", "themes", "imprint", "greeter", ]; Promise.all(files.map((f) => fetchFile(f))).then( ([apps, bookmarks, search, themes, imprint, greeter]: any) => { setAppData({ response: apps }); setBookmarkData({ response: bookmarks, }); setSearchData({ response: search }); setThemeData({ response: themes }); setImprintData({ response: imprint }); setGreeterData({ response: greeter }); }, ); }, []); useEffect(() => callback(), [callback]); return { appData, bookmarkData, searchData, themeData, imprintData, greeterData, }; }; export default useFetcher;