Kaleido-Desktop/src/screens/Downloads.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

222 lines
9 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, useState } from 'react';
import { useDownloads, type Download } from '../downloads/store';
import { useSession } from '../app/session';
import { navigate } from '../app/router';
import { duration, episodeLabel, fileSize } from '../app/format';
import { usePlayer } from '../player/store';
import { invoke, isNative, localFileUrl, revealPath } from '../platform/native';
import { Button, EmptyState, IconButton } from '../ui/primitives';
import { Icon } from '../ui/Icon';
import { confirmDialog, openMenu } from '../ui/overlays';
function useLocalArt(d: Download, key: string) {
const [url, setUrl] = useState('');
const path = d.meta.localAssets?.[key];
useEffect(() => {
if (path) localFileUrl(path).then(setUrl);
}, [path]);
return url;
}
function DownloadRow({ d }: { d: Download }) {
const store = useDownloads();
const server = useSession((s) => s.server);
const item = store.itemOf(d);
const still = useLocalArt(d, item.type === 'episode' ? 'still' : 'art');
const poster = useLocalArt(d, 'poster');
const pct = d.total ? Math.min(100, (d.received / d.total) * 100) : 0;
const isEp = item.type === 'episode';
const onServer = server?.machineId === d.meta.serverId;
const play = () => usePlayer.getState().play(item, { localFile: d.filePath, resume: true });
const remove = async () => {
if (await confirmDialog({ title: 'Remove download?', message: `“${item.title}” will be deleted from this computer. It stays available on your Plex server.`, confirm: 'Remove', danger: true })) store.remove(d.id);
};
const status =
d.status === 'completed'
? fileSize(d.total || d.received)
: d.status === 'downloading'
? `${fileSize(d.received)} of ${fileSize(d.total)}${d.speed ? ` · ${fileSize(d.speed)}/s` : ''}`
: d.status === 'queued'
? 'Waiting…'
: d.status === 'paused'
? `Paused · ${fileSize(d.received)} of ${fileSize(d.total)}`
: (d.error ?? 'Failed');
return (
<div className={`download-row ${d.status}`}>
<button className="dl-thumb" onClick={d.status === 'completed' ? play : undefined} aria-label={`Play ${item.title}`}>
{(still || poster) && <img src={still || poster} alt="" draggable={false} className={still ? '' : 'poster'} />}
{d.status === 'completed' && (
<span className="dl-play">
<Icon name="play" size={18} />
</span>
)}
</button>
<div className="dl-details">
<h3>{isEp ? item.grandparentTitle : item.title}</h3>
<p>
{isEp ? `${episodeLabel(item)} · ${item.title}` : [item.year, duration(item.duration)].filter(Boolean).join(' · ')}
{d.meta.serverName && <span className="dim"> · {d.meta.serverName}</span>}
</p>
{d.status !== 'completed' ? (
<div className="dl-progress">
<span className="progress-bar">
<i style={{ width: `${pct}%` }} />
</span>
<span className="dl-status">{status}</span>
</div>
) : (
<span className="file-status">
<Icon name="check" size={14} /> Ready offline · {status}
</span>
)}
</div>
<div className="dl-actions">
{d.status === 'completed' && (
<Button variant="ghost" size="sm" icon="play" onClick={play}>
Play
</Button>
)}
{d.status === 'downloading' || d.status === 'queued' ? (
<IconButton icon="pause" label="Pause" onClick={() => store.pause(d.id)} />
) : d.status === 'paused' || d.status === 'failed' ? (
<IconButton icon="refresh" label="Resume" onClick={() => store.resume(d.id)} />
) : null}
<IconButton
icon="dots"
label="More"
onClick={(e) =>
openMenu(
[
...(onServer ? [{ label: 'Details', icon: 'info', onSelect: () => navigate({ name: 'item', id: item.id }) }] : []),
...(d.status === 'completed' ? [{ label: 'Show in folder', icon: 'folder', onSelect: () => revealPath(d.filePath) }] : []),
{ divider: true },
{ label: 'Remove download', icon: 'trash', danger: true, onSelect: remove },
],
e.currentTarget,
{ align: 'end' },
)
}
/>
</div>
</div>
);
}
export default function Downloads({ offline }: { offline?: boolean }) {
const store = useDownloads();
const [space, setSpace] = useState<{ free: number; total: number } | null>(null);
useEffect(() => {
store.init();
}, []);
useEffect(() => {
if (isNative && store.dir) invoke<{ free: number; total: number }>('disk_space', { path: store.dir }).then(setSpace).catch(() => {});
}, [store.dir, store.items.length]);
const active = store.items.filter((d) => d.status !== 'completed');
const done = store.items.filter((d) => d.status === 'completed');
const used = store.items.reduce((a, d) => a + (d.status === 'completed' ? d.total || d.received : d.received), 0);
const movies = done.filter((d) => d.meta.raw?.type !== 'episode');
const episodes = done.filter((d) => d.meta.raw?.type === 'episode');
const shows = new Map<string, Download[]>();
for (const e of episodes) {
const k = e.meta.raw?.grandparentTitle ?? 'Episodes';
shows.set(k, [...(shows.get(k) ?? []), e]);
}
return (
<div className={`downloads-page page-pad${offline ? '' : ' top'}`}>
<div className="download-intro">
<div>
<div className="micro">Offline · This computer</div>
<h1 className="page-title">Downloads</h1>
<p className="page-subtitle">Movies and episodes saved to this computer play without a connection. Progress syncs to Plex when you’re back online.</p>
</div>
{isNative && (
<div className="storage-card">
<div className="storage-head">
<b>Storage</b>
<span>{space ? `${fileSize(space.free)} free` : ''}</span>
</div>
<div className="storage-bar">
{space && (
<>
<span className="other" style={{ width: `${Math.max(0, ((space.total - space.free - used) / space.total) * 100)}%` }} />
<span className="mine" style={{ width: `${(used / space.total) * 100}%` }} />
</>
)}
</div>
<div className="storage-caption">
{fileSize(used) || '0 MB'} used by Kaleido
{store.dir && (
<button className="inline-link" onClick={() => revealPath(store.dir)} title={store.dir}>
Open folder
</button>
)}
</div>
</div>
)}
</div>
{!isNative && (
<EmptyState icon="download" title="Downloads need the desktop app">
Install Kaleido for Windows to save movies and episodes for offline viewing.
</EmptyState>
)}
{isNative && !store.items.length && (
<EmptyState icon="download" title="Nothing downloaded yet" action={!offline && <Button variant="ghost" icon="film" onClick={() => navigate({ name: 'section', kind: 'movie', tab: 'library' })}>Browse movies</Button>}>
Choose Download on a movie or episode, or download a whole season from its page.
</EmptyState>
)}
{active.length > 0 && (
<section className="dl-section">
<div className="dl-section-head">
<h2>
In progress <small>{active.length}</small>
</h2>
<div className="actions">
{active.some((d) => d.status === 'downloading' || d.status === 'queued') ? (
<Button variant="text" onClick={() => active.forEach((d) => (d.status === 'downloading' || d.status === 'queued') && store.pause(d.id))}>
Pause all
</Button>
) : (
<Button variant="text" onClick={() => active.forEach((d) => store.resume(d.id))}>
Resume all
</Button>
)}
</div>
</div>
{active.map((d) => (
<DownloadRow key={d.id} d={d} />
))}
</section>
)}
{movies.length > 0 && (
<section className="dl-section">
<div className="dl-section-head">
<h2>
Movies <small>{movies.length}</small>
</h2>
</div>
{movies.map((d) => (
<DownloadRow key={d.id} d={d} />
))}
</section>
)}
{[...shows.entries()].map(([show, list]) => (
<section className="dl-section" key={show}>
<div className="dl-section-head">
<h2>
{show} <small>{list.length} episode{list.length === 1 ? '' : 's'}</small>
</h2>
</div>
{list
.sort((a, b) => (a.meta.raw.parentIndex - b.meta.raw.parentIndex) * 1000 + (a.meta.raw.index - b.meta.raw.index))
.map((d) => (
<DownloadRow key={d.id} d={d} />
))}
</section>
))}
</div>
);
}