From 7b355234898dc9f2f0f1a78e2d1ca98ac1bbe7f7 Mon Sep 17 00:00:00 2001 From: NovaBot <154629622+NovaBot13@users.noreply.github.com> Date: Thu, 16 May 2024 14:51:26 -0400 Subject: [PATCH] [MIRROR] Fixes simplebot ui [no gbp] (#2507) * Fixes simplebot ui [no gbp] (#83235) ## About The Pull Request I made this a very long time ago and whether or not it's been changed since to make this error is spilled milk. UI displays `0` when it's locked. This is bad. Cast your booleanlikes in JSX. ## Why It's Good For The Game Fixes a ui bug + simplifies code a bit ## Changelog :cl: fix: Simplebot UI won't display '0' anymore when locked /:cl: * Fixes simplebot ui [no gbp] --------- Co-authored-by: Jeremiah <42397676+jlsnow301@users.noreply.github.com> Co-authored-by: NovaBot13 --- tgui/packages/tgui/interfaces/SimpleBot.tsx | 263 ++++++++++---------- 1 file changed, 128 insertions(+), 135 deletions(-) diff --git a/tgui/packages/tgui/interfaces/SimpleBot.tsx b/tgui/packages/tgui/interfaces/SimpleBot.tsx index 1c63b5af9c7..8ac2b4201a7 100644 --- a/tgui/packages/tgui/interfaces/SimpleBot.tsx +++ b/tgui/packages/tgui/interfaces/SimpleBot.tsx @@ -1,3 +1,4 @@ +import { BooleanLike } from 'common/react'; import { capitalizeAll } from 'common/string'; import { useBackend } from 'tgui/backend'; import { @@ -12,34 +13,30 @@ import { } from 'tgui/components'; import { Window } from 'tgui/layouts'; -type SimpleBotContext = { - can_hack: number; - locked: number; - emagged: number; - has_access: number; +type Data = { + can_hack: BooleanLike; + custom_controls: Record; + emagged: BooleanLike; + has_access: BooleanLike; + locked: BooleanLike; settings: Settings; - custom_controls: Controls; }; type Settings = { - power: number; - airplane_mode: number; - maintenance_lock: number; - patrol_station: number; - allow_possession: number; - possession_enabled: number; - has_personality: number; + airplane_mode: BooleanLike; + allow_possession: BooleanLike; + has_personality: BooleanLike; + maintenance_lock: BooleanLike; pai_inserted: boolean; + patrol_station: BooleanLike; + possession_enabled: BooleanLike; + power: BooleanLike; }; -type Controls = { - [Control: string]: [Value: number]; -}; - -export const SimpleBot = (props) => { - const { data } = useBackend(); - const { can_hack, locked } = data; - const access = !locked || can_hack; +export function SimpleBot(props) { + const { data } = useBackend(); + const { can_hack, custom_controls, locked } = data; + const access = !locked || !!can_hack; return ( @@ -50,10 +47,20 @@ export const SimpleBot = (props) => { {!access ? Locked! : } - {access && ( + {!!access && (
- + + {Object.entries(custom_controls).map((control) => ( + + + + ))} +
)} @@ -61,17 +68,37 @@ export const SimpleBot = (props) => {
); -}; +} /** Creates a lock button at the top of the controls */ -const TabDisplay = (props) => { - const { act, data } = useBackend(); - const { can_hack, has_access, locked } = data; - const { allow_possession } = data.settings; +function TabDisplay(props) { + const { act, data } = useBackend(); + const { + can_hack, + emagged, + has_access, + locked, + settings: { allow_possession }, + } = data; return ( <> - {!!can_hack && } + {!!can_hack && ( + + )} {!!allow_possession && } ); -}; - -/** If user is a bad silicon, they can press this button to hack the bot */ -const HackButton = (props) => { - const { act, data } = useBackend(); - const { can_hack, emagged } = data; - - return ( - - ); -}; +} /** Creates a button indicating PAI status and offers the eject action */ -const PaiButton = (props) => { - const { act, data } = useBackend(); - const { pai_inserted } = data.settings; +function PaiButton(props) { + const { act, data } = useBackend(); + const { + settings: { pai_inserted }, + } = data; if (!pai_inserted) { return ( @@ -133,32 +139,33 @@ const PaiButton = (props) => { No PAI Inserted ); - } else { - return ( - - ); } -}; + + return ( + + ); +} /** Displays the bot's standard settings: Power, patrol, etc. */ -const SettingsDisplay = (props) => { - const { act, data } = useBackend(); - const { settings } = data; +function SettingsDisplay(props) { + const { act, data } = useBackend(); const { - airplane_mode, - patrol_station, - power, - maintenance_lock, - allow_possession, - possession_enabled, - } = settings; + settings: { + airplane_mode, + patrol_station, + power, + maintenance_lock, + allow_possession, + possession_enabled, + }, + } = data; return ( @@ -236,64 +243,50 @@ const SettingsDisplay = (props) => { )} ); -}; +} -/** Iterates over custom controls. - * Calls the helper to identify which button to use. - */ -const ControlsDisplay = (props) => { - const { data } = useBackend(); - const { custom_controls } = data; +enum ControlType { + MedbotSync = 'sync_tech', + MedbotThreshold = 'heal_threshold', + FloorbotTiles = 'tile_stack', + FloorbotLine = 'line_mode', +} - return ( - - {Object.entries(custom_controls).map((control) => { - return ( - - - - ); - })} - - ); +type ControlProps = { + control: [string, number]; }; /** Helper function which identifies which button to create. * Might need some fine tuning if you are using more advanced controls. */ -const ControlHelper = (props) => { - const { act } = useBackend(); +function ControlHelper(props: ControlProps) { + const { act } = useBackend(); const { control } = props; - if (control[0] === 'sync_tech') { - /** Control is for sync - this is medbot specific */ - return ; - } else if (control[0] === 'heal_threshold') { - /** Control is a threshold - this is medbot specific */ - return ; - } else if (control[0] === 'tile_stack') { - return ; - } else if (control[0] === 'line_mode') { - return ; - } else { - /** Control is a boolean of some type */ - return ( - act(control[0])} - /> - ); + + switch (control[0]) { + case ControlType.MedbotSync: + return ; + case ControlType.MedbotThreshold: + return ; + case ControlType.FloorbotTiles: + return ; + case ControlType.FloorbotLine: + return ; + default: + return ( + act(control[0])} + /> + ); } -}; +} /** Small button to sync medbots with research. */ -const MedbotSync = (props) => { - const { act } = useBackend(); +function MedbotSync(props) { + const { act } = useBackend(); return ( { /> ); -}; +} /** Slider button for medbot healing thresholds */ -const MedbotThreshold = (props) => { - const { act } = useBackend(); +function MedbotThreshold(props: ControlProps) { + const { act } = useBackend(); const { control } = props; return ( @@ -332,11 +325,11 @@ const MedbotThreshold = (props) => { /> ); -}; +} /** Tile stacks for floorbots - shows number and eject button */ -const FloorbotTiles = (props) => { - const { act } = useBackend(); +function FloorbotTiles(props: ControlProps) { + const { act } = useBackend(); const { control } = props; return ( @@ -349,11 +342,11 @@ const FloorbotTiles = (props) => { {control[1] ? `${control[1]}` : 'Empty'} ); -}; +} /** Direction indicator for floorbot when line mode is chosen. */ -const FloorbotLine = (props) => { - const { act } = useBackend(); +function FloorbotLine(props: ControlProps) { + const { act } = useBackend(); const { control } = props; return ( @@ -369,4 +362,4 @@ const FloorbotLine = (props) => { ); -}; +}