-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the top artist relations it's own page
- Loading branch information
Showing
3 changed files
with
95 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { LoaderArgs, json } from '@remix-run/node' | ||
import { useLoaderData } from '@remix-run/react' | ||
import retry from 'async-retry' | ||
|
||
import { spotifyStrategy } from '~/lib/auth.server' | ||
import { AppMetaFunction, mergeMeta } from '~/lib/remix' | ||
import { forwardServerTimingHeaders } from '~/lib/responses.server' | ||
import spotifyLib, { topArtistSearch } from '~/lib/spotify.server' | ||
import userSettings from '~/lib/userSettings.server' | ||
import wikipedia from '~/lib/wikipedia.server' | ||
|
||
import Album from '~/components/Album' | ||
import AlbumErrorBoundary from '~/components/Album/ErrorBoundary' | ||
import { Layout, Link } from '~/components/Base' | ||
import type { SearchBreadcrumbsProps } from '~/components/SearchBreadcrumbs' | ||
import config from '~/config' | ||
|
||
export async function loader({ request, context }: LoaderArgs) { | ||
const { serverTiming } = context | ||
const params = topArtistSearch.parse(new URL(request.url).searchParams) | ||
|
||
await serverTiming.track('spotify.session', () => | ||
spotifyStrategy.getSession(request, { | ||
failureRedirect: config.requiredLoginFailureRedirect, | ||
}), | ||
) | ||
|
||
const spotify = await serverTiming.track('spotify.init', () => | ||
spotifyLib.initializeFromRequest(request, context), | ||
) | ||
const { album, targetArtist } = await retry(async (_, attempt) => { | ||
const resp = await spotify.getRandomAlbumFromUsersTopArtists({ | ||
...params, | ||
related: true, | ||
}) | ||
serverTiming.add({ | ||
label: 'attempts', | ||
desc: `${attempt} Attempt(s)`, | ||
}) | ||
|
||
return resp | ||
}, config.asyncRetryConfig) | ||
const wiki = await serverTiming.track('wikipedia', () => | ||
wikipedia.getSummaryForAlbum({ | ||
album: album.name, | ||
artist: album.artists[0].name, | ||
}), | ||
) | ||
|
||
return json( | ||
{ | ||
album, | ||
targetArtist, | ||
wiki, | ||
}, | ||
{ | ||
headers: { | ||
'set-cookie': await userSettings.setLastPresented({ | ||
request, | ||
lastPresented: album.id, | ||
}), | ||
[serverTiming.headerKey]: serverTiming.toString(), | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
export const ErrorBoundary = AlbumErrorBoundary | ||
export const headers = forwardServerTimingHeaders | ||
export const meta: AppMetaFunction<typeof loader> = ({ matches }) => | ||
mergeMeta(matches, [ | ||
{ title: `Spotify Top Artist Relations | ${config.siteTitle}` }, | ||
]) | ||
|
||
export default function RandomAlbumFromRelatedTopArtistOnSpotify() { | ||
const data = useLoaderData<typeof loader>() | ||
const headerBreadcrumbs: SearchBreadcrumbsProps['crumbs'] = [ | ||
'Top Artists', | ||
'Related', | ||
[ | ||
data.targetArtist.name, | ||
<Link key="artist-link" to={`/spotify/artist-id/${data.targetArtist.id}`}> | ||
{data.targetArtist.name} | ||
</Link>, | ||
], | ||
] | ||
|
||
return ( | ||
<Layout hideFooter headerBreadcrumbs={headerBreadcrumbs}> | ||
<Album album={data.album} wiki={data.wiki} /> | ||
</Layout> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters