Skip to content

Commit

Permalink
change favicon when mode changes and load previous timer used
Browse files Browse the repository at this point in the history
  • Loading branch information
MacielG1 committed Jul 31, 2024
1 parent 3feb607 commit 4456e0f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/components/MainMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import vibrate from "../utils/Vibrate";
import sound1 from "../assets/sounds/sound1.mp3";
import sound2 from "../assets/sounds/sound2.mp3";
import { inputSettings as lang } from "../utils/lang";
import { changeFavicon } from "../utils/changeFavicon";
import convertSecToMinSec from "../utils/convertSecToMinSec";

export default function MainMenu() {
const [totalRounds, currentRound, currentRoundIncrease] = useStore((state) => [state.roundsSelected, state.currentRound, state.currentRoundIncrease]);
Expand All @@ -32,6 +34,10 @@ export default function MainMenu() {
const [enableSounds, enableVibrate, prepareonEveryRound] = useStore((state) => [state.enableSounds, state.enableVibrate, state.prepareonEveryRound]);
const preferredLanguage = useStore((state) => state.preferredLanguage);

const totalTimePassed = useStore((state) => state.totalTimePassed);

Check failure on line 37 in src/components/MainMenu.tsx

View workflow job for this annotation

GitHub Actions / deploy

'totalTimePassed' is declared but its value is never read.

const changeTitle = (title: string) => (document.title = title);

useEffect(() => {
let isMounted = true; // used in the cleanup function

Expand All @@ -45,6 +51,8 @@ export default function MainMenu() {
if (whichInterval === "prepare") {
setProgressBarMax(prepTime);
setCurrentProgressColor(prepColor);
changeFavicon("timer.svg");

if (enableBackgroundColors) {
setCurrentBackgroundColor(prepColor);
setRemoveUIBorders(true);
Expand All @@ -54,6 +62,7 @@ export default function MainMenu() {
} else if (whichInterval === "work") {
setProgressBarMax(workTime);
setCurrentProgressColor(workColor);
changeFavicon("timer-green.svg");
if (enableBackgroundColors) {
setCurrentBackgroundColor(workColor);
setRemoveUIBorders(true);
Expand All @@ -63,6 +72,9 @@ export default function MainMenu() {
} else if (whichInterval === "rest") {
setProgressBarMax(restTime);
setCurrentProgressColor(restColor);
changeFavicon("timer-red.svg");
changeTitle("Timer Interval");

if (enableBackgroundColors) {
setCurrentBackgroundColor(restColor);
setRemoveUIBorders(true);
Expand Down Expand Up @@ -93,12 +105,17 @@ export default function MainMenu() {
if (currentRound === totalRounds && skipLastRest) {
timer?.stop();
resetTimer();
changeFavicon("timer.svg");
changeTitle("Timer Interval");
return;
}
} else if (whichInterval === "rest" && time > +restTime) {
if (currentRound === totalRounds) {
timer?.stop();
resetTimer();
changeFavicon("timer.svg");
changeTitle("Timer Interval");

return;
} else {
currentRoundIncrease();
Expand All @@ -112,11 +129,15 @@ export default function MainMenu() {
} else if (time !== 0 && (whichInterval === "work" || whichInterval === "rest") && currentRound <= totalRounds) {
increaseTotalTimePassed();
}
changeTitle(convertSecToMinSec(time));

setProgressBarValue(`${time}`);
}

return () => {
isMounted = false;
changeFavicon("timer.svg");
changeTitle("Interval Timer");
}; // eslint-disable-next-line react-hooks/exhaustive-deps
}, [time]);

Expand Down
13 changes: 7 additions & 6 deletions src/components/Progress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,20 @@ export default function Progress() {
setWhichInterval("rest");
changeInterval();

setTimer(
new Timer(1000, () => {
timeIncrease();
}),
);

if (currentRound === totalRounds && skipLastRest) {
timer?.stop();
resetTimer();
return;
} else {
setTimer(
new Timer(1000, () => {
timeIncrease();
}),
);
}
} else if (whichInterval === "rest") {
setCurrentRound(currentRound + 1);

if (currentRound === totalRounds) {
timer?.stop();
resetTimer();
Expand Down
1 change: 1 addition & 0 deletions src/components/SidebarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default function SidebarItem({ item }: { item: savedWorkoutType }) {
PrepareSeconds: timer.PrepareSeconds,
PrepColor: timer.PrepColor,
});
localStorage.setItem("latestTimer", JSON.stringify(timer));

// adds an effect to the inputs when the button is clicked
SetIsLoadingSavedTimer(true);
Expand Down
3 changes: 3 additions & 0 deletions src/components/TimerApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import SaveTimer from "./SaveTimer";
import useStore from "../store/useStore";
import MenuButtons from "./MenuButtons";
import { useState, useEffect } from "react";
import { changeFavicon } from "../utils/changeFavicon";

Check failure on line 8 in src/components/TimerApp.tsx

View workflow job for this annotation

GitHub Actions / deploy

'changeFavicon' is declared but its value is never read.

export default function TimerApp() {
const [removeBorders, mainTimerBorder] = useStore((state) => [state.removeUIBorders, state.mainTimerBorder]);
const [enableBackgroundColors, savedWorkouts] = useStore((state) => [state.enableBackgroundColors, state.savedWorkouts]);
const [isSmallScreen, setIsSmallScreen] = useState(window.matchMedia("(max-width: 600px)").matches);
const whichInterval = useStore((state) => state.whichInterval);

Check failure on line 14 in src/components/TimerApp.tsx

View workflow job for this annotation

GitHub Actions / deploy

'whichInterval' is declared but its value is never read.
const timer = useStore((state) => state.timer);

Check failure on line 15 in src/components/TimerApp.tsx

View workflow job for this annotation

GitHub Actions / deploy

'timer' is declared but its value is never read.

function handleMediaQueryChange(mediaQuery: MediaQueryListEvent) {
if (mediaQuery.matches) {
Expand Down
13 changes: 11 additions & 2 deletions src/components/inputs/InputNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react";
import { useEffect, useRef, useState } from "react";
import useStore from "../../store/useStore";
import pad from "../../utils/PadNum";

Expand All @@ -13,12 +13,21 @@ export default function InputNumber(props: InputNumberProps) {
const { label, className, nogap, inputStoreType, maxLength = 3 } = props;

const [isFocused, setIsFocused] = useState(false);
const count = useStore((state) => state[inputStoreType]);
let count = useStore((state) => state[inputStoreType]);
const increase = useStore((state) => state[`increase${inputStoreType}`]);
const decrease = useStore((state) => state[`decrease${inputStoreType}`]);
const setCount = useStore((state) => state[`set${inputStoreType}`]);
const isLoadingSavedTimer = useStore((state) => state.isLoadingSavedTimer);

useEffect(() => {
const latestTimer = localStorage.getItem("latestTimer");

if (latestTimer) {
const parsedTimer = JSON.parse(latestTimer);
setCount(parsedTimer[inputStoreType]);
}
}, []);

const timerRef = useRef<number | null>(null);

function incrementHold() {
Expand Down
4 changes: 2 additions & 2 deletions src/store/slices/TimerStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export type timerStatusType = {
time: number;
setTime: (value: number) => void;
timeIncrease: () => void;
whichInterval: string;
setWhichInterval: (value: string) => void;
whichInterval: "work" | "rest" | "prepare";
setWhichInterval: (value: "work" | "rest" | "prepare") => void;
currentRound: number;
setCurrentRound: (value: number) => void;
currentRoundIncrease: () => void;
Expand Down
1 change: 1 addition & 0 deletions src/store/slices/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default storageSlice;
export type localStorageType = {
savedWorkouts: savedWorkoutType[];
setSavedWorkouts: (value: savedWorkoutType[]) => void;

skipLastRest: boolean;
setSkipLastRest: (value: boolean) => void;
enableBackgroundColors: boolean;
Expand Down
7 changes: 7 additions & 0 deletions src/utils/changeFavicon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function changeFavicon(url: string) {
const link = (document.querySelector("link[rel*='icon']") || document.createElement("link")) as HTMLLinkElement;
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}

0 comments on commit 4456e0f

Please sign in to comment.