diff --git a/web/src/App.test.tsx b/web/src/App.test.tsx index 45034f0fa..76541c121 100644 --- a/web/src/App.test.tsx +++ b/web/src/App.test.tsx @@ -55,6 +55,15 @@ describe('ChicoryTV SPA scaffold', () => { expect(window.location.pathname).toBe('/app/schedules'); }); + it('preserves native browser behavior for modified nav link clicks', () => { + render(); + + fireEvent.click(screen.getByRole('link', { name: 'Channels' }), { ctrlKey: true }); + + expect(screen.getByRole('heading', { name: 'Dashboard' })).toBeInTheDocument(); + expect(window.location.pathname).toBe('/app'); + }); + it('selects the active screen from the current location', () => { window.history.replaceState(null, '', '/app/guide'); @@ -65,7 +74,27 @@ describe('ChicoryTV SPA scaffold', () => { expect(screen.getByRole('link', { name: 'Guide' })).toHaveAttribute('aria-current', 'page'); }); - it('opens the Connect menu with player endpoints and enabled-only option', () => { + it('shows a not-found screen for unknown app routes', () => { + window.history.replaceState(null, '', '/app/channels/42'); + + render(); + + expect(screen.getByRole('heading', { name: 'Page not found' })).toBeInTheDocument(); + expect(screen.getByText('/app/channels/42')).toBeInTheDocument(); + expect(screen.getByRole('link', { name: 'Dashboard' })).not.toHaveAttribute( + 'aria-current', + 'page' + ); + }); + + it('opens the Connect menu with absolute player endpoints and live channel count', async () => { + mockDashboardApi({ + channels: [ + { id: 1, name: 'Retro Cartoons', number: '5.1' }, + { id: 2, name: 'News 24', number: '24' } + ] + }); + render(); fireEvent.click(screen.getByRole('button', { name: 'Connect' })); @@ -73,9 +102,23 @@ describe('ChicoryTV SPA scaffold', () => { expect(screen.getByRole('dialog', { name: 'Connect a player' })).toBeInTheDocument(); expect(screen.getByText('M3U playlist')).toBeInTheDocument(); expect(screen.getByText('XMLTV guide')).toBeInTheDocument(); - expect(screen.getByText('/iptv/channels.m3u?enabled=1')).toBeInTheDocument(); - expect(screen.getByText('/iptv/xmltv.xml?enabled=1')).toBeInTheDocument(); - expect(screen.getByRole('checkbox', { name: 'Only enabled channels' })).toBeChecked(); + expect(screen.getByText(`${window.location.origin}/iptv/channels.m3u`)).toBeInTheDocument(); + expect(screen.getByText(`${window.location.origin}/iptv/xmltv.xml`)).toBeInTheDocument(); + expect(await screen.findByText('2 channels')).toBeInTheDocument(); + expect(screen.queryByRole('checkbox', { name: 'Only enabled channels' })).not.toBeInTheDocument(); + }); + + it('focuses the Connect menu and closes it with Escape', () => { + render(); + + fireEvent.click(screen.getByRole('button', { name: 'Connect' })); + + const dialog = screen.getByRole('dialog', { name: 'Connect a player' }); + expect(dialog).toHaveFocus(); + + fireEvent.keyDown(dialog, { key: 'Escape' }); + + expect(screen.queryByRole('dialog', { name: 'Connect a player' })).not.toBeInTheDocument(); }); it('applies the warm design-system default by removing the theme attribute', () => { diff --git a/web/src/App.tsx b/web/src/App.tsx index 84572a949..ea0ee7c0b 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1,4 +1,12 @@ -import { useEffect, useMemo, useState, type MouseEvent, type ReactNode } from 'react'; +import { + useEffect, + useMemo, + useRef, + useState, + type KeyboardEvent, + type MouseEvent, + type ReactNode +} from 'react'; import { Bell, CalendarClock, @@ -36,7 +44,7 @@ import { StatusDot, Tag } from './components'; -import { useDashboardQuery, type DashboardChannel } from './api'; +import { useChannelsQuery, useDashboardQuery, type DashboardChannel } from './api'; import { applyDesignSystemTheme, designSystemThemes, @@ -172,7 +180,6 @@ const routes: ScreenRoute[] = [ ]; const routeById = new Map(routes.map((route) => [route.id, route])); -const dashboardRoute = routeById.get('dashboard')!; const primaryNavIds: ScreenId[] = [ 'dashboard', @@ -189,10 +196,10 @@ function normalizePath(pathname: string): string { return pathname.replace(/\/+$/, '') || '/app'; } -function routeFromLocation(): ScreenRoute { +function routeFromLocation(): ScreenRoute | null { const pathname = normalizePath(window.location.pathname); - return routes.find((route) => route.path === pathname) ?? dashboardRoute; + return routes.find((route) => route.path === pathname) ?? null; } function routeHref(route: ScreenRoute): string { @@ -204,7 +211,7 @@ function SidebarNavGroup({ ids, onNavigate }: { - activeRoute: ScreenRoute; + activeRoute: ScreenRoute | null; ids: ScreenId[]; onNavigate: (route: ScreenRoute, event: MouseEvent) => void; }) { @@ -218,7 +225,7 @@ function SidebarNavGroup({ key={route.id} icon={route.icon} label={route.label} - active={route.id === activeRoute.id} + active={route.id === activeRoute?.id} badge={route.badge} badgeTone="warn" href={routeHref(route)} @@ -261,7 +268,7 @@ function Sidebar({ activeRoute, onNavigate }: { - activeRoute: ScreenRoute; + activeRoute: ScreenRoute | null; onNavigate: (route: ScreenRoute, event: MouseEvent) => void; }) { return ( @@ -318,8 +325,35 @@ function EndpointRow({ icon, label, url }: { icon: ReactNode; label: string; url function ConnectMenu() { const [open, setOpen] = useState(false); - const [enabledOnly, setEnabledOnly] = useState(true); - const suffix = enabledOnly ? '?enabled=1' : ''; + const menuRef = useRef(null); + const channelsQuery = useChannelsQuery(); + const playlistUrl = `${window.location.origin}/iptv/channels.m3u`; + const guideUrl = `${window.location.origin}/iptv/xmltv.xml`; + + useEffect(() => { + if (open) { + menuRef.current?.focus(); + } + }, [open]); + + const onMenuKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + setOpen(false); + } + }; + + const channelCountLabel = (() => { + if (channelsQuery.status === 'loading') { + return 'Loading channels'; + } + + if (channelsQuery.status === 'error') { + return 'Channels unavailable'; + } + + const count = channelsQuery.channels.length; + return `${count} ${count === 1 ? 'channel' : 'channels'}`; + })(); return (
@@ -342,29 +376,28 @@ function ConnectMenu() { aria-label="Close Connect menu" onClick={() => setOpen(false)} /> -
+
Connect a player - 128 channels + {channelCountLabel}
- + ); } @@ -670,7 +707,29 @@ function PlaceholderScreen({ route }: { route: ScreenRoute }) { ); } -function ScreenContent({ route }: { route: ScreenRoute }) { +function NotFoundScreen() { + const pathname = normalizePath(window.location.pathname); + + return ( +
+ Unknown app route} subtitle="This URL does not match a ChicoryTV screen."> +
+
+
+

Requested path

+

{pathname}

+
+
+
+
+ ); +} + +function ScreenContent({ route }: { route: ScreenRoute | null }) { + if (route === null) { + return ; + } + if (route.id === 'dashboard') { return ; } @@ -680,7 +739,7 @@ function ScreenContent({ route }: { route: ScreenRoute }) { export function App() { const [theme, setTheme] = useState(() => getStoredDesignSystemTheme()); - const [activeRoute, setActiveRoute] = useState(() => routeFromLocation()); + const [activeRoute, setActiveRoute] = useState(() => routeFromLocation()); useEffect(() => { applyDesignSystemTheme(theme); @@ -697,9 +756,13 @@ export function App() { const route = useMemo(() => activeRoute, [activeRoute]); const navigate = (nextRoute: ScreenRoute, event: MouseEvent) => { + if (event.metaKey || event.ctrlKey || event.shiftKey || event.button !== 0) { + return; + } + event.preventDefault(); - if (nextRoute.id === route.id) { + if (nextRoute.id === route?.id) { return; }