This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/src/components/SongInfo.js

133 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-11-13 15:23:34 +00:00
import Image from "next/image";
2022-10-28 18:52:47 +00:00
import { useSettingsStore } from "../store/overlaySettingsStore";
import { useSongDataStore } from "../store/songDataStore";
2022-10-23 20:47:57 +00:00
import styles from "../styles/songInfo.module.css";
2022-10-10 12:14:25 +00:00
2022-10-28 18:52:47 +00:00
/**
* Format the given ms
*
* @param {Number} millis
* @returns The formatted time
*/
function msToMinSeconds(millis) {
const minutes = Math.floor(millis / 60000);
const seconds = Number(((millis % 60000) / 1000).toFixed(0));
return seconds === 60
? minutes + 1 + ":00"
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
}
2022-10-28 18:52:47 +00:00
/**
* Update the difficulity color from the given difficulity
*
* @param {string} diff
*/
function formatDiff(diff) {
if (diff === "ExpertPlus") {
return "#8f48db";
2022-10-14 19:00:47 +00:00
}
2022-10-28 18:52:47 +00:00
if (diff === "Expert") {
return "#bf2a42";
}
if (diff === "Hard") {
return "tomato";
}
if (diff === "Normal") {
return "#59b0f4";
}
if (diff === "Easy") {
return "MediumSeaGreen";
2022-10-14 19:00:47 +00:00
}
2022-10-28 18:52:47 +00:00
}
export default function SongInfo() {
const [showSongInfo, shouldReplacePlayerInfoWithScore, songTimeHex] =
useSettingsStore((store) => [
store.showSongInfo,
store.shouldReplacePlayerInfoWithScore,
store.songTimeHex,
]);
2022-10-28 18:52:47 +00:00
const [
isLoading,
bsr,
mapArt,
songTitle,
songSubTitle,
songDifficulty,
currentSongTime,
songLength,
] = useSongDataStore((store) => [
store.isLoading,
store.bsr,
store.mapArt,
store.songTitle,
store.songSubTitle,
store.songDifficulty,
store.currentSongTime,
store.songLength,
]);
2022-10-28 18:52:47 +00:00
if (!showSongInfo) {
return null;
2022-10-14 19:00:47 +00:00
}
2022-10-28 18:52:47 +00:00
if (isLoading) {
return null;
}
2022-10-28 18:52:47 +00:00
const songTimerPercentage = (currentSongTime / songLength) * 100000;
const diffColor = formatDiff(songDifficulty);
2022-10-28 18:52:47 +00:00
return (
<div
className={styles.songInfoContainer}
style={{
bottom: shouldReplacePlayerInfoWithScore ? "" : 0,
left: shouldReplacePlayerInfoWithScore ? "" : 0,
position: shouldReplacePlayerInfoWithScore ? "" : "fixed",
2022-10-28 18:52:47 +00:00
}}
>
<Image
width={150}
height={150}
alt="Song artwork"
src={mapArt}
loading="lazy"
placeholder="blur"
2022-12-13 12:06:16 +00:00
blurDataURL="https://cdn.fascinated.cc/yb4fgdc1.jpg"
2022-10-28 18:52:47 +00:00
/>
<div className={styles.songInfo}>
<p className={styles.songInfoSongName}>
{songTitle.length > 35
? songTitle.substring(0, 35) + "..."
: songTitle}
</p>
<p className={styles.songInfoSongSubName}>{songSubTitle}</p>
<div className={styles.songInfoSongOtherContainer}>
<p
className={styles.songInfoDiff}
style={{ backgroundColor: diffColor }}
>
{songDifficulty.replace("Plus", "+")}
2022-10-14 19:00:47 +00:00
</p>
2022-10-28 18:52:47 +00:00
<p className={styles.songInfoBsr}>!bsr {bsr}</p>
</div>
<p className={styles.songTimeText}>
{msToMinSeconds(currentSongTime * 1000)}/{msToMinSeconds(songLength)}
</p>
<div className={styles.songTimeContainer}>
<div className={styles.songTimeBackground} />
<div
className={styles.songTime}
style={{
width: songTimerPercentage + "%",
backgroundColor: "#" + songTimeHex,
}}
2022-10-28 18:52:47 +00:00
/>
2022-10-14 19:00:47 +00:00
</div>
</div>
2022-10-28 18:52:47 +00:00
</div>
);
2022-10-14 19:00:47 +00:00
}