Add brewery discovery & detail pages with location-based search (#253)

This commit is contained in:
Aaron Po
2026-08-29 00:44:33 -04:00
committed by GitHub
parent 92a30b11db
commit a580314583
90 changed files with 3874 additions and 187 deletions

View File

@@ -0,0 +1,102 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import { expect, within } from 'storybook/test';
import BreweryCard from './BreweryCard';
import type { Brewery, BreweryLocation } from '../../breweries.server';
import { formatBreweryAddress } from '../../utils/format-address';
const breweryCardDescription = `Link-card for a brewery shown in listing grids. These stories cover a brewery with a full mapped location, one whose location has no coordinates (no "View on map" link), and one with no location at all.`;
const portlandLocation: BreweryLocation = {
breweryPostLocationId: 'loc-1',
breweryPostId: 'brewery-1',
cityId: 'city-1',
cityName: 'Portland',
stateProvinceName: 'Oregon',
stateProvinceCode: 'OR',
countryName: 'United States',
countryCode: 'US',
addressLine1: '123 Hop Street',
addressLine2: null,
postalCode: '97201',
coordinates: { latitude: 45.5152, longitude: -122.6784 },
};
const breweryWithLocation: Brewery = {
breweryPostId: 'brewery-1',
postedById: 'user-1',
breweryName: 'Cascade Hollow Brewing',
description:
'A small-batch brewery known for hazy IPAs and barrel-aged sours, tucked into an old warehouse district.',
createdAt: '2024-03-01T12:00:00.000Z',
updatedAt: null,
location: portlandLocation,
};
const breweryWithoutCoordinates: Brewery = {
...breweryWithLocation,
breweryPostId: 'brewery-2',
breweryName: 'Foggy Bottom Brewhouse',
location: { ...portlandLocation, coordinates: null },
};
const breweryWithoutLocation: Brewery = {
...breweryWithLocation,
breweryPostId: 'brewery-3',
breweryName: 'Unmapped Ales',
location: null,
};
const meta = {
title: 'Breweries/BreweryCard',
component: BreweryCard,
tags: ['autodocs'],
parameters: {
layout: 'padded',
docs: {
description: {
component: breweryCardDescription,
},
},
},
} satisfies Meta<typeof BreweryCard>;
export default meta;
type Story = StoryObj<typeof meta>;
export const WithLocation: Story = {
args: { brewery: breweryWithLocation },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(
canvas.getByRole('heading', { name: 'Cascade Hollow Brewing' }),
).toBeInTheDocument();
await expect(canvas.getByText('Portland, OR')).toBeInTheDocument();
await expect(canvas.getByText(formatBreweryAddress(portlandLocation))).toBeInTheDocument();
const cardLink = canvas.getByRole('link', { name: /cascade hollow brewing/i });
await expect(cardLink).toHaveAttribute('href', '/breweries/brewery-1');
const mapLink = canvas.getByRole('link', { name: 'View on map →' });
await expect(mapLink).toHaveAttribute('href', expect.stringContaining('mlat=45.5152'));
await expect(mapLink).toHaveAttribute('target', '_blank');
},
};
export const WithoutCoordinates: Story = {
args: { brewery: breweryWithoutCoordinates },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(canvas.getByText('Portland, OR')).toBeInTheDocument();
await expect(canvas.queryByRole('link', { name: /view on map/i })).not.toBeInTheDocument();
},
};
export const WithoutLocation: Story = {
args: { brewery: breweryWithoutLocation },
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
await expect(canvas.getByRole('heading', { name: 'Unmapped Ales' })).toBeInTheDocument();
await expect(canvas.queryByText('Portland, OR')).not.toBeInTheDocument();
await expect(canvas.queryByRole('link', { name: /view on map/i })).not.toBeInTheDocument();
},
};

View File

@@ -0,0 +1,46 @@
import { Link } from 'react-router';
import type { Brewery } from '../../breweries.server';
import { formatBreweryAddress } from '../../utils/format-address';
interface BreweryCardProps {
brewery: Brewery;
}
const BreweryCard = ({ brewery }: BreweryCardProps) => (
<Link
to={`/breweries/${brewery.breweryPostId}`}
className="card bg-base-100 shadow hover:shadow-lg transition-shadow"
>
<div className="card-body">
<h2 className="card-title">{brewery.breweryName}</h2>
{brewery.location && (
<p className="text-sm font-medium text-base-content/70">
{brewery.location.cityName}, {brewery.location.stateProvinceCode}
</p>
)}
{brewery.description && (
<p className="text-sm leading-5 text-base-content/70 line-clamp-2 max-h-10 overflow-hidden">
{brewery.description}
</p>
)}
{brewery.location && (
<p className="text-sm text-base-content/50 mt-2">
{formatBreweryAddress(brewery.location)}
</p>
)}
{brewery.location?.coordinates && (
<a
href={`https://www.openstreetmap.org/?mlat=${brewery.location.coordinates.latitude}&mlon=${brewery.location.coordinates.longitude}#map=16/${brewery.location.coordinates.latitude}/${brewery.location.coordinates.longitude}`}
target="_blank"
rel="noreferrer"
onClick={(e) => e.stopPropagation()}
className="link link-primary text-sm mt-2 self-start"
>
View on map &rarr;
</a>
)}
</div>
</Link>
);
export default BreweryCard;

View File

