From bde9598a53d34458ca56d1b9162ebf4b5689574a Mon Sep 17 00:00:00 2001 From: sivertlv Date: Fri, 27 Nov 2026 15:22:22 +0100 Subject: [PATCH] feat/add open-status chip for venues --- apps/frontend/src/components/NearbyVenues.tsx | 11 +- .../src/components/OpenStatusChip.tsx | 55 +++++++++ apps/frontend/src/components/SearchBox.tsx | 10 ++ apps/frontend/src/components/ViewVenue.tsx | 9 +- .../__tests__/OpenStatusChip.test.tsx | 40 ++++++ .../src/lib/__tests__/opening-hours.test.ts | 85 +++++++++++++ apps/frontend/src/lib/opening-hours.ts | 116 ++++++++++++++++++ apps/frontend/src/lib/use-now.ts | 17 +++ 8 files changed, 340 insertions(+), 3 deletions(-) create mode 100644 apps/frontend/src/components/OpenStatusChip.tsx create mode 100644 apps/frontend/src/components/__tests__/OpenStatusChip.test.tsx create mode 100644 apps/frontend/src/lib/__tests__/opening-hours.test.ts create mode 100644 apps/frontend/src/lib/use-now.ts diff --git a/apps/frontend/src/components/NearbyVenues.tsx b/apps/frontend/src/components/NearbyVenues.tsx index 565854d..863a31c 100644 --- a/apps/frontend/src/components/NearbyVenues.tsx +++ b/apps/frontend/src/components/NearbyVenues.tsx @@ -1,6 +1,7 @@ import { Link } from '@tanstack/react-router' import { HarpIcon } from '#/components/HarpIcon' +import { OpenStatusChip } from '#/components/OpenStatusChip' import { formatOpeningHours } from '#/lib/format-opening-hours' import { formatPrice } from '#/lib/format-price' import { useUserTimeZone } from '#/lib/use-user-time-zone' @@ -36,8 +37,14 @@ export function NearbyVenueRow({ }) { return ( <> - - {venue.name} + + + {venue.name} + + {venue.averageRating != null && ( = { + open: 'Open now', + closed: 'Closed', + unknown: 'Hours unknown', +} + +const STATUS_TITLES: Record = { + open: 'Open right now', + closed: 'Closed right now', + unknown: 'No opening hours available', +} + +const STATUS_STYLES: Record = { + open: 'border-emerald-600/30 bg-emerald-500/15 text-(--sea-ink)', + closed: 'border-rose-600/30 bg-rose-500/15 text-(--sea-ink)', + unknown: 'border-(--line) bg-(--chip-bg) text-(--sea-ink-soft)', +} + +const DOT_STYLES: Record = { + open: 'bg-emerald-500', + closed: 'bg-rose-500', + unknown: 'bg-(--sea-ink-soft)', +} + +export function OpenStatusChip({ + openingHours, + timeZone = 'Europe/Oslo', + className = '', +}: { + openingHours: readonly OpeningHoursLike[] | null | undefined + timeZone?: string + className?: string +}) { + const now = useNow() + const status = getOpenStatus(openingHours, timeZone, now) + + return ( + + + ) +} + +export default OpenStatusChip diff --git a/apps/frontend/src/components/SearchBox.tsx b/apps/frontend/src/components/SearchBox.tsx index 683a5b8..b78975b 100644 --- a/apps/frontend/src/components/SearchBox.tsx +++ b/apps/frontend/src/components/SearchBox.tsx @@ -3,6 +3,7 @@ import { useNavigate, useSearch } from '@tanstack/react-router' import { useEffect, useId, useMemo, useState } from 'react' import { CountryLabel } from '#/components/CountryLabel' import { HarpIcon } from '#/components/HarpIcon' +import { OpenStatusChip } from '#/components/OpenStatusChip' import type { FormEvent, KeyboardEvent } from 'react' import { graphqlFetch } from '../graphql/client' @@ -10,6 +11,7 @@ import { formatPrice } from '../lib/format-price' import { searchFilterVariables } from '../lib/search-filters' import type { SearchQueryParams } from '../lib/search-filters' import { useDebounce } from '../lib/use-debounce' +import { useUserTimeZone } from '../lib/use-user-time-zone' import type { Coordinates } from '../lib/location' import type { SearchFilters, SearchResult } from '../types/search' @@ -122,6 +124,7 @@ export function SearchBox({ const searchTerm = useSearch({ from, select: (search) => search.q }) const inputId = useId() const listId = useId() + const timeZone = useUserTimeZone() const term = searchTerm ?? '' // The URL is the source of truth for the term, but the request should only @@ -337,6 +340,13 @@ export function SearchBox({ {typeLabel(result)} + {result.type === 'venue' && ( + + )} {result.averageRating != null && (
-

{venue.name}

+
+

{venue.name}

+ +
{venue.averageRating != null && (

{ + afterEach(() => { + vi.useRealTimers() + }) + + it('shows when a venue is open', () => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-10-05T15:00:00.000Z')) + + render() + + expect(screen.getByText('Open now')).toBeInTheDocument() + }) + + it('shows when a venue is closed', () => { + vi.useFakeTimers() + vi.setSystemTime(new Date('2026-10-05T03:00:00.000Z')) + + render() + + expect(screen.getByText('Closed')).toBeInTheDocument() + }) + + it('shows an unknown state when hours are missing', () => { + render() + + expect(screen.getByText('Hours unknown')).toBeInTheDocument() + }) +}) diff --git a/apps/frontend/src/lib/__tests__/opening-hours.test.ts b/apps/frontend/src/lib/__tests__/opening-hours.test.ts new file mode 100644 index 0000000..d505980 --- /dev/null +++ b/apps/frontend/src/lib/__tests__/opening-hours.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from 'vitest' + +import { getOpenStatus } from '../opening-hours' + +const weekdayHours = { + days: ['MON', 'TUE', 'WED', 'THU', 'FRI'], + opening: '2026-10-01T14:00:00.000Z', + closing: '2026-10-02T02:00:00.000Z', +} + +describe('getOpenStatus', () => { + it('returns open during a same-day interval', () => { + expect( + getOpenStatus( + [weekdayHours], + 'UTC', + new Date('2026-10-05T15:00:00.000Z'), + ), + ).toBe('open') + }) + + it('returns closed before opening', () => { + expect( + getOpenStatus( + [weekdayHours], + 'UTC', + new Date('2026-10-05T03:00:00.000Z'), + ), + ).toBe('closed') + }) + + it('returns open after midnight for the previous day schedule', () => { + expect( + getOpenStatus( + [weekdayHours], + 'UTC', + new Date('2026-10-06T01:00:00.000Z'), + ), + ).toBe('open') + expect( + getOpenStatus( + [weekdayHours], + 'UTC', + new Date('2026-10-03T01:00:00.000Z'), + ), + ).toBe('open') + }) + + it('returns closed after an overnight interval ends', () => { + expect( + getOpenStatus( + [weekdayHours], + 'UTC', + new Date('2026-10-06T03:00:00.000Z'), + ), + ).toBe('closed') + }) + + it('returns open for a matching weekend schedule', () => { + expect( + getOpenStatus( + [ + { + days: ['SAT', 'SUN'], + opening: '2026-10-01T12:00:00.000Z', + closing: '2026-10-01T23:00:00.000Z', + }, + ], + 'UTC', + new Date('2026-10-04T15:00:00.000Z'), + ), + ).toBe('open') + }) + + it('returns unknown when no usable schedule exists', () => { + expect(getOpenStatus([], 'UTC', new Date())).toBe('unknown') + expect( + getOpenStatus( + [{ ...weekdayHours, days: [] }], + 'UTC', + new Date('2026-10-05T15:00:00.000Z'), + ), + ).toBe('unknown') + }) +}) diff --git a/apps/frontend/src/lib/opening-hours.ts b/apps/frontend/src/lib/opening-hours.ts index 4424f09..7b4c4fc 100644 --- a/apps/frontend/src/lib/opening-hours.ts +++ b/apps/frontend/src/lib/opening-hours.ts @@ -1,3 +1,7 @@ +import { DAYS } from './days' +import type { Day } from './days' +import { formatOpeningTime } from './format-opening-hours' + /** * Opening hours are stored as `timetz` values. The date part is only a * reference and never shown, so we attach times to a fixed date in the @@ -6,3 +10,115 @@ export function timeToIso(time: string): string { return new Date(`2000-01-01T${time}:00+01:00`).toISOString() } + +export interface OpeningHoursLike { + days: string[] + opening: string + closing: string +} + +export type OpenStatus = 'open' | 'closed' | 'unknown' + +const localTimeFormatters = new Map() + +function getLocalTimeFormatter(timeZone: string) { + const existing = localTimeFormatters.get(timeZone) + + if (existing) return existing + + const formatter = new Intl.DateTimeFormat('en-GB', { + timeZone, + weekday: 'short', + hour: '2-digit', + minute: '2-digit', + hourCycle: 'h23', + }) + localTimeFormatters.set(timeZone, formatter) + + return formatter +} + +function isDay(value: string): value is Day { + return (DAYS as readonly string[]).includes(value) +} + +function getLocalDayAndMinutes(now: Date, timeZone: string) { + const parts = getLocalTimeFormatter(timeZone).formatToParts(now) + const day = (parts.find((part) => part.type === 'weekday')?.value ?? '') + .slice(0, 3) + .toUpperCase() + const hour = Number(parts.find((part) => part.type === 'hour')?.value) + const minute = Number(parts.find((part) => part.type === 'minute')?.value) + + if (!isDay(day) || !Number.isFinite(hour) || !Number.isFinite(minute)) { + return null + } + + return { day, minutes: hour * 60 + minute } +} + +function timeToMinutes(iso: string, timeZone: string) { + try { + const [hour, minute] = formatOpeningTime(iso, timeZone) + .split(':') + .map(Number) + + return Number.isFinite(hour) && Number.isFinite(minute) + ? hour * 60 + minute + : null + } catch { + return null + } +} + +/** + * Returns whether any opening-hours interval covers the current moment. + * Overnight intervals belong to the day they open, so the previous day's + * schedule is also checked for closures after midnight. + */ +export function getOpenStatus( + entries: readonly OpeningHoursLike[] | null | undefined, + timeZone = 'Europe/Oslo', + now = new Date(), +): OpenStatus { + if (!entries || entries.length === 0) return 'unknown' + + const local = getLocalDayAndMinutes(now, timeZone) + + if (!local) return 'unknown' + + const todayIndex = DAYS.indexOf(local.day) + const yesterday = DAYS[(todayIndex + DAYS.length - 1) % DAYS.length] + let hasValidSchedule = false + + for (const entry of entries) { + const days = entry.days.map((day) => day.toUpperCase()) + const opening = timeToMinutes(entry.opening, timeZone) + const closing = timeToMinutes(entry.closing, timeZone) + + if (opening === null || closing === null || !days.some(isDay)) { + continue + } + + hasValidSchedule = true + + const opensToday = days.includes(local.day) + const openedYesterday = days.includes(yesterday) + + if (opening === closing) { + if (opensToday) return 'open' + continue + } + + if (closing > opening) { + if (opensToday && local.minutes >= opening && local.minutes < closing) { + return 'open' + } + } else { + if (opensToday && local.minutes >= opening) return 'open' + if (openedYesterday && local.minutes < closing) return 'open' + } + } + + return hasValidSchedule ? 'closed' : 'unknown' +} diff --git a/apps/frontend/src/lib/use-now.ts b/apps/frontend/src/lib/use-now.ts new file mode 100644 index 0000000..88068f6 --- /dev/null +++ b/apps/frontend/src/lib/use-now.ts @@ -0,0 +1,17 @@ +import { useEffect, useState } from 'react' + +/** + * Keeps a Date ticking so time-sensitive UI (such as an open-now chip) + * refreshes while the page stays open. + */ +export function useNow(intervalMs = 60_000) { + const [now, setNow] = useState(() => new Date()) + + useEffect(() => { + const interval = window.setInterval(() => setNow(new Date()), intervalMs) + + return () => window.clearInterval(interval) + }, [intervalMs]) + + return now +}