Skip to content

Commit

Permalink
[FE] FEAT: 가독성을 위해 displayStyleToggle 을 toggleType 으로 변경 #1649
Browse files Browse the repository at this point in the history
  • Loading branch information
gykoh42 committed Dec 19, 2024
1 parent fb0c171 commit 6d0b837
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions frontend/src/Cabinet/components/Common/DarkModeToggleSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
} from "@/Cabinet/types/enum/displayStyle.type.enum";

const DarkModeToggleSwitch = ({ id }: { id: string }) => {
const [displayStyleToggle, setDisplayStyleToggle] =
useRecoilState(displayStyleState);
const [toggleType, setToggleType] = useRecoilState(displayStyleState);
const darkModeQuery = isDeviceDarkMode();
const [displayStyleType, setDisplayStyleType] = useState<DisplayStyleType>(
() => {
Expand All @@ -32,47 +31,47 @@ const DarkModeToggleSwitch = ({ id }: { id: string }) => {
const isDarkMode = displayStyleType === DisplayStyleType.DARK;

useEffect(() => {
setDisplayStyleToggle(getDisplayStyleFromLocalStorage());
setToggleType(getDisplayStyleFromLocalStorage());
}, []);

useEffect(() => {
const updateDisplayStyleType = () => {
const newDisplayStyleType = getInitialDisplayStyle(
displayStyleToggle,
toggleType,
darkModeQuery
);
setDisplayStyleType(newDisplayStyleType);
};

updateDisplayStyleType();
if (displayStyleToggle === DisplayStyleToggleType.DEVICE) {
if (toggleType === DisplayStyleToggleType.DEVICE) {
darkModeQuery.addEventListener("change", updateDisplayStyleType);
return () => {
darkModeQuery.removeEventListener("change", updateDisplayStyleType);
};
}
}, [displayStyleToggle]);
}, [toggleType]);

useEffect(() => {
updateBodyDisplayStyle(displayStyleType);
}, [displayStyleType]);

const handleToggleChange = useCallback(() => {
let newToggleType;
if (displayStyleToggle === DisplayStyleToggleType.DEVICE) {
if (toggleType === DisplayStyleToggleType.DEVICE) {
newToggleType =
displayStyleType === DisplayStyleType.LIGHT
? DisplayStyleToggleType.DARK
: DisplayStyleToggleType.LIGHT;
} else {
newToggleType =
displayStyleToggle === DisplayStyleToggleType.LIGHT
toggleType === DisplayStyleToggleType.LIGHT
? DisplayStyleToggleType.DARK
: DisplayStyleToggleType.LIGHT;
}

updateLocalStorageDisplayStyleToggle(newToggleType);
setDisplayStyleToggle(newToggleType);
setToggleType(newToggleType);
}, [displayStyleType]);

return (
Expand Down

0 comments on commit 6d0b837

Please sign in to comment.