feat: add breathing exercise

This commit is contained in:
MAZE 2024-08-31 00:19:55 +03:30
parent 2bbdc7e09e
commit 1f2b6b952c
9 changed files with 205 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import { IoMdFlower } from 'react-icons/io/index';
import { Item } from '../item';
interface BreathingExerciseProps {
open: () => void;
}
export function BreathingExercise({ open }: BreathingExerciseProps) {
return (
<Item
icon={<IoMdFlower />}
label="Breathing Exercise"
shortcut="Shift + B"
onClick={open}
/>
);
}

View file

@ -5,3 +5,4 @@ export { Source as SourceItem } from './source';
export { Presets as PresetsItem } from './presets'; export { Presets as PresetsItem } from './presets';
export { Shortcuts as ShortcutsItem } from './shortcuts'; export { Shortcuts as ShortcutsItem } from './shortcuts';
export { SleepTimer as SleepTimerItem } from './sleep-timer'; export { SleepTimer as SleepTimerItem } from './sleep-timer';
export { BreathingExercise as BreathingExerciseItem } from './breathing-exercise';

View file

@ -12,12 +12,14 @@ import {
PresetsItem, PresetsItem,
ShortcutsItem, ShortcutsItem,
SleepTimerItem, SleepTimerItem,
BreathingExerciseItem,
} from './items'; } from './items';
import { Divider } from './divider'; import { Divider } from './divider';
import { ShareLinkModal } from '@/components/modals/share-link'; import { ShareLinkModal } from '@/components/modals/share-link';
import { PresetsModal } from '@/components/modals/presets'; import { PresetsModal } from '@/components/modals/presets';
import { ShortcutsModal } from '@/components/modals/shortcuts'; import { ShortcutsModal } from '@/components/modals/shortcuts';
import { SleepTimerModal } from '@/components/modals/sleep-timer'; import { SleepTimerModal } from '@/components/modals/sleep-timer';
import { BreathingExerciseModal } from '../modals/breathing';
import { fade, mix, slideY } from '@/lib/motion'; import { fade, mix, slideY } from '@/lib/motion';
import { useSoundStore } from '@/stores/sound'; import { useSoundStore } from '@/stores/sound';
@ -32,6 +34,7 @@ export function Menu() {
const initial = useMemo( const initial = useMemo(
() => ({ () => ({
breathing: false,
presets: false, presets: false,
shareLink: false, shareLink: false,
shortcuts: false, shortcuts: false,
@ -61,6 +64,7 @@ export function Menu() {
useHotkeys('shift+m', () => setIsOpen(prev => !prev)); useHotkeys('shift+m', () => setIsOpen(prev => !prev));
useHotkeys('shift+p', () => open('presets')); useHotkeys('shift+p', () => open('presets'));
useHotkeys('shift+h', () => open('shortcuts')); useHotkeys('shift+h', () => open('shortcuts'));
useHotkeys('shift+b', () => open('breathing'));
useHotkeys('shift+s', () => open('shareLink'), { enabled: !noSelected }); useHotkeys('shift+s', () => open('shareLink'), { enabled: !noSelected });
useHotkeys('shift+t', () => open('sleepTimer')); useHotkeys('shift+t', () => open('sleepTimer'));
@ -99,6 +103,7 @@ export function Menu() {
<ShareItem open={() => open('shareLink')} /> <ShareItem open={() => open('shareLink')} />
<ShuffleItem /> <ShuffleItem />
<SleepTimerItem open={() => open('sleepTimer')} /> <SleepTimerItem open={() => open('sleepTimer')} />
<BreathingExerciseItem open={() => open('breathing')} />
<Divider /> <Divider />
<ShortcutsItem open={() => open('shortcuts')} /> <ShortcutsItem open={() => open('shortcuts')} />
@ -118,6 +123,10 @@ export function Menu() {
show={modals.shareLink} show={modals.shareLink}
onClose={() => close('shareLink')} onClose={() => close('shareLink')}
/> />
<BreathingExerciseModal
show={modals.breathing}
onClose={() => close('breathing')}
/>
<ShortcutsModal <ShortcutsModal
show={modals.shortcuts} show={modals.shortcuts}
onClose={() => close('shortcuts')} onClose={() => close('shortcuts')}

View file

@ -0,0 +1 @@
/* WIP */

View file

@ -0,0 +1,18 @@
import { Modal } from '@/components/modal';
import { Exercise } from './exercise';
import styles from './breathing.module.css';
interface TimerProps {
onClose: () => void;
show: boolean;
}
export function BreathingExerciseModal({ onClose, show }: TimerProps) {
return (
<Modal show={show} onClose={onClose}>
<h2 className={styles.title}>Breathing Exercise</h2>
<Exercise />
</Modal>
);
}

View file

@ -0,0 +1,47 @@
.exercise {
position: relative;
z-index: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 75px 0;
margin-top: 12px;
background-color: var(--color-neutral-50);
border: 1px solid var(--color-neutral-200);
border-radius: 8px;
& .phase {
font-family: var(--font-display);
font-size: var(--font-lg);
font-weight: 600;
}
& .circle {
position: absolute;
top: 50%;
left: 50%;
z-index: -1;
height: 55%;
aspect-ratio: 1 / 1;
background-image: radial-gradient(
var(--color-neutral-50),
var(--color-neutral-100)
);
border: 1px solid var(--color-neutral-200);
border-radius: 50%;
transform: translate(-50%, -50%);
}
}
.selectBox {
width: 100%;
min-width: 0;
height: 45px;
padding: 0 12px;
margin-top: 8px;
font-size: var(--font-sm);
color: var(--color-foreground);
background-color: var(--color-neutral-100);
border: 1px solid var(--color-neutral-200);
border-radius: 8px;
}

View file

@ -0,0 +1,109 @@
import { useState, useEffect, useMemo, useCallback } from 'react';
import { motion } from 'framer-motion';
import styles from './exercise.module.css';
type Exercise = 'Box Breathing' | 'Resonant Breathing' | '4-7-8 Breathing';
type Phase = 'inhale' | 'exhale' | 'holdInhale' | 'holdExhale';
const EXERCISE_PHASES: Record<Exercise, Phase[]> = {
'4-7-8 Breathing': ['inhale', 'holdInhale', 'exhale'],
'Box Breathing': ['inhale', 'holdInhale', 'exhale', 'holdExhale'],
'Resonant Breathing': ['inhale', 'exhale'],
};
const EXERCISE_DURATIONS: Record<Exercise, Partial<Record<Phase, number>>> = {
'4-7-8 Breathing': { exhale: 8, holdInhale: 7, inhale: 4 },
'Box Breathing': { exhale: 4, holdExhale: 4, holdInhale: 4, inhale: 4 },
'Resonant Breathing': { exhale: 5, inhale: 5 }, // No holdExhale
};
const PHASE_LABELS: Record<Phase, string> = {
exhale: 'Exhale',
holdExhale: 'Hold',
holdInhale: 'Hold',
inhale: 'Inhale',
};
export function Exercise() {
const [selectedExercise, setSelectedExercise] =
useState<Exercise>('4-7-8 Breathing');
const [phaseIndex, setPhaseIndex] = useState(0);
const phases = useMemo(
() => EXERCISE_PHASES[selectedExercise],
[selectedExercise],
);
const durations = useMemo(
() => EXERCISE_DURATIONS[selectedExercise],
[selectedExercise],
);
const currentPhase = phases[phaseIndex];
const animationVariants = useMemo(
() => ({
exhale: {
transform: 'translate(-50%, -50%) scale(1)',
transition: { duration: durations.exhale },
},
holdExhale: {
transform: 'translate(-50%, -50%) scale(1)',
transition: { duration: durations.holdExhale },
},
holdInhale: {
transform: 'translate(-50%, -50%) scale(1.5)',
transition: { duration: durations.holdInhale },
},
inhale: {
transform: 'translate(-50%, -50%) scale(1.5)',
transition: { duration: durations.inhale },
},
}),
[durations],
);
const resetExercise = useCallback(() => {
setPhaseIndex(0);
}, []);
const updatePhase = useCallback(() => {
setPhaseIndex(prevIndex => (prevIndex + 1) % phases.length);
}, [phases.length]);
useEffect(() => {
resetExercise();
}, [selectedExercise, resetExercise]);
useEffect(() => {
const intervalDuration = (durations[currentPhase] || 4) * 1000;
const interval = setInterval(updatePhase, intervalDuration);
return () => clearInterval(interval);
}, [currentPhase, durations, updatePhase]);
return (
<>
<div className={styles.exercise}>
<motion.div
animate={currentPhase}
className={styles.circle}
key={selectedExercise}
variants={animationVariants}
/>
<p className={styles.phase}>{PHASE_LABELS[currentPhase]}</p>
</div>
<select
className={styles.selectBox}
value={selectedExercise}
onChange={e => setSelectedExercise(e.target.value as Exercise)}
>
{Object.keys(EXERCISE_PHASES).map(exercise => (
<option key={exercise} value={exercise}>
{exercise}
</option>
))}
</select>
</>
);
}

View file

@ -0,0 +1 @@
export { Exercise } from './exercise';

View file

@ -0,0 +1 @@
export { BreathingExerciseModal } from './breathing';