2023-11-18 19:27:19 +01:00
|
|
|
import classNames from "classnames";
|
2023-11-24 18:39:40 +01:00
|
|
|
import { useCallback, useEffect, useMemo } from "react";
|
2023-11-26 16:33:04 +01:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-11-18 17:28:10 +01:00
|
|
|
import { useAsyncFn } from "react-use";
|
2023-10-27 21:51:14 +02:00
|
|
|
|
2023-11-24 21:54:44 +01:00
|
|
|
import {
|
|
|
|
base64ToBuffer,
|
|
|
|
decryptData,
|
|
|
|
encryptData,
|
|
|
|
} from "@/backend/accounts/crypto";
|
|
|
|
import { getSessions, updateSession } from "@/backend/accounts/sessions";
|
2023-11-24 18:39:40 +01:00
|
|
|
import { updateSettings } from "@/backend/accounts/settings";
|
2023-11-24 21:54:44 +01:00
|
|
|
import { editUser } from "@/backend/accounts/user";
|
2023-11-26 16:04:23 +01:00
|
|
|
import { Button } from "@/components/buttons/Button";
|
2023-10-25 18:05:40 +02:00
|
|
|
import { WideContainer } from "@/components/layout/WideContainer";
|
2023-11-24 21:54:44 +01:00
|
|
|
import { UserIcons } from "@/components/UserIcon";
|
2023-11-18 17:28:10 +01:00
|
|
|
import { Heading1 } from "@/components/utils/Text";
|
2023-11-29 17:42:51 +01:00
|
|
|
import { Transition } from "@/components/utils/Transition";
|
2023-11-24 21:54:44 +01:00
|
|
|
import { useAuth } from "@/hooks/auth/useAuth";
|
2023-11-18 17:28:10 +01:00
|
|
|
import { useBackendUrl } from "@/hooks/auth/useBackendUrl";
|
2023-11-18 19:27:19 +01:00
|
|
|
import { useIsMobile } from "@/hooks/useIsMobile";
|
2023-11-24 17:11:00 +01:00
|
|
|
import { useSettingsState } from "@/hooks/useSettingsState";
|
|
|
|
import { AccountActionsPart } from "@/pages/parts/settings/AccountActionsPart";
|
|
|
|
import { AccountEditPart } from "@/pages/parts/settings/AccountEditPart";
|
|
|
|
import { CaptionsPart } from "@/pages/parts/settings/CaptionsPart";
|
2023-11-24 18:39:40 +01:00
|
|
|
import { ConnectionsPart } from "@/pages/parts/settings/ConnectionsPart";
|
2023-11-24 17:11:00 +01:00
|
|
|
import { DeviceListPart } from "@/pages/parts/settings/DeviceListPart";
|
|
|
|
import { RegisterCalloutPart } from "@/pages/parts/settings/RegisterCalloutPart";
|
|
|
|
import { SidebarPart } from "@/pages/parts/settings/SidebarPart";
|
|
|
|
import { ThemePart } from "@/pages/parts/settings/ThemePart";
|
2023-11-29 18:49:04 +01:00
|
|
|
import { PageTitle } from "@/pages/parts/util/PageTitle";
|
2023-11-18 17:28:10 +01:00
|
|
|
import { AccountWithToken, useAuthStore } from "@/stores/auth";
|
2023-11-24 17:11:00 +01:00
|
|
|
import { useLanguageStore } from "@/stores/language";
|
|
|
|
import { useSubtitleStore } from "@/stores/subtitles";
|
2023-11-18 17:28:10 +01:00
|
|
|
import { useThemeStore } from "@/stores/theme";
|
2023-10-25 18:05:40 +02:00
|
|
|
|
|
|
|
import { SubPageLayout } from "./layouts/SubPageLayout";
|
2023-11-24 17:11:00 +01:00
|
|
|
import { LocalePart } from "./parts/settings/LocalePart";
|
2023-10-25 18:05:40 +02:00
|
|
|
|
|
|
|
function SettingsLayout(props: { children: React.ReactNode }) {
|
2023-11-18 19:27:19 +01:00
|
|
|
const { isMobile } = useIsMobile();
|
|
|
|
|
2023-10-25 18:05:40 +02:00
|
|
|
return (
|
2023-11-24 17:11:00 +01:00
|
|
|
<WideContainer ultraWide classNames="overflow-visible">
|
2023-11-18 19:27:19 +01:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
"grid gap-12",
|
2023-12-23 06:24:43 +01:00
|
|
|
isMobile ? "grid-cols-1" : "lg:grid-cols-[280px,1fr]",
|
2023-11-18 19:27:19 +01:00
|
|
|
)}
|
|
|
|
>
|
2023-11-18 17:28:10 +01:00
|
|
|
<SidebarPart />
|
2023-11-20 19:36:35 +01:00
|
|
|
<div>{props.children}</div>
|
2023-10-25 18:05:40 +02:00
|
|
|
</div>
|
|
|
|
</WideContainer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-24 21:54:44 +01:00
|
|
|
export function AccountSettings(props: {
|
|
|
|
account: AccountWithToken;
|
|
|
|
deviceName: string;
|
|
|
|
setDeviceName: (s: string) => void;
|
|
|
|
colorA: string;
|
|
|
|
setColorA: (s: string) => void;
|
|
|
|
colorB: string;
|
|
|
|
setColorB: (s: string) => void;
|
|
|
|
userIcon: UserIcons;
|
|
|
|
setUserIcon: (s: UserIcons) => void;
|
|
|
|
}) {
|
2023-11-18 17:28:10 +01:00
|
|
|
const url = useBackendUrl();
|
|
|
|
const { account } = props;
|
|
|
|
const [sessionsResult, execSessions] = useAsyncFn(() => {
|
|
|
|
return getSessions(url, account);
|
|
|
|
}, [account, url]);
|
|
|
|
useEffect(() => {
|
|
|
|
execSessions();
|
|
|
|
}, [execSessions]);
|
2023-10-27 21:51:14 +02:00
|
|
|
|
|
|
|
return (
|
2023-11-18 17:28:10 +01:00
|
|
|
<>
|
2023-11-24 21:54:44 +01:00
|
|
|
<AccountEditPart
|
|
|
|
deviceName={props.deviceName}
|
|
|
|
setDeviceName={props.setDeviceName}
|
|
|
|
colorA={props.colorA}
|
|
|
|
setColorA={props.setColorA}
|
|
|
|
colorB={props.colorB}
|
|
|
|
setColorB={props.setColorB}
|
|
|
|
userIcon={props.userIcon}
|
|
|
|
setUserIcon={props.setUserIcon}
|
|
|
|
/>
|
2023-11-18 17:28:10 +01:00
|
|
|
<DeviceListPart
|
|
|
|
error={!!sessionsResult.error}
|
|
|
|
loading={sessionsResult.loading}
|
|
|
|
sessions={sessionsResult.value ?? []}
|
|
|
|
onChange={execSessions}
|
|
|
|
/>
|
|
|
|
<AccountActionsPart />
|
|
|
|
</>
|
2023-10-27 21:51:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:05:40 +02:00
|
|
|
export function SettingsPage() {
|
2023-11-26 16:33:04 +01:00
|
|
|
const { t } = useTranslation();
|
2023-11-18 17:28:10 +01:00
|
|
|
const activeTheme = useThemeStore((s) => s.theme);
|
|
|
|
const setTheme = useThemeStore((s) => s.setTheme);
|
2023-11-24 17:11:00 +01:00
|
|
|
|
|
|
|
const appLanguage = useLanguageStore((s) => s.language);
|
2023-11-24 18:39:40 +01:00
|
|
|
const setAppLanguage = useLanguageStore((s) => s.setLanguage);
|
2023-11-24 17:11:00 +01:00
|
|
|
|
|
|
|
const subStyling = useSubtitleStore((s) => s.styling);
|
2023-11-24 18:39:40 +01:00
|
|
|
const setSubStyling = useSubtitleStore((s) => s.updateStyling);
|
2023-11-24 17:11:00 +01:00
|
|
|
|
2023-11-24 21:54:44 +01:00
|
|
|
const proxySet = useAuthStore((s) => s.proxySet);
|
|
|
|
const setProxySet = useAuthStore((s) => s.setProxySet);
|
|
|
|
|
|
|
|
const backendUrlSetting = useAuthStore((s) => s.backendUrl);
|
|
|
|
const setBackendUrl = useAuthStore((s) => s.setBackendUrl);
|
|
|
|
|
2023-11-24 18:39:40 +01:00
|
|
|
const account = useAuthStore((s) => s.account);
|
2023-11-24 21:54:44 +01:00
|
|
|
const updateProfile = useAuthStore((s) => s.setAccountProfile);
|
|
|
|
const updateDeviceName = useAuthStore((s) => s.updateDeviceName);
|
2023-11-24 18:39:40 +01:00
|
|
|
const decryptedName = useMemo(() => {
|
|
|
|
if (!account) return "";
|
|
|
|
return decryptData(account.deviceName, base64ToBuffer(account.seed));
|
|
|
|
}, [account]);
|
|
|
|
|
|
|
|
const backendUrl = useBackendUrl();
|
2023-11-24 17:11:00 +01:00
|
|
|
|
2023-11-24 21:54:44 +01:00
|
|
|
const { logout } = useAuth();
|
2023-11-18 17:28:10 +01:00
|
|
|
const user = useAuthStore();
|
|
|
|
|
2023-11-24 17:11:00 +01:00
|
|
|
const state = useSettingsState(
|
|
|
|
activeTheme,
|
|
|
|
appLanguage,
|
|
|
|
subStyling,
|
2023-11-24 21:54:44 +01:00
|
|
|
decryptedName,
|
|
|
|
proxySet,
|
|
|
|
backendUrlSetting,
|
2023-12-23 06:24:43 +01:00
|
|
|
account?.profile,
|
2023-11-24 17:11:00 +01:00
|
|
|
);
|
|
|
|
|
2023-11-24 18:39:40 +01:00
|
|
|
const saveChanges = useCallback(async () => {
|
|
|
|
if (account) {
|
2023-12-29 22:58:15 +01:00
|
|
|
if (
|
|
|
|
state.appLanguage.changed ||
|
|
|
|
state.theme.changed ||
|
|
|
|
state.proxyUrls.changed
|
|
|
|
) {
|
2023-11-24 21:54:44 +01:00
|
|
|
await updateSettings(backendUrl, account, {
|
|
|
|
applicationLanguage: state.appLanguage.state,
|
|
|
|
applicationTheme: state.theme.state,
|
2023-12-29 22:58:15 +01:00
|
|
|
proxyUrls: state.proxyUrls.state?.filter((v) => v !== "") ?? null,
|
2023-11-24 21:54:44 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (state.deviceName.changed) {
|
|
|
|
const newDeviceName = await encryptData(
|
|
|
|
state.deviceName.state,
|
2023-12-23 06:24:43 +01:00
|
|
|
base64ToBuffer(account.seed),
|
2023-11-24 21:54:44 +01:00
|
|
|
);
|
|
|
|
await updateSession(backendUrl, account, {
|
|
|
|
deviceName: newDeviceName,
|
|
|
|
});
|
|
|
|
updateDeviceName(newDeviceName);
|
|
|
|
}
|
|
|
|
if (state.profile.changed) {
|
|
|
|
await editUser(backendUrl, account, {
|
|
|
|
profile: state.profile.state,
|
|
|
|
});
|
|
|
|
}
|
2023-11-24 18:39:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setAppLanguage(state.appLanguage.state);
|
|
|
|
setTheme(state.theme.state);
|
|
|
|
setSubStyling(state.subtitleStyling.state);
|
2023-12-29 22:58:15 +01:00
|
|
|
setProxySet(state.proxyUrls.state?.filter((v) => v !== "") ?? null);
|
2023-11-24 21:54:44 +01:00
|
|
|
|
|
|
|
if (state.profile.state) {
|
|
|
|
updateProfile(state.profile.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
// when backend url gets changed, log the user out first
|
|
|
|
if (state.backendUrl.changed) {
|
|
|
|
await logout();
|
|
|
|
setBackendUrl(state.backendUrl.state);
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
state,
|
|
|
|
account,
|
|
|
|
backendUrl,
|
|
|
|
setAppLanguage,
|
|
|
|
setTheme,
|
|
|
|
setSubStyling,
|
|
|
|
updateDeviceName,
|
|
|
|
updateProfile,
|
|
|
|
setProxySet,
|
|
|
|
setBackendUrl,
|
|
|
|
logout,
|
|
|
|
]);
|
2023-10-25 18:05:40 +02:00
|
|
|
return (
|
|
|
|
<SubPageLayout>
|
2023-11-29 18:49:04 +01:00
|
|
|
<PageTitle subpage k="global.pages.settings" />
|
2023-10-25 18:05:40 +02:00
|
|
|
<SettingsLayout>
|
2023-11-18 19:27:19 +01:00
|
|
|
<div id="settings-account">
|
|
|
|
<Heading1 border className="!mb-0">
|
2023-12-21 20:47:38 +01:00
|
|
|
{t("settings.account.title")}
|
2023-11-18 19:27:19 +01:00
|
|
|
</Heading1>
|
2023-11-24 21:54:44 +01:00
|
|
|
{user.account && state.profile.state ? (
|
|
|
|
<AccountSettings
|
|
|
|
account={user.account}
|
|
|
|
deviceName={state.deviceName.state}
|
|
|
|
setDeviceName={state.deviceName.set}
|
|
|
|
colorA={state.profile.state.colorA}
|
|
|
|
setColorA={(v) => {
|
|
|
|
state.profile.set((s) => (s ? { ...s, colorA: v } : undefined));
|
|
|
|
}}
|
|
|
|
colorB={state.profile.state.colorB}
|
|
|
|
setColorB={(v) =>
|
|
|
|
state.profile.set((s) => (s ? { ...s, colorB: v } : undefined))
|
|
|
|
}
|
|
|
|
userIcon={state.profile.state.icon as any}
|
|
|
|
setUserIcon={(v) =>
|
|
|
|
state.profile.set((s) => (s ? { ...s, icon: v } : undefined))
|
|
|
|
}
|
|
|
|
/>
|
2023-11-18 19:27:19 +01:00
|
|
|
) : (
|
|
|
|
<RegisterCalloutPart />
|
|
|
|
)}
|
|
|
|
</div>
|
2023-11-20 19:36:35 +01:00
|
|
|
<div id="settings-locale" className="mt-48">
|
2023-11-24 17:11:00 +01:00
|
|
|
<LocalePart
|
|
|
|
language={state.appLanguage.state}
|
|
|
|
setLanguage={state.appLanguage.set}
|
|
|
|
/>
|
2023-11-18 20:55:46 +01:00
|
|
|
</div>
|
2023-11-20 19:36:35 +01:00
|
|
|
<div id="settings-appearance" className="mt-48">
|
2023-11-24 17:11:00 +01:00
|
|
|
<ThemePart active={state.theme.state} setTheme={state.theme.set} />
|
2023-11-18 19:27:19 +01:00
|
|
|
</div>
|
2023-11-20 19:36:35 +01:00
|
|
|
<div id="settings-captions" className="mt-48">
|
2023-11-24 17:11:00 +01:00
|
|
|
<CaptionsPart
|
|
|
|
styling={state.subtitleStyling.state}
|
2023-11-24 18:39:40 +01:00
|
|
|
setStyling={state.subtitleStyling.set}
|
2023-11-24 17:11:00 +01:00
|
|
|
/>
|
2023-11-18 19:27:19 +01:00
|
|
|
</div>
|
2023-11-24 18:39:40 +01:00
|
|
|
<div id="settings-connection" className="mt-48">
|
2023-11-24 21:54:44 +01:00
|
|
|
<ConnectionsPart
|
|
|
|
backendUrl={state.backendUrl.state}
|
|
|
|
setBackendUrl={state.backendUrl.set}
|
|
|
|
proxyUrls={state.proxyUrls.state}
|
|
|
|
setProxyUrls={state.proxyUrls.set}
|
|
|
|
/>
|
2023-11-24 18:39:40 +01:00
|
|
|
</div>
|
2023-10-25 18:05:40 +02:00
|
|
|
</SettingsLayout>
|
2023-11-29 17:42:51 +01:00
|
|
|
<Transition
|
|
|
|
animation="fade"
|
|
|
|
show={state.changed}
|
|
|
|
className="bg-settings-saveBar-background border-t border-settings-card-border/50 py-4 transition-opacity w-full fixed bottom-0 flex justify-between flex-col md:flex-row px-8 items-start md:items-center gap-3"
|
2023-11-24 18:39:40 +01:00
|
|
|
>
|
2023-11-26 16:33:04 +01:00
|
|
|
<p className="text-type-danger">{t("settings.unsaved")}</p>
|
2023-11-25 17:09:01 +01:00
|
|
|
<div className="space-x-3 w-full md:w-auto flex">
|
|
|
|
<Button
|
|
|
|
className="w-full md:w-auto"
|
|
|
|
theme="secondary"
|
|
|
|
onClick={state.reset}
|
|
|
|
>
|
2023-11-26 16:33:04 +01:00
|
|
|
{t("settings.reset")}
|
2023-11-24 18:39:40 +01:00
|
|
|
</Button>
|
2023-11-25 17:09:01 +01:00
|
|
|
<Button
|
|
|
|
className="w-full md:w-auto"
|
|
|
|
theme="purple"
|
|
|
|
onClick={saveChanges}
|
|
|
|
>
|
2023-11-26 16:33:04 +01:00
|
|
|
{t("settings.save")}
|
2023-11-24 18:39:40 +01:00
|
|
|
</Button>
|
2023-11-24 17:11:00 +01:00
|
|
|
</div>
|
2023-11-29 17:42:51 +01:00
|
|
|
</Transition>
|
2023-10-25 18:05:40 +02:00
|
|
|
</SubPageLayout>
|
|
|
|
);
|
|
|
|
}
|
2023-12-23 06:24:43 +01:00
|
|
|
|
|
|
|
export default SettingsPage;
|