Skip to content

Commit

Permalink
fix(bgm): migrate to tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
shalluv committed Jan 31, 2024
1 parent ede849e commit e378389
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
37 changes: 37 additions & 0 deletions src/components/story/BGM.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Howl } from "howler";
import { useEffect, useMemo, useState } from "react";

const sound = new Howl({
src: ["/music/night-jazz.mp3"],
loop: true,
volume: 0.5,
autoplay: false,
html5: true,
onplayerror: function () {
sound.once("unlock", function () {
sound.play();
});
},
});
const BGM = (): JSX.Element => {
const playing = useMemo(() => {
return sound?.playing();
}, [sound]);
const [isPlaying, setPlaying] = useState(playing);

useEffect(() => {
setPlaying(playing);
}, [playing]);

useEffect(() => {
if (isPlaying) {
if (window.location.pathname === "/") sound.stop();
} else {
if (window.location.pathname.startsWith("/story")) sound.play();
}
}, [isPlaying]);

return <></>;
};

export default BGM;
23 changes: 2 additions & 21 deletions src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "@fontsource/ibm-plex-sans-thai/thai-500.css";
import "@/styles/global.css";
import { ViewTransitions } from "astro:transitions";
import BGM from "@/components/story/BGM";
interface Props {
pageTitle?: string;
}
Expand All @@ -29,26 +30,6 @@ const { pageTitle = "GearFestival" } = Astro.props;
>
<slot />
</main>
<script>
import { Howl } from "howler";
const sound = new Howl({
src: ["/music/night-jazz.mp3"],
loop: true,
volume: 0.5,
html5: true,
onplayerror: function () {
sound.once("unlock", function () {
sound.play();
});
},
});
document.addEventListener("astro:page-load", () => {
if (window.location.pathname === "/story/scene1") {
sound.play();
} else if (window.location.pathname === "/") {
sound.stop();
}
});
</script>
<BGM client:only />
</body>
</html>
2 changes: 1 addition & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kntuLogo from "@/assets/logo/kntu-logo.webp";
class="rounded-full backdrop-blur-lg"
/>
</div>
<Hero client:load />
<Hero client:only />
</div>
</SignInBackground>
</BaseLayout>
6 changes: 0 additions & 6 deletions src/pages/story/scene1.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,3 @@ import SceneLowRes from "@/assets/images/Scene1-low-res.webp";
text="เฮ้ออ<br />หมดไปอีกหนึ่งวัน"
redirect="/story/scene2"
/>

<script lang="ts">
import { resetScores } from "@/store/score";

resetScores();
</script>

0 comments on commit e378389

Please sign in to comment.