Skip to content

Commit

Permalink
feat() add options to enable/disable music
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Nov 30, 2023
1 parent 9bd1ea2 commit c240e88
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RouterProvider, createHashRouter, redirect } from 'react-router-dom';

import App from './App';
import { Game, JoinGame } from './modules/game';
import { About, Home, Rules } from './modules/home';
import { About, Home, Options, Rules } from './modules/home';

import './index.css';

Expand All @@ -18,6 +18,14 @@ const router = createHashRouter([
</React.StrictMode>
),
},
{
path: 'options',
element: (
<React.StrictMode>
<App music>{() => <Options />}</App>
</React.StrictMode>
),
},
{
path: 'rules',
element: (
Expand Down
3 changes: 3 additions & 0 deletions src/modules/home/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const Home = ({ account, provider }: HomeProps) => {
<div className={classNames('Home__link Home__link', { 'Home__link--disabled': createLoading })}>
<Link to="/join">Join a room</Link>
</div>
<div className={classNames('Home__link Home__link', { 'Home__link--disabled': createLoading })}>
<Link to="/options">Options</Link>
</div>
<div className={classNames('Home__link Home__link', { 'Home__link--disabled': createLoading })}>
<Link to="/rules">Rules</Link>
</div>
Expand Down
44 changes: 44 additions & 0 deletions src/modules/home/components/Options/Options.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.Options__account {
text-overflow: ellipsis;
max-width: 80%;
overflow: hidden;
}

.Options__menu {
margin: 15px 0;
}

.Options__link {
display: flex;
align-items: center;
}

.Options__link a,
.Options__link span {
font-family: Bungee;
font-size: 32px;
color: #fff;
text-shadow: -5px 5px 0 #000;
text-decoration: none;
cursor: pointer;
}

.Options__link a:hover,
.Options__link span:hover {
color: var(--primary-color);
}

.Options__link--disabled a,
.Options__link--disabled span {
color: #444 !important;
pointer-events: none;
}

.Options__link--small {
margin-top: 30px;
}

.Options__link--small a {
color: var(--fifth-color);
font-size: 1.4em;
}
37 changes: 37 additions & 0 deletions src/modules/home/components/Options/Options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import classNames from 'classnames';
import { useEffect, useState } from 'react';

import { demuteMusic, isMuted, muteMusic } from '../../../../utils/music';
import { Back, Title } from '../../../common-ui';

import './Options.css';

export const Options = () => {
const [muted, setMuted] = useState(isMuted());

useEffect(() => {
setMuted(isMuted());
}, []);

const onMute = () => {
if (!muted) {
console.log('muted');
muteMusic();
} else {
demuteMusic();
}
setMuted(!muted);
};

return (
<div className="Options">
<Back />
<Title>Options</Title>
<div className="Options__menu">
<div className={classNames('Options__link Options__link')}>
<span onClick={onMute}>Music: {muted ? 'OFF' : 'ON'}</span>
</div>
</div>
</div>
);
};
1 change: 1 addition & 0 deletions src/modules/home/components/Options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Options';
1 change: 1 addition & 0 deletions src/modules/home/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './components/Home';
export * from './components/Options';
export * from './components/Rules';
export * from './components/About';
29 changes: 27 additions & 2 deletions src/utils/music.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import music from '../assets/music.mp3';

const MUSIC_MUTED = 'musicMuted';

let enabled = false;
let waitToPlay = false;
const storedMuted = window.localStorage.getItem(MUSIC_MUTED);
let muted = (storedMuted && storedMuted === '1') || false;

const musicAudio = new Audio(music);
musicAudio.loop = true;
musicAudio.volume = 0.5;
musicAudio.volume = 0.3;

export const isMuted = () => {
return muted;
};

export const muteMusic = () => {
muted = true;
window.localStorage.setItem(MUSIC_MUTED, '1');
pauseMusic();
};

export const demuteMusic = () => {
muted = false;
window.localStorage.setItem(MUSIC_MUTED, '0');
playMusic();
};

export const playMusic = () => {
console.log(muted);
if (muted) return;
if (enabled) {
void musicAudio.play();
} else {
Expand All @@ -16,6 +38,7 @@ export const playMusic = () => {
};

export const pauseMusic = () => {
console.log('pause', enabled);
if (enabled) {
void musicAudio.pause();
} else {
Expand All @@ -25,7 +48,9 @@ export const pauseMusic = () => {

const enableMusic = () => {
enabled = true;
if (waitToPlay) void musicAudio.play();
if (waitToPlay) playMusic();
document.removeEventListener('click', enableMusic);
document.removeEventListener('tap', enableMusic);
};

document.addEventListener('click', enableMusic);
Expand Down

0 comments on commit c240e88

Please sign in to comment.