Skip to content

Commit

Permalink
[✨feat]: 동적import, 상수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
yejinleee committed Apr 23, 2024
1 parent 9033848 commit d0d692f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/pages/GamePage/GameCode/GameCode.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCallback, useMemo, useRef } from 'react';
import IngameHeader from '@/common/Ingame/IngameHeader';
import IngameRank from '@/common/Ingame/IngameRank';
import playSoundEffect from '@/pages/GamePage/common/playSoundEffect';
import playSoundEffect, {
SOUND_SCORE,
} from '@/pages/GamePage/common/playSoundEffect';
import useIngameStore from '@/store/useIngameStore';
import CanvasTrack from '../common/CanvasTrack';
import TrackLine from '../common/TrackLine';
Expand Down Expand Up @@ -92,8 +94,8 @@ const GameCode = ({ publishIngame, userId }: GameCodeProps) => {
const handleUpdateScore = useCallback(() => {
const newScore = currentScore + scorePerSubmit;
publishIngame('/info', { currentScore: newScore });
const sound = playSoundEffect('SCORE');
sound.play();
const sound = playSoundEffect(SOUND_SCORE);
sound.then((audio) => audio.play());
}, [currentScore, scorePerSubmit]);

return (
Expand Down
10 changes: 6 additions & 4 deletions src/pages/GamePage/GameSentence/GameSentence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import Dashboard from '@/common/Ingame/Dashboard';
import IngameHeader from '@/common/Ingame/IngameHeader';
import IngameRank from '@/common/Ingame/IngameRank';
import { SentenceNext } from '@/common/Ingame/SentenceBlocks';
import playSoundEffect from '@/pages/GamePage/common/playSoundEffect';
import playSoundEffect, {
SOUND_SCORE,
} from '@/pages/GamePage/common/playSoundEffect';
import useIngameStore from '@/store/useIngameStore';
import CanvasTrack from '../common/CanvasTrack';
import TrackLine from '../common/TrackLine';
Expand All @@ -27,7 +29,7 @@ export interface I_RankInfoList {
}

const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
const sound = playSoundEffect('SCORE');
const sound = playSoundEffect(SOUND_SCORE);

const { ingameRoomRes, isRoundWaiting } = useIngameStore();

Expand All @@ -43,7 +45,7 @@ const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
const handleNextRound = useCallback(() => {
sentenceList.current = ingameRoomRes.questions!;
setSentenceIdx(0);
sound.play();
sound.then((audio) => audio.play());
}, [ingameRoomRes.questions]);

const {
Expand Down Expand Up @@ -96,7 +98,7 @@ const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
publishIngame('/info', {
currentScore: newScore,
});
sound.play();
sound.then((audio) => audio.play());
}, [currentScore, scorePerTrankLength]);

return (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/GamePage/GameWord/WordGameLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
I_IngameWsResponse,
PublishIngameType,
} from '../../GamePage/types/websocketType';
import playSoundEffect from '../common/playSoundEffect';
import playSoundEffect, { SOUND_SCORE } from '../common/playSoundEffect';
import useFocusInput from '../hooks/useFocusInput';
import WordCell from './WordCell';
import WordRankContainer from './WordRankContainer';
Expand Down Expand Up @@ -66,7 +66,7 @@ const WordGameLayout = ({
useEffect(() => {
onInputChange(0, 100, 150); //동기화..
}, [ingameRoomRes]);
const sound = playSoundEffect('SCORE');
const sound = playSoundEffect(SOUND_SCORE);

return (
<>
Expand Down Expand Up @@ -101,7 +101,7 @@ const WordGameLayout = ({
setValue('wordInput', '');
initializeTyping();
submitCount.current += 1;
sound.play();
sound.then((audio) => audio.play());
})}>
<input
autoFocus
Expand Down
6 changes: 3 additions & 3 deletions src/pages/GamePage/common/RoundWaitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Dialog from '@radix-ui/react-dialog';
import { useEffect } from 'react';
import { convertTime } from '@/common/Timer/utils/convertTime';
import useTimer from '@/hooks/useTimer';
import playSoundEffect from './playSoundEffect';
import playSoundEffect, { SOUND_COUNTDOWN } from './playSoundEffect';

interface RoundWaitModalProps {
isOpen: boolean;
Expand All @@ -18,11 +18,11 @@ const RoundWaitModal = ({ isOpen, onTimeFinish }: RoundWaitModalProps) => {
},
autoStart: false,
});
const sound = playSoundEffect('COUNTDOWN');
const sound = playSoundEffect(SOUND_COUNTDOWN);
useEffect(() => {
if (isOpen) {
startTimer();
sound.play();
sound.then((audio) => audio.play());
}
}, [isOpen]);

Expand Down
28 changes: 23 additions & 5 deletions src/pages/GamePage/common/playSoundEffect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import countDown from '@/assets/audio/countDown.mp3';
import soundEffect from '@/assets/audio/soundEffect.mp3';
export const SOUND_SCORE = 'SCORE';
export const SOUND_COUNTDOWN = 'COUNTDOWN';
type SoundEffectType = typeof SOUND_SCORE | typeof SOUND_COUNTDOWN;

const playSoundEffect = (type: 'SCORE' | 'COUNTDOWN') => {
const src = type === 'SCORE' ? soundEffect : countDown;
return new Audio(src);
const mappedSoundFiles: Record<SoundEffectType, () => Promise<string>> = {
SCORE: () =>
import('@/assets/audio/soundEffect.mp3').then((module) => module.default),
COUNTDOWN: () =>
import('@/assets/audio/countDown.mp3').then((module) => module.default),
};

const playSoundEffect = async (type: SoundEffectType) => {
const src = await mappedSoundFiles[type]();
const audio = new Audio(src);
return audio;
};
export default playSoundEffect;

// import countDown from '@/assets/audio/countDown.mp3';
// import soundEffect from '@/assets/audio/soundEffect.mp3';

// const playSoundEffect = (type: 'SCORE' | 'COUNTDOWN') => {
// const src = type === 'SCORE' ? soundEffect : countDown;
// return new Audio(src);
// };
// export default playSoundEffect;

0 comments on commit d0d692f

Please sign in to comment.