diff --git a/src-ui/views/app/config_page/setting_section/setting_box/_components/message_format/MessageFormat.jsx b/src-ui/views/app/config_page/setting_section/setting_box/_components/message_format/MessageFormat.jsx index 10fc3ab8..35f072e9 100644 --- a/src-ui/views/app/config_page/setting_section/setting_box/_components/message_format/MessageFormat.jsx +++ b/src-ui/views/app/config_page/setting_section/setting_box/_components/message_format/MessageFormat.jsx @@ -10,6 +10,7 @@ import { import { useAppearance } from "@logics_configs"; import { ui_configs } from "@ui_configs"; import { ResetButton } from "@common_components"; +import { useState, useEffect, useRef } from "react"; const ENTRY_WIDTH = "8rem"; @@ -199,23 +200,51 @@ const InputComponent = ({id, variable, setFunction, format_id }) => { return replaced; }; + const [local_var, setLocalVar] = useState(variable); + const debounce_ref = useRef(null); + + useEffect(() => { + setLocalVar(variable); + }, [variable]); + + useEffect(() => { + return () => { + if (debounce_ref.current) { + clearTimeout(debounce_ref.current); + debounce_ref.current = null; + } + }; + }, []); + + const scheduleUpdate = (new_var) => { + if (debounce_ref.current) clearTimeout(debounce_ref.current); + debounce_ref.current = setTimeout(() => { + setFunction(new_var); + debounce_ref.current = null; + }, 500); + }; + const handleChange = (parent_key, child_key) => (e) => { - const rawValue = e.target.value; - const parsedValue = replaceValue(rawValue); + const raw_value = e.target.value; + const parsed_value = replaceValue(raw_value); if (child_key !== undefined) { - setFunction({ - ...variable, + const new_var = { + ...local_var, [parent_key]: { - ...variable[parent_key], - [child_key]: parsedValue - } - }); + ...local_var[parent_key], + [child_key]: parsed_value, + }, + }; + setLocalVar(new_var); + scheduleUpdate(new_var); } else { - setFunction({ - ...variable, - [parent_key]: parsedValue - }); + const new_var = { + ...local_var, + [parent_key]: parsed_value, + }; + setLocalVar(new_var); + scheduleUpdate(new_var); } }; @@ -229,16 +258,16 @@ const InputComponent = ({id, variable, setFunction, format_id }) => { }; const resetFunction = () => { - if (format_id === "send") { - setFunction(ui_configs.send_message_format_parts); - } else if (format_id === "received") { - setFunction(ui_configs.received_message_format_parts); - } + const new_val = format_id === "send" ? ui_configs.send_message_format_parts : ui_configs.received_message_format_parts; + setLocalVar(new_val); + setFunction(new_val); }; const SwapButton = ({ variable, setFunction }) => { const swapMessageAndTranslate = () => { - setFunction({ ...variable, translation_first: !variable.translation_first }); + const new_var = { ...variable, translation_first: !variable.translation_first }; + setLocalVar(new_var); + setFunction(new_var); }; return ( @@ -255,38 +284,38 @@ const InputComponent = ({id, variable, setFunction, format_id }) => {
{label_title}
{LABEL_ORIGINAL}
- <_Entry ui_variable={toUiValue(variable.message.suffix)} width={ENTRY_WIDTH} onChange={handleChange("message", "suffix")} /> + <_Entry ui_variable={toUiValue(local_var.message.suffix)} width={ENTRY_WIDTH} onChange={handleChange("message", "suffix")} />{LABEL_TRANSLATED}
- <_Entry ui_variable={toUiValue(variable.translation.suffix)} width={ENTRY_WIDTH} onChange={handleChange("translation", "suffix")} /> + <_Entry ui_variable={toUiValue(local_var.translation.suffix)} width={ENTRY_WIDTH} onChange={handleChange("translation", "suffix")} />{LABEL_TRANSLATED}
- <_Entry ui_variable={toUiValue(variable.translation.suffix)} width={ENTRY_WIDTH} onChange={handleChange("translation", "suffix")} /> + <_Entry ui_variable={toUiValue(local_var.translation.suffix)} width={ENTRY_WIDTH} onChange={handleChange("translation", "suffix")} />{LABEL_ORIGINAL}
- <_Entry ui_variable={toUiValue(variable.message.suffix)} width={ENTRY_WIDTH} onChange={handleChange("message", "suffix")} /> + <_Entry ui_variable={toUiValue(local_var.message.suffix)} width={ENTRY_WIDTH} onChange={handleChange("message", "suffix")} />{LABEL_FOR_MULTI_TRANSLATION}
{LABEL_TRANSLATED}
- <_Entry ui_variable={toUiValue(variable.translation.separator)} width={ENTRY_WIDTH} onChange={handleChange("translation", "separator")} /> + <_Entry ui_variable={toUiValue(local_var.translation.separator)} width={ENTRY_WIDTH} onChange={handleChange("translation", "separator")} />{LABEL_TRANSLATED}