All files / src/components/playlist PlaylistManager.tsx

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

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                                                                                                                                                                                                                                                                                                                               
import { useState } from "react";
import { Trash2, 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 { AddProvider } from "./AddProvider";
import { ProviderSettingsModal } from "./ProviderSettingsModal";
import { useChannels } from "@/hooks/useChannels";
import type { Provider } from "@/lib/types";
import { cn } from "@/lib/utils";
 
const displayUrl = (url: string): string => {
	if (url.startsWith("file://")) {
		const path = url.slice(7);
		return path.split("/").pop() || path;
	}
	return url;
};
 
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 handleAddM3u = async (name: string, url: string) => {
		await loadM3u(name, url);
	};
 
	const handleAddM3uFile = async (name: string, path: string) => {
		await loadM3uFile(name, path);
	};
 
	const handleAddXtream = async (
		name: string,
		url: string,
		username: string,
		password: string
	) => {
		await loadXtream(name, url, username, password);
	};
 
	const handleRefresh = async (id: string) => {
		setRefreshingIds((s) => new Set(s).add(id));
		try {
			await refreshProvider(id);
		} catch {}
		setRefreshingIds((s) => {
			const next = new Set(s);
			next.delete(id);
			return next;
		});
	};
 
	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={handleAddM3u}
				onAddM3uFile={handleAddM3uFile}
				onAddXtream={handleAddXtream}
			/>
 
			{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 = p.url.startsWith("file://");
						return (
							<Card key={p.id}>
								<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={() => removeProvider(p.id)}
											>
												<Trash2 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">
										{p.channelCount.toLocaleString()} channels
										{p.lastUpdated &&
											` \u00b7 Updated ${new Date(p.lastUpdated).toLocaleString()}`}
									</p>
								</CardContent>
							</Card>
						);
					})}
				</div>
			)}
 
			{settingsProvider && (
				<ProviderSettingsModal
					provider={settingsProvider}
					onClose={() => setSettingsProvider(null)}
					onRefreshNow={handleRefresh}
					onUpdateProvider={updateProvider}
				/>
			)}
		</div>
	);
};