mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
made nextjs-ready config
This commit is contained in:
parent
28b8242b4d
commit
8bac6bb2f7
8 changed files with 45 additions and 40 deletions
5
src/utils/config/index.ts
Normal file
5
src/utils/config/index.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
export const CONFIG = {
|
||||
API_HOST: process.env.REACT_APP_API_HOST || process.env.NEXT_PUBLIC_API_HOST || '',
|
||||
REMOTE_CURRENT:
|
||||
process.env.REACT_APP_REMOTE_CURRENT || process.env.NEXT_PUBLIC_REMOTE_CURRENT || '',
|
||||
};
|
|
@ -19,6 +19,7 @@ import {
|
|||
formatTextTodos,
|
||||
} from '~/utils/formatText';
|
||||
import { splitTextByYoutube, splitTextOmitEmpty } from '~/utils/splitText';
|
||||
import { CONFIG } from '~/utils/config';
|
||||
|
||||
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
|
||||
const angleInRadians = ((angleInDegrees - 90) * Math.PI) / 180.0;
|
||||
|
@ -67,13 +68,10 @@ export const getURLFromString = (
|
|||
size?: typeof PRESETS[keyof typeof PRESETS]
|
||||
): string => {
|
||||
if (size) {
|
||||
return (url || '').replace(
|
||||
'REMOTE_CURRENT://',
|
||||
`${process.env.REACT_APP_REMOTE_CURRENT}cache/${size}/`
|
||||
);
|
||||
return (url || '').replace('REMOTE_CURRENT://', `${CONFIG.REMOTE_CURRENT}cache/${size}/`);
|
||||
}
|
||||
|
||||
return (url || '').replace('REMOTE_CURRENT://', process.env.REACT_APP_REMOTE_CURRENT);
|
||||
return (url || '').replace('REMOTE_CURRENT://', CONFIG.REMOTE_CURRENT);
|
||||
};
|
||||
|
||||
export const getURL = (
|
||||
|
|
|
@ -42,23 +42,27 @@ const PlayerContext = createContext<AudioPlayerProps>({
|
|||
});
|
||||
|
||||
export const AudioPlayerProvider: FC = ({ children }) => {
|
||||
const audio = useRef(new Audio()).current;
|
||||
const audio = useRef(typeof Audio !== 'undefined' ? new Audio() : undefined).current;
|
||||
const [status, setStatus] = useState(PlayerState.UNSET);
|
||||
const [file, setFile] = useState<IFile | undefined>();
|
||||
const [progress, setProgress] = useState<PlayerProgress>({ progress: 0, current: 0, total: 0 });
|
||||
|
||||
/** controls */
|
||||
const play = audio.play.bind(audio);
|
||||
const pause = audio.pause.bind(audio);
|
||||
const play = async () => audio?.play();
|
||||
const pause = () => audio?.pause();
|
||||
const stop = useCallback(() => {
|
||||
audio.pause();
|
||||
audio.dispatchEvent(new CustomEvent('stop'));
|
||||
audio?.pause();
|
||||
audio?.dispatchEvent(new CustomEvent('stop'));
|
||||
setFile(undefined);
|
||||
setStatus(PlayerState.UNSET);
|
||||
}, [audio, setFile]);
|
||||
|
||||
const toTime = useCallback(
|
||||
(time: number) => {
|
||||
if (!audio) {
|
||||
return;
|
||||
}
|
||||
|
||||
audio.currentTime = time;
|
||||
},
|
||||
[audio]
|
||||
|
@ -66,18 +70,18 @@ export const AudioPlayerProvider: FC = ({ children }) => {
|
|||
|
||||
const toPercent = useCallback(
|
||||
(percent: number) => {
|
||||
audio.currentTime = (progress.total * percent) / 100;
|
||||
toTime((progress.total * percent) / 100);
|
||||
},
|
||||
[progress, audio]
|
||||
[progress, toTime]
|
||||
);
|
||||
|
||||
/** handles progress update */
|
||||
useEffect(() => {
|
||||
const onProgress = () => {
|
||||
setProgress({
|
||||
total: audio.duration,
|
||||
current: audio.currentTime,
|
||||
progress: (audio.currentTime / audio.duration) * 100,
|
||||
total: audio?.duration ?? 0,
|
||||
current: audio?.currentTime ?? 0,
|
||||
progress: audio ? (audio.currentTime / audio.duration) * 100 : 0,
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -89,21 +93,23 @@ export const AudioPlayerProvider: FC = ({ children }) => {
|
|||
setStatus(PlayerState.PLAYING);
|
||||
};
|
||||
|
||||
audio.addEventListener('playprogress', onProgress);
|
||||
audio.addEventListener('timeupdate', onProgress);
|
||||
audio.addEventListener('pause', onPause);
|
||||
audio.addEventListener('playing', onPlay);
|
||||
audio?.addEventListener('playprogress', onProgress);
|
||||
audio?.addEventListener('timeupdate', onProgress);
|
||||
audio?.addEventListener('pause', onPause);
|
||||
audio?.addEventListener('playing', onPlay);
|
||||
|
||||
return () => {
|
||||
audio.removeEventListener('playprogress', onProgress);
|
||||
audio.removeEventListener('timeupdate', onProgress);
|
||||
audio.removeEventListener('pause', onPause);
|
||||
audio.removeEventListener('playing', onPlay);
|
||||
audio?.removeEventListener('playprogress', onProgress);
|
||||
audio?.removeEventListener('timeupdate', onProgress);
|
||||
audio?.removeEventListener('pause', onPause);
|
||||
audio?.removeEventListener('playing', onPlay);
|
||||
};
|
||||
}, [audio]);
|
||||
|
||||
/** update audio src */
|
||||
useEffect(() => {
|
||||
if (!audio) return;
|
||||
|
||||
audio.src = file ? getURL(file) : '';
|
||||
}, [file, audio]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue