Skip to content

Commit

Permalink
feat: implement UI configuration modal and refactor UI state manageme…
Browse files Browse the repository at this point in the history
…nt (#80)
  • Loading branch information
jLynx authored Dec 9, 2024
1 parent eb33319 commit af7a2dd
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 79 deletions.
91 changes: 12 additions & 79 deletions src/components/Controller/Controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {
faRotate,
faCheckCircle,
faGears,
faGear,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
Expand All @@ -21,13 +20,15 @@ import { useSerial } from "@/components/SerialLoader/SerialLoader";
import ToggleSwitch from "@/components/ToggleSwitch/ToggleSwitch";
import { useDeviceSetup } from "@/hooks/useDeviceSetup";
import { useScreenFrame } from "@/hooks/useScreenFrame";
import { ConfigItem, ILatestVersions, IUIConfig } from "@/types";
import { useUIConfig } from "@/hooks/useUIConfig";
import { ILatestVersions } from "@/types";
import { downloadFileFromUrl, useWriteCommand } from "@/utils/serialUtils";
import {
getVersionType,
nightlyVersionFormat,
stableVersionFormat,
} from "@/utils/versionUtils";
import UIConfigurationModal from "../UIConfigurationModal/UIConfigurationModal";

const Controller = () => {
const [consoleMessageList, setConsoleMessageList] = useState<string>("");
Expand All @@ -49,37 +50,6 @@ const Controller = () => {

const [UIConfigurationOpen, setUIConfigurationOpen] =
useState<boolean>(false);
const [UIConfig, setUiConfig] = useState<IUIConfig>({
screenHide: false,
controlButtonsHide: false,
fileSystemHide: false,
serialConsoleHide: false,
firmwareManagerHide: false,
});

const uiConfigItems: ConfigItem[] = [
{
key: "controlButtonsHide",
label: "Hide Control Buttons",
},
{
key: "screenHide",
label: "Hide Screen",
onToggle: () => toggleLiveScreen(!UIConfig.screenHide),
},
{
key: "fileSystemHide",
label: "Hide File System",
},
{
key: "serialConsoleHide",
label: "Hide Serial Console",
},
{
key: "firmwareManagerHide",
label: "Hide Firmware Manager",
},
];

const { serial, consoleMessage } = useSerial();
const { write, uploadFile, disableTransmitAction, setLoadingFrame } =
Expand All @@ -92,6 +62,7 @@ const Controller = () => {
setLatestVersion,
});
const { canvasRef, renderFrame } = useScreenFrame();
const { UIConfig, setUiConfig, handleUpdateUiHide } = useUIConfig();

const sendCommand = async () => {
await write(command, false);
Expand Down Expand Up @@ -282,17 +253,6 @@ const Controller = () => {
}
};

const handleUpdateUiHide = (
value: boolean,
key: keyof IUIConfig,
setInState: (stateValue: boolean) => void
) => {
const updatedValue = !value;
const newConfig = { ...UIConfig, [key]: updatedValue };
localStorage.setItem("uiConfig", JSON.stringify(newConfig));
setInState(updatedValue);
};

const toggleLiveScreen = (shouldUpdate: boolean) => {
if (!shouldUpdate) write("screenframeshort", false);
setAutoUpdateFrame(!shouldUpdate);
Expand Down Expand Up @@ -321,13 +281,6 @@ const Controller = () => {
}
}, [consoleMessageList]);

useEffect(() => {
const storedConfig = localStorage.getItem("uiConfig");
if (storedConfig) {
setUiConfig(JSON.parse(storedConfig));
}
}, []);

return (
<>
{setupComplete ? (
Expand Down Expand Up @@ -493,34 +446,14 @@ const Controller = () => {
/>
)}
</Modal>
<Modal
title="UI Configuration"
isModalOpen={UIConfigurationOpen}
closeModal={() => setUIConfigurationOpen(false)}
className="w-[20%]"
>
<div className="mb-3 flex flex-col items-center justify-center rounded-lg p-4 font-medium text-white outline-none focus:ring-0 md:items-start">
<div className="flex w-full flex-col items-start justify-start gap-5">
{uiConfigItems.map((item) => (
<ToggleSwitch
key={item.key}
toggleLabel={item.label}
isToggle={UIConfig[item.key]}
toggleSwitch={() => {
handleUpdateUiHide(
UIConfig[item.key],
item.key,
(updatedValue) => {
setUiConfig({ ...UIConfig, [item.key]: updatedValue });
item.onToggle?.();
}
);
}}
/>
))}
</div>
</div>
</Modal>
<UIConfigurationModal
isOpen={UIConfigurationOpen}
onClose={() => setUIConfigurationOpen(false)}
UIConfig={UIConfig}
setUiConfig={setUiConfig}
handleUpdateUiHide={handleUpdateUiHide}
toggleLiveScreen={toggleLiveScreen}
/>
</>
);
};
Expand Down
82 changes: 82 additions & 0 deletions src/components/UIConfigurationModal/UIConfigurationModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Modal from "@/components/Modal/Modal";
import ToggleSwitch from "@/components/ToggleSwitch/ToggleSwitch";
import { ConfigItem, IUIConfig } from "@/types";

interface IUIConfigurationModal {
isOpen: boolean;
onClose: () => void;
UIConfig: IUIConfig;
setUiConfig: (config: IUIConfig) => void;
handleUpdateUiHide: (
value: boolean,
key: keyof IUIConfig,
setInState: (stateValue: boolean) => void
) => void;
toggleLiveScreen: (shouldUpdate: boolean) => void;
}

const UIConfigurationModal: React.FC<IUIConfigurationModal> = ({
isOpen,
onClose,
UIConfig,
setUiConfig,
handleUpdateUiHide,
toggleLiveScreen,
}) => {
const uiConfigItems: ConfigItem[] = [
{
key: "controlButtonsHide",
label: "Hide Control Buttons",
},
{
key: "screenHide",
label: "Hide Screen",
onToggle: () => toggleLiveScreen(!UIConfig.screenHide),
},
{
key: "fileSystemHide",
label: "Hide File System",
},
{
key: "serialConsoleHide",
label: "Hide Serial Console",
},
{
key: "firmwareManagerHide",
label: "Hide Firmware Manager",
},
];

return (
<Modal
title="UI Configuration"
isModalOpen={isOpen}
closeModal={onClose}
className="w-[20%]"
>
<div className="mb-3 flex flex-col items-center justify-center rounded-lg p-4 font-medium text-white outline-none focus:ring-0 md:items-start">
<div className="flex w-full flex-col items-start justify-start gap-5">
{uiConfigItems.map((item) => (
<ToggleSwitch
key={item.key}
toggleLabel={item.label}
isToggle={UIConfig[item.key]}
toggleSwitch={() => {
handleUpdateUiHide(
UIConfig[item.key],
item.key,
(updatedValue) => {
setUiConfig({ ...UIConfig, [item.key]: updatedValue });
item.onToggle?.();
}
);
}}
/>
))}
</div>
</div>
</Modal>
);
};

export default UIConfigurationModal;
32 changes: 32 additions & 0 deletions src/hooks/useUIConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useEffect, useState } from "react";
import { IUIConfig } from "@/types";

export const useUIConfig = () => {
const [UIConfig, setUiConfig] = useState<IUIConfig>({
screenHide: false,
controlButtonsHide: false,
fileSystemHide: false,
serialConsoleHide: false,
firmwareManagerHide: false,
});

const handleUpdateUiHide = (
value: boolean,
key: keyof IUIConfig,
setInState: (stateValue: boolean) => void
) => {
const updatedValue = !value;
const newConfig = { ...UIConfig, [key]: updatedValue };
localStorage.setItem("uiConfig", JSON.stringify(newConfig));
setInState(updatedValue);
};

useEffect(() => {
const storedConfig = localStorage.getItem("uiConfig");
if (storedConfig) {
setUiConfig(JSON.parse(storedConfig));
}
}, []);

return { UIConfig, setUiConfig, handleUpdateUiHide };
};

0 comments on commit af7a2dd

Please sign in to comment.