add show helptext user config value

This commit is contained in:
Simon
2025-01-26 16:18:45 +07:00
parent c633544eac
commit d408b5bb52
4 changed files with 34 additions and 5 deletions

View File

@@ -25,6 +25,7 @@ class UserConfigType(TypedDict, total=False):
hide_watched: bool
show_ignored_only: bool
show_subed_only: bool
show_help_text: bool
class UserConfig:
@@ -43,6 +44,7 @@ class UserConfig:
hide_watched=False,
show_ignored_only=False,
show_subed_only=False,
show_help_text=True,
)
VALID_STYLESHEETS = get_stylesheets()
@@ -83,14 +85,12 @@ class UserConfig:
def set_value(self, key: str, value: str | bool | int):
"""Set or replace a configuration value for the user"""
self._validate(key, value)
old = self.get_value(key)
data = {"doc": {"config": {key: value}}}
response, status = ElasticWrap(self.es_update_url).post(data)
if status < 200 or status > 299:
raise ValueError(f"Failed storing user value {status}: {response}")
print(f"User {self._user_id} value '{key}' change: {old} -> {value}")
print(f"User {self._user_id} value '{key}' change: to {value}")
def _validate(self, key, value):
"""validate key and value"""
@@ -115,6 +115,7 @@ class UserConfig:
"hide_watched": bool,
"show_ignored_only": bool,
"show_subed_only": bool,
"show_help_text": bool,
}
validation_value = valid_values.get(key)
@@ -138,7 +139,7 @@ class UserConfig:
self.sync_defaults()
config = self._DEFAULT_USER_SETTINGS
else:
config = response["_source"]["config"]
config = self.sync_new_defaults(response["_source"]["config"])
return config
@@ -148,3 +149,12 @@ class UserConfig:
{"config": self._DEFAULT_USER_SETTINGS}
)
print(f"set default config for user {self._user_id}: {response}")
def sync_new_defaults(self, config):
"""sync new defaults"""
for key, value in self._DEFAULT_USER_SETTINGS.items():
if key not in config:
self.set_value(key, value)
config.update({key: value})
return config

View File

@@ -27,6 +27,7 @@ export type UserConfigType = {
hide_watched: boolean;
show_ignored_only: boolean;
show_subed_only: boolean;
show_help_text: boolean;
};
const updateUserConfig = async (config: Partial<UserConfigType>): Promise<UserConfigType> => {

View File

@@ -7,6 +7,7 @@ import Button from '../components/Button';
import useIsAdmin from '../functions/useIsAdmin';
import { useUserConfigStore } from '../stores/UserConfigStore';
import { useEffect, useState } from 'react';
import ToggleConfig from '../components/ToggleConfig';
const SettingsUser = () => {
const { userConfig, setPartialConfig } = useUserConfigStore();
@@ -16,13 +17,15 @@ const SettingsUser = () => {
const [styleSheet, setStyleSheet] = useState<ColourVariants>(userConfig.config.stylesheet);
const [styleSheetRefresh, setStyleSheetRefresh] = useState(false);
const [pageSize, setPageSize] = useState<number>(userConfig.config.page_size);
const [showHelpText, setShowHelpText] = useState(userConfig.config.show_help_text);
useEffect(() => {
(async () => {
setStyleSheet(userConfig.config.stylesheet);
setPageSize(userConfig.config.page_size);
setShowHelpText(userConfig.config.show_help_text);
})();
}, [userConfig.config.page_size, userConfig.config.stylesheet]);
}, [userConfig.config.page_size, userConfig.config.stylesheet, userConfig.config.show_help_text]);
const handleStyleSheetChange = async (selectedStyleSheet: ColourVariants) => {
setPartialConfig({ stylesheet: selectedStyleSheet });
@@ -34,6 +37,10 @@ const SettingsUser = () => {
setPartialConfig({ page_size: pageSize });
};
const handleShowHelpTextChange = async (configKey: string, configValue: boolean) => {
setPartialConfig({ [configKey]: configValue });
};
const handlePageRefresh = () => {
navigate(0);
setStyleSheetRefresh(false);
@@ -103,6 +110,16 @@ const SettingsUser = () => {
</div>
</div>
</div>
<div className="settings-box-wrapper">
<div>
<p>Show help text</p>
</div>
<ToggleConfig
name="show_help_text"
value={showHelpText}
updateCallback={handleShowHelpTextChange}
/>
</div>
</div>
</div>
{isAdmin && (

View File

@@ -29,6 +29,7 @@ export const useUserConfigStore = create<UserConfigState>(set => ({
hide_watched: false,
show_ignored_only: false,
show_subed_only: false,
show_help_text: true,
},
},
setUserConfig: userConfig => set({ userConfig }),