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>
193 lines
6.5 KiB
TypeScript
193 lines
6.5 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
||
import { authUrl, checkPin, createPin, type Pin } from '../plex/account';
|
||
import { useSession } from '../app/session';
|
||
import { openExternal } from '../platform/native';
|
||
import { Button, KaleidoMark, Spinner } from '../ui/primitives';
|
||
import { Icon } from '../ui/Icon';
|
||
import { WindowDrag } from './WindowDrag';
|
||
|
||
type Mode = 'start' | 'browser' | 'code' | 'manual';
|
||
|
||
export function SignIn() {
|
||
const completeSignIn = useSession((s) => s.completeSignIn);
|
||
const connectManual = useSession((s) => s.connectManual);
|
||
const sessionError = useSession((s) => s.error);
|
||
const [mode, setMode] = useState<Mode>('start');
|
||
const [pin, setPin] = useState<Pin | null>(null);
|
||
const [error, setError] = useState(sessionError);
|
||
const [busy, setBusy] = useState(false);
|
||
const [url, setUrl] = useState('');
|
||
const [token, setToken] = useState('');
|
||
const poll = useRef<ReturnType<typeof setInterval> | null>(null);
|
||
|
||
const stopPolling = () => {
|
||
if (poll.current) clearInterval(poll.current);
|
||
poll.current = null;
|
||
};
|
||
useEffect(() => stopPolling, []);
|
||
|
||
const begin = async (strong: boolean) => {
|
||
setError('');
|
||
setBusy(true);
|
||
stopPolling();
|
||
try {
|
||
const p = await createPin(strong);
|
||
setPin(p);
|
||
setMode(strong ? 'browser' : 'code');
|
||
if (strong) await openExternal(authUrl(p));
|
||
const started = Date.now();
|
||
poll.current = setInterval(async () => {
|
||
if (Date.now() - started > 15 * 60_000) {
|
||
stopPolling();
|
||
setError('The sign-in request expired. Try again.');
|
||
setMode('start');
|
||
return;
|
||
}
|
||
try {
|
||
const t = await checkPin(p);
|
||
if (t) {
|
||
stopPolling();
|
||
await completeSignIn(t);
|
||
}
|
||
} catch {
|
||
/* keep polling */
|
||
}
|
||
}, 1500);
|
||
} catch (e: any) {
|
||
setError(e.message ?? String(e));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
};
|
||
|
||
const manual = async () => {
|
||
setError('');
|
||
setBusy(true);
|
||
try {
|
||
let u = url.trim();
|
||
if (!/^https?:\/\//.test(u)) u = `http://${u}`;
|
||
if (!/:\d+/.test(u.replace(/^https?:\/\//, ''))) u = `${u.replace(/\/$/, '')}:32400`;
|
||
await connectManual(u, token.trim());
|
||
} catch (e: any) {
|
||
setError(e.message ?? String(e));
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="auth-screen">
|
||
<WindowDrag />
|
||
<div className="auth-backdrop" />
|
||
<div className="auth-card">
|
||
<div className="auth-brand">
|
||
<KaleidoMark size={46} />
|
||
<span>KALEIDO</span>
|
||
</div>
|
||
{mode === 'start' && (
|
||
<>
|
||
<h1>Your Plex library, front and center.</h1>
|
||
<p className="auth-lede">Sign in with your Plex account to browse and play everything on your servers.</p>
|
||
<div className="auth-actions">
|
||
<Button variant="primary" size="lg" icon="external" busy={busy} onClick={() => begin(true)}>
|
||
Sign in with Plex
|
||
</Button>
|
||
<button className="auth-link" onClick={() => begin(false)}>
|
||
Use a code at plex.tv/link instead
|
||
</button>
|
||
</div>
|
||
</>
|
||
)}
|
||
{mode === 'browser' && (
|
||
<>
|
||
<h1>Continue in your browser</h1>
|
||
<p className="auth-lede">We opened Plex’s sign-in page. Kaleido continues automatically once you approve it.</p>
|
||
<div className="auth-waiting">
|
||
<Spinner size={18} /> Waiting for Plex…
|
||
</div>
|
||
<div className="auth-actions">
|
||
<Button variant="ghost" icon="external" onClick={() => pin && openExternal(authUrl(pin))}>
|
||
Open the page again
|
||
</Button>
|
||
<button
|
||
className="auth-link"
|
||
onClick={() => {
|
||
stopPolling();
|
||
setMode('start');
|
||
}}
|
||
>
|
||
Cancel
|
||
</button>
|
||
</div>
|
||
</>
|
||
)}
|
||
{mode === 'code' && pin && (
|
||
<>
|
||
<h1>Link Kaleido</h1>
|
||
<p className="auth-lede">
|
||
On any device, go to <b>plex.tv/link</b> and enter this code.
|
||
</p>
|
||
<div className="auth-code selectable">{pin.code}</div>
|
||
<div className="auth-waiting">
|
||
<Spinner size={18} /> Waiting for Plex…
|
||
</div>
|
||
<div className="auth-actions">
|
||
<Button variant="ghost" icon="external" onClick={() => openExternal('https://plex.tv/link')}>
|
||
Open plex.tv/link
|
||
</Button>
|
||
<button
|
||
className="auth-link"
|
||
onClick={() => {
|
||
stopPolling();
|
||
setMode('start');
|
||
}}
|
||
>
|
||
Back
|
||
</button>
|
||
</div>
|
||
</>
|
||
)}
|
||
{mode === 'manual' && (
|
||
<>
|
||
<h1>Connect to a server</h1>
|
||
<p className="auth-lede">Enter a Plex Media Server address and an access token. Account features such as profiles need a full sign-in.</p>
|
||
<form
|
||
className="auth-form"
|
||
onSubmit={(e) => {
|
||
e.preventDefault();
|
||
manual();
|
||
}}
|
||
>
|
||
<label className="field">
|
||
<span>Server address</span>
|
||
<input autoFocus value={url} placeholder="192.168.1.20:32400" onChange={(e) => setUrl(e.target.value)} />
|
||
</label>
|
||
<label className="field">
|
||
<span>Access token</span>
|
||
<input type="password" value={token} onChange={(e) => setToken(e.target.value)} />
|
||
</label>
|
||
<div className="auth-actions">
|
||
<Button variant="primary" busy={busy} disabled={!url || !token} type="submit">
|
||
Connect
|
||
</Button>
|
||
<button type="button" className="auth-link" onClick={() => setMode('start')}>
|
||
Back
|
||
</button>
|
||
</div>
|
||
</form>
|
||
</>
|
||
)}
|
||
{error && (
|
||
<div className="auth-error">
|
||
<Icon name="alert" size={16} /> {error}
|
||
</div>
|
||
)}
|
||
{mode === 'start' && (
|
||
<button className="auth-manual" onClick={() => setMode('manual')}>
|
||
Connect to a server manually
|
||
</button>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|