1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00
vault-frontend/src/containers/player/PlayerView/components/PlayerBar/index.tsx
2023-11-20 22:29:13 +06:00

85 lines
2.1 KiB
TypeScript

import React, { useCallback, VFC } from 'react';
import { Icon } from '~/components/common/Icon';
import { PlayerState } from '~/constants/player';
import { IFile } from '~/types';
import { PlayerProgress } from '~/types/player';
import { path } from '~/utils/ramda';
import styles from './styles.module.scss';
interface Props {
progress: PlayerProgress;
status: PlayerState;
file?: IFile;
playerPlay: () => void;
playerPause: () => void;
playerSeek: (pos: number) => void;
playerStop: () => void;
}
const PlayerBar: VFC<Props> = ({
status,
playerPlay,
playerPause,
playerSeek,
playerStop,
progress,
file,
}) => {
const onClick = useCallback(() => {
if (status === PlayerState.PLAYING) return playerPause();
return playerPlay();
}, [playerPlay, playerPause, status]);
const onSeek = useCallback(
(event) => {
event.stopPropagation();
const { clientX, target } = event;
const { left, width } = target.getBoundingClientRect();
playerSeek(((clientX - left) / width) * 100);
},
[playerSeek],
);
if (status === PlayerState.UNSET) return null;
const metadata: IFile['metadata'] = path(['metadata'], file);
const title =
metadata &&
(metadata.title ||
[metadata.id3artist, metadata.id3title].filter((el) => !!el).join(' - '));
return (
<div className={styles.place}>
<div className={styles.wrap}>
<div className={styles.status}>
<div className={styles.playpause} onClick={onClick}>
{status === PlayerState.PLAYING ? (
<Icon icon="pause" />
) : (
<Icon icon="play" />
)}
</div>
<div className={styles.info}>
<div className={styles.title}>{title}</div>
<div className={styles.progress} onClick={onSeek}>
<div
className={styles.bar}
style={{ width: `${progress.progress}%` }}
/>
</div>
</div>
<div className={styles.close} onClick={playerStop}>
<Icon icon="close" />
</div>
</div>
</div>
</div>
);
};
export { PlayerBar };