diff --git a/src/pages/settings/SettingsGroup.tsx b/src/pages/settings/SettingsGroup.tsx new file mode 100644 index 0000000..b6945c4 --- /dev/null +++ b/src/pages/settings/SettingsGroup.tsx @@ -0,0 +1,66 @@ +// Copyright (c) 2020-2021 Drew Lemmy +// This file is part of KristWeb 2 under GPL-3.0. +// Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt +import React from "react"; +import { Menu } from "antd"; + +import { useTFns } from "@utils/i18n"; + +import { SettingName } from "@utils/settings"; +import { SettingBoolean } from "./SettingBoolean"; +import { SettingInteger } from "./SettingInteger"; + +export interface SettingDesc { + component: React.ComponentType<{ + setting: SettingName; + title?: string; + titleKey?: string; + description?: string; + descriptionKey?: string; + }>; + + setting: SettingName; +} + +interface Props { + subKey: string; + icon?: React.ReactNode; + + settings: SettingDesc[]; +} + +export const booleanSetting = (setting: SettingName): SettingDesc => + ({ component: SettingBoolean, setting }); +export const integerSetting = (setting: SettingName): SettingDesc => + ({ component: SettingInteger, setting }); + +export function SettingsGroup({ + subKey, + icon, + + settings, + + ...props +}: Props): JSX.Element { + const { tStr, tKey, i18n } = useTFns("settings."); + + return + {/* Render each setting */} + {settings.map(s => ( + + {React.createElement(s.component, { + setting: s.setting, + titleKey: tKey(s.setting), + descriptionKey: i18n.exists(tKey(s.setting + "Description")) + ? tKey(s.setting + "Description") + : undefined + })} + + ))} + ; +} diff --git a/src/pages/settings/SettingsPage.tsx b/src/pages/settings/SettingsPage.tsx index 660dd71..e8229dd 100644 --- a/src/pages/settings/SettingsPage.tsx +++ b/src/pages/settings/SettingsPage.tsx @@ -9,8 +9,8 @@ import { Link } from "react-router-dom"; import { PageLayout, PageLayoutProps } from "@layout/PageLayout"; +import { SettingsGroup, booleanSetting, integerSetting } from "./SettingsGroup"; import { SettingBoolean } from "./SettingBoolean"; -import { SettingInteger } from "./SettingInteger"; import { getLanguageItems } from "./translations/LanguageItem"; import "./SettingsPage.less"; @@ -41,87 +41,33 @@ {/* Auto-refresh settings */} - } title={t("settings.subMenuAutoRefresh")}> - {/* Auto-refresh tables */} - - - - - {/* Auto-refresh address page */} - - - - - {/* Auto-refresh name page */} - - - - + } + settings={[ + booleanSetting("autoRefreshTables"), + booleanSetting("autoRefreshAddressPage"), + booleanSetting("autoRefreshNamePage") + ]} + /> {/* Advanced settings */} - } title={t("settings.subMenuAdvanced")}> - {/* Always include mined transactions */} - - - - - {/* Copy name suffixes */} - - - - - {/* Address copy buttons */} - - - - - {/* Name copy buttons */} - - - - - {/* Block hash copy buttons */} - - - - - {/* Show relative dates */} - - - - - {/* Show native dates */} - - - - - {/* Default to 'Raw' on transaction page */} - - - - - {/* Default page size for table listings */} - - - - - {/* Enable table navigation hotkeys (left and right arrows) */} - - - - + } + settings={[ + booleanSetting("alwaysIncludeMined"), + booleanSetting("copyNameSuffixes"), + booleanSetting("addressCopyButtons"), + booleanSetting("nameCopyButtons"), + booleanSetting("blockHashCopyButtons"), + booleanSetting("showRelativeDates"), + booleanSetting("showNativeDates"), + booleanSetting("transactionDefaultRaw"), + integerSetting("defaultPageSize"), + booleanSetting("tableHotkeys"), + ]} + /> {/* Debug settings */} } title={t("settings.subMenuDebug")}> diff --git a/src/utils/i18n/fns.ts b/src/utils/i18n/fns.ts index 2236e60..edfaf3f 100644 --- a/src/utils/i18n/fns.ts +++ b/src/utils/i18n/fns.ts @@ -1,6 +1,7 @@ // Copyright (c) 2020-2021 Drew Lemmy // This file is part of KristWeb 2 under GPL-3.0. // Full details: https://github.com/tmpim/KristWeb2/blob/master/LICENSE.txt +import { i18n } from "i18next"; import { useTranslation, TFunction } from "react-i18next"; import { TranslatedError } from "./errors"; @@ -12,13 +13,14 @@ tKey: TKeyFn; tStr: TStrFn; tErr: TErrFn; + i18n: i18n; } export function useTranslationFns(prefix?: string): TFns { - const { t } = useTranslation(); + const { t, i18n } = useTranslation(); const tKey = (key: string) => prefix + key; const tStr = (key: string) => t(tKey(key)); const tErr = (key: string) => new TranslatedError(tKey(key)); - return { t, tKey, tStr, tErr }; + return { t, tKey, tStr, tErr, i18n }; } export const useTFns = useTranslationFns;