2022-09-08 11:47:21 +03:00
|
|
|
/* eslint-disable react/no-array-index-key */
|
2022-08-24 10:44:35 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
import Head from "next/head";
|
|
|
|
import dynamic from "next/dynamic";
|
2022-09-15 05:17:30 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-09-15 19:58:41 +03:00
|
|
|
import { useEffect, useContext } from "react";
|
2022-08-24 10:44:35 +03:00
|
|
|
|
|
|
|
import ServicesGroup from "components/services/group";
|
|
|
|
import BookmarksGroup from "components/bookmarks/group";
|
|
|
|
import Widget from "components/widget";
|
2022-09-09 06:45:43 +03:00
|
|
|
import Revalidate from "components/revalidate";
|
|
|
|
import { getSettings } from "utils/config";
|
2022-09-15 19:58:41 +03:00
|
|
|
import { ColorContext } from "utils/color-context";
|
|
|
|
import { ThemeContext } from "utils/theme-context";
|
2022-08-24 10:44:35 +03:00
|
|
|
|
|
|
|
const ThemeToggle = dynamic(() => import("components/theme-toggle"), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2022-08-25 11:14:17 +03:00
|
|
|
const ColorToggle = dynamic(() => import("components/color-toggle"), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2022-09-16 10:53:12 +03:00
|
|
|
const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"];
|
2022-08-27 15:26:00 +03:00
|
|
|
|
2022-09-09 06:45:43 +03:00
|
|
|
export async function getStaticProps() {
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
settings,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-09-15 05:17:30 +03:00
|
|
|
|
2022-09-09 06:45:43 +03:00
|
|
|
export default function Home({ settings }) {
|
2022-09-15 05:17:30 +03:00
|
|
|
const { i18n } = useTranslation();
|
2022-09-15 19:58:41 +03:00
|
|
|
const { theme, setTheme } = useContext(ThemeContext);
|
|
|
|
const { color, setColor } = useContext(ColorContext);
|
2022-09-15 05:17:30 +03:00
|
|
|
|
2022-09-07 16:53:24 +03:00
|
|
|
const { data: services } = useSWR("/api/services");
|
|
|
|
const { data: bookmarks } = useSWR("/api/bookmarks");
|
|
|
|
const { data: widgets } = useSWR("/api/widgets");
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2022-09-09 06:45:43 +03:00
|
|
|
const wrappedStyle = {};
|
|
|
|
if (settings.background) {
|
|
|
|
wrappedStyle.backgroundImage = `url(${settings.background})`;
|
|
|
|
wrappedStyle.backgroundSize = "cover";
|
|
|
|
}
|
|
|
|
|
2022-09-15 05:17:30 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (settings.language) {
|
|
|
|
i18n.changeLanguage(settings.language);
|
|
|
|
}
|
2022-09-15 19:58:41 +03:00
|
|
|
|
|
|
|
if (settings.theme && theme !== settings.theme) {
|
|
|
|
setTheme(settings.theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.color && color !== settings.color) {
|
|
|
|
setColor(settings.color);
|
|
|
|
}
|
|
|
|
}, [i18n, settings, color, setColor, theme, setTheme]);
|
2022-09-15 05:17:30 +03:00
|
|
|
|
2022-08-24 10:44:35 +03:00
|
|
|
return (
|
2022-09-15 19:58:41 +03:00
|
|
|
<>
|
|
|
|
<Head>
|
|
|
|
<title>{settings.title || "Homepage"}</title>
|
|
|
|
{settings.base && <base href={settings.base} />}
|
|
|
|
{settings.favicon && <link rel="icon" href={settings.favicon} />}
|
|
|
|
</Head>
|
|
|
|
<div className="fixed w-full h-full m-0 p-0" style={wrappedStyle} />
|
|
|
|
<div className="relative w-full container m-auto flex flex-col h-screen justify-between">
|
|
|
|
<div className="flex flex-row flex-wrap m-8 pb-4 mt-10 border-b-2 border-theme-800 dark:border-theme-200 justify-between">
|
|
|
|
{widgets && (
|
|
|
|
<>
|
|
|
|
{widgets
|
|
|
|
.filter((widget) => !rightAlignedWidgets.includes(widget.type))
|
|
|
|
.map((widget, i) => (
|
|
|
|
<Widget key={i} widget={widget} />
|
|
|
|
))}
|
|
|
|
|
|
|
|
<div className="ml-4 flex flex-wrap basis-full grow sm:basis-auto justify-between md:justify-end mt-2 md:mt-0">
|
2022-08-27 00:55:13 +03:00
|
|
|
{widgets
|
2022-09-15 19:58:41 +03:00
|
|
|
.filter((widget) => rightAlignedWidgets.includes(widget.type))
|
2022-09-08 11:47:21 +03:00
|
|
|
.map((widget, i) => (
|
|
|
|
<Widget key={i} widget={widget} />
|
2022-08-27 00:55:13 +03:00
|
|
|
))}
|
2022-09-15 19:58:41 +03:00
|
|
|
</div>
|
|
|
|
</>
|
2022-08-24 10:44:35 +03:00
|
|
|
)}
|
2022-09-15 19:58:41 +03:00
|
|
|
</div>
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2022-09-15 19:58:41 +03:00
|
|
|
{services && (
|
|
|
|
<div className="flex flex-wrap p-8 items-start">
|
|
|
|
{services.map((group) => (
|
|
|
|
<ServicesGroup key={group.name} services={group} layout={settings.layout?.[group.name]} />
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-08-24 10:44:35 +03:00
|
|
|
|
2022-09-15 19:58:41 +03:00
|
|
|
{bookmarks && (
|
|
|
|
<div className="grow flex flex-wrap pt-0 p-8">
|
|
|
|
{bookmarks.map((group) => (
|
|
|
|
<BookmarksGroup key={group.name} group={group} />
|
|
|
|
))}
|
2022-08-24 10:44:35 +03:00
|
|
|
</div>
|
2022-09-15 19:58:41 +03:00
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="rounded-full flex p-8 w-full justify-end">
|
|
|
|
{!settings?.color && <ColorToggle />}
|
|
|
|
<Revalidate />
|
|
|
|
{!settings?.theme && <ThemeToggle />}
|
2022-08-24 10:44:35 +03:00
|
|
|
</div>
|
2022-09-15 19:58:41 +03:00
|
|
|
</div>
|
|
|
|
</>
|
2022-08-24 10:44:35 +03:00
|
|
|
);
|
|
|
|
}
|