Skip to content
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
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"@tanstack/react-query-next-experimental": "^5.35.1",
"@types/react-redux": "^7.1.33",
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"framer-motion": "^11.1.9",
"isomorphic-fetch": "3.0.0",
"ky": "^1.2.4",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/(secondary)/layout.tsx
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>;
}
100 changes: 100 additions & 0 deletions src/app/(main)/(secondary)/search/page.tsx
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',
};
5 changes: 5 additions & 0 deletions src/app/(main)/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export default async function NavBar() {
</Flex>
</SimplePopover>
<div className="nav hidden h-full flex-[2] cursor-pointer items-center justify-end text-center font-medium 2xl:flex">
<div className="h-full px-1">
<Link href="/search" legacyBehavior prefetch={false}>
<a className="nav-btn">SEARCH</a>
</Link>
</div>
Copy link
Author

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

<div className="h-full px-1">
<Link href="/today" legacyBehavior prefetch={false}>
<a className="nav-btn">TIH</a>
Expand Down
79 changes: 79 additions & 0 deletions src/components/search/SearchBar.tsx
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>
);
}
29 changes: 29 additions & 0 deletions src/components/search/SearchFilterPill.tsx
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>
);
}
Loading