Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 48 additions & 23 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

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

import { useAlbumSearch } from './hooks/useAlbumSearch.js';

export default function App() {
const search = useAlbumSearch();
const detailMatch = /^\/album\/(\d+)$/.exec(window.location.pathname);
const detailId = detailMatch?.[1];

return (
<>
Expand All @@ -14,35 +17,57 @@ export default function App() {
</a>
<header className="site-header">
<nav className="navigation" aria-label="Main navigation">
<a className="brand" href={window.location.pathname}>
<a className="brand" href="/">
Crate<span aria-hidden="true">●</span>
</a>
<a href="#catalogue" aria-current="page">
<a href="/" aria-current={detailId ? undefined : 'page'}>
Discover
</a>
</nav>
</header>
<main id="main" tabIndex={-1}>
<section className="search-section" aria-labelledby="search-heading">
<p className="eyebrow">A world of records</p>
<h1 id="search-heading">Discover your next album.</h1>
<p className="tagline">Find something worth listening to.</p>
<SearchForm query={search.query} onSearch={search.search} />
</section>
<div id="catalogue" className="catalogue-layout">
<ResultsLayout
albums={search.albums}
totalCount={search.totalCount}
hasQuery={search.hasQuery}
loading={search.loading}
retrying={search.retrying}
error={search.error}
onRetry={search.retry}
onReset={() => search.search('')}
/>
</div>
</main>
{detailId ? (
<Suspense
fallback={
<main id="main" tabIndex={-1}>
<p role="status">Loading album…</p>
</main>
}
>
<AlbumDetailPage id={detailId} />
</Suspense>
) : (
<DiscoveryPage />
)}
<footer className="site-footer">Crate · Find something worth listening to.</footer>
</>
);
}

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

return (
<main id="main" tabIndex={-1}>
<section className="search-section" aria-labelledby="search-heading">
<p className="eyebrow">A world of records</p>
<h1 id="search-heading">Discover your next album.</h1>
<p className="tagline">Find something worth listening to.</p>
<SearchForm query={search.query} onSearch={search.search} />
</section>
<div id="catalogue" className="catalogue-layout">
<ResultsLayout
albums={search.albums}
totalCount={search.totalCount}
hasQuery={search.hasQuery}
loading={search.loading}
retrying={search.retrying}
error={search.error}
onRetry={search.retry}
onReset={() => search.search('')}
/>
</div>
</main>
);
}

const AlbumDetailPage = lazy(() => import('./pages/AlbumDetailPage.js'));
9 changes: 9 additions & 0 deletions client/src/api/albumDetail.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import schemaSource from '../../../schema.graphql?raw';
import { buildSchema, validate } from 'graphql';
import { expect, it } from 'vitest';

import { ALBUM_DETAIL } from './albumDetail.js';

it('validates the album-detail operation against the repository schema', () => {
expect(validate(buildSchema(schemaSource), ALBUM_DETAIL)).toEqual([]);
});
85 changes: 85 additions & 0 deletions client/src/api/albumDetail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { gql } from '@apollo/client';
import type { TypedDocumentNode } from '@apollo/client';

export type AlbumDetail = {
id: string;
title: string;
releaseYear: number | null;
artists: { id: string; name: string }[];
genres: { id: string; name: string }[];
tracks: {
id: string;
title: string;
trackNumber: number;
durationSeconds: number | null;
}[];
averageRating: number | null;
reviewCount: number;
reviews: {
edges: {
cursor: string;
node: {
id: string;
rating: number;
comment: string;
createdAt: string;
user: { id: string; displayName: string };
};
}[];
pageInfo: { hasNextPage: boolean; endCursor: string | null };
totalCount: number;
};
};

type AlbumDetailData = { album: AlbumDetail | null };
type AlbumDetailVariables = {
id: string;
reviewFirst: number;
reviewAfter?: string | null;
};

export const ALBUM_DETAIL: TypedDocumentNode<AlbumDetailData, AlbumDetailVariables> = gql`
query AlbumDetail($id: ID!, $reviewFirst: Int!, $reviewAfter: String) {
album(id: $id) {
id
title
releaseYear
artists {
id
name
}
genres {
id
name
}
tracks {
id
title
trackNumber
durationSeconds
}
averageRating
reviewCount
reviews(first: $reviewFirst, after: $reviewAfter) {
edges {
cursor
node {
id
rating
comment
createdAt
user {
id
displayName
}
}
}
totalCount
pageInfo {
hasNextPage
endCursor
}
}
}
}
`;
6 changes: 6 additions & 0 deletions client/src/components/AlbumCard.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
line-height: 1.4;
overflow-wrap: anywhere;
}
.album-info h3 a {
text-decoration: none;
}
.album-info h3 a:hover {
text-decoration: underline;
}
.artist {
margin: 0.25rem 0;
color: var(--muted);
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/AlbumCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export default function AlbumCard({ album }: { album: AlbumSummary }) {
return (
<article className="album-card" aria-labelledby={`album-${album.id}`}>
<div className="album-info">
<h3 id={`album-${album.id}`}>{album.title}</h3>
<h3 id={`album-${album.id}`}>
<a href={`/album/${album.id}`}>{album.title}</a>
</h3>
<p className="artist">{album.artists.map((artist) => artist.name).join(', ')}</p>
<p className="release-year">{album.releaseYear ?? 'Year unknown'}</p>
<ul className="genre-tags" aria-label="Genres">
Expand Down
85 changes: 85 additions & 0 deletions client/src/pages/AlbumDetailPage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.album-detail {
padding-block: 2rem 3rem;
}
.back-link {
color: var(--accent);
font-weight: 700;
}
.album-detail__header {
border-bottom: 1px solid var(--border);
margin: 1.5rem 0;
padding-bottom: 1.5rem;
}
.album-detail__header h1 {
font-size: clamp(2rem, 4vw, 3.5rem);
letter-spacing: -0.05em;
line-height: 1.1;
margin: 0;
}
.album-detail__artists {
font-size: 1.15rem;
margin: 0.5rem 0 0;
}
.album-detail__columns {
display: grid;
gap: 2rem;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
.album-detail h2 {
font-size: 1.25rem;
}
.tracklist,
.review-list {
margin: 1rem 0;
padding: 0;
list-style: none;
}
.tracklist {
counter-reset: tracks;
}
.tracklist li {
border-bottom: 1px solid var(--border);
counter-increment: tracks;
display: flex;
gap: 0.75rem;
justify-content: space-between;
padding: 0.65rem 0;
}
.tracklist li::before {
color: var(--muted);
content: counter(tracks);
min-width: 1.5rem;
}
.tracklist li > span:first-of-type {
flex: 1;
}
.review-list {
display: grid;
gap: 0.8rem;
}
.review-list article {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 1rem;
}
.review-list header {
display: flex;
gap: 0.75rem;
justify-content: space-between;
}
.review-list header span {
color: var(--accent);
}
.review-list p {
margin-bottom: 0.5rem;
}
.review-list time {
color: var(--muted);
font-size: 0.85rem;
}
@media (max-width: 700px) {
.album-detail__columns {
grid-template-columns: 1fr;
}
}
84 changes: 84 additions & 0 deletions client/src/pages/AlbumDetailPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { MockedProvider } from '@apollo/client/testing/react';
import { render, screen, within } from '@testing-library/react';
import { expect, it } from 'vitest';

import { ALBUM_DETAIL } from '../api/albumDetail.js';
import AlbumDetailPage from './AlbumDetailPage.js';

const variables = { id: '1', reviewFirst: 10, reviewAfter: null };

const detail = {
id: '1',
title: 'Abbey Road',
releaseYear: 1969,
artists: [{ id: '1', name: 'The Beatles' }],
genres: [{ id: '1', name: 'Rock' }],
tracks: [
{ id: '1', title: 'Come Together', trackNumber: 1, durationSeconds: 259 },
{ id: '2', title: 'Something', trackNumber: 2, durationSeconds: null },
],
averageRating: 4.5,
reviewCount: 1,
reviews: {
edges: [
{
cursor: 'cursor-1',
node: {
id: '1',
rating: 5,
comment: 'A classic.',
createdAt: '2026-10-20T12:00:00.000Z',
user: { id: 'user-1', displayName: 'Listener user-1' },
},
},
],
totalCount: 1,
pageInfo: { hasNextPage: false, endCursor: 'cursor-1' },
},
};

it('renders an album tracklist and its first page of reviews', async () => {
render(
<MockedProvider
mocks={[
{
request: { query: ALBUM_DETAIL, variables },
result: { data: { album: detail } },
},
]}
>
<AlbumDetailPage id="1" />
</MockedProvider>,
);

expect(await screen.findByRole('heading', { name: 'Abbey Road' })).toBeInTheDocument();
const tracklist = screen.getByRole('region', { name: 'Tracklist' });
expect(within(tracklist).getByText('Come Together')).toBeInTheDocument();
expect(within(tracklist).getByText('4:19')).toBeInTheDocument();
expect(within(tracklist).getByText('Something')).toBeInTheDocument();
const reviews = screen.getByRole('region', { name: 'Reviews' });
expect(within(reviews).getByText('Listener user-1')).toBeInTheDocument();
expect(within(reviews).getByText('A classic.')).toBeInTheDocument();
expect(within(reviews).getByLabelText('5 out of 5 stars')).toBeInTheDocument();
});

it('shows a useful error state when an album cannot be loaded', async () => {
render(
<MockedProvider
mocks={[
{
request: { query: ALBUM_DETAIL, variables },
result: { data: { album: null } },
},
]}
>
<AlbumDetailPage id="1" />
</MockedProvider>,
);

expect(await screen.findByRole('alert')).toHaveTextContent('could not load this album');
expect(screen.getByRole('link', { name: '← Back to discovery' })).toHaveAttribute(
'href',
'/',
);
});
Loading