All files / src/components/playlist PlaylistManager.tsx

100% Statements 40/40
94.44% Branches 17/18
100% Functions 16/16
100% Lines 35/35

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                                  1x                 29x   29x 29x 29x     29x       29x 5x 5x 5x 5x 5x   5x 5x   3x 3x         3x   5x 5x 5x 5x           29x   29x                   36x 36x 36x   36x   36x                                         2x                     3x                             2x                                                             1x                       1x 1x   1x          
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { useState } from "react";
import { Trash, RefreshCw, Settings2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { ConfirmDialog } from "@/components/downloads/ConfirmDialog";
import { AddProvider } from "./AddProvider";
import { ProviderSettingsModal } from "./ProviderSettingsModal";
import { useChannels } from "@/hooks/useChannels";
import { displayUrl, isFileUrl } from "@/lib/file-url";
import type { Provider } from "@/lib/types";
import { formatCount } from "@/lib/format";
import { cn } from "@/lib/utils";
 
export const PlaylistManager = () => {
	const {
		providers,
		loadM3u,
		loadM3uFile,
		loadXtream,
		removeProvider,
		refreshProvider,
		updateProvider,
	} = useChannels();
 
	const [refreshingIds, setRefreshingIds] = useState<Set<string>>(new Set());
	const [settingsProvider, setSettingsProvider] = useState<Provider | null>(null);
	const [pendingRemoval, setPendingRemoval] = useState<Provider | null>(null);
	// Keeps the provider's update time from when the refresh failed, so the error
	// hides once a later refresh succeeds, including an automatic one.
	const [refreshErrors, setRefreshErrors] = useState<
		Record<string, { message: string; lastUpdated?: string }>
	>({});
 
	const refreshNow = async (id: string) => {
		setRefreshingIds((s) => new Set(s).add(id));
		setRefreshErrors((errors) => {
			const next = { ...errors };
			delete next[id];
			return next;
		});
		try {
			await refreshProvider(id);
		} catch (e) {
			const lastUpdated = providers.find((p) => p.id === id)?.lastUpdated;
			setRefreshErrors((errors) => ({
				...errors,
				[id]: { message: String(e), lastUpdated },
			}));
			// Rethrown so the settings modal can show the error next to its own button.
			throw e;
		} finally {
			setRefreshingIds((s) => {
				const next = new Set(s);
				next.delete(id);
				return next;
			});
		}
	};
 
	// The card already shows the error, so the click has nothing left to handle.
	const handleRefresh = (id: string) => refreshNow(id).catch(() => {});
 
	return (
		<div className="flex flex-col gap-6 p-4 max-w-2xl mx-auto h-full overflow-y-auto">
			<h1 className="text-2xl font-bold">Playlists</h1>
 
			<AddProvider onAddM3u={loadM3u} onAddM3uFile={loadM3uFile} onAddXtream={loadXtream} />
 
			{providers.length > 0 && (
				<div className="flex flex-col gap-3">
					<h2 className="text-lg font-semibold">Your Providers</h2>
					{providers.map((p) => {
						const isRefreshing = refreshingIds.has(p.id);
						const isFile = isFileUrl(p.url);
						const refreshError = refreshErrors[p.id];
						const showRefreshError =
							refreshError !== undefined &&
							refreshError.lastUpdated === p.lastUpdated;
						return (
							<Card key={p.id} role="group" aria-label={p.name}>
								<CardHeader className="py-4">
									<div className="flex items-center justify-between">
										<div className="flex items-center gap-2 min-w-0">
											<CardTitle className="text-base truncate">
												{p.name}
											</CardTitle>
											<Badge
												variant="secondary"
												className="text-[10px] shrink-0"
											>
												{p.type}
											</Badge>
										</div>
										<div className="flex items-center gap-1 shrink-0 ml-2">
											<Button
												variant="ghost"
												size="icon"
												className="h-8 w-8"
												title="Settings"
												onClick={() => setSettingsProvider(p)}
											>
												<Settings2 className="h-3.5 w-3.5" />
											</Button>
											{!isFile && (
												<Button
													variant="ghost"
													size="icon"
													className="h-8 w-8"
													title="Refresh"
													disabled={isRefreshing}
													onClick={() => handleRefresh(p.id)}
												>
													<RefreshCw
														className={cn(
															"h-3.5 w-3.5",
															isRefreshing && "animate-spin"
														)}
													/>
												</Button>
											)}
											<Button
												variant="ghost"
												size="icon"
												className="h-8 w-8 text-destructive hover:text-destructive"
												title="Remove"
												onClick={() => setPendingRemoval(p)}
											>
												<Trash className="h-3.5 w-3.5" />
											</Button>
										</div>
									</div>
								</CardHeader>
								<CardContent className="pt-0 pb-4">
									<p className="text-xs text-muted-foreground truncate">
										{displayUrl(p.url)}
									</p>
									<p className="text-xs text-muted-foreground mt-1">
										{formatCount(p.channelCount, "channel")}
										{p.lastUpdated &&
											` \u00b7 Updated ${new Date(p.lastUpdated).toLocaleString()}`}
									</p>
									{showRefreshError && (
										<p role="alert" className="text-xs text-destructive mt-1">
											{refreshError.message}
										</p>
									)}
								</CardContent>
							</Card>
						);
					})}
				</div>
			)}
 
			{settingsProvider && (
				<ProviderSettingsModal
					provider={settingsProvider}
					onClose={() => setSettingsProvider(null)}
					onRefreshNow={refreshNow}
					onUpdateProvider={updateProvider}
				/>
			)}
 
			<ConfirmDialog
				open={pendingRemoval !== null}
				title="Remove provider?"
				message={`Remove "${pendingRemoval?.name ?? ""}"? Its channels and favorites are removed too.`}
				confirmLabel="Remove"
				onConfirm={() => {
					Eif (pendingRemoval) removeProvider(pendingRemoval.id);
					setPendingRemoval(null);
				}}
				onCancel={() => setPendingRemoval(null)}
			/>
		</div>
	);
};