From dcc05d953679c30b81ea3fb94a920af42fa3b44e Mon Sep 17 00:00:00 2001 From: eXhumer Date: Sat, 26 Oct 2024 09:02:18 -0600 Subject: [PATCH] refactor: decouple Controller component * 275ms delayPromise no longer needed * loading state no longer needed Signed-off-by: eXhumer --- backend/Cargo.lock | 2 +- backend/Cargo.toml | 2 +- package.json | 2 +- src/components/Controller.tsx | 54 ++++++++++++++++++++++++++++ src/components/ControllersView.tsx | 47 +++++------------------- src/components/NoControllersView.tsx | 8 ++--- src/components/PluginContent.tsx | 24 +++++-------- 7 files changed, 76 insertions(+), 63 deletions(-) create mode 100644 src/components/Controller.tsx diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 15cfb55..31525e5 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -178,7 +178,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "controller-tools" -version = "2.0.1" +version = "2.0.2" dependencies = [ "anyhow", "axum", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 64ed5f0..53957bd 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "controller-tools" -version = "2.0.1" +version = "2.0.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/package.json b/package.json index c8fc853..77f29f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "controller-tools", - "version": "2.0.1", + "version": "2.0.2", "description": "The missing game controller menu. Displays the current battery % and charging status", "type": "module", "scripts": { diff --git a/src/components/Controller.tsx b/src/components/Controller.tsx new file mode 100644 index 0000000..ce26228 --- /dev/null +++ b/src/components/Controller.tsx @@ -0,0 +1,54 @@ +import { + PanelSectionRow, + gamepadDialogClasses, + joinClassNames, +} from "@decky/ui"; + +import { IconContext } from "react-icons"; +import { BiBluetooth, BiUsb } from "react-icons/bi"; + +import BatteryIcon from "./BatteryIcon"; +import VendorIcon from "./VendorIcon"; +import { IController } from "../types"; + +const FieldWithSeparator = joinClassNames(gamepadDialogClasses.Field, gamepadDialogClasses.WithBottomSeparatorStandard); + +type ControllerProps = { + controller: IController; +}; + +const Controller = ({ controller }: ControllerProps) => { + return ( + +
+
+
+ + {controller.bluetooth ? : } + + + + + {controller.name} +
+ { + (controller.capacity > 0 || controller.status !== "unknown") && +
+ { + // only show battery capacity for non-MS vendors unless capacity is > 0 and over BT + // since we don't have the battery capacity yet for Xbox over USB + (controller.vendorId != 0x045E || (controller.capacity > 0 && controller.bluetooth)) && + {controller.capacity}% + } + + + +
+ } +
+
+
+ ); +}; + +export default Controller; diff --git a/src/components/ControllersView.tsx b/src/components/ControllersView.tsx index 9d0a2be..e8d0786 100644 --- a/src/components/ControllersView.tsx +++ b/src/components/ControllersView.tsx @@ -1,11 +1,5 @@ -import { gamepadDialogClasses, joinClassNames, PanelSectionRow } from "@decky/ui"; import { IController } from "../types"; -import { IconContext } from "react-icons"; -import { BiBluetooth, BiUsb } from "react-icons/bi"; -import VendorIcon from "./VendorIcon"; -import BatteryIcon from "./BatteryIcon"; - -const FieldWithSeparator = joinClassNames(gamepadDialogClasses.Field, gamepadDialogClasses.WithBottomSeparatorStandard); +import Controller from "./Controller"; type ControllersViewProps = { controllers: IController[]; @@ -13,37 +7,14 @@ type ControllersViewProps = { const ControllersView = ({ controllers }: ControllersViewProps) => { return ( - controllers.sort((a, b) => a.name.localeCompare(b.name)).map((controller) => ( - -
-
-
- - {controller.bluetooth ? : } - - - - - {controller.name} -
- { - (controller.capacity > 0 || controller.status !== "unknown") && -
- { - // only show battery capacity for non-MS vendors unless capacity is > 0 and over BT - // since we don't have the battery capacity yet for Xbox over USB - (controller.vendorId != 0x045E || (controller.capacity > 0 && controller.bluetooth)) && - {controller.capacity}% - } - - - -
- } -
-
-
- )) + controllers + .sort((a, b) => a.name.localeCompare(b.name)) + .map(controller => ( + + )) ); }; diff --git a/src/components/NoControllersView.tsx b/src/components/NoControllersView.tsx index c30852c..3b181e6 100644 --- a/src/components/NoControllersView.tsx +++ b/src/components/NoControllersView.tsx @@ -2,17 +2,13 @@ import { gamepadDialogClasses, joinClassNames, PanelSectionRow } from "@decky/ui const FieldWithSeparator = joinClassNames(gamepadDialogClasses.Field, gamepadDialogClasses.WithBottomSeparatorStandard); -type NoControllersViewProps = { - loading: boolean; -}; - -const NoControllersView = ({ loading }: NoControllersViewProps) => { +const NoControllersView = () => { return (
- {loading ? 'Loading...' : 'No controllers found'} + No controllers found
diff --git a/src/components/PluginContent.tsx b/src/components/PluginContent.tsx index c8697e4..7d1da0a 100644 --- a/src/components/PluginContent.tsx +++ b/src/components/PluginContent.tsx @@ -10,22 +10,15 @@ import * as backend from "../backend"; import { IController } from "../types"; import ControllersView from "./ControllersView"; -const delayPromise = (value: T) => { - return new Promise(resolve => { - setTimeout(() => resolve(value), 275); - }); -}; - const PluginContent = () => { const [debug, setDebug] = useState(false); const [notifications, setNotifications] = useState(true); - const [loading, setLoading] = useState(false); const [controllers, setControllers] = useState([]); // For fetching controller & settings data on render useEffect(() => { backend.getControllers() - .then((controllers) => { setControllers(controllers); }); + .then(controllers => { setControllers(controllers); }); backend.getDebugSetting() .then(debug => { setDebug(debug); }); @@ -34,11 +27,10 @@ const PluginContent = () => { .then(notifications => { setNotifications(notifications); }); }, []); - const onRefresh = async () => { - setControllers([]); - setLoading(true); - setControllers(await delayPromise(backend.getControllers())); - setLoading(false); + const onRefresh = () => { + backend + .getControllers() + .then(controllers => { setControllers(controllers); }); }; const onDebugChange = (e: boolean) => { @@ -60,9 +52,9 @@ const PluginContent = () => { return ( {controllers.length === 0 ? - : - } - + : + } +