revite/src/pages/settings/GenericSettings.tsx

186 lines
6.8 KiB
TypeScript
Raw Normal View History

2021-07-05 18:20:55 -04:00
import { ArrowBack, X } from "@styled-icons/boxicons-regular";
import { Helmet } from "react-helmet";
2021-07-05 06:23:23 -04:00
import { Switch, useHistory, useParams } from "react-router-dom";
2021-06-19 17:37:12 -04:00
import styles from "./Settings.module.scss";
2021-08-01 16:08:42 -04:00
import classNames from "classnames";
2021-07-05 06:23:23 -04:00
import { Text } from "preact-i18n";
2021-08-01 16:08:42 -04:00
import { useContext, useEffect, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { isTouchscreenDevice } from "../../lib/isTouchscreenDevice";
import { ThemeContext } from "../../context/Theme";
import Category from "../../components/ui/Category";
import Header from "../../components/ui/Header";
2021-06-19 17:37:12 -04:00
import IconButton from "../../components/ui/IconButton";
import LineDivider from "../../components/ui/LineDivider";
2021-07-05 06:23:23 -04:00
2021-06-19 17:37:12 -04:00
import ButtonItem from "../../components/navigation/items/ButtonItem";
2021-07-05 06:23:23 -04:00
import { Children } from "../../types/Preact";
2021-06-19 17:37:12 -04:00
interface Props {
2021-07-05 06:25:20 -04:00
pages: {
category?: Children;
divider?: boolean;
id: string;
icon: Children;
title: Children;
2021-08-01 16:55:13 -04:00
hidden?: boolean;
2021-07-05 06:25:20 -04:00
hideTitle?: boolean;
}[];
custom?: Children;
children: Children;
defaultPage: string;
showExitButton?: boolean;
switchPage: (to?: string) => void;
category: "pages" | "channel_pages" | "server_pages";
2021-06-19 17:37:12 -04:00
}
2021-07-05 06:23:23 -04:00
export function GenericSettings({
2021-07-05 06:25:20 -04:00
pages,
switchPage,
category,
custom,
children,
defaultPage,
showExitButton,
2021-07-05 06:23:23 -04:00
}: Props) {
2021-07-05 06:25:20 -04:00
const history = useHistory();
const theme = useContext(ThemeContext);
const { page } = useParams<{ page: string }>();
2021-06-19 17:37:12 -04:00
2021-08-01 16:08:42 -04:00
const [closing, setClosing] = useState(false);
2021-07-05 06:25:20 -04:00
function exitSettings() {
2021-08-01 16:55:13 -04:00
if (history.length > 1) {
setClosing(true);
2021-08-01 16:08:42 -04:00
2021-08-01 16:55:13 -04:00
setTimeout(() => {
2021-08-01 16:08:42 -04:00
history.goBack();
2021-08-01 16:55:13 -04:00
}, 100);
} else {
history.push("/");
}
2021-07-05 06:25:20 -04:00
}
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
useEffect(() => {
function keyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
exitSettings();
}
}
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
document.body.addEventListener("keydown", keyDown);
return () => document.body.removeEventListener("keydown", keyDown);
}, []);
2021-06-19 17:37:12 -04:00
2021-07-05 06:25:20 -04:00
return (
2021-08-01 16:08:42 -04:00
<div
className={classNames(styles.settings, {
[styles.closing]: closing,
2021-08-01 16:55:13 -04:00
[styles.native]: window.isNative,
2021-08-01 16:08:42 -04:00
})}
data-mobile={isTouchscreenDevice}>
2021-07-05 06:25:20 -04:00
<Helmet>
<meta
name="theme-color"
content={
isTouchscreenDevice
? theme["background"]
2021-07-10 12:39:01 -04:00
: theme["secondary-background"]
2021-07-05 06:25:20 -04:00
}
/>
</Helmet>
{isTouchscreenDevice && (
<Header placement="primary">
{typeof page === "undefined" ? (
<>
{showExitButton && (
<IconButton onClick={exitSettings}>
2021-07-06 14:29:27 -04:00
<X
size={27}
style={{ marginInlineEnd: "8px" }}
/>
2021-07-05 06:25:20 -04:00
</IconButton>
)}
<Text id="app.settings.title" />
</>
) : (
<>
<IconButton onClick={() => switchPage()}>
2021-07-06 14:29:27 -04:00
<ArrowBack
size={24}
style={{ marginInlineEnd: "10px" }}
/>
2021-07-05 06:25:20 -04:00
</IconButton>
<Text
id={`app.settings.${category}.${page}.title`}
/>
</>
)}
</Header>
)}
{(!isTouchscreenDevice || typeof page === "undefined") && (
<div className={styles.sidebar}>
2021-08-02 06:22:39 -04:00
<div className={styles.scrollbox}>
<div className={styles.container}>
{pages.map((entry, i) =>
entry.hidden ? undefined : (
<>
{entry.category && (
<Category
variant="uniform"
text={entry.category}
/>
)}
<ButtonItem
active={
page === entry.id ||
(i === 0 &&
!isTouchscreenDevice &&
typeof page === "undefined")
}
onClick={() => switchPage(entry.id)}
compact>
{entry.icon} {entry.title}
</ButtonItem>
{entry.divider && <LineDivider />}
</>
),
)}
{custom}
</div>
2021-07-05 06:25:20 -04:00
</div>
</div>
)}
{(!isTouchscreenDevice || typeof page === "string") && (
<div className={styles.content}>
<div className={styles.scrollbox}>
<div className={styles.contentcontainer}>
{!isTouchscreenDevice &&
!pages.find((x) => x.id === page && x.hideTitle) && (
<h1>
<Text
id={`app.settings.${category}.${
page ?? defaultPage
}.title`}
/>
</h1>
)}
<Switch>{children}</Switch>
</div>
{!isTouchscreenDevice && (
<div onClick={exitSettings} className={styles.closeButton}>
<X size={28} />
<span className={styles.esc} />
</div>
2021-07-05 06:25:20 -04:00
)}
2021-07-06 14:29:27 -04:00
</div>
2021-07-05 06:25:20 -04:00
</div>
)}
2021-07-05 06:25:20 -04:00
</div>
);
2021-06-19 17:37:12 -04:00
}