tutu/src/settings/Settings.tsx

244 lines
7.3 KiB
TypeScript
Raw Normal View History

import {
children,
createSignal,
For,
Show,
type ParentComponent,
} from "solid-js";
2024-07-22 13:57:04 +00:00
import Scaffold from "../material/Scaffold.js";
import {
AppBar,
Divider,
IconButton,
List,
ListItem,
ListItemButton,
2024-09-26 09:23:40 +00:00
ListItemIcon,
2024-07-22 13:57:04 +00:00
ListItemSecondaryAction,
ListItemText,
ListSubheader,
Switch,
Toolbar,
} from "@suid/material";
import {
Animation as AnimationIcon,
Close as CloseIcon,
Logout,
2024-09-26 09:23:40 +00:00
Public as PublicIcon,
Refresh as RefreshIcon,
2024-09-26 09:23:40 +00:00
Translate as TranslateIcon,
} from "@suid/icons-material";
import { A, useNavigate } from "@solidjs/router";
2024-07-22 13:57:04 +00:00
import { Title } from "../material/typography.jsx";
import { css } from "solid-styled";
import { signOut, type Account } from "../accounts/stores.js";
2024-09-26 11:42:44 +00:00
import { format } from "date-fns";
2024-07-23 00:28:47 +00:00
import { useStore } from "@nanostores/solid";
import { $settings } from "./stores.js";
2024-09-26 09:23:40 +00:00
import {
autoMatchLangTag,
autoMatchRegion,
2024-09-26 11:42:44 +00:00
createTranslator,
2024-09-26 09:23:40 +00:00
useDateFnLocale,
} from "../platform/i18n.jsx";
2024-09-26 11:42:44 +00:00
import { type Template } from "@solid-primitives/i18n";
import BottomSheet from "../material/BottomSheet.jsx";
import { useServiceWorker } from "../platform/host.js";
import { useSessions } from "../masto/clients.js";
type Strings = {
["lang.auto"]: Template<{ detected: string }>;
} & Record<string, string | undefined>;
2024-07-22 13:57:04 +00:00
const Settings: ParentComponent = (props) => {
2024-09-26 11:42:44 +00:00
const [t] = createTranslator(
(code) =>
import(`./i18n/${code}.json`) as Promise<{
default: Strings;
}>,
() => import(`./i18n/lang-names.json`),
2024-09-26 09:23:40 +00:00
);
2024-07-22 13:57:04 +00:00
const navigate = useNavigate();
2024-07-23 00:28:47 +00:00
const settings$ = useStore($settings);
const { needRefresh, offlineReady } = useServiceWorker();
2024-09-26 09:23:40 +00:00
const dateFnLocale = useDateFnLocale();
2024-07-22 13:57:04 +00:00
const profiles = useSessions();
2024-07-22 13:57:04 +00:00
const doSignOut = (acct: Account) => {
signOut((a) => a.site === acct.site && a.accessToken === acct.accessToken);
};
const subpage = children(() => props.children);
2024-07-22 13:57:04 +00:00
css`
ul {
padding: 0;
}
.setting-list {
2024-07-24 06:27:20 +00:00
padding-bottom: calc(var(--safe-area-inset-bottom, 0px) + 16px);
2024-07-22 13:57:04 +00:00
}
`;
return (
<Scaffold
topbar={
<AppBar position="static">
<Toolbar
variant="dense"
sx={{ paddingTop: "var(--safe-area-inset-top, 0px)" }}
>
<IconButton color="inherit" onClick={[navigate, -1]} disableRipple>
2024-07-22 13:57:04 +00:00
<CloseIcon />
</IconButton>
2024-09-26 09:23:40 +00:00
<Title>{t("Settings")}</Title>
2024-07-22 13:57:04 +00:00
</Toolbar>
</AppBar>
}
>
<BottomSheet open={!!subpage()} onClose={() => navigate(-1)}>
{subpage()}
</BottomSheet>
2024-07-22 13:57:04 +00:00
<List class="setting-list" use:solid-styled>
<li>
<ul>
2024-09-26 09:23:40 +00:00
<ListSubheader>{t("Accounts")}</ListSubheader>
<ListItemButton disabled>
2024-09-26 09:23:40 +00:00
<ListItemText>{t("All Notifications")}</ListItemText>
2024-07-22 13:57:04 +00:00
<ListItemSecondaryAction>
<Switch value={false} disabled />
2024-07-22 13:57:04 +00:00
</ListItemSecondaryAction>
</ListItemButton>
<Divider />
<ListItemButton disabled>
2024-09-26 09:23:40 +00:00
<ListItemText>{t("Sign in...")}</ListItemText>
</ListItemButton>
<Divider />
2024-07-22 13:57:04 +00:00
</ul>
<For each={profiles()}>
{({ account: acct }) => (
<ul data-site={acct.site} data-username={acct.inf?.username}>
<ListSubheader>{`@${acct.inf?.username ?? "..."}@${new URL(acct.site).host}`}</ListSubheader>
<ListItemButton disabled>
<ListItemText>{t("Notifications")}</ListItemText>
2024-07-22 13:57:04 +00:00
<ListItemSecondaryAction>
<Switch value={false} disabled />
2024-07-22 13:57:04 +00:00
</ListItemSecondaryAction>
</ListItemButton>
<Divider />
2024-07-22 13:57:04 +00:00
<ListItemButton onClick={[doSignOut, acct]}>
<ListItemIcon>
<Logout />
</ListItemIcon>
<ListItemText>{t("Sign out")}</ListItemText>
2024-07-22 13:57:04 +00:00
</ListItemButton>
<Divider />
2024-07-22 13:57:04 +00:00
</ul>
)}
</For>
</li>
<li>
<ListSubheader>{t("timelines")}</ListSubheader>
<ListItemButton
onClick={(e) =>
$settings.setKey(
"prefetchTootsDisabled",
!settings$().prefetchTootsDisabled,
)
}
>
2024-09-26 09:23:40 +00:00
<ListItemText secondary={t("Prefetch Toots.2nd")}>
{t("Prefetch Toots")}
2024-07-22 13:57:04 +00:00
</ListItemText>
<ListItemSecondaryAction>
<Switch checked={!settings$().prefetchTootsDisabled} />
2024-07-22 13:57:04 +00:00
</ListItemSecondaryAction>
</ListItemButton>
<Divider />
<ListItemButton component={A} href="./motions">
<ListItemIcon>
<AnimationIcon></AnimationIcon>
</ListItemIcon>
<ListItemText>{t("motions")}</ListItemText>
</ListItemButton>
<Divider />
2024-07-22 13:57:04 +00:00
</li>
<li>
2024-09-26 09:23:40 +00:00
<ListSubheader>{t("This Application")}</ListSubheader>
<ListItemButton component={A} href="./language">
2024-09-26 09:23:40 +00:00
<ListItemIcon>
<TranslateIcon />
</ListItemIcon>
<ListItemText
secondary={
settings$().language === undefined
? t("lang.auto", {
detected:
t("lang." + autoMatchLangTag()) ?? autoMatchLangTag(),
})
: t("lang." + settings$().language)
}
>
{t("Language")}
</ListItemText>
</ListItemButton>
2024-09-26 09:23:40 +00:00
<Divider />
<ListItemButton component={A} href="./region">
2024-09-26 09:23:40 +00:00
<ListItemIcon>
<PublicIcon />
</ListItemIcon>
<ListItemText
secondary={
settings$().region === undefined
? t("region.auto", {
detected:
t("region." + autoMatchRegion()) ?? autoMatchRegion(),
})
: t("region." + settings$().region)
}
>
{t("Region")}
</ListItemText>
</ListItemButton>
2024-09-26 09:23:40 +00:00
<Divider />
2024-07-22 13:57:04 +00:00
<ListItem>
2024-09-26 09:23:40 +00:00
<ListItemText secondary={t("About Tutu.2nd")}>
{t("About Tutu")}
2024-07-22 13:57:04 +00:00
</ListItemText>
</ListItem>
<Divider />
2024-07-22 13:57:04 +00:00
<ListItem>
<ListItemText
2024-09-26 09:23:40 +00:00
secondary={t("version", {
packageVersion: import.meta.env.PACKAGE_VERSION,
builtAt: format(
import.meta.env.BUILT_AT,
t("datefmt") || "yyyy/MM/dd",
{ locale: dateFnLocale() },
),
buildMode: import.meta.env.MODE,
})}
2024-07-22 13:57:04 +00:00
>
2024-09-26 09:23:40 +00:00
{needRefresh() ? t("updates.ready") : t("updates.no")}
2024-07-22 13:57:04 +00:00
</ListItemText>
<Show when={needRefresh()}>
<ListItemSecondaryAction>
2024-09-26 09:23:40 +00:00
<IconButton
aria-label="Restart Now"
onClick={() => window.location.reload()}
>
<RefreshIcon />
</IconButton>
</ListItemSecondaryAction>
</Show>
2024-07-22 13:57:04 +00:00
</ListItem>
<Divider />
2024-07-22 13:57:04 +00:00
</li>
</List>
</Scaffold>
);
};
export default Settings;