revite/src/pages/settings/GenericSettings.tsx

159 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-07-05 06:23:23 -04:00
import { ArrowBack, X, XCircle } 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-07-05 06:23:23 -04:00
import { Text } from "preact-i18n";
import { useContext, useEffect } 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;
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-07-05 06:25:20 -04:00
function exitSettings() {
if (history.length > 0) {
history.goBack();
} else {
history.push("/");
}
}
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 (
<div className={styles.settings} data-mobile={isTouchscreenDevice}>
<Helmet>
<meta
name="theme-color"
content={
isTouchscreenDevice
? theme["primary-header"]
: theme["secondary-background"]
}
/>
</Helmet>
{isTouchscreenDevice && (
<Header placement="primary">
{typeof page === "undefined" ? (
<>
{showExitButton && (
<IconButton onClick={exitSettings}>
2021-07-05 10:24:25 -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-05 10:24:25 -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}>
<div className={styles.container}>
{pages.map((entry, i) => (
<>
{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>
</div>
)}
{(!isTouchscreenDevice || typeof page === "string") && (
<div className={styles.content}>
{!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 className={styles.action}>
<IconButton onClick={exitSettings}>
<XCircle size={48} />
</IconButton>
</div>
)}
</div>
);
2021-06-19 17:37:12 -04:00
}