import { useEffect, useState } from 'react';
import { navigate, topLevel, useRouter } from './router';
import { useSession } from './session';
import { useSettings } from './settings';
import { Icon } from '../ui/Icon';
import { KaleidoMark } from '../ui/primitives';
import { openMenu, type MenuItem } from '../ui/overlays';
import { isMac, isNative, shortcut, win } from '../platform/native';
import { useDownloads } from '../downloads/store';
import { useHomeUsers } from './useHomeUsers';
const NAV = [
{ id: 'home', label: 'Home', route: { name: 'home' } as const },
{ id: 'movies', label: 'Movies', route: { name: 'section', kind: 'movie', tab: 'discover' } as const },
{ id: 'shows', label: 'TV Shows', route: { name: 'section', kind: 'show', tab: 'discover' } as const },
{ id: 'downloads', label: 'Downloads', route: { name: 'downloads' } as const },
];
/** Caption buttons drawn by the page; macOS shows its own traffic lights instead. */
export function WindowControls() {
const [maximized, setMaximized] = useState(false);
useEffect(() => {
if (!isNative || isMac) return;
let off: (() => void) | undefined;
const sync = () => win.isMaximized().then(setMaximized);
sync();
win.onResized(sync).then((u) => (off = u));
return () => off?.();
}, []);
if (!isNative || isMac) return null;
return (
);
}
function Avatar() {
const user = useSession((s) => s.user);
const server = useSession((s) => s.server);
const connection = useSession((s) => s.connection);
const showServerPicker = useSession((s) => s.showServerPicker);
const signOut = useSession((s) => s.signOut);
const home = useHomeUsers();
const initial = (user?.title || server?.name || 'K').slice(0, 1).toUpperCase();
const menu = (el: HTMLElement) => {
const items: MenuItem[] = [
{ heading: user ? user.title : 'Kaleido' },
{
label: server ? `${server.name}` : 'No server',
hint: connection ? (connection.relay ? 'Relay' : connection.local ? 'Local' : 'Remote') : '',
icon: 'server',
onSelect: showServerPicker,
},
];
if (home.users.length > 1) {
items.push({
label: 'Switch profile',
icon: 'users',
submenu: home.users.map((u) => ({ label: u.title, checked: u.uuid === user?.uuid, onSelect: () => home.switchTo(u) })),
});
}
// Settings has its own button in the bar; the menu keeps account actions.
if (server?.owned) items.push({ divider: true }, { label: 'Server dashboard', icon: 'activity', onSelect: () => navigate({ name: 'settings', mode: 'server', tab: 'dashboard' }) });
items.push({ divider: true }, { label: 'Sign out', icon: 'logout', onSelect: signOut });
openMenu(items, el, { align: 'end', minWidth: 250 });
};
return (
);
}
export function TopBar({ solid }: { solid: boolean }) {
const current = useRouter((s) => s.current.route);
const canBack = useRouter((s) => s.index > 0);
const back = useRouter((s) => s.back);
const active = topLevel(current);
const activeDownloads = useDownloads((s) => s.items.filter((d) => d.status === 'downloading' || d.status === 'queued').length);
const startPage = useSettings((s) => s.startPage);
void startPage;
const showBack = canBack && !active;
return (
);
}