@@ -0,0 +1,61 @@
import type { Meta, StoryObj } from '@storybook/react-vite';
import BreweryMap, { type BreweryMapPin } from './BreweryMap';
import type { BreweryLocation } from '../../breweries.server';
const breweryMapDescription = `Leaflet map plotting brewery locations, with an optional current-location marker. Smoke-render only - this hits the real OpenStreetMap tile server in the browser test run, and its \`navigator.geolocation\` call is tolerated whether or not it succeeds.`;
const portlandLocation: BreweryLocation = {
breweryPostLocationId: 'loc-1',
breweryPostId: 'brewery-1',
cityId: 'city-1',
cityName: 'Portland',
stateProvinceName: 'Oregon',
stateProvinceCode: 'OR',
countryName: 'United States',
countryCode: 'US',
addressLine1: '123 Hop Street',
addressLine2: null,
postalCode: '97201',
coordinates: { latitude: 45.5152, longitude: -122.6784 },
};
const pins: BreweryMapPin[] = [
{
id: 'brewery-1',
name: 'Cascade Hollow Brewing',
latitude: 45.5152,
longitude: -122.6784,
location: portlandLocation,
},
{
id: 'brewery-2',
name: 'Foggy Bottom Brewhouse',
latitude: 44.06,
longitude: -121.31,
location: null,
},
];
const meta = {
title: 'Breweries/BreweryMap',
component: BreweryMap,
parameters: {
layout: 'padded',
docs: {
description: {
component: breweryMapDescription,
},
},
},
} satisfies Meta<typeof BreweryMap>;
export default meta;
type Story = StoryObj<typeof meta>;
export const Default: Story = {
args: { breweries: pins },
};
export const NoBreweries: Story = {
args: { breweries: [] },
};

View File

@@ -0,0 +1,124 @@
import { useEffect, useState } from 'react';
import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet';
import L, { type LatLngExpression, type LatLngTuple } from 'leaflet';
import iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png';
import iconUrl from 'leaflet/dist/images/marker-icon.png';
import shadowUrl from 'leaflet/dist/images/marker-shadow.png';
import 'leaflet/dist/leaflet.css';
import useMediaQuery from '../../../../hooks/useMediaQuery';
import type { BreweryLocation } from '../../breweries.server';
import { formatBreweryAddress } from '../../utils/format-address';
delete (L.Icon.Default.prototype as unknown as { _getIconUrl?: unknown })._getIconUrl;
L.Icon.Default.mergeOptions({ iconRetinaUrl, iconUrl, shadowUrl });
export interface BreweryMapPin {
id: string;
name: string;
latitude: number;
longitude: number;
location: BreweryLocation | null;
}
interface BreweryMapProps {
breweries: BreweryMapPin[];
}
const FALLBACK_CENTER: LatLngExpression = [20, 0];
const WORLD_BOUNDS: LatLngTuple[] = [
[-90, -180],
[90, 180],
];
const CURRENT_LOCATION_ICON = L.divIcon({
className: 'brewery-map-current-location',
html: '<span class="block h-3.5 w-3.5 rounded-full bg-primary border-2 border-base-100 shadow-md"></span>',
iconSize: [14, 14],
iconAnchor: [7, 7],
});
const useCurrentLocation = () => {
const [position, setPosition] = useState<LatLngTuple | null>(null);
useEffect(() => {
if (!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(
(pos) => setPosition([pos.coords.latitude, pos.coords.longitude]),
() => setPosition(null),
{ enableHighAccuracy: true },
);
}, []);
return position;
};
const CurrentLocationMarker = ({ position }: { position: LatLngTuple }) => (
<Marker position={position} icon={CURRENT_LOCATION_ICON}>
<Popup>Your location</Popup>
</Marker>
);
const CURRENT_LOCATION_ZOOM = 6;
const RecenterMap = ({
bounds,
currentLocation,
}: {
bounds: LatLngTuple[];
currentLocation: LatLngTuple | null;
}) => {
const map = useMap();
useEffect(() => {
if (currentLocation) {
map.setView(currentLocation, CURRENT_LOCATION_ZOOM, { animate: false });
} else if (bounds.length > 0) {
map.fitBounds(bounds, { padding: [32, 32], maxZoom: 6, animate: false });
}
}, [map, bounds, currentLocation]);
return null;
};
const BreweryMap = ({ breweries }: BreweryMapProps) => {
const currentLocation = useCurrentLocation();
const bounds: LatLngTuple[] = breweries.map((brewery) => [brewery.latitude, brewery.longitude]);
return (
<MapContainer
center={currentLocation ?? bounds[0] ?? FALLBACK_CENTER}
zoom={4}
scrollWheelZoom={false}
maxBounds={WORLD_BOUNDS}
maxBoundsViscosity={1.0}
worldCopyJump={false}
className="h-112 md:h-136 w-full rounded-box relative z-0"
>
<TileLayer
attribution='&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
noWrap
/>
<RecenterMap bounds={bounds} currentLocation={currentLocation} />
{currentLocation && <CurrentLocationMarker position={currentLocation} />}
{breweries.map((brewery) => (
<Marker key={brewery.id} position={[brewery.latitude, brewery.longitude]}>
<Popup>
<p className="font-semibold">{brewery.name}</p>
{brewery.location && (
<p className="text-sm text-base-content/70">
{formatBreweryAddress(brewery.location)}
</p>
)}
<a href={`/breweries/${brewery.id}`} className="link link-primary text-sm">
View brewery &rarr;
</a>
</Popup>
</Marker>
))}
</MapContainer>
);
};
export default BreweryMap;