From eae861456f5de2df08a8f6a2ecd9e6cf7ca68194 Mon Sep 17 00:00:00 2001 From: Beebles <102569435+beebls@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:32:13 -0600 Subject: [PATCH] update beta translation toggle to dropdown --- .../pythonMethods/pluginSettingsMethods.ts | 10 ++++-- src/backend/pythonMethods/storeUtils.ts | 20 +++++++++++ src/pages/settings/PluginSettings.tsx | 35 +++++++++++-------- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/backend/pythonMethods/pluginSettingsMethods.ts b/src/backend/pythonMethods/pluginSettingsMethods.ts index 4663694..20f35bf 100644 --- a/src/backend/pythonMethods/pluginSettingsMethods.ts +++ b/src/backend/pythonMethods/pluginSettingsMethods.ts @@ -1,6 +1,6 @@ -import { toast } from "../../python"; +import { storeRead, toast } from "../../python"; import { server, globalState } from "../pythonRoot"; -import { booleanStoreRead } from "./storeUtils"; +import { booleanStoreRead, stringStoreRead } from "./storeUtils"; export function enableServer() { return server!.callPluginMethod("enable_server", {}); @@ -25,7 +25,7 @@ export async function getWatchState() { } export async function getBetaTranslationsState() { - return booleanStoreRead("beta_translations"); + return stringStoreRead("beta_translations"); } export function toggleWatchState(bool: boolean, onlyThisSession: boolean = false) { @@ -50,3 +50,7 @@ export function getHiddenMotd() { key: "hiddenMotd", }); } + +export function fetchClassMappings() { + return server!.callPluginMethod<{}>("fetch_class_mappings", {}); +} diff --git a/src/backend/pythonMethods/storeUtils.ts b/src/backend/pythonMethods/storeUtils.ts index a5789a0..585e75a 100644 --- a/src/backend/pythonMethods/storeUtils.ts +++ b/src/backend/pythonMethods/storeUtils.ts @@ -16,7 +16,27 @@ export async function booleanStoreWrite(key: string, value: boolean) { key, val: value ? "1" : "0", }); + if (!deckyRes.success) { + toast(`Error setting ${key}`, deckyRes.result); + } +} + +export async function stringStoreRead(key: string) { + const deckyRes = await server!.callPluginMethod<{ key: string }, string>("store_read", { + key, + }); if (!deckyRes.success) { toast(`Error fetching ${key}`, deckyRes.result); + return ""; + } + return deckyRes.result; +} +export async function stringStoreWrite(key: string, value: string) { + const deckyRes = await server!.callPluginMethod<{ key: string; val: string }>("store_write", { + key, + val: value, + }); + if (!deckyRes.success) { + toast(`Error setting ${key}`, deckyRes.result); } } diff --git a/src/pages/settings/PluginSettings.tsx b/src/pages/settings/PluginSettings.tsx index c8d86f0..6184f61 100644 --- a/src/pages/settings/PluginSettings.tsx +++ b/src/pages/settings/PluginSettings.tsx @@ -1,4 +1,4 @@ -import { Focusable, ToggleField } from "decky-frontend-lib"; +import { DropdownItem, Focusable, ToggleField } from "decky-frontend-lib"; import { useMemo, useState, useEffect } from "react"; import { useCssLoaderState } from "../../state"; import { toast } from "../../python"; @@ -9,15 +9,16 @@ import { enableServer, toggleWatchState, getBetaTranslationsState, + fetchClassMappings, } from "../../backend/pythonMethods/pluginSettingsMethods"; -import { booleanStoreWrite } from "../../backend/pythonMethods/storeUtils"; +import { booleanStoreWrite, stringStoreWrite } from "../../backend/pythonMethods/storeUtils"; import { disableUnminifyMode, enableUnminifyMode } from "../../deckyPatches/UnminifyMode"; export function PluginSettings() { const { navPatchInstance, unminifyModeOn, setGlobalState } = useCssLoaderState(); const [serverOn, setServerOn] = useState(false); const [watchOn, setWatchOn] = useState(false); - const [betaTranslationsOn, setBetaTranslationsOn] = useState(false); + const [betaTranslationsOn, setBetaTranslationsOn] = useState("-1"); const navPatchEnabled = useMemo(() => !!navPatchInstance, [navPatchInstance]); @@ -31,6 +32,10 @@ export function PluginSettings() { } async function fetchBetaTranslationsState() { const value = await getBetaTranslationsState(); + if (![!"0", "1", "-1"].includes(value)) { + setBetaTranslationsOn("-1"); + return; + } setBetaTranslationsOn(value); } @@ -60,23 +65,25 @@ export function PluginSettings() { await fetchServerState(); } - async function setBetaTranslations(enabled: boolean) { - await booleanStoreWrite("beta_translations", enabled); + async function setBetaTranslations(value: string) { + await stringStoreWrite("beta_translations", value); + await fetchClassMappings(); await fetchBetaTranslationsState(); - toast( - "Beta translations " + (enabled ? "enabled" : "disabled") + ".", - "Please restart your Deck to apply changes." - ); } return (
- setBetaTranslations(data.data)} />