Kaleido-Desktop/src/ui/primitives.tsx
Elijah 00d72e70f4 Create Kaleido Desktop, a cinematic Plex client for Windows
Tauri 2 shell with a React/TypeScript interface and libmpv playback composed
beneath the web view (DirectComposition on Windows). Shared code with per-OS
platform modules and configs so macOS and Linux can follow.

- Plex sign-in (browser, plex.tv/link or manual), server selection, Plex Home
- Home, Movies and TV Shows (Discover, Library, Collections), search, people
- Title pages with logos, ratings, cast, extras, seasons and episodes
- Direct play with Plex conversion fallback, tracks, markers, autoplay
- Metadata editing, artwork, fix match, refresh, analyze and deletion
- Resumable offline downloads
- App settings and Plex server administration with a live dashboard
- Signed updater feed and Forgejo CI

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 16:26:37 -07:00

202 lines
6.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useEffect, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type ReactNode } from 'react';
import { Icon } from './Icon';
import { openMenu, type MenuItem } from './overlays';
type Variant = 'primary' | 'muted' | 'ghost' | 'outline' | 'danger' | 'text';
export function Button({
variant = 'muted',
icon,
iconRight,
children,
className = '',
busy,
size,
...rest
}: ButtonHTMLAttributes<HTMLButtonElement> & { variant?: Variant; icon?: string; iconRight?: string; busy?: boolean; size?: 'sm' | 'lg' }) {
return (
<button className={`btn ${variant}${size ? ` ${size}` : ''} ${className}`} {...rest} disabled={rest.disabled || busy}>
{busy ? <Spinner size={16} /> : icon ? <Icon name={icon} size={size === 'sm' ? 15 : 17} /> : null}
{children && <span>{children}</span>}
{iconRight && <Icon name={iconRight} size={15} />}
</button>
);
}
export function IconButton({
icon,
label,
className = '',
size = 19,
round,
...rest
}: ButtonHTMLAttributes<HTMLButtonElement> & { icon: string; label: string; size?: number; round?: boolean }) {
return (
<button className={`${round ? 'circle-btn' : 'icon-btn'} ${className}`} aria-label={label} title={label} {...rest}>
<Icon name={icon} size={size} />
</button>
);
}
export function Spinner({ size = 22, className = '' }: { size?: number; className?: string }) {
return <span className={`spinner ${className}`} style={{ width: size, height: size }} aria-label="Loading" />;
}
/** An image that fades in once decoded, keeping its box stable. */
export function Artwork({
src,
alt = '',
className = '',
style,
fit = 'cover',
eager,
onLoad,
fallback,
}: {
src?: string;
alt?: string;
className?: string;
style?: CSSProperties;
fit?: 'cover' | 'contain';
eager?: boolean;
onLoad?: () => void;
fallback?: ReactNode;
}) {
const [state, setState] = useState<'loading' | 'ready' | 'error'>('loading');
const ref = useRef<HTMLImageElement>(null);
useEffect(() => {
setState('loading');
const img = ref.current;
if (img?.complete && img.naturalWidth) setState('ready');
}, [src]);
if (!src || state === 'error') return <div className={`artwork empty ${className}`} style={style}>{fallback}</div>;
return (
<img
ref={ref}
src={src}
alt={alt}
draggable={false}
loading={eager ? 'eager' : 'lazy'}
decoding="async"
className={`artwork ${state === 'ready' ? 'ready' : ''} ${className}`}
style={{ objectFit: fit, ...style }}
onLoad={() => {
setState('ready');
onLoad?.();
}}
onError={() => setState('error')}
/>
);
}
export function Switch({ checked, onChange, label, disabled }: { checked: boolean; onChange: (v: boolean) => void; label?: string; disabled?: boolean }) {
return (
<button role="switch" aria-checked={checked} aria-label={label} disabled={disabled} className={`switch${checked ? ' on' : ''}`} onClick={() => onChange(!checked)}>
<span />
</button>
);
}
export interface Option<T extends string | number> {
value: T;
label: string;
hint?: string;
}
/** A dropdown that opens a Kaleido menu (not a native select). */
export function Select<T extends string | number>({
value,
options,
onChange,
label,
prefix,
className = '',
minWidth,
disabled,
placeholder,
}: {
value: T;
options: Option<T>[];
onChange: (v: T) => void;
label?: string;
prefix?: string;
className?: string;
minWidth?: number;
disabled?: boolean;
placeholder?: string;
}) {
const current = options.find((o) => o.value === value);
return (
<button
className={`select ${className}`}
aria-label={label}
disabled={disabled}
onClick={(e) =>
openMenu(
options.map((o): MenuItem => ({ label: o.label, hint: o.hint, checked: o.value === value, onSelect: () => onChange(o.value) })),
e.currentTarget,
{ minWidth: Math.max(minWidth ?? 0, (e.currentTarget as HTMLElement).offsetWidth) },
)
}
>
{prefix && <span className="select-prefix">{prefix}</span>}
<span className="select-value">{current?.label ?? placeholder ?? 'Choose'}</span>
<Icon name="chevronDown" size={15} />
</button>
);
}
export function Segmented<T extends string>({ value, options, onChange, className = '' }: { value: T; options: Option<T>[]; onChange: (v: T) => void; className?: string }) {
return (
<div className={`seg ${className}`} role="tablist">
{options.map((o) => (
<button key={o.value} role="tab" aria-selected={o.value === value} className={o.value === value ? 'selected' : ''} onClick={() => onChange(o.value)}>
{o.label}
</button>
))}
</div>
);
}
export function EmptyState({ icon = 'info', title, children, action }: { icon?: string; title: string; children?: ReactNode; action?: ReactNode }) {
return (
<div className="empty-state">
<div className="empty-icon">
<Icon name={icon} size={26} />
</div>
<h3>{title}</h3>
{children && <p>{children}</p>}
{action}
</div>
);
}
export function ErrorState({ error, onRetry }: { error: unknown; onRetry?: () => void }) {
const message = error instanceof Error ? error.message : String(error ?? 'Something went wrong.');
return (
<EmptyState icon="alert" title="Couldn’t load this" action={onRetry && <Button icon="refresh" onClick={onRetry}>Try again</Button>}>
{message}
</EmptyState>
);
}
export function PageLoader() {
return (
<div className="page-loader">
<Spinner size={28} />
</div>
);
}
export function KaleidoMark({ size = 26, className = '' }: { size?: number; className?: string }) {
return (
<svg width={size} height={size} viewBox="0 0 64 64" className={className} aria-hidden="true">
<path fill="#C3E8D3" d="M33,4 L55.5,17 L42.5,24.5 L33,19 Z" />
<path fill="#71B9B0" d="M56.5,19 L56.5,45 L43.5,37.5 L43.5,26.5 Z" />
<path fill="#3D7C7B" d="M55.5,47 L33,60 L33,45 L42.5,39.5 Z" />
<path fill="#D0A977" d="M31,60 L8.5,47 L21.5,39.5 L31,45 Z" />
<path fill="#E9D8AE" d="M7.5,45 L7.5,19 L20.5,26.5 L20.5,37.5 Z" />
<path fill="#EEF2DD" d="M8.5,17 L31,4 L31,19 L21.5,24.5 Z" />
</svg>
);
}