Skip to content
Merged
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
3 changes: 3 additions & 0 deletions client/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
min-block-size: 100svh;
padding-block: 2rem 3rem;
}
.route-error {
padding-block: 3rem;
}
.skip-link {
position: absolute;
left: -9999px;
Expand Down
214 changes: 101 additions & 113 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,141 +1,129 @@
import './App.css';

import SearchForm from './components/SearchForm.js';
import ResultsLayout from './components/ResultsLayout.js';
import { lazy, Suspense } from 'react';
import { lazy, Suspense, useCallback, useEffect, useRef } from 'react';

import { useAlbumSearch } from './hooks/useAlbumSearch.js';
import { useSearchHistory } from './hooks/useSearchHistory.js';
import Link from './components/Link.js';
import RouteErrorBoundary from './components/RouteErrorBoundary.js';
import DiscoveryPage from './pages/DiscoveryPage.js';
import { appHref, stripBasePath, useLocation } from './router.js';

function focusMain(): void {
const main = document.getElementById('main');
if (main instanceof HTMLElement) {
main.focus({ preventScroll: true });
}
}

/**
* Fires after a lazy route inside `<Suspense>` finishes mounting so focus
* moves from the unmounted fallback `<main>` to the real page `<main id="main">`.
*/
function RouteFocusSettler({
pathname,
onSettled,
}: {
pathname: string;
onSettled: (pathname: string) => void;
}) {
useEffect(() => {
onSettled(pathname);
}, [pathname, onSettled]);
return null;
}

export default function App() {
const basePath = import.meta.env.BASE_URL.replace(/\/$/, '');
const discoveryHref = import.meta.env.BASE_URL;
const pathname = window.location.pathname.startsWith(basePath)
? window.location.pathname.slice(basePath.length) || '/'
: window.location.pathname;
const location = useLocation();
const discoveryHref = appHref('/');
const crateHref = appHref('/crate');
const pathname = stripBasePath(location.pathname);
const detailMatch = /^\/album\/(\d+)$/.exec(pathname);
const detailId = detailMatch?.[1];
const isCrate = pathname === '/crate';

const previousPathname = useRef(pathname);
const pendingFocusPathname = useRef<string | null>(null);

useEffect(() => {
if (previousPathname.current === pathname) return;
previousPathname.current = pathname;
focusMain();
const main = document.getElementById('main');
pendingFocusPathname.current = main?.hasAttribute('data-route-fallback')
? pathname
: null;
}, [pathname]);

const handleRouteSettled = useCallback((settledPathname: string) => {
if (pendingFocusPathname.current !== settledPathname) return;
pendingFocusPathname.current = null;
// Only move focus if it fell back to <body> when the Suspense fallback unmounted.
if (
!document.activeElement ||
document.activeElement === document.body ||
document.activeElement === document.documentElement
) {
focusMain();
}
}, []);

return (
<>
<a href="#main" className="skip-link">
Skip to content
</a>
<header className="site-header">
<nav className="navigation" aria-label="Main navigation">
<a className="brand" href={discoveryHref}>
<Link className="brand" href={discoveryHref}>
Crate<span aria-hidden="true">●</span>
</a>
<a href={discoveryHref} aria-current={detailId || isCrate ? undefined : 'page'}>
</Link>
<Link
href={discoveryHref}
aria-current={detailId || isCrate ? undefined : 'page'}
>
Discover
</a>
<a href={`${basePath}/crate`} aria-current={isCrate ? 'page' : undefined}>
</Link>
<Link href={crateHref} aria-current={isCrate ? 'page' : undefined}>
Your crate
</a>
</Link>
</nav>
</header>
{detailId ? (
<Suspense
fallback={
<main
id="main"
className="album-detail-loading"
tabIndex={-1}
aria-busy="true"
>
<p role="status">Loading album…</p>
</main>
}
>
<AlbumDetailPage id={detailId} />
</Suspense>
) : isCrate ? (
<Suspense
fallback={
<main id="main" tabIndex={-1}>
<p role="status">Loading your crate…</p>
</main>
}
>
<CratePage />
</Suspense>
) : (
<DiscoveryPage />
)}
<RouteErrorBoundary resetKey={pathname}>
{detailId ? (
<Suspense
fallback={
<main
id="main"
className="album-detail-loading"
tabIndex={-1}
aria-busy="true"
data-route-fallback="true"
>
<p role="status">Loading album…</p>
</main>
}
>
<AlbumDetailPage key={detailId} id={detailId} />
<RouteFocusSettler pathname={pathname} onSettled={handleRouteSettled} />
</Suspense>
) : isCrate ? (
<Suspense
fallback={
<main id="main" tabIndex={-1} data-route-fallback="true">
<p role="status">Loading your crate…</p>
</main>
}
>
<CratePage />
<RouteFocusSettler pathname={pathname} onSettled={handleRouteSettled} />
</Suspense>
) : (
<DiscoveryPage />
)}
</RouteErrorBoundary>
<footer className="site-footer">Crate · Find something worth listening to.</footer>
</>
);
}

function DiscoveryPage() {
const search = useAlbumSearch();
const history = useSearchHistory();

function submitSearch(query: string) {
search.search(query);
history.recordSearch(query);
}

return (
<main id="main" className="discovery-main" tabIndex={-1}>
<section className="search-section" aria-labelledby="search-heading">
<div className="search-section__content">
<p className="eyebrow">Your next favorite is in here</p>
<h1 id="search-heading">
Music worth
<br /> <em>holding onto.</em>
</h1>
<p className="tagline">
Search, rate and collect albums you’ll want to return to.
</p>
<SearchForm query={search.query} onSearch={submitSearch} history={history} />
<p className="hero-credit">
Photo by{' '}
<a href="https://unsplash.com/photos/black-turntable-on-brown-wooden-table-TcSckNRL9J8">
Kevin McCutcheon on Unsplash
</a>
</p>
</div>
</section>
<div id="catalogue" className="catalogue-layout">
<ResultsLayout
filters={{
genres: search.filters.genres,
decades: search.filters.decades,
countries: search.filters.countries,
reviewed: search.filters.reviewed,
genreFacets: search.facets.genres,
decadeFacets: search.facets.decades,
countryFacets: search.facets.countries,
reviewedCount: search.facets.reviewedCount,
onGenresChange: search.setGenres,
onDecadesChange: search.setDecades,
onCountriesChange: search.setCountries,
onReviewedChange: search.setReviewed,
onClear: search.clearFilters,
}}
albums={search.albums}
totalCount={search.totalCount}
hasQuery={search.hasQuery}
sort={search.sort}
onSortChange={search.setSort}
loading={search.loading}
loadingMore={search.loadingMore}
pageError={search.pageError}
hasNextPage={search.hasNextPage}
onLoadMore={search.loadMore}
canLoadAutomatically={search.canLoadAutomatically}
onLoadMoreAutomatically={search.loadMoreAutomatically}
retrying={search.retrying}
error={search.error}
onRetry={search.retry}
onReset={search.resetSearch}
/>
</div>
</main>
);
}

const AlbumDetailPage = lazy(() => import('./pages/AlbumDetailPage.js'));
const CratePage = lazy(() => import('./pages/CratePage.js'));
Loading