All files / src/components/playlist ProviderSettingsModal.tsx

92.1% Statements 70/76
65% Branches 39/60
84% Functions 21/25
91.78% Lines 67/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457                                                                      1x                 1x               1x           1x   1x 120x                       1x 31x             156x                     1x 120x 120x   120x 8x 8x 8x 8x   2x   8x       120x     1x           30x   30x 30x 30x     30x 30x 30x 30x     30x     30x     30x 30x 30x   30x 30x 30x 30x   30x   30x   30x 1x 1x 1x     1x       30x 5x 5x 5x     4x 4x       30x 1x   1x   1x       1x 1x                 1x 1x       1x           1x     30x                                                                         30x     2x                                                                                                                                 1x                                                                                             30x                                                                                                 30x                                                                                                        
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { useId, useState } from "react";
import { X } from "lucide-react";
import { BusyButton } from "@/components/ui/busy-button";
import { Button } from "@/components/ui/button";
import { Dialog } from "@/components/ui/dialog";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { SegmentedControl, type SegmentedOption } from "@/components/ui/segmented-control";
import { fileUrlFromPath, isFileUrl, pathFromFileUrl } from "@/lib/file-url";
import type { Provider } from "@/lib/types";
import {
	type ProviderSettings,
	loadProviderSettings,
	saveProviderSettings,
	setEpgLastRefresh,
} from "@/hooks/useChannels";
import { refreshEpg, setEpgUrl, detectEpgUrl } from "@/lib/tauri";
import { XtreamFields } from "./XtreamFields";
 
interface ProviderSettingsModalProps {
	provider: Provider;
	onClose: () => void;
	onRefreshNow: (id: string) => Promise<void>;
	onUpdateProvider: (
		id: string,
		name: string,
		url: string,
		username?: string,
		password?: string
	) => Promise<void>;
}
 
const INTERVAL_OPTIONS = [
	{ label: "Every hour", hours: 1 },
	{ label: "Every 6 hours", hours: 6 },
	{ label: "Every 12 hours", hours: 12 },
	{ label: "Every day", hours: 24 },
	{ label: "Every 2 days", hours: 48 },
	{ label: "Every week", hours: 168 },
];
 
const EPG_INTERVAL_OPTIONS = [
	{ label: "Every 6 hours", hours: 6 },
	{ label: "Every 12 hours", hours: 12 },
	{ label: "Every day", hours: 24 },
	{ label: "Every 2 days", hours: 48 },
	{ label: "Every 3 days", hours: 72 },
];
 
const OFF_ON: SegmentedOption<boolean>[] = [
	{ value: false, label: "Off" },
	{ value: true, label: "On" },
];
 
/** Shown when a guide refreshed by hand had no programs. */
const EMPTY_GUIDE_MESSAGE = "The guide came back with no programs, so nothing changed.";
 
const SectionLabel = ({ children }: { children: React.ReactNode }) => (
	<p className="text-[10px] font-semibold text-muted-foreground uppercase tracking-wider mb-2">
		{children}
	</p>
);
 
interface IntervalSelectProps {
	id: string;
	value: number;
	onChange: (hours: number) => void;
	options: { label: string; hours: number }[];
}
 
const IntervalSelect = ({ id, value, onChange, options }: IntervalSelectProps) => (
	<select
		id={id}
		value={value}
		onChange={(e) => onChange(Number(e.target.value))}
		className="w-full bg-secondary text-sm rounded-lg px-3 py-2 border border-transparent focus:outline-hidden focus:border-primary appearance-none cursor-pointer"
	>
		{options.map((opt) => (
			<option key={opt.hours} value={opt.hours}>
				{opt.label}
			</option>
		))}
	</select>
);
 
/**
 * Each button in the modal runs one action and shows how its last run went
 * right under it, independent of the other buttons.
 */
const useAction = () => {
	const [busy, setBusy] = useState(false);
	const [message, setMessage] = useState<string | null>(null);
 
	const run = async (action: () => Promise<void>) => {
		setBusy(true);
		setMessage(null);
		try {
			await action();
		} catch (e) {
			setMessage(String(e));
		} finally {
			setBusy(false);
		}
	};
 
	return { busy, message, setMessage, run };
};
 
