[Refactor] Implement useSaveButtonLogic for managing save functionality in Translation settings.

This commit is contained in:
Sakamoto Shiina
2025-11-14 11:07:04 +09:00
parent 11c16fadbd
commit 28b65deb95
3 changed files with 66 additions and 91 deletions

View File

@@ -309,6 +309,39 @@ export const useSliderLogic = ({
}; };
}; };
export const useSaveButtonLogic = ({
variable,
state,
setFunction,
deleteFunction
}) => {
const [input_value, setInputValue] = useState(variable);
const onChangeFunction = (value) => {
setInputValue(value);
};
const saveFunction = () => {
if (input_value === "" || input_value === null) {
return deleteFunction();
}
setFunction(input_value);
};
useEffect(() => {
if (state === "pending") return;
setInputValue(variable);
}, [variable]);
return {
variable: input_value,
onChangeFunction: onChangeFunction,
saveFunction: saveFunction,
};
};
const createMarks = (min, max, marks_step = 1, labelFormatter = (value) => value) => { const createMarks = (min, max, marks_step = 1, labelFormatter = (value) => value) => {
const marks = []; const marks = [];
let variable = min; let variable = min;

View File

@@ -18,4 +18,5 @@ export { useSettingBoxScrollPosition } from "./config_page_setter/_aux/useSettin
export { export {
useSliderLogic, useSliderLogic,
useSaveButtonLogic,
} from "./config_page_setter/useSettingsLogics.js"; } from "./config_page_setter/useSettingsLogics.js";

View File

@@ -6,6 +6,8 @@ import { useStore_IsBreakPoint } from "@store";
import { import {
useTranslation, useTranslation,
useSaveButtonLogic,
} from "@logics_configs"; } from "@logics_configs";
import { import {
@@ -32,7 +34,7 @@ export const Translation = () => {
<CTranslate2WeightType_Box /> <CTranslate2WeightType_Box />
<TranslationComputeDevice_Box /> <TranslationComputeDevice_Box />
<DeeplAuthKey_Box /> <DeepLAuthKey_Box />
<PlamoAuthKey_Box /> <PlamoAuthKey_Box />
<PlamoModelContainer /> <PlamoModelContainer />
@@ -217,26 +219,16 @@ const TranslationComputeDevice_Box = () => {
); );
}; };
const DeeplAuthKey_Box = () => { const DeepLAuthKey_Box = () => {
const { t } = useI18n(); const { t } = useI18n();
const { currentDeepLAuthKey, setDeepLAuthKey, deleteDeepLAuthKey } = useTranslation(); const { currentDeepLAuthKey, setDeepLAuthKey, deleteDeepLAuthKey } = useTranslation();
const [input_value, seInputValue] = useState(currentDeepLAuthKey.data);
const onChangeFunction = (value) => { const { variable, onChangeFunction, saveFunction } = useSaveButtonLogic({
seInputValue(value); variable: currentDeepLAuthKey.data,
}; state: currentDeepLAuthKey.state,
setFunction: setDeepLAuthKey,
const saveFunction = () => { deleteFunction: deleteDeepLAuthKey,
if (input_value === "" || input_value === null) { });
return deleteDeepLAuthKey();
};
setDeepLAuthKey(input_value);
};
useEffect(() => {
if (currentDeepLAuthKey.state === "pending") return;
seInputValue(currentDeepLAuthKey.data);
}, [currentDeepLAuthKey]);
return ( return (
<> <>
@@ -248,7 +240,7 @@ const DeeplAuthKey_Box = () => {
)} )}
webpage_url={deepl_auth_key_url} webpage_url={deepl_auth_key_url}
open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")} open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")}
variable={input_value} variable={variable}
state={currentDeepLAuthKey.state} state={currentDeepLAuthKey.state}
onChangeFunction={onChangeFunction} onChangeFunction={onChangeFunction}
saveFunction={saveFunction} saveFunction={saveFunction}
@@ -260,37 +252,22 @@ const DeeplAuthKey_Box = () => {
const PlamoAuthKey_Box = () => { const PlamoAuthKey_Box = () => {
const { t } = useI18n(); const { t } = useI18n();
const { currentPlamoAuthKey, setPlamoAuthKey, deletePlamoAuthKey } = useTranslation(); const { currentPlamoAuthKey, setPlamoAuthKey, deletePlamoAuthKey } = useTranslation();
const [input_value, seInputValue] = useState(currentPlamoAuthKey.data);
const onChangeFunction = (value) => { const { variable, onChangeFunction, saveFunction } = useSaveButtonLogic({
seInputValue(value); variable: currentPlamoAuthKey.data,
}; state: currentPlamoAuthKey.state,
setFunction: setPlamoAuthKey,
const saveFunction = () => { deleteFunction: deletePlamoAuthKey,
if (input_value === "" || input_value === null) { });
return deletePlamoAuthKey();
};
setPlamoAuthKey(input_value);
};
useEffect(() => {
if (currentPlamoAuthKey.state === "pending") return;
seInputValue(currentPlamoAuthKey.data);
}, [currentPlamoAuthKey]);
return ( return (
<> <>
<AuthKeyContainer <AuthKeyContainer
label="Plamo Auth Key" label="Plamo Auth Key"
desc="Plamo Auth Desc" desc="Plamo Auth Desc"
// label={t("config_page.translation.deepl_auth_key.label")}
// desc={t(
// "config_page.translation.deepl_auth_key.desc",
// {translator: t("main_page.translator")}
// )}
// webpage_url={deepl_auth_key_url} // webpage_url={deepl_auth_key_url}
// open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")} // open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")}
variable={input_value} variable={variable}
state={currentPlamoAuthKey.state} state={currentPlamoAuthKey.state}
onChangeFunction={onChangeFunction} onChangeFunction={onChangeFunction}
saveFunction={saveFunction} saveFunction={saveFunction}
@@ -318,8 +295,6 @@ const PlamoModelContainer = () => {
<DropdownMenuContainer <DropdownMenuContainer
dropdown_id="select_plamo_model" dropdown_id="select_plamo_model"
label="Select Plamo Model" label="Select Plamo Model"
// label={t("config_page.transcription.mic_phrase_timeout.label")}
// desc={t("config_page.transcription.mic_phrase_timeout.desc")}
selected_id={currentSelectedPlamoModel.data} selected_id={currentSelectedPlamoModel.data}
list={currentSelectablePlamoModelList.data} list={currentSelectablePlamoModelList.data}
selectFunction={selectFunction} selectFunction={selectFunction}
@@ -333,37 +308,22 @@ const PlamoModelContainer = () => {
const GeminiAuthKey_Box = () => { const GeminiAuthKey_Box = () => {
const { t } = useI18n(); const { t } = useI18n();
const { currentGeminiAuthKey, setGeminiAuthKey, deleteGeminiAuthKey } = useTranslation(); const { currentGeminiAuthKey, setGeminiAuthKey, deleteGeminiAuthKey } = useTranslation();
const [input_value, seInputValue] = useState(currentGeminiAuthKey.data);
const onChangeFunction = (value) => { const { variable, onChangeFunction, saveFunction } = useSaveButtonLogic({
seInputValue(value); variable: currentGeminiAuthKey.data,
}; state: currentGeminiAuthKey.state,
setFunction: setGeminiAuthKey,
const saveFunction = () => { deleteFunction: deleteGeminiAuthKey,
if (input_value === "" || input_value === null) { });
return deleteGeminiAuthKey();
};
setGeminiAuthKey(input_value);
};
useEffect(() => {
if (currentGeminiAuthKey.state === "pending") return;
seInputValue(currentGeminiAuthKey.data);
}, [currentGeminiAuthKey]);
return ( return (
<> <>
<AuthKeyContainer <AuthKeyContainer
label="Gemini Auth Key" label="Gemini Auth Key"
desc="Gemini Auth Desc" desc="Gemini Auth Desc"
// label={t("config_page.translation.deepl_auth_key.label")}
// desc={t(
// "config_page.translation.deepl_auth_key.desc",
// {translator: t("main_page.translator")}
// )}
// webpage_url={deepl_auth_key_url} // webpage_url={deepl_auth_key_url}
// open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")} // open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")}
variable={input_value} variable={variable}
state={currentGeminiAuthKey.state} state={currentGeminiAuthKey.state}
onChangeFunction={onChangeFunction} onChangeFunction={onChangeFunction}
saveFunction={saveFunction} saveFunction={saveFunction}
@@ -391,8 +351,6 @@ const GeminiModelContainer = () => {
<DropdownMenuContainer <DropdownMenuContainer
dropdown_id="select_gemini_model" dropdown_id="select_gemini_model"
label="Select Gemini Model" label="Select Gemini Model"
// label={t("config_page.transcription.mic_phrase_timeout.label")}
// desc={t("config_page.transcription.mic_phrase_timeout.desc")}
selected_id={currentSelectedGeminiModel.data} selected_id={currentSelectedGeminiModel.data}
list={currentSelectableGeminiModelList.data} list={currentSelectableGeminiModelList.data}
selectFunction={selectFunction} selectFunction={selectFunction}
@@ -405,37 +363,22 @@ const GeminiModelContainer = () => {
const OpenAIAuthKey_Box = () => { const OpenAIAuthKey_Box = () => {
const { t } = useI18n(); const { t } = useI18n();
const { currentOpenAIAuthKey, setOpenAIAuthKey, deleteOpenAIAuthKey } = useTranslation(); const { currentOpenAIAuthKey, setOpenAIAuthKey, deleteOpenAIAuthKey } = useTranslation();
const [input_value, seInputValue] = useState(currentOpenAIAuthKey.data);
const onChangeFunction = (value) => { const { variable, onChangeFunction, saveFunction } = useSaveButtonLogic({
seInputValue(value); variable: currentOpenAIAuthKey.data,
}; state: currentOpenAIAuthKey.state,
setFunction: setOpenAIAuthKey,
const saveFunction = () => { deleteFunction: deleteOpenAIAuthKey,
if (input_value === "" || input_value === null) { });
return deleteOpenAIAuthKey();
};
setOpenAIAuthKey(input_value);
};
useEffect(() => {
if (currentOpenAIAuthKey.state === "pending") return;
seInputValue(currentOpenAIAuthKey.data);
}, [currentOpenAIAuthKey]);
return ( return (
<> <>
<AuthKeyContainer <AuthKeyContainer
label="OpenAI Auth Key" label="OpenAI Auth Key"
desc="OpenAI Auth Desc" desc="OpenAI Auth Desc"
// label={t("config_page.translation.deepl_auth_key.label")}
// desc={t(
// "config_page.translation.deepl_auth_key.desc",
// {translator: t("main_page.translator")}
// )}
// webpage_url={deepl_auth_key_url} // webpage_url={deepl_auth_key_url}
// open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")} // open_webpage_label={t("config_page.translation.deepl_auth_key.open_auth_key_webpage")}
variable={input_value} variable={variable}
state={currentOpenAIAuthKey.state} state={currentOpenAIAuthKey.state}
onChangeFunction={onChangeFunction} onChangeFunction={onChangeFunction}
saveFunction={saveFunction} saveFunction={saveFunction}
@@ -463,8 +406,6 @@ const OpenAIModelContainer = () => {
<DropdownMenuContainer <DropdownMenuContainer
dropdown_id="select_openai_model" dropdown_id="select_openai_model"
label="Select OpenAI Model" label="Select OpenAI Model"
// label={t("config_page.transcription.mic_phrase_timeout.label")}
// desc={t("config_page.transcription.mic_phrase_timeout.desc")}
selected_id={currentSelectedOpenAIModel.data} selected_id={currentSelectedOpenAIModel.data}
list={currentSelectableOpenAIModelList.data} list={currentSelectableOpenAIModelList.data}
selectFunction={selectFunction} selectFunction={selectFunction}