2021-06-19 17:37:12 -04:00
|
|
|
import { Text } from "preact-i18n";
|
2021-07-03 09:27:57 -04:00
|
|
|
import { Helmet } from "react-helmet";
|
2021-06-19 17:37:12 -04:00
|
|
|
import styles from "./Settings.module.scss";
|
|
|
|
import { Children } from "../../types/Preact";
|
|
|
|
import Header from '../../components/ui/Header';
|
2021-07-03 09:27:57 -04:00
|
|
|
import { ThemeContext } from "../../context/Theme";
|
2021-06-19 17:37:12 -04:00
|
|
|
import Category from '../../components/ui/Category';
|
2021-07-03 09:27:57 -04:00
|
|
|
import { useContext, useEffect } from "preact/hooks";
|
2021-06-19 17:37:12 -04:00
|
|
|
import IconButton from "../../components/ui/IconButton";
|
|
|
|
import LineDivider from "../../components/ui/LineDivider";
|
|
|
|
import { Switch, useHistory, useParams } from "react-router-dom";
|
|
|
|
import { isTouchscreenDevice } from "../../lib/isTouchscreenDevice";
|
|
|
|
import ButtonItem from "../../components/navigation/items/ButtonItem";
|
2021-07-03 09:27:57 -04:00
|
|
|
import { ArrowBack, X, XCircle } from "@styled-icons/boxicons-regular";
|
2021-06-19 17:37:12 -04:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
pages: {
|
2021-07-02 17:08:03 -04:00
|
|
|
category?: Children
|
|
|
|
divider?: boolean
|
|
|
|
id: string
|
2021-06-19 17:37:12 -04:00
|
|
|
icon: Children
|
|
|
|
title: Children
|
2021-07-02 17:08:03 -04:00
|
|
|
hideTitle?: boolean
|
2021-06-19 17:37:12 -04:00
|
|
|
}[]
|
|
|
|
custom?: Children
|
|
|
|
children: Children
|
|
|
|
defaultPage: string
|
|
|
|
showExitButton?: boolean
|
|
|
|
switchPage: (to?: string) => void
|
|
|
|
category: 'pages' | 'channel_pages' | 'server_pages'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function GenericSettings({ pages, switchPage, category, custom, children, defaultPage, showExitButton }: Props) {
|
|
|
|
const history = useHistory();
|
2021-07-03 09:27:57 -04:00
|
|
|
const theme = useContext(ThemeContext);
|
2021-06-19 17:37:12 -04:00
|
|
|
const { page } = useParams<{ page: string; }>();
|
|
|
|
|
|
|
|
function exitSettings() {
|
|
|
|
if (history.length > 0) {
|
|
|
|
history.goBack();
|
|
|
|
} else {
|
|
|
|
history.push('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function keyDown(e: KeyboardEvent) {
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
exitSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.addEventListener("keydown", keyDown);
|
|
|
|
return () => document.body.removeEventListener("keydown", keyDown);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.settings} data-mobile={isTouchscreenDevice}>
|
2021-07-03 09:27:57 -04:00
|
|
|
<Helmet>
|
|
|
|
<meta
|
|
|
|
name="theme-color"
|
|
|
|
content={
|
|
|
|
isTouchscreenDevice
|
|
|
|
? theme["primary-header"]
|
|
|
|
: theme["background"]
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Helmet>
|
2021-06-19 17:37:12 -04:00
|
|
|
{isTouchscreenDevice && (
|
|
|
|
<Header placement="primary">
|
|
|
|
{typeof page === "undefined" ? (
|
|
|
|
<>
|
|
|
|
{ showExitButton &&
|
|
|
|
<IconButton onClick={exitSettings}>
|
|
|
|
<X size={24} />
|
|
|
|
</IconButton> }
|
|
|
|
<Text id="app.settings.title" />
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<IconButton onClick={() => switchPage()}>
|
2021-07-02 07:56:38 -04:00
|
|
|
<ArrowBack size={24} />
|
2021-06-19 17:37:12 -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}>
|
2021-07-02 17:08:03 -04:00
|
|
|
{!isTouchscreenDevice && !(pages.find(x => x.id === page && x.hideTitle)) && (
|
2021-06-19 17:37:12 -04:00
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|