/* eslint-disable react/no-array-index-key */ import useSWR, { SWRConfig } from "swr"; import Head from "next/head"; import dynamic from "next/dynamic"; import classNames from "classnames"; import { useTranslation } from "next-i18next"; import { useEffect, useContext, useState } from "react"; import { BiError } from "react-icons/bi"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; import ServicesGroup from "components/services/group"; import BookmarksGroup from "components/bookmarks/group"; import Widget from "components/widgets/widget"; import Revalidate from "components/toggles/revalidate"; import createLogger from "utils/logger"; import useWindowFocus from "utils/hooks/window-focus"; import { getSettings } from "utils/config/config"; import { ColorContext } from "utils/contexts/color"; import { ThemeContext } from "utils/contexts/theme"; import { SettingsContext } from "utils/contexts/settings"; import { bookmarksResponse, servicesResponse, widgetsResponse } from "utils/config/api-response"; import ErrorBoundary from "components/errorboundry"; import themes from "utils/styles/themes"; import QuickLaunch from "components/quicklaunch"; const ThemeToggle = dynamic(() => import("components/toggles/theme"), { ssr: false, }); const ColorToggle = dynamic(() => import("components/toggles/color"), { ssr: false, }); const Version = dynamic(() => import("components/version"), { ssr: false, }); const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"]; export async function getStaticProps() { let logger; try { logger = createLogger("index"); const { providers, ...settings } = getSettings(); const services = await servicesResponse(); const bookmarks = await bookmarksResponse(); const widgets = await widgetsResponse(); return { props: { initialSettings: settings, fallback: { "/api/services": services, "/api/bookmarks": bookmarks, "/api/widgets": widgets, "/api/hash": false, }, ...(await serverSideTranslations(settings.language ?? "en")), }, }; } catch (e) { if (logger) { logger.error(e); } return { props: { initialSettings: {}, fallback: { "/api/services": [], "/api/bookmarks": [], "/api/widgets": [], "/api/hash": false, }, ...(await serverSideTranslations("en")), }, }; } } function Index({ initialSettings, fallback }) { const windowFocused = useWindowFocus(); const [stale, setStale] = useState(false); const { data: errorsData } = useSWR("/api/validate"); const { data: hashData, mutate: mutateHash } = useSWR("/api/hash"); useEffect(() => { if (windowFocused) { mutateHash(); } }, [windowFocused, mutateHash]); useEffect(() => { if (hashData) { if (typeof window !== "undefined") { const previousHash = localStorage.getItem("hash"); if (!previousHash) { localStorage.setItem("hash", hashData.hash); } if (previousHash && previousHash !== hashData.hash) { setStale(true); localStorage.setItem("hash", hashData.hash); fetch("/api/revalidate").then((res) => { if (res.ok) { window.location.reload(); } }); } } } }, [hashData]); if (stale) { return (
{error.reason}
{error.mark.snippet}