All files / src/components/playlist AddProvider.tsx

57.31% Statements 47/82
55.07% Branches 38/69
64.7% Functions 11/17
56.96% Lines 45/79

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                                                    1x                     1x 15x 15x 15x 15x 15x 15x 15x 15x 15x   15x 15x 15x     15x 15x     15x   3x 3x 3x 3x 3x 3x           15x                           15x 8x 8x                     8x 8x       15x 3x 3x                 3x           15x 1x 1x     15x                                                     15x   15x                                 2x                   15x                                               7x                                                                                             2x 2x 2x                                                                                                                                      
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import { useState, useEffect, useCallback } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Field } from "@/components/ui/field";
import { SegmentedControl, type SegmentedOption } from "@/components/ui/segmented-control";
import { LoaderCircle, Upload, FileText, Link, X } from "lucide-react";
import { usePlatform } from "@/hooks/usePlatform";
import {
	filenameFromPath,
	isPlaylistFile,
	nameFromFilename,
	PLAYLIST_EXTENSIONS,
} from "@/lib/file-url";
import { isMobilePlatform } from "@/lib/platform";
import { cn } from "@/lib/utils";
import { open } from "@tauri-apps/plugin-dialog";
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
import { ContentDisclaimer } from "./ContentDisclaimer";
import { XtreamFields } from "./XtreamFields";
 
type M3uMode = "url" | "file";
 
const M3U_MODES: SegmentedOption<M3uMode>[] = [
	{ value: "url", label: "URL", icon: <Link className="h-3.5 w-3.5" /> },
	{ value: "file", label: "File", icon: <FileText className="h-3.5 w-3.5" /> },
];
 
interface AddProviderProps {
	onAddM3u: (name: string, url: string) => Promise<void>;
	onAddM3uFile: (name: string, path: string) => Promise<void>;
	onAddXtream: (name: string, url: string, username: string, password: string) => Promise<void>;
}
 
