Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sound on timer #244

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/app/assets/cuckoo-clock.mp3
Binary file not shown.
26 changes: 18 additions & 8 deletions src/app/components/timer.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { Button, Group, Menu, Text } from "@mantine/core";
import { IconBellRinging, IconClock } from "@tabler/icons";
import { useEffect, useState } from "react";
import sound from "../../assets/cuckoo-clock.mp3";
import * as State from "../state";
import { Button, Group, Menu, Text } from "@mantine/core";
import { IconBellRinging, IconClock } from "@tabler/icons-react";

const audio = new Audio(sound);

export const Timer = () => {
const onTimerSet = (seconds: number) => State.startTimer(seconds);
const settings = () => State.getAppState().timer;

const reset = () => {
State.resetTimer();
setTimeLeft(undefined);
};

const calculateTimeLeft = () => {
const timeLeft = Math.floor(
((settings()?.duration ?? 0) * 1000 -
(new Date().getTime() - (settings()?.start ?? 0))) /
1000,
);
if (settings() === null) {
if (settings() === null || settings() === undefined) {
return undefined;
} else if (timeLeft <= 0) {
if (timeLeft >= -1 && timeLeft <= 0) {
audio.play();
setTimeout(() => audio.pause(), 6000);
}
reset();
return 0;
} else {
return timeLeft;
Expand All @@ -33,11 +47,6 @@
return () => clearInterval(interval);
});

const reset = () => {
State.resetTimer();
setTimeLeft(undefined);
};

return (
<Menu shadow="md" width={200}>
<Menu.Target>
Expand All @@ -49,10 +58,10 @@
{`${timeLeft % 60}`.padStart(2, "0")}
</Text>
)}
{timeLeft === 0 && <IconBellRinging color="orange" size={25} />}

Check failure on line 61 in src/app/components/timer.tsx

View workflow job for this annotation

GitHub Actions / build

Type '{ color: string; size: number; }' is not assignable to type 'IntrinsicAttributes'.
{timeLeft !== 0 && (
<IconClock
color={(timeLeft ?? 0) > 0 ? "red" : undefined}

Check failure on line 64 in src/app/components/timer.tsx

View workflow job for this annotation

GitHub Actions / build

Type '{ color: string | undefined; size: number; }' is not assignable to type 'IntrinsicAttributes'.
size={25}
/>
)}
Expand All @@ -68,6 +77,7 @@
)}
{(timeLeft === 0 || timeLeft === undefined) && (
<Menu.Dropdown>
<Menu.Item onClick={() => onTimerSet?.(10)}>10 seconds</Menu.Item>
<Menu.Item onClick={() => onTimerSet?.(120)}>2 minutes</Menu.Item>
<Menu.Item onClick={() => onTimerSet?.(5 * 60)}>5 minutes</Menu.Item>
<Menu.Item onClick={() => onTimerSet?.(10 * 60)}>
Expand Down
4 changes: 4 additions & 0 deletions types/asset.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.mp3" {
const value: any;
export default value;
}
Loading