Skip to content

Commit

Permalink
bring back client rendered music section
Browse files Browse the repository at this point in the history
  • Loading branch information
pauleks committed Sep 9, 2022
1 parent bfe4eaf commit 5cff54c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
31 changes: 31 additions & 0 deletions components/Music.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import useSWR from "swr";
import ExtLink from "./ExtLink";
import { SWRResponse } from "swr";
import { SongInformation } from "../lib/Music.types";

const fetcher = (...args: any[]) => fetch(args[0]).then((res) => res.json());

const Music = () => {
const { data, error }: SWRResponse<SongInformation, Error> = useSWR(
"/api/v1/music",
fetcher
);

if (error || !data || !data.listening) return null;

return (
<p>
I&#39;m currently listening to{" "}
<ExtLink href={data.url as string}>
{data.title} by {data.artist}
</ExtLink>
<style jsx>{`
p {
opacity: 0.5;
}
`}</style>
</p>
);
};

export default Music;
22 changes: 0 additions & 22 deletions lib/Music.ts

This file was deleted.

30 changes: 30 additions & 0 deletions pages/api/v1/music.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { SongInformation } from "../../../lib/Music.types";

export default async (
req: NextApiRequest,
res: NextApiResponse<SongInformation>
) => {
if (!process.env?.LASTFMAPI)
return res.status(500).json({ listening: false });

let result = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=ghostwolfy&api_key=${process.env.LASTFMAPI}&format=json&limit=1`
).then((request) => request.json());

if (!result?.recenttracks?.track[0]["@attr"])
return res.status(200).json({ listening: false });

res.setHeader("Cache-Control", "s-maxage=30, stale-while-revalidate");

res.status(200).json({
listening: true,
artist: result.recenttracks.track[0].artist["#text"],
title: result.recenttracks.track[0].name,
url: `https://open.spotify.com/search/${encodeURIComponent(
result.recenttracks.track[0].artist["#text"] +
" " +
result.recenttracks.track[0].name
)}`,
});
}
24 changes: 3 additions & 21 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ import styles from "../styles/Home.module.css";
import ExtLink from "../components/ExtLink";
import Section from "../components/Section";
import Project from "../components/Project";
import Music from "../components/Music";
import { useState, useEffect } from "react";
import { GetMusic } from "../lib/Music";
import { SongInformation } from "../lib/Music.types";

export async function getStaticProps() {
const music = await GetMusic();
return {
props: {
music,
},
revalidate: 20,
};
}

const Home = ({ music }: { music: SongInformation }) => {
const Home = () => {
const [introductionPlace, setIntroductionPlace] = useState(0);
const [currentTime, setCurrentTime] = useState("");
const [animatedHeader, setAnimatedHeader] = useState("");
Expand Down Expand Up @@ -179,14 +168,7 @@ const Home = ({ music }: { music: SongInformation }) => {
</p>
</Section>

{music.listening && (
<p style={{ opacity: 0.5 }}>
I&#39;m currently listening to{" "}
<ExtLink href={music.url as string}>
{music.title} by {music.artist}
</ExtLink>
</p>
)}
<Music />
</>
);
};
Expand Down

1 comment on commit 5cff54c

@vercel
Copy link

@vercel vercel bot commented on 5cff54c Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.