export const AddProvider = ({ onAddM3u, onAddM3uFile, onAddXtream }: AddProviderProps) => {
	const { layoutMode } = usePlatform();
	const [tab, setTab] = useState<"m3u" | "xtream">("m3u");
	const [m3uMode, setM3uMode] = useState<M3uMode>("url");
	const [name, setName] = useState("");
	const [url, setUrl] = useState("");
	const [username, setUsername] = useState("");
	const [password, setPassword] = useState("");
	const [loading, setLoading] = useState(false);
	const [error, setError] = useState<string | null>(null);
 
	const [filePath, setFilePath] = useState<string | null>(null);
	const [fileName, setFileName] = useState<string | null>(null);
	const [isDragOver, setIsDragOver] = useState(false);
 
	// An iPad in landscape gets the desktop layout but can't drop files.
	const showDragDrop = layoutMode === "desktop" && !isMobilePlatform();
	const showFileBrowse = layoutMode !== "tv";
 
	// A picked or dropped file names the provider unless the user typed a name.
	const selectFile = useCallback(
		(path: string) => {
			const fname = filenameFromPath(path);
			setFilePath(path);
			setFileName(fname);
			setError(null);
			Eif (!name) {
				setName(nameFromFilename(fname));
			}
		},
		[name]
	);
 
	const acceptFileDrop = useCallback(
		(paths: string[]) => {
			const m3uPath = paths.find(isPlaylistFile);
			if (!m3uPath) {
				setError("Please drop an .m3u or .m3u8 file");
				return;
			}
			selectFile(m3uPath);
			setM3uMode("file");
			setTab("m3u");
		},
		[selectFile]
	);
 
	useEffect(() => {
		const appWindow = getCurrentWebviewWindow();
		const unlisten = appWindow.onDragDropEvent((event) => {
			if (event.payload.type === "enter" || event.payload.type === "over") {
				setIsDragOver(true);
			} else if (event.payload.type === "leave") {
				setIsDragOver(false);
			} else if (event.payload.type === "drop") {
				setIsDragOver(false);
				acceptFileDrop(event.payload.paths);
			}
		});
 
		return () => {
			unlisten.then((fn) => fn());
		};
	}, [acceptFileDrop]);
 
	const handleBrowse = useCallback(async () => {
		try {
			const selected = await open({
				multiple: false,
				filters: [
					{
						name: "M3U Playlist",
						extensions: PLAYLIST_EXTENSIONS,
					},
				],
			});
			Eif (selected) selectFile(selected);
		} catch (e) {
			setError(String(e));
		}
	}, [selectFile]);
 
	const clearFile = useCallback(() => {
		setFilePath(null);
		setFileName(null);
	}, []);
 
	const handleSubmit = async (e: React.SubmitEvent) => {
		e.preventDefault();
		setLoading(true);
		setError(null);
		try {
			if (tab === "m3u") {
				if (m3uMode === "file" && filePath) {
					await onAddM3uFile(name || fileName || "M3U File", filePath);
					clearFile();
				} else {
					await onAddM3u(name || "M3U Playlist", url);
				}
			} else {
				await onAddXtream(name || "Xtream Provider", url, username, password);
			}
			setName("");
			setUrl("");
			setUsername("");
			setPassword("");
		} catch (e) {
			setError(String(e));
		} finally {
			setLoading(false);
		}
	};
 
	const canSubmit =
		!loading && (tab === "xtream" ? !!url : m3uMode === "file" ? !!filePath : !!url);
 
	return (
		<Card>
			<CardHeader>
				<CardTitle className="text-lg">Add Provider</CardTitle>
				<div role="group" aria-label="Provider type" className="flex gap-2">
					<Button
						variant={tab === "m3u" ? "default" : "secondary"}
						size="sm"
						aria-pressed={tab === "m3u"}
						onClick={() => setTab("m3u")}
					>
						M3U Playlist
					</Button>
					<Button
						variant={tab === "xtream" ? "default" : "secondary"}
						size="sm"
						aria-pressed={tab === "xtream"}
						onClick={() => setTab("xtream")}
					>
						Xtream Codes
					</Button>
				</div>
			</CardHeader>
			<CardContent>
				<form onSubmit={handleSubmit} className="flex flex-col gap-3">
					<Field label="Provider name" hideLabel>
						{(id) => (
							<Input
								id={id}
								placeholder="Provider name (optional)"
								value={name}
								onChange={(e) => setName(e.target.value)}
							/>
						)}
					</Field>
 
					{tab === "m3u" && (
						<>
							{showFileBrowse && (
								<SegmentedControl
									aria-label="Playlist source"
									value={m3uMode}
									onChange={setM3uMode}
									options={M3U_MODES}
									fill
								/>
							)}
 
							{m3uMode === "url" && (
								<Field label="Playlist URL" hideLabel>
									{(id) => (
										<Input
											id={id}
											placeholder="M3U playlist URL"
											value={url}
											onChange={(e) => setUrl(e.target.value)}
											required
										/>
									)}
								</Field>
							)}
 
							{m3uMode === "file" && (
								<>
									{filePath ? (
										<div className="flex items-center gap-2 p-3 rounded-lg border border-primary/30 bg-primary/5">
											<FileText className="h-5 w-5 text-primary shrink-0" />
											<div className="flex-1 min-w-0">
												<p className="text-sm font-medium truncate">
													{fileName}
												</p>
												<p className="text-xs text-muted-foreground truncate">
													{filePath}
												</p>
											</div>
											<Button
												type="button"
												variant="ghost"
												size="icon"
												className="h-7 w-7 shrink-0"
												onClick={clearFile}
												aria-label="Clear selected file"
											>
												<X className="h-3.5 w-3.5" />
											</Button>
										</div>
									) : (
										<div
											role="button"
											tabIndex={0}
											className={cn(
												"flex flex-col items-center justify-center gap-2 p-6 rounded-lg border-2 border-dashed transition-colors cursor-pointer ring-offset-background focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
												isDragOver
													? "border-primary bg-primary/10"
													: "border-border hover:border-muted-foreground/50"
											)}
											onClick={handleBrowse}
											onKeyDown={(e) => {
												Iif (e.key !== "Enter" && e.key !== " ") return;
												e.preventDefault();
												handleBrowse();
											}}
										>
											<Upload
												className={cn(
													"h-8 w-8",
													isDragOver
														? "text-primary"
														: "text-muted-foreground"
												)}
											/>
											{showDragDrop ? (
												<p className="text-sm text-muted-foreground text-center">
													Drop an .m3u file here or{" "}
													<span className="text-primary font-medium">
														browse
													</span>
												</p>
											) : (
												<p className="text-sm text-muted-foreground text-center">
													Tap to select an .m3u file
												</p>
											)}
											<p className="text-xs text-muted-foreground">
												Supports .m3u and .m3u8
											</p>
										</div>
									)}
								</>
							)}
						</>
					)}
 
					{tab === "xtream" && (
						<XtreamFields
							variant="add"
							url={url}
							username={username}
							password={password}
							onUrlChange={setUrl}
							onUsernameChange={setUsername}
							onPasswordChange={setPassword}
						/>
					)}
 
					{error && (
						<p role="alert" className="text-sm text-destructive">
							{error}
						</p>
					)}
 
					<Button type="submit" disabled={!canSubmit}>
						{loading && <LoaderCircle className="h-4 w-4 mr-2 animate-spin" />}
						{tab === "m3u"
							? m3uMode === "file"
								? "Import Playlist"
								: "Load Playlist"
							: "Connect"}
					</Button>
				</form>
				<div className="mt-4">
					<ContentDisclaimer />
				</div>
			</CardContent>
		</Card>
	);
};