-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added audio and some system design changes
- Loading branch information
Showing
11 changed files
with
219 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
VITE_API_URL=http://localhost:8000 | ||
VITE_API_URL=http://localhost:8000 | ||
VITE_APP_LOG=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
VITE_API_URL=https://uvm015.dm.informatik.tu-darmstadt.de/api | ||
VITE_API_URL=https://uvm015.dm.informatik.tu-darmstadt.de/api | ||
VITE_APP_LOG=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { useStoreInLS } from './StorageProvider'; | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export enum MyAudio { | ||
BING = '/src/audio/bing.mp3', | ||
} | ||
|
||
const AudioContext = React.createContext({ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
playAudio: (_audio: MyAudio) => {}, | ||
toggleAudio: () => {}, | ||
isAudioEnabled: (): boolean => { | ||
return false; | ||
}, | ||
}); | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export function usePlayAudio() { | ||
const context = React.useContext(AudioContext); | ||
if (!context) { | ||
throw new Error('usePlayAudio must be used within a AudioProvider'); | ||
} | ||
return context.playAudio; | ||
} | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export function useToggleAudio() { | ||
const context = React.useContext(AudioContext); | ||
if (!context) { | ||
throw new Error('useToggleAudio must be used within a AudioProvider'); | ||
} | ||
return context.toggleAudio; | ||
} | ||
|
||
// eslint-disable-next-line react-refresh/only-export-components | ||
export function useIsAudioEnabled() { | ||
const context = React.useContext(AudioContext); | ||
if (!context) { | ||
throw new Error( | ||
'useIsAudioEnabled must be used within a AudioProvider' | ||
); | ||
} | ||
return context.isAudioEnabled; | ||
} | ||
|
||
interface Props { | ||
children: ReactNode; | ||
} | ||
|
||
/** | ||
* A provider that plays audio | ||
*/ | ||
export function AudioProvider({ children }: Props) { | ||
const [enabled, setEnabled] = React.useState(false); | ||
|
||
const storeInLS = useStoreInLS(); | ||
|
||
React.useEffect(() => { | ||
if (localStorage.getItem('audio-allowed') === 'true') { | ||
setEnabled(true); | ||
} | ||
}, []); | ||
|
||
const play = (audio: MyAudio) => { | ||
if (!enabled) { | ||
return; | ||
} | ||
const a = new Audio(audio); | ||
a.addEventListener('canplaythrough', () => { | ||
/* the audio is now playable; play it if permissions allow */ | ||
a.play(); | ||
}); | ||
a.play(); | ||
}; | ||
|
||
const toggle = () => { | ||
storeInLS('audio-allowed', JSON.stringify(!enabled)); | ||
setEnabled(!enabled); | ||
}; | ||
|
||
const isEnabled = () => { | ||
return enabled; | ||
}; | ||
|
||
return ( | ||
<AudioContext.Provider | ||
value={{ | ||
playAudio: play, | ||
toggleAudio: toggle, | ||
isAudioEnabled: isEnabled, | ||
}} | ||
> | ||
{children} | ||
</AudioContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* A logger class to log messages to the console only if the environment variable VITE_APP_LOG is set to true | ||
*/ | ||
class Logger { | ||
/** | ||
* Log a message to the console | ||
* @param message The message to log | ||
*/ | ||
public static log(message: unknown): void { | ||
if (import.meta.env.VITE_APP_LOG === 'false') return; | ||
console.log(message); | ||
} | ||
|
||
/** | ||
* Log a error to the console | ||
* @param message The message to log | ||
*/ | ||
public static error(message: unknown): void { | ||
if (import.meta.env.VITE_APP_LOG === 'false') return; | ||
console.error(message); | ||
} | ||
/** | ||
* Log a error to the console | ||
* @param message The message to log | ||
*/ | ||
public static warn(message: unknown): void { | ||
if (import.meta.env.VITE_APP_LOG === 'false') return; | ||
console.warn(message); | ||
} | ||
} | ||
|
||
export default Logger; |
Oops, something went wrong.