-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add search page #73
Open
nth-chile
wants to merge
12
commits into
RelistenNet:master
Choose a base branch
from
nth-chile:add-search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8deb860
Add search page (WIP)
nth-chile d66204c
Merge branch 'master' into add-search
nth-chile 4566357
More initial work on search page
nth-chile 688c7c3
Remove console.logs
nth-chile 556455c
Merge branch 'master' into add-search
nth-chile 513c2e9
Convert search to server components; add w-full class to secondary la…
nth-chile 104a564
search: Convert buttons to links, and convert search components to se…
nth-chile 3e6e21c
search: Remove artist name and artist slug from search params, making…
nth-chile 4c980fc
Search: Remove artistUuid from query params, get it on backend; use d…
nth-chile 1971fb7
search: If there are query params, back button should go to /search, …
nth-chile 68f769b
Search: remove cache option in fetch; avoid instance of var reassignment
nth-chile 56e6b64
Update package.json
nth-chile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { PropsWithChildren } from 'react'; | ||
|
||
export default function Layout({ children }: PropsWithChildren) { | ||
return <div className="mx-auto max-w-screen-md py-8">{children}</div>; | ||
return <div className="mx-auto w-full max-w-screen-md py-8">{children}</div>; | ||
} |
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,100 @@ | ||
import { SearchParams, SearchResults, SearchResultsType, SongVersions } from '@/types'; | ||
import { API_DOMAIN } from '@/lib/constants'; | ||
import { SimplePopover } from '@/components/Popover'; | ||
import SearchBar from '@/components/search/SearchBar'; | ||
import SearchFilterPill from '@/components/search/SearchFilterPill'; | ||
import SearchResultsCpt from '@/components/search/SearchResults'; | ||
import SearchSortByMenu from '@/components/search/SearchSortByMenu'; | ||
|
||
export default async function Page({ searchParams }: { searchParams: SearchParams }) { | ||
let data: SearchResults | null = null; | ||
let resultsType: SearchResultsType = searchParams.resultsType || 'all'; | ||
let versionsData: SongVersions | null = null; | ||
|
||
if (searchParams.q) { | ||
const response = await fetch(`${API_DOMAIN}/api/v2/search?q=${searchParams.q}`).then((res) => | ||
res.json() | ||
); | ||
|
||
// API sometimes returns null instead of [] | ||
data = { ...response, Songs: !response.Songs ? [] : response.Songs }; | ||
} | ||
|
||
if (data?.Songs.length && searchParams.songUuid) { | ||
const song = data.Songs.find(({ uuid }) => uuid === searchParams.songUuid); | ||
|
||
const versions = await fetch( | ||
`${API_DOMAIN}/api/v3/artists/${song?.slim_artist?.uuid}/songs/${searchParams.songUuid}` | ||
).then((res) => res.json()); | ||
|
||
resultsType = 'versions'; | ||
|
||
versionsData = { | ||
...versions, | ||
artistName: song?.slim_artist?.name || '', | ||
artistSlug: song?.slim_artist?.slug || '', | ||
}; | ||
} | ||
|
||
return ( | ||
<div className="mx-auto w-full max-w-screen-md flex-1"> | ||
<SearchBar resultsType={resultsType} /> | ||
<div className="flex justify-between pb-4"> | ||
{data !== null && resultsType === 'versions' && ( | ||
<div className="font-semibold"> | ||
Showing all versions of “{versionsData?.name}” by {versionsData?.artistName} | ||
</div> | ||
)} | ||
{data !== null && resultsType !== 'versions' && ( | ||
<ul className="search-filters"> | ||
<li> | ||
<SearchFilterPill | ||
buttonType="all" | ||
resultsType={resultsType} | ||
searchParams={searchParams} | ||
> | ||
All {data && `(${data?.Artists?.length + data?.Songs?.length})`} | ||
</SearchFilterPill> | ||
</li> | ||
<li> | ||
<SearchFilterPill | ||
buttonType="artists" | ||
resultsType={resultsType} | ||
searchParams={searchParams} | ||
> | ||
Artists {data && `(${data?.Artists?.length})`} | ||
</SearchFilterPill> | ||
</li> | ||
<li> | ||
<SearchFilterPill | ||
buttonType="songs" | ||
resultsType={resultsType} | ||
searchParams={searchParams} | ||
> | ||
Songs {data && `(${data?.Songs?.length})`} | ||
</SearchFilterPill> | ||
</li> | ||
</ul> | ||
)} | ||
{resultsType === 'versions' && ( | ||
<SimplePopover | ||
content={<SearchSortByMenu resultsType={resultsType} searchParams={searchParams} />} | ||
position="bottom-end" | ||
> | ||
<button>Sort by ⏷</button> | ||
</SimplePopover> | ||
)} | ||
</div> | ||
<SearchResultsCpt | ||
data={data} | ||
resultsType={resultsType} | ||
searchParams={searchParams} | ||
versionsData={versionsData} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export const metadata = { | ||
title: 'Search', | ||
}; |
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,79 @@ | ||
'use client'; | ||
|
||
import { SearchResultsType } from '@/types'; | ||
import { usePathname, useRouter, useSearchParams } from 'next/navigation'; | ||
import { FormEvent, useState, useTransition } from 'react'; | ||
|
||
export default function SearchBar({ resultsType }: { resultsType: SearchResultsType }) { | ||
const pathname = usePathname(); | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
const writableParams = new URLSearchParams(searchParams); | ||
const [value, setValue] = useState<string>(searchParams?.get('q')?.toString() || ''); | ||
const [isPending, startTransition] = useTransition(); | ||
|
||
function handleSubmit(e?: FormEvent<HTMLFormElement>) { | ||
if (e) { | ||
e.preventDefault(); | ||
} | ||
|
||
/** If there are query params, back button should go to /search, otherwise leave the search page */ | ||
const shouldPush = writableParams.toString() === ''; | ||
|
||
if (value) { | ||
writableParams.set('q', value); | ||
} else { | ||
writableParams.delete('q'); | ||
} | ||
|
||
const path = `${pathname}?${writableParams.toString()}`; | ||
|
||
startTransition(() => { | ||
if (shouldPush) { | ||
router.push(path); | ||
} else { | ||
router.replace(path); | ||
} | ||
}); | ||
} | ||
|
||
function clearSearch() { | ||
writableParams.delete('q'); | ||
writableParams.delete('resultsType'); | ||
writableParams.delete('songUuid'); | ||
writableParams.delete('sortBy'); | ||
startTransition(() => { | ||
setValue(''); | ||
router.replace(`${pathname}?${writableParams.toString()}`); | ||
}); | ||
} | ||
|
||
return ( | ||
<form className="w-screen max-w-screen-md" onSubmit={handleSubmit}> | ||
{resultsType === 'versions' ? ( | ||
<button | ||
className="mb-2 h-[42px] font-semibold" | ||
onClick={clearSearch} | ||
aria-label="clear search" | ||
type="button" // Technically a reset button, but maybe best to use "button" to avoid unexpected behavior | ||
> | ||
<i className="fa fa-times" /> Clear search | ||
</button> | ||
) : ( | ||
<div className="search-bar mb-2 flex items-center p-2"> | ||
<i className={isPending ? 'fa fa-spinner px-2' : 'fa fa-search px-2'} /> | ||
<input | ||
className="grow" | ||
type="text" | ||
placeholder="Search..." | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
/> | ||
<button onClick={clearSearch} aria-label="clear search" className="flex" type="button"> | ||
<i className="fa fa-times px-2" /> | ||
</button> | ||
</div> | ||
)} | ||
</form> | ||
); | ||
} |
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,29 @@ | ||
import Link from 'next/link'; | ||
import { SearchParams, SearchResultsType } from '@/types'; | ||
import React from 'react'; | ||
|
||
export default function SearchFilterPill({ | ||
buttonType, | ||
children, | ||
resultsType, | ||
searchParams, | ||
}: { | ||
buttonType: SearchResultsType; | ||
children: React.ReactNode; | ||
resultsType: SearchResultsType; | ||
searchParams: SearchParams; | ||
}) { | ||
const writableParams = new URLSearchParams(searchParams); | ||
|
||
writableParams.set('resultsType', buttonType); | ||
|
||
return ( | ||
<Link | ||
className={`mr-1 mt-1 inline-block rounded-full px-2 py-1 ${resultsType === buttonType ? 'search-filters-item--active' : ''}`} | ||
href={`search?${writableParams.toString()}`} | ||
replace={true} | ||
> | ||
{children} | ||
</Link> | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running out of room on the nav. This goes behind the player