export const ProviderSettingsModal = ({
	provider,
	onClose,
	onRefreshNow,
	onUpdateProvider,
}: ProviderSettingsModalProps) => {
	const titleId = useId();
	// Only seeds the form state below, so storage is read once and not on every keystroke.
	const [initial] = useState(() => loadProviderSettings(provider.id));
	const isFile = isFileUrl(provider.url);
	const isXtream = provider.type === "xtream";
 
	// Details fields
	const [name, setName] = useState(provider.name);
	const [url, setUrl] = useState(pathFromFileUrl(provider.url));
	const [username, setUsername] = useState(provider.username ?? "");
	const [password, setPassword] = useState(provider.password ?? "");
 
	// Auto-refresh settings
	const [autoRefresh, setAutoRefresh] = useState<ProviderSettings["autoRefresh"]>(
		initial.autoRefresh
	);
	const [intervalHours, setIntervalHours] = useState(initial.refreshIntervalHours);
 
	// EPG settings
	const [epgUrl, setEpgUrlState] = useState(provider.epgUrl ?? "");
	const [epgAutoRefresh, setEpgAutoRefresh] = useState(initial.epgAutoRefresh);
	const [epgIntervalHours, setEpgIntervalHours] = useState(initial.epgRefreshIntervalHours);
 
	const refresh = useAction();
	const detect = useAction();
	const epgRefresh = useAction();
	const save = useAction();
	// Set when a guide refreshed by hand saved no programs.
	const [epgRefreshEmpty, setEpgRefreshEmpty] = useState(false);
 
	const handleRefreshNow = () => refresh.run(() => onRefreshNow(provider.id));
 
	const handleDetectEpgUrl = () =>
		detect.run(async () => {
			const detected = await detectEpgUrl(provider.id);
			Iif (detected) {
				setEpgUrlState(detected);
			} else {
				detect.setMessage("No EPG URL detected");
			}
		});
 
	const handleEpgRefreshNow = () => {
		setEpgRefreshEmpty(false);
		return epgRefresh.run(async () => {
			const saved = await refreshEpg(provider.id);
			// The automatic refresh waits a full interval from now, and the
			// channel list loads the guide again.
			setEpgLastRefresh(provider.id);
			setEpgRefreshEmpty(saved === 0);
		});
	};
 
	const handleSave = () =>
		save.run(async () => {
			// Update credentials if anything changed
			const storedUrl = isFile ? fileUrlFromPath(url) : url;
			const credentialsChanged =
				name !== provider.name ||
				storedUrl !== provider.url ||
				username !== (provider.username ?? "") ||
				password !== (provider.password ?? "");
			Eif (credentialsChanged) {
				await onUpdateProvider(
					provider.id,
					name,
					storedUrl,
					isXtream ? username : undefined,
					isXtream ? password : undefined
				);
			}
			// Persist EPG URL if it changed
			const currentEpgUrl = provider.epgUrl ?? "";
			Iif (epgUrl !== currentEpgUrl) {
				await setEpgUrl(provider.id, epgUrl || null);
			}
			// Persist auto-refresh settings (including EPG)
			saveProviderSettings(provider.id, {
				autoRefresh,
				refreshIntervalHours: intervalHours,
				epgAutoRefresh,
				epgRefreshIntervalHours: epgIntervalHours,
			});
			onClose();
		});
 
	return (
		<Dialog
			// Closing mid-save would drop a save error, so the modal stays until it's done.
			onClose={save.busy ? () => {} : onClose}
			labelledBy={titleId}
			overlayClassName="backdrop-blur-xs"
			className="bg-card border border-border rounded-xl shadow-2xl w-full max-w-md mx-4 flex flex-col max-h-[90vh]"
		>
			{/* Header */}
			<div className="flex items-center justify-between px-5 py-4 border-b border-border shrink-0">
				<div className="flex items-center gap-2 min-w-0">
					<h2 id={titleId} className="text-sm font-semibold truncate">
						Playlist Settings
					</h2>
					<span className="text-[10px] px-1.5 py-0.5 rounded bg-secondary text-secondary-foreground font-medium uppercase shrink-0">
						{provider.type}
					</span>
				</div>
				<button
					type="button"
					onClick={onClose}
					disabled={save.busy}
					className="ml-3 shrink-0 text-muted-foreground hover:text-foreground transition-colors disabled:pointer-events-none disabled:opacity-50"
					aria-label="Close"
				>
					<X className="h-4 w-4" />
				</button>
			</div>
 
			{/* Scrollable body */}
			<div className="px-5 py-4 space-y-5 overflow-y-auto">
				{/* Details section */}
				<div>
					<SectionLabel>Details</SectionLabel>
					<div className="space-y-2">
						<Field label="Name">
							{(id) => (
								<Input
									id={id}
									value={name}
									onChange={(e) => setName(e.target.value)}
									placeholder="Provider name"
									className="h-8 text-sm"
								/>
							)}
						</Field>
 
						{isFile ? (
							<div>
								{/* Not a label, since the path can't be edited here. */}
								<p className="text-xs text-muted-foreground mb-1">File path</p>
								<p className="text-xs text-muted-foreground bg-secondary rounded-lg px-3 py-2 truncate">
									{url}
								</p>
							</div>
						) : isXtream ? (
							<XtreamFields
								variant="edit"
								url={url}
								username={username}
								password={password}
								onUrlChange={setUrl}
								onUsernameChange={setUsername}
								onPasswordChange={setPassword}
							/>
						) : (
							<Field label="Playlist URL">
								{(id) => (
									<Input
										id={id}
										value={url}
										onChange={(e) => setUrl(e.target.value)}
										placeholder="https://example.com/playlist.m3u"
										className="h-8 text-sm"
									/>
								)}
							</Field>
						)}
					</div>
				</div>
 
				{/* Auto-refresh section */}
				{!isFile && (
					<div>
						<SectionLabel>Auto-refresh</SectionLabel>
						<div className="space-y-3">
							<div className="flex items-center justify-between">
								<div>
									<p className="text-sm font-medium">Auto-refresh</p>
									<p className="text-xs text-muted-foreground">
										Refresh on launch if overdue, then repeat on schedule
									</p>
								</div>
								<SegmentedControl
									aria-label="Auto-refresh"
									value={autoRefresh}
									onChange={setAutoRefresh}
									options={OFF_ON}
									className="ml-3 shrink-0"
								/>
							</div>
 
							{autoRefresh && (
								<Field label="Refresh interval">
									{(id) => (
										<IntervalSelect
											id={id}
											value={intervalHours}
											onChange={setIntervalHours}
											options={INTERVAL_OPTIONS}
										/>
									)}
								</Field>
							)}
						</div>
					</div>
				)}
 
				{/* Manual refresh section */}
				{!isFile && (
					<div>
						<SectionLabel>Manual refresh</SectionLabel>
						<div className="flex items-center justify-between">
							<p className="text-xs text-muted-foreground">
								{provider.lastUpdated
									? `Last updated ${new Date(provider.lastUpdated).toLocaleString()}`
									: "Never refreshed"}
							</p>
							<BusyButton
								busy={refresh.busy}
								busyLabel="Refreshing…"
								onClick={handleRefreshNow}
								disabled={save.busy}
								className="shrink-0 ml-3"
							>
								Refresh now
							</BusyButton>
						</div>
						{refresh.message && (
							<p className="text-xs text-destructive mt-1">{refresh.message}</p>
						)}
					</div>
				)}
 
				{/* EPG Schedule section */}
				{!isFile && (
					<div>
						<SectionLabel>EPG Schedule</SectionLabel>
						<div className="space-y-3">
							{/* EPG URL */}
							<Field label="EPG URL">
								{(id) => (
									<>
										<div className="flex gap-2">
											<Input
												id={id}
												value={epgUrl}
												onChange={(e) => setEpgUrlState(e.target.value)}
												placeholder="https://example.com/epg.xml"
												className="h-8 text-sm"
											/>
											<BusyButton
												busy={detect.busy}
												busyLabel="Detecting…"
												onClick={handleDetectEpgUrl}
												disabled={save.busy}
												className="shrink-0"
											>
												Auto-detect
											</BusyButton>
										</div>
										{detect.message && (
											<p className="text-xs text-muted-foreground mt-1">
												{detect.message}
											</p>
										)}
									</>
								)}
							</Field>
 
							{/* EPG Auto-refresh toggle */}
							<div className="flex items-center justify-between">
								<div>
									<p className="text-sm font-medium">Auto-refresh EPG</p>
									<p className="text-xs text-muted-foreground">
										Automatically refresh the EPG schedule on a schedule
									</p>
								</div>
								<SegmentedControl
									aria-label="Auto-refresh EPG"
									value={epgAutoRefresh}
									onChange={setEpgAutoRefresh}
									options={OFF_ON}
									className="ml-3 shrink-0"
								/>
							</div>
 
							{/* EPG Refresh interval */}
							{epgAutoRefresh && (
								<Field label="EPG refresh interval">
									{(id) => (
										<IntervalSelect
											id={id}
											value={epgIntervalHours}
											onChange={setEpgIntervalHours}
											options={EPG_INTERVAL_OPTIONS}
										/>
									)}
								</Field>
							)}
 
							{/* EPG Refresh now */}
							<div className="flex items-center justify-between">
								<p className="text-xs text-muted-foreground">
									Fetch latest EPG data for this provider
								</p>
								<BusyButton
									busy={epgRefresh.busy}
									busyLabel="Refreshing…"
									onClick={handleEpgRefreshNow}
									disabled={save.busy}
									className="shrink-0 ml-3"
								>
									Refresh EPG
								</BusyButton>
							</div>
							{epgRefresh.message && (
								<p className="text-xs text-destructive">{epgRefresh.message}</p>
							)}
							{epgRefreshEmpty && (
								<p className="text-xs text-muted-foreground">
									{EMPTY_GUIDE_MESSAGE}
								</p>
							)}
						</div>
					</div>
				)}
 
				{save.message && <p className="text-xs text-destructive">{save.message}</p>}
			</div>
 
			{/* Footer */}
			<div className="flex items-center justify-end gap-2 px-5 py-4 border-t border-border shrink-0">
				<Button variant="ghost" size="sm" onClick={onClose} disabled={save.busy}>
					Cancel
				</Button>
				<Button size="sm" onClick={handleSave} disabled={save.busy}>
					{save.busy ? "Saving…" : "Save"}
				</Button>
			</div>
		</Dialog>
	);
};