-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement UI configuration modal and refactor UI state manageme…
…nt (#80)
- Loading branch information
Showing
3 changed files
with
126 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/components/UIConfigurationModal/UIConfigurationModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |