Skip to content

Commit

Permalink
Merge pull request #1528 from vinaybadgujar102/quick_toggle
Browse files Browse the repository at this point in the history
Quick toggle dark/light mode based on current time
  • Loading branch information
tijlleenders authored Oct 2, 2023
2 parents e8545d2 + bb62b3d commit ca8d38a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/assets/images/darkModeIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/assets/images/lightModeIcon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { inboxIcon, openEnvelopeIcon } from "@assets/index";
import zinzenDarkLogo from "@assets/images/zinzenDarkLogo.svg";
import zinzenLightLogo from "@assets/images/zinzenLightLogo.svg";
import searchIcon from "@assets/images/searchIcon.svg";
import darkModeIcon from "@assets/images/darkModeIcon.svg";
import lightModeIcon from "@assets/images/lightModeIcon.svg";

import {
darkModeState,
Expand Down Expand Up @@ -76,6 +78,9 @@ const Header: React.FC<IHeader> = ({ title, debounceSearch }) => {
handlePopState();
}, [location]);

const currentHour = new Date().getHours();
const isNighttime = currentHour >= 18 || currentHour < 6;

return (
<div className="header">
{displaySearch && debounceSearch ? (
Expand All @@ -101,6 +106,11 @@ const Header: React.FC<IHeader> = ({ title, debounceSearch }) => {
</h6>
</div>
<div className="header-items">
{isNighttime && !darkModeStatus ? (
<HeaderBtn path={darkModeIcon} alt="dark mode" />
) : !isNighttime && darkModeStatus ? (
<HeaderBtn path={lightModeIcon} alt="light mode" />
) : null}
{["myGoals", "Inbox"].includes(title) && !isInboxOpen && (
<HeaderBtn path={searchIcon} alt="zinzen search" />
)}
Expand Down
13 changes: 10 additions & 3 deletions src/components/Header/HeaderBtn.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from "react";
import { openInbox, displayToast } from "@src/store";
import { openInbox, displayToast, darkModeState } from "@src/store";
import { useLocation, useNavigate } from "react-router-dom";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { useRecoilValue, useSetRecoilState, useRecoilState } from "recoil";
import Settings from "./Settings";

const HeaderBtn = ({ path, alt }: { path: string; alt: string }) => {
const { state } = useLocation();
const navigate = useNavigate();
const isInboxOpen = useRecoilValue(openInbox);
const setShowToast = useSetRecoilState(displayToast);
const [darkModeStatus, setDarkModeStatus] = useRecoilState(darkModeState);

const handleClick = async () => {
if (alt === "zinzen hints") {
Expand All @@ -28,7 +29,13 @@ const HeaderBtn = ({ path, alt }: { path: string; alt: string }) => {
}
navigate("/MyGoals", { state: { ...newState, goalsHistory: [], isInboxOpen: true } });
}
}
} else if (alt === "light mode") {
localStorage.setItem("darkMode", darkModeStatus ? "off" : "on");
setDarkModeStatus(!darkModeStatus);
} else if (alt == "dark mode") {
localStorage.setItem("darkMode", darkModeStatus ? "off" : "on");
setDarkModeStatus(!darkModeStatus);
}
};

return (
Expand Down

1 comment on commit ca8d38a

@vercel
Copy link

@vercel vercel bot commented on ca8d38a Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zinzen – ./

zinzen-tijlleenders.vercel.app
zinzen-git-main-tijlleenders.vercel.app
zinzen.vercel.app

Please sign in to comment.