+47
-4
@@ -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(<App />);
|
||||
|
||||
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(<App />);
|
||||
|
||||
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(<App />);
|
||||
|
||||
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(<App />);
|
||||
|
||||
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', () => {
|
||||
|
||||
+92
-29
@@ -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<HTMLDivElement>(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<HTMLDivElement>) => {
|
||||
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 (
|
||||
<div className="ctv-connect">
|
||||
@@ -342,29 +376,28 @@ function ConnectMenu() {
|
||||
aria-label="Close Connect menu"
|
||||
onClick={() => setOpen(false)}
|
||||
/>
|
||||
<div className="ctv-connect-menu" role="dialog" aria-label="Connect a player">
|
||||
<div
|
||||
className="ctv-connect-menu"
|
||||
role="dialog"
|
||||
aria-label="Connect a player"
|
||||
tabIndex={-1}
|
||||
ref={menuRef}
|
||||
onKeyDown={onMenuKeyDown}
|
||||
>
|
||||
<div className="ctv-connect-header">
|
||||
<strong>Connect a player</strong>
|
||||
<span>128 channels</span>
|
||||
<span>{channelCountLabel}</span>
|
||||
</div>
|
||||
<EndpointRow
|
||||
icon={<ListVideo aria-hidden="true" size={15} />}
|
||||
label="M3U playlist"
|
||||
url={`/iptv/channels.m3u${suffix}`}
|
||||
url={playlistUrl}
|
||||
/>
|
||||
<EndpointRow
|
||||
icon={<CalendarClock aria-hidden="true" size={15} />}
|
||||
label="XMLTV guide"
|
||||
url={`/iptv/xmltv.xml${suffix}`}
|
||||
url={guideUrl}
|
||||
/>
|
||||
<label className="ctv-connect-option">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={enabledOnly}
|
||||
onChange={(event) => setEnabledOnly(event.target.checked)}
|
||||
/>
|
||||
<span>Only enabled channels</span>
|
||||
</label>
|
||||
<p>
|
||||
<Info aria-hidden="true" size={13} />
|
||||
Works with any IPTV player.
|
||||
@@ -376,12 +409,16 @@ function ConnectMenu() {
|
||||
);
|
||||
}
|
||||
|
||||
function TopBar({ route }: { route: ScreenRoute }) {
|
||||
function TopBar({ route }: { route: ScreenRoute | null }) {
|
||||
const title = route?.title ?? 'Page not found';
|
||||
const kicker = route?.kicker ?? 'Unknown route';
|
||||
const primaryAction = route?.primaryAction ?? 'Go To Dashboard';
|
||||
|
||||
return (
|
||||
<header className="ctv-topbar">
|
||||
<div className="ctv-topbar-title">
|
||||
<p>{route.kicker}</p>
|
||||
<h1>{route.title}</h1>
|
||||
<p>{kicker}</p>
|
||||
<h1>{title}</h1>
|
||||
</div>
|
||||
|
||||
<label className="ctv-topbar-search">
|
||||
@@ -403,7 +440,7 @@ function TopBar({ route }: { route: ScreenRoute }) {
|
||||
<span className="ctv-avatar">TB</span>
|
||||
</div>
|
||||
|
||||
<Button startIcon={<Plus aria-hidden="true" size={15} />}>{route.primaryAction}</Button>
|
||||
<Button startIcon={<Plus aria-hidden="true" size={15} />}>{primaryAction}</Button>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
@@ -670,7 +707,29 @@ function PlaceholderScreen({ route }: { route: ScreenRoute }) {
|
||||
);
|
||||
}
|
||||
|
||||
function ScreenContent({ route }: { route: ScreenRoute }) {
|
||||
function NotFoundScreen() {
|
||||
const pathname = normalizePath(window.location.pathname);
|
||||
|
||||
return (
|
||||
<div className="ctv-screen-stack">
|
||||
<Card title={<h2>Unknown app route</h2>} subtitle="This URL does not match a ChicoryTV screen.">
|
||||
<div className="ctv-placeholder-layout">
|
||||
<div className="ctv-placeholder-icon"><Info aria-hidden="true" size={20} /></div>
|
||||
<div>
|
||||
<h3>Requested path</h3>
|
||||
<p>{pathname}</p>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ScreenContent({ route }: { route: ScreenRoute | null }) {
|
||||
if (route === null) {
|
||||
return <NotFoundScreen />;
|
||||
}
|
||||
|
||||
if (route.id === 'dashboard') {
|
||||
return <DashboardScreen />;
|
||||
}
|
||||
@@ -680,7 +739,7 @@ function ScreenContent({ route }: { route: ScreenRoute }) {
|
||||
|
||||
export function App() {
|
||||
const [theme, setTheme] = useState<DesignSystemThemeId>(() => getStoredDesignSystemTheme());
|
||||
const [activeRoute, setActiveRoute] = useState<ScreenRoute>(() => routeFromLocation());
|
||||
const [activeRoute, setActiveRoute] = useState<ScreenRoute | null>(() => 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user