-
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.
feat: add a genre + artist search in the header
- Loading branch information
Showing
7 changed files
with
253 additions
and
15 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
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,101 @@ | ||
import { Form, useSubmit } from '@remix-run/react' | ||
import clsx from 'clsx' | ||
import { useCallback, useRef } from 'react' | ||
|
||
import type { SpotifyArtist } from '~/lib/types/spotify' | ||
import { cn } from '~/lib/util' | ||
|
||
import useUser from '~/hooks/useUser' | ||
|
||
import FunSelect, { type Option } from './FunSelect' | ||
|
||
interface Props { | ||
className?: string | ||
} | ||
|
||
const SuperHeaderSearch: React.FC<Props> = ({ className }) => { | ||
const user = useUser() | ||
const submit = useSubmit() | ||
const formRef = useRef<HTMLFormElement>(null) | ||
|
||
const search = useCallback( | ||
async (term?: string) => { | ||
const url = new URL(`${window.location.origin}/api/user-search`) | ||
|
||
if (term) { | ||
url.searchParams.set('search', term) | ||
} | ||
|
||
const resp = await fetch(url.toString(), { | ||
credentials: 'include', | ||
}) | ||
const { | ||
artists, | ||
genres, | ||
}: { artists: SpotifyArtist[]; genres: string[] } = await resp.json() | ||
|
||
let items: Option<{ itemType: string }>[] = [] | ||
|
||
if (artists.length) { | ||
items = items.concat( | ||
artists.map((artist) => ({ | ||
value: artist.id, | ||
label: artist.name, | ||
itemType: 'artist', | ||
labelElement: ( | ||
<div className={clsx('flex', 'flex-row', 'items-center')}> | ||
{artist.image && ( | ||
<img | ||
className={clsx('w-16', 'mr-2', 'rounded-lg')} | ||
src={artist.image.url} | ||
alt={artist.name} | ||
width={artist.image.width} | ||
height={artist.image.height} | ||
/> | ||
)} | ||
<span>{artist.name}</span> | ||
</div> | ||
), | ||
})), | ||
) | ||
} | ||
|
||
if (genres.length) { | ||
items = items.concat( | ||
genres.map((genre) => ({ | ||
value: genre, | ||
label: genre, | ||
itemType: 'genre', | ||
})), | ||
) | ||
} | ||
|
||
return items | ||
}, | ||
[user], | ||
) | ||
|
||
return ( | ||
<Form method="get" action="/search" className={cn(className)} ref={formRef}> | ||
<input type="hidden" name="itemType" /> | ||
<FunSelect | ||
name="itemID" | ||
placeholder="Search" | ||
onChange={(option: Option<{ itemType: string }>) => { | ||
setTimeout(() => { | ||
if (!formRef.current) { | ||
return | ||
} | ||
// @ts-ignore | ||
formRef.current.firstChild!.value = option.itemType | ||
submit(formRef.current) | ||
}, 5) | ||
}} | ||
loadOptions={search} | ||
className={className} | ||
/> | ||
</Form> | ||
) | ||
} | ||
|
||
export default SuperHeaderSearch |
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,52 @@ | ||
import { LoaderFunction, json } from '@remix-run/node' | ||
import { z } from 'zod' | ||
import { zfd } from 'zod-form-data' | ||
|
||
import { getRequestContextValues } from '~/lib/context.server' | ||
import { badRequest } from '~/lib/responses.server' | ||
import spotifyLib from '~/lib/spotify.server' | ||
|
||
import config from '~/config' | ||
|
||
const paramsSchema = zfd.formData({ | ||
search: zfd.text().optional(), | ||
artistLimit: z.coerce.number().min(1).max(50).optional().default(5), | ||
genreLimit: z.coerce.number().min(1).max(50).optional().default(10), | ||
}) | ||
|
||
export const loader: LoaderFunction = async ({ request, context }) => { | ||
const { serverTiming, logger, database } = getRequestContextValues( | ||
request, | ||
context, | ||
) | ||
const paramsParse = paramsSchema.safeParse(new URL(request.url).searchParams) | ||
|
||
if (!paramsParse.success) { | ||
throw badRequest({ | ||
logger, | ||
error: 'invalid query paramters', | ||
issues: paramsParse.error.issues, | ||
}) | ||
} | ||
|
||
const params = paramsParse.data | ||
const spotify = await spotifyLib.initializeFromRequest(request, context) | ||
const [artists, genres] = await Promise.all([ | ||
params.search | ||
? spotify.searchArists(params.search, params.artistLimit) | ||
: spotify.getUserTopArtists(params.artistLimit), | ||
params.search | ||
? database.searchGenres(params.search, params.genreLimit) | ||
: database.getTopGenres(params.genreLimit), | ||
]) | ||
|
||
return json( | ||
{ artists, genres }, | ||
{ | ||
headers: { | ||
'cache-control': config.cacheControl.private, | ||
[serverTiming.headerKey]: serverTiming.toString(), | ||
}, | ||
}, | ||
) | ||
} |
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,56 @@ | ||
import { LoaderFunction, json } from '@remix-run/node' | ||
import { z } from 'zod' | ||
import { zfd } from 'zod-form-data' | ||
|
||
import { spotifyStrategy } from '~/lib/auth.server' | ||
import { getRequestContextValues } from '~/lib/context.server' | ||
import { badRequest } from '~/lib/responses.server' | ||
import spotifyLib from '~/lib/spotify.server' | ||
|
||
import config from '~/config' | ||
|
||
const paramsSchema = zfd.formData({ | ||
search: zfd.text().optional(), | ||
artistLimit: z.coerce.number().min(1).max(50).optional().default(5), | ||
genreLimit: z.coerce.number().min(1).max(50).optional().default(10), | ||
}) | ||
|
||
export const loader: LoaderFunction = async ({ request, context }) => { | ||
const { serverTiming, logger, database } = getRequestContextValues( | ||
request, | ||
context, | ||
) | ||
await spotifyStrategy.getSession(request, { | ||
failureRedirect: config.requiredLoginFailureRedirect, | ||
}) | ||
const paramsParse = paramsSchema.safeParse(new URL(request.url).searchParams) | ||
|
||
if (!paramsParse.success) { | ||
throw badRequest({ | ||
logger, | ||
error: 'invalid query paramters', | ||
issues: paramsParse.error.issues, | ||
}) | ||
} | ||
|
||
const params = paramsParse.data | ||
const spotify = await spotifyLib.initializeFromRequest(request, context) | ||
const [artists, genres] = await Promise.all([ | ||
params.search | ||
? spotify.searchArists(params.search, params.artistLimit) | ||
: spotify.getUserTopArtists(params.artistLimit), | ||
params.search | ||
? database.searchGenres(params.search, params.genreLimit) | ||
: database.getTopGenres(params.genreLimit), | ||
]) | ||
|
||
return json( | ||
{ artists, genres }, | ||
{ | ||
headers: { | ||
'cache-control': config.cacheControl.private, | ||
[serverTiming.headerKey]: serverTiming.toString(), | ||
}, | ||
}, | ||
) | ||
} |
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,21 @@ | ||
import { LoaderFunctionArgs, redirect } from '@remix-run/node' | ||
import { z } from 'zod' | ||
import { zfd } from 'zod-form-data' | ||
|
||
const queryParamSchema = z.object({ | ||
itemID: z.string(), | ||
itemType: z.enum(['artist', 'genre']), | ||
}) | ||
|
||
export async function loader({ request }: LoaderFunctionArgs) { | ||
const params = zfd | ||
.formData(queryParamSchema) | ||
.parse(new URL(request.url).searchParams) | ||
|
||
switch (params.itemType) { | ||
case 'artist': | ||
return redirect(`/spotify/artist-id/${params.itemID}`) | ||
case 'genre': | ||
return redirect(`/genre/${params.itemID}`) | ||
} | ||
} |