All files / src/components/settings Settings.tsx

0% Statements 0/637
0% Branches 0/1
0% Functions 0/1
0% Lines 0/637

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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { useState, useEffect, useRef } from "react";
import { openUrl } from "@/lib/openUrl";
import bmcQr from "@/assets/bmc-qr.png";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Slider } from "@/components/ui/slider";
import { usePlatform } from "@/hooks/usePlatform";
import type { UpdateState } from "@/hooks/useUpdateChecker";
import { getVersion } from "@tauri-apps/api/app";
import {
	Settings as SettingsIcon,
	Monitor,
	Smartphone,
	Tv,
	Eye,
	EyeOff,
	CheckCircle,
	XCircle,
	Download,
	RefreshCw,
} from "lucide-react";
import {
	getOmdbApiKey,
	setOmdbApiKey,
	fetchOmdbData,
	clearWatchHistory,
	getOpenSubtitlesApiKey,
	setOpenSubtitlesApiKey,
	testOpenSubtitlesApiKey,
	getGeminiApiKey,
	setGeminiApiKey,
	testGeminiApiKey,
	clearAllCaches,
} from "@/lib/tauri";
import { ask } from "@tauri-apps/plugin-dialog";
 
type OmdbStatus = "idle" | "valid" | "invalid";
type SaveStatus = "idle" | "saved" | "error";
type HistoryStatus = "idle" | "cleared";
 
const DonationReset = () => {
	const [reset, setReset] = useState(false);
	const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
	useEffect(
		() => () => {
			if (timerRef.current) clearTimeout(timerRef.current);
		},
		[]
	);
	const handleReset = () => {
		localStorage.removeItem("donation-last-shown");
		setReset(true);
		timerRef.current = setTimeout(() => setReset(false), 2000);
	};
	return (
		<Button size="sm" variant="secondary" onClick={handleReset} disabled={reset}>
			{reset ? (
				<span className="flex items-center gap-1 text-green-500">
					<CheckCircle className="h-4 w-4" /> Reset
				</span>
			) : (
				"Reset donation reminder"
			)}
		</Button>
	);
};
 
interface SettingsProps {
	updateState: UpdateState;
}
 
