Skip to content

Commit

Permalink
Add live updates for timer and queue
Browse files Browse the repository at this point in the history
  • Loading branch information
au2001 committed Dec 27, 2023
1 parent 7caf055 commit cac2191
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ export async function searchTracks(query: string) {

export async function pushTrack(uri: string) {
const spotify = await getSpotify();
if (spotify === null)
return Promise.reject(new Error("Spotify API not initialized"));
if (spotify === null) throw new Error("Spotify API not initialized");

if ((await spotify.user.player.addItem(uri)) === true) Promise.resolve();
else Promise.reject(new Error("Failed to push track"));
const success = await spotify.user.player.addItem(uri);
if (!success) throw new Error("Failed to push track");
}
27 changes: 27 additions & 0 deletions src/app/timer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use client";

import { formatDuration } from "@/utils/ui";
import { useEffect, useState } from "react";

interface TimerProps {
timestamp: number;
duration: number;
}

export function Timer({ timestamp, duration }: TimerProps) {
const [now, setNow] = useState(Date.now());

useEffect(() => {
const interval = setInterval(() => setNow(Date.now()), 200);

return () => clearInterval(interval);
}, []);

return (
<>
{formatDuration(Math.min(Math.max(now - timestamp, 0), duration))}
{" / "}
{formatDuration(duration)}
</>
);
}
10 changes: 9 additions & 1 deletion src/app/titlebar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
"use client";

import { useState } from "react";
import { useEffect, useState } from "react";
import Image from "next/image";
import styles from "./titlebar.module.css";
import Search from "./search";
import Modal from "./modal";
import { useRouter } from "next/navigation";

interface Props {
active: boolean;
}

export default function TitleBar({ active }: Props) {
const router = useRouter();
const [isOpen, setIsOpen] = useState(false);

useEffect(() => {
const interval = setInterval(() => router.refresh(), 10 * 1000);

return () => clearInterval(interval);
}, [router]);

return (
<>
<header className={styles.header}>
Expand Down
9 changes: 5 additions & 4 deletions src/app/track_cover.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Image from "next/image";
import * as Spotify from "spotify-api.js";
import { formatDuration } from "@/utils/ui";
import styles from "./track_cover.module.css";
import { Timer } from "./timer";

interface Props {
currentPlayback: Spotify.CurrentPlayback;
Expand Down Expand Up @@ -33,9 +33,10 @@ export default function TrackCover({ currentPlayback }: Props) {
</td>
<td>
<div className={styles.duration}>
{formatDuration(currentPlayback.progress)}
{" / "}
{formatDuration(track.duration)}
<Timer
timestamp={currentPlayback.timestamp}
duration={track.duration}
/>
</div>
</td>
</tr>
Expand Down

0 comments on commit cac2191

Please sign in to comment.