export const Settings = ({ updateState }: SettingsProps) => {
	const { platform, layoutMode } = usePlatform();
	const [appVersion, setAppVersion] = useState("");
	const [hwAccel, setHwAccel] = useState(true);
	const [defaultVolume, setDefaultVolume] = useState(100);
 
	// OMDB state
	const [omdbKey, setOmdbKey] = useState("");
	const [omdbKeyVisible, setOmdbKeyVisible] = useState(false);
	const [omdbStatus, setOmdbStatus] = useState<OmdbStatus>("idle");
	const [saveStatus, setSaveStatus] = useState<SaveStatus>("idle");
	const [saveError, setSaveError] = useState<string | null>(null);
	const [omdbTesting, setOmdbTesting] = useState(false);
	const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	// OpenSubtitles state
	const [openSubtitlesKey, setOpenSubtitlesKey] = useState("");
	const [openSubtitlesKeyVisible, setOpenSubtitlesKeyVisible] = useState(false);
	const [openSubtitlesSaveStatus, setOpenSubtitlesSaveStatus] = useState<SaveStatus>("idle");
	const [openSubtitlesTestStatus, setOpenSubtitlesTestStatus] = useState<OmdbStatus>("idle");
	const [openSubtitlesTesting, setOpenSubtitlesTesting] = useState(false);
	const [openSubtitlesSaveError, setOpenSubtitlesSaveError] = useState<string | null>(null);
	const openSubtitlesSaveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	// Gemini state
	const [geminiKey, setGeminiKey] = useState("");
	const [geminiKeyVisible, setGeminiKeyVisible] = useState(false);
	const [geminiSaveStatus, setGeminiSaveStatus] = useState<SaveStatus>("idle");
	const [geminiTestStatus, setGeminiTestStatus] = useState<OmdbStatus>("idle");
	const [geminiTesting, setGeminiTesting] = useState(false);
	const geminiSaveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	// History state
	const [historyStatus, setHistoryStatus] = useState<HistoryStatus>("idle");
	const [historyError, setHistoryError] = useState<string | null>(null);
	const historyTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	// Cache state
	const [cacheStatus, setCacheStatus] = useState<HistoryStatus>("idle");
	const [cacheError, setCacheError] = useState<string | null>(null);
	const cacheTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
 
	useEffect(() => {
		getVersion()
			.then(setAppVersion)
			.catch(() => {});
	}, []);
 
	useEffect(() => {
		getOmdbApiKey().then((key) => {
			if (key) setOmdbKey(key);
		});
		getOpenSubtitlesApiKey().then((key) => {
			if (key) setOpenSubtitlesKey(key);
		});
		getGeminiApiKey()
			.then((key) => {
				if (key) setGeminiKey(key);
			})
			.catch(() => {});
		return () => {
			if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
			if (historyTimerRef.current) clearTimeout(historyTimerRef.current);
			if (openSubtitlesSaveTimerRef.current) clearTimeout(openSubtitlesSaveTimerRef.current);
			if (geminiSaveTimerRef.current) clearTimeout(geminiSaveTimerRef.current);
			if (cacheTimerRef.current) clearTimeout(cacheTimerRef.current);
		};
	}, []);
 
	const platformIcon = {
		desktop: Monitor,
		mobile: Smartphone,
		tv: Tv,
	}[layoutMode];
	const PlatformIcon = platformIcon;
 
	const handleSaveOmdbKey = async () => {
		try {
			await setOmdbApiKey(omdbKey);
			setSaveStatus("saved");
			setSaveError(null);
			setOmdbStatus("idle");
			if (saveTimerRef.current) clearTimeout(saveTimerRef.current);
			saveTimerRef.current = setTimeout(() => setSaveStatus("idle"), 2000);
		} catch {
			setSaveError("Failed to save. Please try again.");
		}
	};
 
	const handleTestOmdbKey = async () => {
		setOmdbTesting(true);
		setOmdbStatus("idle");
		try {
			const result = await fetchOmdbData("test", "The Matrix", "movie");
			setOmdbStatus(result ? "valid" : "invalid");
		} catch {
			setOmdbStatus("invalid");
		} finally {
			setOmdbTesting(false);
		}
	};
 
	const handleSaveOpenSubtitlesKey = async () => {
		try {
			await setOpenSubtitlesApiKey(openSubtitlesKey);
			setOpenSubtitlesSaveStatus("saved");
			setOpenSubtitlesSaveError(null);
			setOpenSubtitlesTestStatus("idle");
			if (openSubtitlesSaveTimerRef.current) clearTimeout(openSubtitlesSaveTimerRef.current);
			openSubtitlesSaveTimerRef.current = setTimeout(
				() => setOpenSubtitlesSaveStatus("idle"),
				2000
			);
		} catch {
			setOpenSubtitlesSaveError("Failed to save. Please try again.");
		}
	};
 
	const handleTestOpenSubtitlesKey = async () => {
		setOpenSubtitlesTesting(true);
		setOpenSubtitlesTestStatus("idle");
		try {
			const valid = await testOpenSubtitlesApiKey(openSubtitlesKey);
			setOpenSubtitlesTestStatus(valid ? "valid" : "invalid");
		} catch {
			setOpenSubtitlesTestStatus("invalid");
		} finally {
			setOpenSubtitlesTesting(false);
		}
	};
 
	const handleGeminiSave = async () => {
		try {
			await setGeminiApiKey(geminiKey);
			setGeminiSaveStatus("saved");
			setGeminiTestStatus("idle");
			if (geminiSaveTimerRef.current) clearTimeout(geminiSaveTimerRef.current);
			geminiSaveTimerRef.current = setTimeout(() => setGeminiSaveStatus("idle"), 2000);
		} catch {
			setGeminiSaveStatus("error");
			if (geminiSaveTimerRef.current) clearTimeout(geminiSaveTimerRef.current);
			geminiSaveTimerRef.current = setTimeout(() => setGeminiSaveStatus("idle"), 3000);
		}
	};
 
	const handleGeminiTest = async () => {
		setGeminiTesting(true);
		setGeminiTestStatus("idle");
		try {
			const valid = await testGeminiApiKey(geminiKey);
			setGeminiTestStatus(valid ? "valid" : "invalid");
		} catch {
			setGeminiTestStatus("invalid");
		} finally {
			setGeminiTesting(false);
		}
	};
 
	const handleClearHistory = async () => {
		const confirmed = await ask(
			"Are you sure you want to clear all watch history? This cannot be undone.",
			{ title: "Clear History", kind: "warning" }
		);
		if (!confirmed) return;
		try {
			await clearWatchHistory();
			setHistoryStatus("cleared");
			setHistoryError(null);
			if (historyTimerRef.current) clearTimeout(historyTimerRef.current);
			historyTimerRef.current = setTimeout(() => setHistoryStatus("idle"), 2000);
		} catch {
			setHistoryError("Failed to clear history. Please try again.");
		}
	};
 
	const handleClearCaches = async () => {
		const confirmed = await ask(
			"Clear all cached data (OMDB, MDBList, OpenSubtitles, EPG)? This cannot be undone.",
			{ title: "Clear Caches", kind: "warning" }
		);
		if (!confirmed) return;
		try {
			await clearAllCaches();
			setCacheStatus("cleared");
			setCacheError(null);
			if (cacheTimerRef.current) clearTimeout(cacheTimerRef.current);
			cacheTimerRef.current = setTimeout(() => setCacheStatus("idle"), 2000);
		} catch {
			setCacheError("Failed to clear caches. Please try again.");
		}
	};
 
	return (
		<div className="h-full overflow-y-auto">
			<div className="flex flex-col gap-6 p-4 max-w-2xl mx-auto">
				<div className="flex items-center gap-3">
					<SettingsIcon className="h-6 w-6 text-primary" />
					<h1 className="text-2xl font-bold">Settings</h1>
				</div>
 
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Platform</CardTitle>
					</CardHeader>
					<CardContent>
						<div className="flex items-center gap-3">
							<PlatformIcon className="h-5 w-5 text-muted-foreground" />
							<div>
								<p className="text-sm font-medium capitalize">{platform}</p>
								<p className="text-xs text-muted-foreground">
									Layout: {layoutMode}
								</p>
							</div>
						</div>
					</CardContent>
				</Card>
 
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Playback</CardTitle>
					</CardHeader>
					<CardContent className="space-y-4">
						<div className="flex items-center justify-between">
							<div>
								<p className="text-sm font-medium">Hardware Acceleration</p>
								<p className="text-xs text-muted-foreground">
									Use GPU decoding when available
								</p>
							</div>
							<Button
								variant={hwAccel ? "default" : "secondary"}
								size="sm"
								onClick={() => setHwAccel(!hwAccel)}
							>
								{hwAccel ? "On" : "Off"}
							</Button>
						</div>
 
						<div>
							<div className="flex items-center justify-between mb-2">
								<p className="text-sm font-medium">Default Volume</p>
								<span className="text-sm text-muted-foreground">
									{defaultVolume}%
								</span>
							</div>
							<Slider
								value={defaultVolume}
								min={0}
								max={150}
								step={5}
								onValueChange={setDefaultVolume}
							/>
						</div>
					</CardContent>
				</Card>
 
				{/* AI section */}
				<Card>
					<CardHeader>
						<CardTitle className="text-base">AI</CardTitle>
					</CardHeader>
					<CardContent className="space-y-4">
						<div>
							<p className="text-sm font-medium mb-1">Gemini API Key</p>
							<p className="text-xs text-muted-foreground mb-2">
								Used for automatic channel categorization
							</p>
							<div className="flex items-center gap-2">
								<div className="relative flex-1">
									<Input
										type={geminiKeyVisible ? "text" : "password"}
										placeholder="Enter Gemini API key…"
										value={geminiKey}
										onChange={(e) => {
											setGeminiKey(e.target.value);
											setGeminiTestStatus("idle");
											setGeminiSaveStatus("idle");
										}}
										className="pr-10"
									/>
									<button
										type="button"
										className="absolute inset-y-0 right-2 flex items-center text-muted-foreground hover:text-foreground"
										onClick={() => setGeminiKeyVisible((v) => !v)}
										aria-label={geminiKeyVisible ? "Hide key" : "Show key"}
									>
										{geminiKeyVisible ? (
											<EyeOff className="h-4 w-4" />
										) : (
											<Eye className="h-4 w-4" />
										)}
									</button>
								</div>
								<Button
									size="sm"
									variant="secondary"
									onClick={handleGeminiSave}
									disabled={!geminiKey.trim()}
								>
									{geminiSaveStatus === "saved" ? (
										<span className="flex items-center gap-1 text-green-500">
											<CheckCircle className="h-4 w-4" /> Saved
										</span>
									) : geminiSaveStatus === "error" ? (
										<span className="text-destructive">Failed</span>
									) : (
										"Save"
									)}
								</Button>
								<Button
									size="sm"
									variant="outline"
									onClick={handleGeminiTest}
									disabled={!geminiKey.trim() || geminiTesting}
								>
									{geminiTesting ? "Testing…" : "Test"}
								</Button>
							</div>
							<div className="mt-2 text-xs">
								{geminiTestStatus === "valid" && (
									<span className="flex items-center gap-1 text-green-500">
										<CheckCircle className="h-3 w-3" /> Valid key
									</span>
								)}
								{geminiTestStatus === "invalid" && (
									<span className="flex items-center gap-1 text-destructive">
										<XCircle className="h-3 w-3" /> Invalid key
									</span>
								)}
								{geminiTestStatus === "idle" && !geminiKey && (
									<span className="text-muted-foreground">
										Get a key at{" "}
										<a
											href="https://aistudio.google.com/apikey"
											target="_blank"
											rel="noreferrer"
											className="underline hover:text-foreground"
										>
											aistudio.google.com
										</a>
									</span>
								)}
							</div>
						</div>
					</CardContent>
				</Card>
 
				{/* Integrations section */}
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Integrations</CardTitle>
					</CardHeader>
					<CardContent className="space-y-4">
						<div>
							<p className="text-sm font-medium mb-1">OMDB API</p>
							<div className="flex items-center gap-2">
								<div className="relative flex-1">
									<Input
										type={omdbKeyVisible ? "text" : "password"}
										placeholder="Enter API key…"
										value={omdbKey}
										onChange={(e) => {
											setOmdbKey(e.target.value);
											setOmdbStatus("idle");
											setSaveStatus("idle");
										}}
										className="pr-10"
									/>
									<button
										type="button"
										className="absolute inset-y-0 right-2 flex items-center text-muted-foreground hover:text-foreground"
										onClick={() => setOmdbKeyVisible((v) => !v)}
										aria-label={omdbKeyVisible ? "Hide key" : "Show key"}
									>
										{omdbKeyVisible ? (
											<EyeOff className="h-4 w-4" />
										) : (
											<Eye className="h-4 w-4" />
										)}
									</button>
								</div>
								<Button
									size="sm"
									variant="secondary"
									onClick={handleSaveOmdbKey}
									disabled={!omdbKey.trim()}
								>
									{saveStatus === "saved" ? (
										<span className="flex items-center gap-1 text-green-500">
											<CheckCircle className="h-4 w-4" /> Saved
										</span>
									) : (
										"Save"
									)}
								</Button>
								<Button
									size="sm"
									variant="outline"
									onClick={handleTestOmdbKey}
									disabled={!omdbKey.trim() || omdbTesting}
								>
									{omdbTesting ? "Testing…" : "Test"}
								</Button>
							</div>
 
							{/* Save error */}
							{saveError && (
								<p className="mt-1 text-xs text-destructive">{saveError}</p>
							)}
 
							{/* Status line */}
							<div className="mt-2 text-xs">
								{omdbStatus === "valid" && (
									<span className="flex items-center gap-1 text-green-500">
										<CheckCircle className="h-3 w-3" /> Valid key · 1000
										calls/day limit
									</span>
								)}
								{omdbStatus === "invalid" && (
									<span className="flex items-center gap-1 text-destructive">
										<XCircle className="h-3 w-3" /> Invalid key
									</span>
								)}
								{omdbStatus === "idle" && !omdbKey && (
									<span className="text-muted-foreground">
										No API key configured.{" "}
										<a
											href="https://www.omdbapi.com/apikey.aspx"
											target="_blank"
											rel="noreferrer"
											className="underline hover:text-foreground"
										>
											Get a free key at omdbapi.com
										</a>
									</span>
								)}
							</div>
						</div>
 
						{/* Optional enrichment services — only shown when OMDB key is set */}
						{omdbKey.trim() && (
							<div className="border-t border-border pt-4 space-y-4">
								<p className="text-xs text-muted-foreground font-medium uppercase tracking-wide">
									Optional enrichment (require OMDB key)
								</p>
 
								{/* OpenSubtitles section */}
								<div>
									<p className="text-sm font-medium mb-1">OpenSubtitles API</p>
									<div className="flex items-center gap-2">
										<div className="relative flex-1">
											<Input
												type={openSubtitlesKeyVisible ? "text" : "password"}
												placeholder="Enter API key…"
												value={openSubtitlesKey}
												onChange={(e) => {
													setOpenSubtitlesKey(e.target.value);
													setOpenSubtitlesTestStatus("idle");
													setOpenSubtitlesSaveStatus("idle");
												}}
												className="pr-10"
											/>
											<button
												type="button"
												className="absolute inset-y-0 right-2 flex items-center text-muted-foreground hover:text-foreground"
												onClick={() =>
													setOpenSubtitlesKeyVisible((v) => !v)
												}
												aria-label={
													openSubtitlesKeyVisible
														? "Hide key"
														: "Show key"
												}
											>
												{openSubtitlesKeyVisible ? (
													<EyeOff className="h-4 w-4" />
												) : (
													<Eye className="h-4 w-4" />
												)}
											</button>
										</div>
										<Button
											size="sm"
											variant="secondary"
											onClick={handleSaveOpenSubtitlesKey}
											disabled={!openSubtitlesKey.trim()}
										>
											{openSubtitlesSaveStatus === "saved" ? (
												<span className="flex items-center gap-1 text-green-500">
													<CheckCircle className="h-4 w-4" /> Saved
												</span>
											) : (
												"Save"
											)}
										</Button>
										<Button
											size="sm"
											variant="outline"
											onClick={handleTestOpenSubtitlesKey}
											disabled={
												!openSubtitlesKey.trim() || openSubtitlesTesting
											}
										>
											{openSubtitlesTesting ? "Testing…" : "Test"}
										</Button>
									</div>
 
									{openSubtitlesSaveError && (
										<p className="mt-1 text-xs text-destructive">
											{openSubtitlesSaveError}
										</p>
									)}
 
									<div className="mt-2 text-xs">
										{openSubtitlesTestStatus === "valid" && (
											<span className="flex items-center gap-1 text-green-500">
												<CheckCircle className="h-3 w-3" /> Valid key
											</span>
										)}
										{openSubtitlesTestStatus === "invalid" && (
											<span className="flex items-center gap-1 text-destructive">
												<XCircle className="h-3 w-3" /> Invalid key
											</span>
										)}
									</div>
 
									<div className="mt-1 text-xs text-muted-foreground">
										Get an API key at{" "}
										<a
											href="https://www.opensubtitles.com/consumers"
											target="_blank"
											rel="noreferrer"
											className="underline hover:text-foreground"
										>
											opensubtitles.com
										</a>
									</div>
								</div>
							</div>
						)}
					</CardContent>
				</Card>
 
				{/* History section */}
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Watch History</CardTitle>
					</CardHeader>
					<CardContent>
						<div className="space-y-2">
							<div className="flex items-center gap-4">
								<Button
									variant="destructive"
									size="sm"
									onClick={handleClearHistory}
								>
									Clear All History…
								</Button>
								{historyStatus === "cleared" && (
									<span className="flex items-center gap-1 text-xs text-green-500">
										<CheckCircle className="h-3 w-3" /> History cleared
									</span>
								)}
							</div>
							{historyError && (
								<p className="text-xs text-destructive">{historyError}</p>
							)}
						</div>
					</CardContent>
				</Card>
 
				{/* Cache section */}
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Cache</CardTitle>
					</CardHeader>
					<CardContent>
						<div className="space-y-2">
							<p className="text-xs text-muted-foreground">
								Clears all cached metadata (OMDB, MDBList, OpenSubtitles, WhatsonTV,
								EPG). Channel data and settings are not affected.
							</p>
							<div className="flex items-center gap-4">
								<Button variant="destructive" size="sm" onClick={handleClearCaches}>
									Clear All Caches…
								</Button>
								{cacheStatus === "cleared" && (
									<span className="flex items-center gap-1 text-xs text-green-500">
										<CheckCircle className="h-3 w-3" /> Caches cleared
									</span>
								)}
							</div>
							{cacheError && <p className="text-xs text-destructive">{cacheError}</p>}
						</div>
					</CardContent>
				</Card>
 
				<Card>
					<CardHeader>
						<CardTitle className="text-base">Support</CardTitle>
					</CardHeader>
					<CardContent className="space-y-3">
						<p className="text-sm text-muted-foreground">
							Max Video Player is free and open source. If you find it useful,
							consider supporting development.
						</p>
						<div className="flex items-center gap-4">
							<button
								type="button"
								onClick={() => openUrl("https://buymeacoffee.com/MaxMB15")}
								className="w-32 shrink-0 rounded-lg overflow-hidden border border-border hover:border-primary transition-colors"
								aria-label="Donate via Buy Me a Coffee"
							>
								<img
									src={bmcQr}
									alt="Buy me a coffee QR code"
									className="w-full h-auto"
								/>
							</button>
							<button
								type="button"
								onClick={() => openUrl("https://buymeacoffee.com/MaxMB15")}
								className="text-sm font-semibold bg-[#5F7FFF] text-white px-5 py-2 rounded-lg hover:opacity-90 transition-opacity"
							>
								Buy me a coffee
							</button>
						</div>
						<div className="pt-1 border-t border-border">
							<p className="text-xs text-muted-foreground mb-2">
								Reset the donation reminder to show it again.
							</p>
							<DonationReset />
						</div>
					</CardContent>
				</Card>
 
				<Card>
					<CardHeader>
						<CardTitle className="text-base">About</CardTitle>
					</CardHeader>
					<CardContent className="space-y-4">
						<p className="text-sm text-muted-foreground">
							Max Video Player {appVersion ? `v${appVersion}` : ""}
						</p>
 
						<div className="space-y-3">
							<div className="flex items-center gap-3">
								<Button
									size="sm"
									variant="secondary"
									onClick={() => updateState.checkForUpdates()}
									disabled={updateState.checking}
								>
									{updateState.checking ? (
										<span className="flex items-center gap-1.5">
											<RefreshCw className="h-3 w-3 animate-spin" />
											Checking…
										</span>
									) : (
										"Check for Updates"
									)}
								</Button>
								{!updateState.checking && !updateState.update && (
									<span className="text-xs text-muted-foreground">
										You're up to date.
									</span>
								)}
							</div>
 
							{updateState.update && (
								<div className="rounded-lg bg-primary/10 border border-primary/25 px-4 py-3 space-y-2">
									<p className="text-sm font-semibold text-primary">
										Update available — v{updateState.update.version}
									</p>
									<p className="text-xs text-muted-foreground leading-relaxed">
										{updateState.update.body ??
											"A new version is ready to install."}
									</p>
									{updateState.installing && updateState.progress !== null && (
										<div className="h-1 w-full rounded-full bg-secondary overflow-hidden">
											<div
												className="h-full bg-primary transition-all duration-200"
												style={{ width: `${updateState.progress}%` }}
											/>
										</div>
									)}
									{updateState.error && (
										<p className="text-xs text-destructive">
											{updateState.error}
										</p>
									)}
									<Button
										size="sm"
										onClick={updateState.install}
										disabled={updateState.installing}
									>
										{updateState.installing ? (
											<span className="flex items-center gap-1.5">
												<RefreshCw className="h-3 w-3 animate-spin" />
												{updateState.progress !== null
													? `Downloading… ${updateState.progress}%`
													: "Installing…"}
											</span>
										) : (
											<span className="flex items-center gap-1.5">
												<Download className="h-3 w-3" />
												Install Update
											</span>
										)}
									</Button>
								</div>
							)}
						</div>
					</CardContent>
				</Card>
			</div>
		</div>
	);
};