refactor(frontend): extract logic to hooks, split inline components
Area 1 (tv/page.tsx 964→423 lines): - hooks: use-fullscreen, use-idle, use-volume, use-quality, use-subtitles, use-channel-input, use-channel-passwords, use-tv-keyboard - components: SubtitlePicker, VolumeControl, QualityPicker, TopControlBar, LogoWatermark, AutoplayPrompt, ChannelNumberOverlay, TvBaseLayer Area 2 (edit-channel-sheet.tsx 1244→678 lines): - hooks: use-channel-form (all form state + reset logic) - lib/schemas.ts: extracted Zod schemas + extractErrors - components: AlgorithmicFilterEditor, RecyclePolicyEditor, WebhookEditor, AccessSettingsEditor, LogoEditor Area 3 (dashboard/page.tsx 406→261 lines): - hooks: use-channel-order, use-import-channel, use-regenerate-all - lib/channel-export.ts: pure export utility - components: DashboardHeader
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import type { AccessMode } from "@/lib/types";
|
||||
|
||||
interface AccessSettingsEditorProps {
|
||||
accessMode: AccessMode;
|
||||
accessPassword: string;
|
||||
onAccessModeChange: (mode: AccessMode) => void;
|
||||
onAccessPasswordChange: (pw: string) => void;
|
||||
label?: string;
|
||||
passwordLabel?: string;
|
||||
passwordHint?: string;
|
||||
}
|
||||
|
||||
export function AccessSettingsEditor({
|
||||
accessMode,
|
||||
accessPassword,
|
||||
onAccessModeChange,
|
||||
onAccessPasswordChange,
|
||||
label = "Access",
|
||||
passwordLabel = "Password",
|
||||
passwordHint = "Leave blank to keep existing password",
|
||||
}: AccessSettingsEditorProps) {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">{label}</label>
|
||||
<select
|
||||
value={accessMode}
|
||||
onChange={(e) => {
|
||||
onAccessModeChange(e.target.value as AccessMode);
|
||||
onAccessPasswordChange("");
|
||||
}}
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 focus:border-zinc-500 focus:outline-none"
|
||||
>
|
||||
<option value="public">Public</option>
|
||||
<option value="password_protected">Password protected</option>
|
||||
<option value="account_required">Account required</option>
|
||||
<option value="owner_only">Owner only</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{accessMode === "password_protected" && (
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">
|
||||
{passwordLabel}
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
placeholder={passwordHint}
|
||||
value={accessPassword}
|
||||
onChange={(e) => onAccessPasswordChange(e.target.value)}
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,342 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { TagInput } from "./tag-input";
|
||||
import { SeriesPicker } from "./series-picker";
|
||||
import { FilterPreview } from "./filter-preview";
|
||||
import { useCollections, useSeries, useGenres } from "@/hooks/use-library";
|
||||
import type {
|
||||
BlockContent,
|
||||
ContentType,
|
||||
FillStrategy,
|
||||
MediaFilter,
|
||||
ProviderInfo,
|
||||
} from "@/lib/types";
|
||||
import type { FieldErrors } from "@/lib/schemas";
|
||||
|
||||
function Field({
|
||||
label,
|
||||
hint,
|
||||
error,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
hint?: string;
|
||||
error?: string;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">{label}</label>
|
||||
{children}
|
||||
{error ? (
|
||||
<p className="text-[11px] text-red-400">{error}</p>
|
||||
) : hint ? (
|
||||
<p className="text-[11px] text-zinc-600">{hint}</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function NativeSelect({
|
||||
value,
|
||||
onChange,
|
||||
children,
|
||||
}: {
|
||||
value: string;
|
||||
onChange: (v: string) => void;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<select
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 focus:border-zinc-500 focus:outline-none"
|
||||
>
|
||||
{children}
|
||||
</select>
|
||||
);
|
||||
}
|
||||
|
||||
function NumberInput({
|
||||
value,
|
||||
onChange,
|
||||
min,
|
||||
placeholder,
|
||||
error,
|
||||
}: {
|
||||
value: number | "";
|
||||
onChange: (v: number | "") => void;
|
||||
min?: number;
|
||||
placeholder?: string;
|
||||
error?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<input
|
||||
type="number"
|
||||
min={min}
|
||||
value={value}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) =>
|
||||
onChange(e.target.value === "" ? "" : Number(e.target.value))
|
||||
}
|
||||
className={`w-full rounded-md border bg-zinc-800 px-3 py-2 text-sm text-zinc-100 placeholder:text-zinc-600 focus:outline-none ${error ? "border-red-500 focus:border-red-400" : "border-zinc-700 focus:border-zinc-500"}`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface AlgorithmicFilterEditorProps {
|
||||
content: Extract<BlockContent, { type: "algorithmic" }>;
|
||||
pfx: string;
|
||||
errors: FieldErrors;
|
||||
providers: ProviderInfo[];
|
||||
setFilter: (patch: Partial<MediaFilter>) => void;
|
||||
setStrategy: (strategy: FillStrategy) => void;
|
||||
setProviderId: (id: string) => void;
|
||||
}
|
||||
|
||||
export function AlgorithmicFilterEditor({
|
||||
content,
|
||||
pfx,
|
||||
errors,
|
||||
providers,
|
||||
setFilter,
|
||||
setStrategy,
|
||||
setProviderId,
|
||||
}: AlgorithmicFilterEditorProps) {
|
||||
const [showGenres, setShowGenres] = useState(false);
|
||||
|
||||
const providerId = content.provider_id ?? "";
|
||||
const capabilities =
|
||||
providers.find((p) => p.id === providerId)?.capabilities ??
|
||||
providers[0]?.capabilities;
|
||||
|
||||
const { data: collections, isLoading: loadingCollections } = useCollections(
|
||||
providerId || undefined,
|
||||
);
|
||||
const { data: series, isLoading: loadingSeries } = useSeries(undefined, {
|
||||
enabled: capabilities?.series !== false,
|
||||
provider: providerId || undefined,
|
||||
});
|
||||
const { data: genreOptions } = useGenres(
|
||||
content.filter.content_type ?? undefined,
|
||||
{
|
||||
enabled: capabilities?.genres !== false,
|
||||
provider: providerId || undefined,
|
||||
},
|
||||
);
|
||||
|
||||
const isEpisode = content.filter.content_type === "episode";
|
||||
const collectionLabel =
|
||||
capabilities?.collections && !capabilities?.series && !capabilities?.genres
|
||||
? "Directory"
|
||||
: "Library";
|
||||
|
||||
return (
|
||||
<div className="space-y-3 rounded-md border border-zinc-700/50 bg-zinc-800 p-3">
|
||||
<p className="text-[11px] font-medium uppercase tracking-wider text-zinc-500">
|
||||
Filter
|
||||
</p>
|
||||
|
||||
{providers.length > 1 && (
|
||||
<Field label="Provider">
|
||||
<NativeSelect value={providerId} onChange={setProviderId}>
|
||||
{providers.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.id}
|
||||
</option>
|
||||
))}
|
||||
</NativeSelect>
|
||||
</Field>
|
||||
)}
|
||||
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<Field label="Media type">
|
||||
<NativeSelect
|
||||
value={content.filter.content_type ?? ""}
|
||||
onChange={(v) =>
|
||||
setFilter({
|
||||
content_type: v === "" ? null : (v as ContentType),
|
||||
series_names:
|
||||
v !== "episode" ? [] : content.filter.series_names,
|
||||
})
|
||||
}
|
||||
>
|
||||
<option value="">Any</option>
|
||||
<option value="movie">Movie</option>
|
||||
<option value="episode">Episode</option>
|
||||
<option value="short">Short</option>
|
||||
</NativeSelect>
|
||||
</Field>
|
||||
<Field label="Strategy">
|
||||
<NativeSelect
|
||||
value={content.strategy}
|
||||
onChange={(v) => setStrategy(v as FillStrategy)}
|
||||
>
|
||||
<option value="random">Random</option>
|
||||
<option value="best_fit">Best fit</option>
|
||||
<option value="sequential">Sequential</option>
|
||||
</NativeSelect>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
{isEpisode && capabilities?.series !== false && (
|
||||
<Field
|
||||
label="Series"
|
||||
hint={
|
||||
content.strategy === "sequential"
|
||||
? "Episodes will play in chronological order"
|
||||
: "Filter to specific shows, or leave empty for all"
|
||||
}
|
||||
>
|
||||
<SeriesPicker
|
||||
values={content.filter.series_names ?? []}
|
||||
onChange={(v) => setFilter({ series_names: v })}
|
||||
series={series ?? []}
|
||||
isLoading={loadingSeries}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
|
||||
<Field
|
||||
label={collectionLabel}
|
||||
hint={
|
||||
loadingCollections
|
||||
? `Loading ${collectionLabel.toLowerCase()}s…`
|
||||
: collections
|
||||
? `Scope this block to one ${collectionLabel.toLowerCase()}`
|
||||
: `Enter a provider ${collectionLabel.toLowerCase()} ID`
|
||||
}
|
||||
>
|
||||
{collections && collections.length > 0 ? (
|
||||
<NativeSelect
|
||||
value={content.filter.collections[0] ?? ""}
|
||||
onChange={(v) => setFilter({ collections: v ? [v] : [] })}
|
||||
>
|
||||
<option value="">All libraries</option>
|
||||
{collections.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.name}
|
||||
{c.collection_type ? ` (${c.collection_type})` : ""}
|
||||
</option>
|
||||
))}
|
||||
</NativeSelect>
|
||||
) : (
|
||||
<TagInput
|
||||
values={content.filter.collections}
|
||||
onChange={(v) => setFilter({ collections: v })}
|
||||
placeholder="Library ID…"
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
|
||||
{capabilities?.genres !== false && (
|
||||
<Field label="Genres" hint="Press Enter or comma to add">
|
||||
<TagInput
|
||||
values={content.filter.genres}
|
||||
onChange={(v) => setFilter({ genres: v })}
|
||||
placeholder="Comedy, Animation…"
|
||||
/>
|
||||
{genreOptions && genreOptions.length > 0 && (
|
||||
<div className="mt-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowGenres((s) => !s)}
|
||||
className="text-[11px] text-zinc-600 hover:text-zinc-400"
|
||||
>
|
||||
{showGenres ? "Hide" : "Browse"} available genres
|
||||
</button>
|
||||
{showGenres && (
|
||||
<div className="mt-1.5 flex flex-wrap gap-1">
|
||||
{genreOptions
|
||||
.filter((g) => !content.filter.genres.includes(g))
|
||||
.map((g) => (
|
||||
<button
|
||||
key={g}
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setFilter({ genres: [...content.filter.genres, g] })
|
||||
}
|
||||
className="rounded px-1.5 py-0.5 text-[11px] bg-zinc-700/50 text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200"
|
||||
>
|
||||
+ {g}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
)}
|
||||
|
||||
<Field label="Tags" hint="Press Enter or comma to add">
|
||||
<TagInput
|
||||
values={content.filter.tags}
|
||||
onChange={(v) => setFilter({ tags: v })}
|
||||
placeholder="classic, family…"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className="grid grid-cols-3 gap-3">
|
||||
<Field
|
||||
label="Decade"
|
||||
hint="e.g. 1990"
|
||||
error={errors[`${pfx}.content.filter.decade`]}
|
||||
>
|
||||
<NumberInput
|
||||
value={content.filter.decade ?? ""}
|
||||
onChange={(v) =>
|
||||
setFilter({ decade: v === "" ? null : (v as number) })
|
||||
}
|
||||
placeholder="1990"
|
||||
error={!!errors[`${pfx}.content.filter.decade`]}
|
||||
/>
|
||||
</Field>
|
||||
<Field
|
||||
label="Min duration (min)"
|
||||
error={errors[`${pfx}.content.filter.min_duration_secs`]}
|
||||
>
|
||||
<NumberInput
|
||||
value={
|
||||
content.filter.min_duration_secs != null
|
||||
? Math.round(content.filter.min_duration_secs / 60)
|
||||
: ""
|
||||
}
|
||||
onChange={(v) =>
|
||||
setFilter({
|
||||
min_duration_secs: v === "" ? null : (v as number) * 60,
|
||||
})
|
||||
}
|
||||
placeholder="30"
|
||||
error={!!errors[`${pfx}.content.filter.min_duration_secs`]}
|
||||
/>
|
||||
</Field>
|
||||
<Field
|
||||
label="Max duration (min)"
|
||||
error={errors[`${pfx}.content.filter.max_duration_secs`]}
|
||||
>
|
||||
<NumberInput
|
||||
value={
|
||||
content.filter.max_duration_secs != null
|
||||
? Math.round(content.filter.max_duration_secs / 60)
|
||||
: ""
|
||||
}
|
||||
onChange={(v) =>
|
||||
setFilter({
|
||||
max_duration_secs: v === "" ? null : (v as number) * 60,
|
||||
})
|
||||
}
|
||||
placeholder="120"
|
||||
error={!!errors[`${pfx}.content.filter.max_duration_secs`]}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<FilterPreview
|
||||
filter={content.filter}
|
||||
strategy={content.strategy}
|
||||
provider={providerId || undefined}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { Plus, Upload, RefreshCw, Antenna, Settings2 } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { ProviderCapabilities } from "@/lib/types";
|
||||
|
||||
interface DashboardHeaderProps {
|
||||
hasChannels: boolean;
|
||||
canTranscode: boolean;
|
||||
canRescan: boolean;
|
||||
isRegeneratingAll: boolean;
|
||||
isRescanPending: boolean;
|
||||
capabilities: ProviderCapabilities | undefined;
|
||||
onTranscodeOpen: () => void;
|
||||
onRescan: () => void;
|
||||
onRegenerateAll: () => void;
|
||||
onIptvOpen: () => void;
|
||||
onImportOpen: () => void;
|
||||
onCreateOpen: () => void;
|
||||
}
|
||||
|
||||
export function DashboardHeader({
|
||||
hasChannels,
|
||||
canTranscode,
|
||||
canRescan,
|
||||
isRegeneratingAll,
|
||||
isRescanPending,
|
||||
onTranscodeOpen,
|
||||
onRescan,
|
||||
onRegenerateAll,
|
||||
onIptvOpen,
|
||||
onImportOpen,
|
||||
onCreateOpen,
|
||||
}: DashboardHeaderProps) {
|
||||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-semibold text-zinc-100">My Channels</h1>
|
||||
<p className="mt-0.5 text-sm text-zinc-500">Build your broadcast lineup</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{canTranscode && (
|
||||
<Button onClick={onTranscodeOpen} title="Transcode settings">
|
||||
<Settings2 className="size-4" />
|
||||
Transcode
|
||||
</Button>
|
||||
)}
|
||||
{canRescan && (
|
||||
<Button
|
||||
onClick={onRescan}
|
||||
disabled={isRescanPending}
|
||||
title="Rescan local files directory"
|
||||
>
|
||||
<RefreshCw
|
||||
className={`size-4 ${isRescanPending ? "animate-spin" : ""}`}
|
||||
/>
|
||||
Rescan library
|
||||
</Button>
|
||||
)}
|
||||
{hasChannels && (
|
||||
<Button
|
||||
onClick={onRegenerateAll}
|
||||
disabled={isRegeneratingAll}
|
||||
title="Regenerate schedules for all channels"
|
||||
>
|
||||
<RefreshCw
|
||||
className={`size-4 ${isRegeneratingAll ? "animate-spin" : ""}`}
|
||||
/>
|
||||
Regenerate all
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={onIptvOpen}>
|
||||
<Antenna className="size-4" />
|
||||
IPTV
|
||||
</Button>
|
||||
<Button onClick={onImportOpen}>
|
||||
<Upload className="size-4" />
|
||||
Import
|
||||
</Button>
|
||||
<Button onClick={onCreateOpen}>
|
||||
<Plus className="size-4" />
|
||||
New channel
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
142
k-tv-frontend/app/(main)/dashboard/components/logo-editor.tsx
Normal file
142
k-tv-frontend/app/(main)/dashboard/components/logo-editor.tsx
Normal file
@@ -0,0 +1,142 @@
|
||||
import { useRef } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import type { LogoPosition } from "@/lib/types";
|
||||
|
||||
const LOGO_POSITIONS: { value: LogoPosition; label: string }[] = [
|
||||
{ value: "top_right", label: "Top right" },
|
||||
{ value: "top_left", label: "Top left" },
|
||||
{ value: "bottom_right", label: "Bottom right" },
|
||||
{ value: "bottom_left", label: "Bottom left" },
|
||||
];
|
||||
|
||||
interface LogoEditorProps {
|
||||
logo: string | null;
|
||||
logoPosition: LogoPosition;
|
||||
logoOpacity: number;
|
||||
onLogoChange: (logo: string | null) => void;
|
||||
onPositionChange: (position: LogoPosition) => void;
|
||||
onOpacityChange: (opacity: number) => void;
|
||||
}
|
||||
|
||||
export function LogoEditor({
|
||||
logo,
|
||||
logoPosition,
|
||||
logoOpacity,
|
||||
onLogoChange,
|
||||
onPositionChange,
|
||||
onOpacityChange,
|
||||
}: LogoEditorProps) {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const handleFile = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
const reader = new FileReader();
|
||||
reader.onload = (ev) => {
|
||||
const result = ev.target?.result;
|
||||
if (typeof result === "string") onLogoChange(result);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
e.target.value = "";
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-3 rounded-md border border-zinc-700/50 bg-zinc-800/50 p-3">
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="xs"
|
||||
className="border-zinc-700 text-zinc-300 hover:text-zinc-100"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
>
|
||||
Upload image
|
||||
</Button>
|
||||
{logo && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
size="xs"
|
||||
className="text-zinc-500 hover:text-red-400"
|
||||
onClick={() => onLogoChange(null)}
|
||||
>
|
||||
Clear
|
||||
</Button>
|
||||
)}
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="hidden"
|
||||
onChange={handleFile}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">
|
||||
URL or SVG
|
||||
</label>
|
||||
<textarea
|
||||
rows={2}
|
||||
value={logo ?? ""}
|
||||
onChange={(e) => onLogoChange(e.target.value || null)}
|
||||
placeholder="https://example.com/logo.png or <svg>…</svg>"
|
||||
className="w-full resize-none rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-xs text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">
|
||||
Position
|
||||
</label>
|
||||
<select
|
||||
value={logoPosition}
|
||||
onChange={(e) => onPositionChange(e.target.value as LogoPosition)}
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 focus:border-zinc-500 focus:outline-none"
|
||||
>
|
||||
{LOGO_POSITIONS.map((p) => (
|
||||
<option key={p.value} value={p.value}>
|
||||
{p.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">
|
||||
Opacity ({logoOpacity}%)
|
||||
</label>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={100}
|
||||
value={logoOpacity}
|
||||
onChange={(e) => onOpacityChange(Number(e.target.value))}
|
||||
className="w-full accent-zinc-400"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{logo && (
|
||||
<div className="flex items-center justify-center rounded-md border border-zinc-700/50 bg-zinc-900 p-2">
|
||||
{logo.trimStart().startsWith("<") ? (
|
||||
<div
|
||||
dangerouslySetInnerHTML={{ __html: logo }}
|
||||
className="h-10 w-auto"
|
||||
style={{ opacity: logoOpacity / 100 }}
|
||||
/>
|
||||
) : (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img
|
||||
src={logo}
|
||||
alt="Logo preview"
|
||||
className="h-10 w-auto object-contain"
|
||||
style={{ opacity: logoOpacity / 100 }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
import type { RecyclePolicy } from "@/lib/types";
|
||||
import type { FieldErrors } from "@/lib/schemas";
|
||||
|
||||
function NumberInput({
|
||||
value,
|
||||
onChange,
|
||||
min,
|
||||
max,
|
||||
step,
|
||||
placeholder,
|
||||
error,
|
||||
}: {
|
||||
value: number | "";
|
||||
onChange: (v: number | "") => void;
|
||||
min?: number;
|
||||
max?: number;
|
||||
step?: number | "any";
|
||||
placeholder?: string;
|
||||
error?: boolean;
|
||||
}) {
|
||||
return (
|
||||
<input
|
||||
type="number"
|
||||
min={min}
|
||||
max={max}
|
||||
step={step}
|
||||
value={value}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) =>
|
||||
onChange(e.target.value === "" ? "" : Number(e.target.value))
|
||||
}
|
||||
className={`w-full rounded-md border bg-zinc-800 px-3 py-2 text-sm text-zinc-100 placeholder:text-zinc-600 focus:outline-none ${error ? "border-red-500 focus:border-red-400" : "border-zinc-700 focus:border-zinc-500"}`}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function Field({
|
||||
label,
|
||||
hint,
|
||||
error,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
hint?: string;
|
||||
error?: string;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">{label}</label>
|
||||
{children}
|
||||
{error ? (
|
||||
<p className="text-[11px] text-red-400">{error}</p>
|
||||
) : hint ? (
|
||||
<p className="text-[11px] text-zinc-600">{hint}</p>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface RecyclePolicyEditorProps {
|
||||
policy: RecyclePolicy;
|
||||
errors: FieldErrors;
|
||||
onChange: (policy: RecyclePolicy) => void;
|
||||
}
|
||||
|
||||
export function RecyclePolicyEditor({
|
||||
policy,
|
||||
errors,
|
||||
onChange,
|
||||
}: RecyclePolicyEditorProps) {
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<Field label="Cooldown (days)" hint="Don't replay within N days">
|
||||
<NumberInput
|
||||
value={policy.cooldown_days ?? ""}
|
||||
onChange={(v) =>
|
||||
onChange({ ...policy, cooldown_days: v === "" ? null : (v as number) })
|
||||
}
|
||||
min={0}
|
||||
placeholder="7"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Cooldown (generations)" hint="Don't replay within N schedules">
|
||||
<NumberInput
|
||||
value={policy.cooldown_generations ?? ""}
|
||||
onChange={(v) =>
|
||||
onChange({ ...policy, cooldown_generations: v === "" ? null : (v as number) })
|
||||
}
|
||||
min={0}
|
||||
placeholder="3"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
<Field
|
||||
label="Min available ratio"
|
||||
hint="0.0–1.0 · Fraction of the pool kept selectable even if cooldown is active"
|
||||
error={errors["recycle_policy.min_available_ratio"]}
|
||||
>
|
||||
<NumberInput
|
||||
value={policy.min_available_ratio}
|
||||
onChange={(v) =>
|
||||
onChange({ ...policy, min_available_ratio: v === "" ? 0.1 : (v as number) })
|
||||
}
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.01}
|
||||
placeholder="0.1"
|
||||
error={!!errors["recycle_policy.min_available_ratio"]}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
141
k-tv-frontend/app/(main)/dashboard/components/webhook-editor.tsx
Normal file
141
k-tv-frontend/app/(main)/dashboard/components/webhook-editor.tsx
Normal file
@@ -0,0 +1,141 @@
|
||||
import type { WebhookFormat } from "@/hooks/use-channel-form";
|
||||
import { WEBHOOK_PRESETS } from "@/hooks/use-channel-form";
|
||||
|
||||
function Field({
|
||||
label,
|
||||
hint,
|
||||
children,
|
||||
}: {
|
||||
label: string;
|
||||
hint?: string;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
<label className="block text-xs font-medium text-zinc-400">{label}</label>
|
||||
{children}
|
||||
{hint && <p className="text-[11px] text-zinc-600">{hint}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface WebhookEditorProps {
|
||||
webhookUrl: string;
|
||||
webhookPollInterval: number | "";
|
||||
webhookFormat: WebhookFormat;
|
||||
webhookBodyTemplate: string;
|
||||
webhookHeaders: string;
|
||||
onWebhookUrlChange: (v: string) => void;
|
||||
onWebhookPollIntervalChange: (v: number | "") => void;
|
||||
onWebhookFormatChange: (fmt: WebhookFormat) => void;
|
||||
onWebhookBodyTemplateChange: (v: string) => void;
|
||||
onWebhookHeadersChange: (v: string) => void;
|
||||
}
|
||||
|
||||
export function WebhookEditor({
|
||||
webhookUrl,
|
||||
webhookPollInterval,
|
||||
webhookFormat,
|
||||
webhookBodyTemplate,
|
||||
webhookHeaders,
|
||||
onWebhookUrlChange,
|
||||
onWebhookPollIntervalChange,
|
||||
onWebhookFormatChange,
|
||||
onWebhookBodyTemplateChange,
|
||||
onWebhookHeadersChange,
|
||||
}: WebhookEditorProps) {
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
<Field label="Webhook URL" hint="POST events to this URL on broadcast changes">
|
||||
<input
|
||||
value={webhookUrl}
|
||||
onChange={(e) => onWebhookUrlChange(e.target.value)}
|
||||
placeholder="https://example.com/webhook"
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
{webhookUrl && (
|
||||
<>
|
||||
<Field
|
||||
label="Poll interval (seconds)"
|
||||
hint="How often to check for broadcast changes"
|
||||
>
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
value={webhookPollInterval}
|
||||
placeholder="5"
|
||||
onChange={(e) =>
|
||||
onWebhookPollIntervalChange(
|
||||
e.target.value === "" ? "" : Number(e.target.value),
|
||||
)
|
||||
}
|
||||
className="w-full rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 text-sm text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className="space-y-2">
|
||||
<p className="text-xs text-zinc-400">Payload format</p>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{(["default", "discord", "slack", "custom"] as WebhookFormat[]).map(
|
||||
(fmt) => (
|
||||
<button
|
||||
key={fmt}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
onWebhookFormatChange(fmt);
|
||||
if (fmt === "discord")
|
||||
onWebhookBodyTemplateChange(WEBHOOK_PRESETS.discord);
|
||||
else if (fmt === "slack")
|
||||
onWebhookBodyTemplateChange(WEBHOOK_PRESETS.slack);
|
||||
else if (fmt === "default") onWebhookBodyTemplateChange("");
|
||||
}}
|
||||
className={`rounded px-2.5 py-1 text-xs font-medium capitalize transition-colors ${
|
||||
webhookFormat === fmt
|
||||
? "bg-zinc-600 text-zinc-100"
|
||||
: "bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-zinc-200"
|
||||
}`}
|
||||
>
|
||||
{fmt === "default" ? "K-TV default" : fmt}
|
||||
</button>
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{webhookFormat !== "default" && (
|
||||
<Field
|
||||
label="Body template (Handlebars)"
|
||||
hint="Context: event, timestamp, channel_id, data.item.title, data.item.duration_secs, …"
|
||||
>
|
||||
<textarea
|
||||
rows={6}
|
||||
value={webhookBodyTemplate}
|
||||
onChange={(e) => {
|
||||
onWebhookBodyTemplateChange(e.target.value);
|
||||
onWebhookFormatChange("custom");
|
||||
}}
|
||||
placeholder={'{\n "text": "Now playing: {{data.item.title}}"\n}'}
|
||||
className="w-full resize-y rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 font-mono text-xs text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
|
||||
<Field
|
||||
label="Extra headers (JSON)"
|
||||
hint={'e.g. {"Authorization": "Bearer token"}'}
|
||||
>
|
||||
<textarea
|
||||
rows={2}
|
||||
value={webhookHeaders}
|
||||
onChange={(e) => onWebhookHeadersChange(e.target.value)}
|
||||
placeholder={'{"Authorization": "Bearer xxx"}'}
|
||||
className="w-full resize-none rounded-md border border-zinc-700 bg-zinc-800 px-3 py-2 font-mono text-xs text-zinc-100 placeholder:text-zinc-600 focus:border-zinc-500 focus:outline-none"
|
||||
/>
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { Plus, Upload, RefreshCw, Antenna, Settings2 } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
useChannels,
|
||||
useCreateChannel,
|
||||
@@ -13,9 +12,11 @@ import {
|
||||
import { useAuthContext } from "@/context/auth-context";
|
||||
import { useConfig } from "@/hooks/use-config";
|
||||
import { useRescanLibrary } from "@/hooks/use-library";
|
||||
import { api } from "@/lib/api";
|
||||
import { toast } from "sonner";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { useChannelOrder } from "@/hooks/use-channel-order";
|
||||
import { useImportChannel } from "@/hooks/use-import-channel";
|
||||
import { useRegenerateAllSchedules } from "@/hooks/use-regenerate-all";
|
||||
import { exportChannel } from "@/lib/channel-export";
|
||||
import { DashboardHeader } from "./components/dashboard-header";
|
||||
import { ChannelCard } from "./components/channel-card";
|
||||
import { CreateChannelDialog } from "./components/create-channel-dialog";
|
||||
import { DeleteChannelDialog } from "./components/delete-channel-dialog";
|
||||
@@ -35,7 +36,6 @@ import type {
|
||||
|
||||
export default function DashboardPage() {
|
||||
const { token } = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
const { data: channels, isLoading, error } = useChannels();
|
||||
const { data: config } = useConfig();
|
||||
const capabilities = config?.provider_capabilities;
|
||||
@@ -46,84 +46,18 @@ export default function DashboardPage() {
|
||||
const generateSchedule = useGenerateSchedule();
|
||||
const rescanLibrary = useRescanLibrary();
|
||||
|
||||
// Channel ordering — persisted to localStorage
|
||||
const [channelOrder, setChannelOrder] = useState<string[]>([]);
|
||||
useEffect(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem("k-tv-channel-order");
|
||||
if (stored) setChannelOrder(JSON.parse(stored));
|
||||
} catch {}
|
||||
}, []);
|
||||
|
||||
const saveOrder = (order: string[]) => {
|
||||
setChannelOrder(order);
|
||||
try {
|
||||
localStorage.setItem("k-tv-channel-order", JSON.stringify(order));
|
||||
} catch {}
|
||||
};
|
||||
|
||||
// Sort channels by stored order; new channels appear at the end
|
||||
const sortedChannels = channels
|
||||
? [...channels].sort((a, b) => {
|
||||
const ai = channelOrder.indexOf(a.id);
|
||||
const bi = channelOrder.indexOf(b.id);
|
||||
if (ai === -1 && bi === -1) return 0;
|
||||
if (ai === -1) return 1;
|
||||
if (bi === -1) return -1;
|
||||
return ai - bi;
|
||||
})
|
||||
: [];
|
||||
|
||||
const handleMoveUp = (channelId: string) => {
|
||||
const ids = sortedChannels.map((c) => c.id);
|
||||
const idx = ids.indexOf(channelId);
|
||||
if (idx <= 0) return;
|
||||
const next = [...ids];
|
||||
[next[idx - 1], next[idx]] = [next[idx], next[idx - 1]];
|
||||
saveOrder(next);
|
||||
};
|
||||
|
||||
const handleMoveDown = (channelId: string) => {
|
||||
const ids = sortedChannels.map((c) => c.id);
|
||||
const idx = ids.indexOf(channelId);
|
||||
if (idx === -1 || idx >= ids.length - 1) return;
|
||||
const next = [...ids];
|
||||
[next[idx], next[idx + 1]] = [next[idx + 1], next[idx]];
|
||||
saveOrder(next);
|
||||
};
|
||||
|
||||
// Regenerate all channels
|
||||
const [isRegeneratingAll, setIsRegeneratingAll] = useState(false);
|
||||
const handleRegenerateAll = async () => {
|
||||
if (!token || !channels || channels.length === 0) return;
|
||||
setIsRegeneratingAll(true);
|
||||
let failed = 0;
|
||||
for (const ch of channels) {
|
||||
try {
|
||||
await api.schedule.generate(ch.id, token);
|
||||
queryClient.invalidateQueries({ queryKey: ["schedule", ch.id] });
|
||||
} catch {
|
||||
failed++;
|
||||
}
|
||||
}
|
||||
setIsRegeneratingAll(false);
|
||||
if (failed === 0)
|
||||
toast.success(`All ${channels.length} schedules regenerated`);
|
||||
else toast.error(`${failed} schedule(s) failed to generate`);
|
||||
};
|
||||
const { sortedChannels, handleMoveUp, handleMoveDown } = useChannelOrder(channels);
|
||||
const { isRegeneratingAll, handleRegenerateAll } = useRegenerateAllSchedules(channels, token);
|
||||
const importChannel = useImportChannel(token);
|
||||
|
||||
// Dialog state
|
||||
const [iptvOpen, setIptvOpen] = useState(false);
|
||||
const [transcodeOpen, setTranscodeOpen] = useState(false);
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
const [importOpen, setImportOpen] = useState(false);
|
||||
const [importPending, setImportPending] = useState(false);
|
||||
const [importError, setImportError] = useState<string | null>(null);
|
||||
const [editChannel, setEditChannel] = useState<ChannelResponse | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<ChannelResponse | null>(
|
||||
null,
|
||||
);
|
||||
const [scheduleChannel, setScheduleChannel] =
|
||||
useState<ChannelResponse | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<ChannelResponse | null>(null);
|
||||
const [scheduleChannel, setScheduleChannel] = useState<ChannelResponse | null>(null);
|
||||
|
||||
const handleCreate = (data: {
|
||||
name: string;
|
||||
@@ -171,52 +105,8 @@ export default function DashboardPage() {
|
||||
};
|
||||
|
||||
const handleImport = async (data: ChannelImportData) => {
|
||||
if (!token) return;
|
||||
setImportPending(true);
|
||||
setImportError(null);
|
||||
try {
|
||||
const created = await api.channels.create(
|
||||
{
|
||||
name: data.name,
|
||||
timezone: data.timezone,
|
||||
description: data.description,
|
||||
},
|
||||
token,
|
||||
);
|
||||
await api.channels.update(
|
||||
created.id,
|
||||
{
|
||||
schedule_config: { blocks: data.blocks },
|
||||
recycle_policy: data.recycle_policy,
|
||||
},
|
||||
token,
|
||||
);
|
||||
await queryClient.invalidateQueries({ queryKey: ["channels"] });
|
||||
setImportOpen(false);
|
||||
} catch (e) {
|
||||
setImportError(e instanceof Error ? e.message : "Import failed");
|
||||
} finally {
|
||||
setImportPending(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleExport = (channel: ChannelResponse) => {
|
||||
const payload = {
|
||||
name: channel.name,
|
||||
description: channel.description ?? undefined,
|
||||
timezone: channel.timezone,
|
||||
blocks: channel.schedule_config.blocks,
|
||||
recycle_policy: channel.recycle_policy,
|
||||
};
|
||||
const blob = new Blob([JSON.stringify(payload, null, 2)], {
|
||||
type: "application/json",
|
||||
});
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${channel.name.toLowerCase().replace(/\s+/g, "-")}.json`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
const ok = await importChannel.handleImport(data);
|
||||
if (ok) setImportOpen(false);
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
@@ -226,69 +116,32 @@ export default function DashboardPage() {
|
||||
});
|
||||
};
|
||||
|
||||
const canTranscode = !!config?.providers?.some((p) => p.capabilities.transcode);
|
||||
const canRescan = !!capabilities?.rescan;
|
||||
|
||||
return (
|
||||
<div className="mx-auto w-full max-w-5xl space-y-6 px-6 py-8">
|
||||
{/* Header */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-semibold text-zinc-100">My Channels</h1>
|
||||
<p className="mt-0.5 text-sm text-zinc-500">
|
||||
Build your broadcast lineup
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{config?.providers?.some((p) => p.capabilities.transcode) && (
|
||||
<Button
|
||||
onClick={() => setTranscodeOpen(true)}
|
||||
title="Transcode settings"
|
||||
>
|
||||
<Settings2 className="size-4" />
|
||||
Transcode
|
||||
</Button>
|
||||
)}
|
||||
{capabilities?.rescan && (
|
||||
<Button
|
||||
onClick={() =>
|
||||
rescanLibrary.mutate(undefined, {
|
||||
onSuccess: (d) => toast.success(`Rescan complete: ${d.items_found} files found`),
|
||||
onError: () => toast.error("Rescan failed"),
|
||||
})
|
||||
}
|
||||
disabled={rescanLibrary.isPending}
|
||||
title="Rescan local files directory"
|
||||
>
|
||||
<RefreshCw className={`size-4 ${rescanLibrary.isPending ? "animate-spin" : ""}`} />
|
||||
Rescan library
|
||||
</Button>
|
||||
)}
|
||||
{channels && channels.length > 0 && (
|
||||
<Button
|
||||
onClick={handleRegenerateAll}
|
||||
disabled={isRegeneratingAll}
|
||||
title="Regenerate schedules for all channels"
|
||||
>
|
||||
<RefreshCw
|
||||
className={`size-4 ${isRegeneratingAll ? "animate-spin" : ""}`}
|
||||
/>
|
||||
Regenerate all
|
||||
</Button>
|
||||
)}
|
||||
<Button onClick={() => setIptvOpen(true)}>
|
||||
<Antenna className="size-4" />
|
||||
IPTV
|
||||
</Button>
|
||||
<Button onClick={() => setImportOpen(true)}>
|
||||
<Upload className="size-4" />
|
||||
Import
|
||||
</Button>
|
||||
<Button onClick={() => setCreateOpen(true)}>
|
||||
<Plus className="size-4" />
|
||||
New channel
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<DashboardHeader
|
||||
hasChannels={!!channels && channels.length > 0}
|
||||
canTranscode={canTranscode}
|
||||
canRescan={canRescan}
|
||||
isRegeneratingAll={isRegeneratingAll}
|
||||
isRescanPending={rescanLibrary.isPending}
|
||||
capabilities={capabilities}
|
||||
onTranscodeOpen={() => setTranscodeOpen(true)}
|
||||
onRescan={() =>
|
||||
rescanLibrary.mutate(undefined, {
|
||||
onSuccess: (d) =>
|
||||
toast.success(`Rescan complete: ${d.items_found} files found`),
|
||||
onError: () => toast.error("Rescan failed"),
|
||||
})
|
||||
}
|
||||
onRegenerateAll={handleRegenerateAll}
|
||||
onIptvOpen={() => setIptvOpen(true)}
|
||||
onImportOpen={() => setImportOpen(true)}
|
||||
onCreateOpen={() => setCreateOpen(true)}
|
||||
/>
|
||||
|
||||
{/* Content */}
|
||||
{isLoading && (
|
||||
<div className="flex items-center justify-center py-20">
|
||||
<div className="h-5 w-5 animate-spin rounded-full border-2 border-zinc-700 border-t-zinc-300" />
|
||||
@@ -304,10 +157,12 @@ export default function DashboardPage() {
|
||||
{!isLoading && channels && channels.length === 0 && (
|
||||
<div className="flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed border-zinc-800 py-20 text-center">
|
||||
<p className="text-sm text-zinc-500">No channels yet</p>
|
||||
<Button onClick={() => setCreateOpen(true)}>
|
||||
<Plus className="size-4" />
|
||||
<button
|
||||
onClick={() => setCreateOpen(true)}
|
||||
className="inline-flex items-center gap-1.5 rounded-md border border-zinc-700 bg-zinc-800 px-3 py-1.5 text-sm text-zinc-300 transition-colors hover:bg-zinc-700 hover:text-zinc-100"
|
||||
>
|
||||
Create your first channel
|
||||
</Button>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -327,7 +182,7 @@ export default function DashboardPage() {
|
||||
onDelete={() => setDeleteTarget(channel)}
|
||||
onGenerateSchedule={() => generateSchedule.mutate(channel.id)}
|
||||
onViewSchedule={() => setScheduleChannel(channel)}
|
||||
onExport={() => handleExport(channel)}
|
||||
onExport={() => exportChannel(channel)}
|
||||
onMoveUp={() => handleMoveUp(channel.id)}
|
||||
onMoveDown={() => handleMoveDown(channel.id)}
|
||||
/>
|
||||
@@ -354,12 +209,12 @@ export default function DashboardPage() {
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
setImportOpen(false);
|
||||
setImportError(null);
|
||||
importChannel.clearError();
|
||||
}
|
||||
}}
|
||||
onSubmit={handleImport}
|
||||
isPending={importPending}
|
||||
error={importError}
|
||||
isPending={importChannel.isPending}
|
||||
error={importChannel.error}
|
||||
/>
|
||||
|
||||
<CreateChannelDialog
|
||||
|
||||
11
k-tv-frontend/app/(main)/tv/components/autoplay-prompt.tsx
Normal file
11
k-tv-frontend/app/(main)/tv/components/autoplay-prompt.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
export function AutoplayPrompt() {
|
||||
return (
|
||||
<div className="pointer-events-none absolute inset-0 z-20 flex items-center justify-center">
|
||||
<div className="rounded-xl bg-black/70 px-8 py-5 text-center backdrop-blur-sm">
|
||||
<p className="text-sm font-medium text-zinc-200">
|
||||
Click or move the mouse to play
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
interface ChannelNumberOverlayProps {
|
||||
channelInput: string;
|
||||
}
|
||||
|
||||
export function ChannelNumberOverlay({ channelInput }: ChannelNumberOverlayProps) {
|
||||
if (!channelInput) return null;
|
||||
return (
|
||||
<div className="pointer-events-none absolute left-1/2 top-1/2 z-30 -translate-x-1/2 -translate-y-1/2 rounded-xl bg-black/80 px-8 py-5 text-center backdrop-blur">
|
||||
<p className="mb-1 text-[10px] uppercase tracking-widest text-zinc-500">
|
||||
Channel
|
||||
</p>
|
||||
<p className="font-mono text-5xl font-bold text-white">{channelInput}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -18,3 +18,12 @@ export { NoSignal } from "./no-signal";
|
||||
export type { NoSignalProps, NoSignalVariant } from "./no-signal";
|
||||
|
||||
export { ChannelPasswordModal } from "./channel-password-modal";
|
||||
|
||||
export { SubtitlePicker } from "./subtitle-picker";
|
||||
export { VolumeControl } from "./volume-control";
|
||||
export { QualityPicker } from "./quality-picker";
|
||||
export { TopControlBar } from "./top-control-bar";
|
||||
export { LogoWatermark } from "./logo-watermark";
|
||||
export { AutoplayPrompt } from "./autoplay-prompt";
|
||||
export { ChannelNumberOverlay } from "./channel-number-overlay";
|
||||
export { TvBaseLayer } from "./tv-base-layer";
|
||||
|
||||
39
k-tv-frontend/app/(main)/tv/components/logo-watermark.tsx
Normal file
39
k-tv-frontend/app/(main)/tv/components/logo-watermark.tsx
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { LogoPosition } from "@/lib/types";
|
||||
|
||||
function logoPositionClass(pos?: LogoPosition) {
|
||||
switch (pos) {
|
||||
case "top_left":
|
||||
return "top-0 left-0";
|
||||
case "bottom_left":
|
||||
return "bottom-0 left-0";
|
||||
case "bottom_right":
|
||||
return "bottom-0 right-0";
|
||||
default:
|
||||
return "top-0 right-0";
|
||||
}
|
||||
}
|
||||
|
||||
interface LogoWatermarkProps {
|
||||
logo: string;
|
||||
position?: LogoPosition;
|
||||
opacity?: number;
|
||||
}
|
||||
|
||||
export function LogoWatermark({ logo, position, opacity = 1 }: LogoWatermarkProps) {
|
||||
return (
|
||||
<div
|
||||
className={`pointer-events-none absolute z-10 p-3 ${logoPositionClass(position)}`}
|
||||
style={{ opacity }}
|
||||
>
|
||||
{logo.trimStart().startsWith("<") ? (
|
||||
<div
|
||||
dangerouslySetInnerHTML={{ __html: logo }}
|
||||
className="h-12 w-auto"
|
||||
/>
|
||||
) : (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img src={logo} alt="" className="h-12 w-auto object-contain" />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
40
k-tv-frontend/app/(main)/tv/components/quality-picker.tsx
Normal file
40
k-tv-frontend/app/(main)/tv/components/quality-picker.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
interface QualityPickerProps {
|
||||
quality: string;
|
||||
options: { value: string; label: string }[];
|
||||
showPicker: boolean;
|
||||
onToggle: () => void;
|
||||
onChangeQuality: (q: string) => void;
|
||||
}
|
||||
|
||||
export function QualityPicker({
|
||||
quality,
|
||||
options,
|
||||
showPicker,
|
||||
onToggle,
|
||||
onChangeQuality,
|
||||
}: QualityPickerProps) {
|
||||
return (
|
||||
<div className="pointer-events-auto relative">
|
||||
<button
|
||||
className="rounded-md bg-black/50 px-3 py-1.5 text-xs text-zinc-400 backdrop-blur transition-colors hover:bg-black/70 hover:text-white"
|
||||
onClick={onToggle}
|
||||
title="Stream quality"
|
||||
>
|
||||
{options.find((o) => o.value === quality)?.label ?? quality}
|
||||
</button>
|
||||
{showPicker && (
|
||||
<div className="absolute right-0 top-9 z-30 min-w-[8rem] overflow-hidden rounded-md border border-zinc-700 bg-zinc-900/95 py-1 shadow-xl backdrop-blur">
|
||||
{options.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
className={`w-full px-3 py-1.5 text-left text-xs transition-colors hover:bg-zinc-700 ${quality === opt.value ? "text-white" : "text-zinc-400"}`}
|
||||
onClick={() => onChangeQuality(opt.value)}
|
||||
>
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
53
k-tv-frontend/app/(main)/tv/components/subtitle-picker.tsx
Normal file
53
k-tv-frontend/app/(main)/tv/components/subtitle-picker.tsx
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { SubtitleTrack } from "./video-player";
|
||||
|
||||
interface SubtitlePickerProps {
|
||||
tracks: SubtitleTrack[];
|
||||
activeTrack: number;
|
||||
show: boolean;
|
||||
onToggle: () => void;
|
||||
onChangeTrack: (id: number) => void;
|
||||
}
|
||||
|
||||
export function SubtitlePicker({
|
||||
tracks,
|
||||
activeTrack,
|
||||
show,
|
||||
onToggle,
|
||||
onChangeTrack,
|
||||
}: SubtitlePickerProps) {
|
||||
if (tracks.length === 0) return null;
|
||||
return (
|
||||
<div className="pointer-events-auto relative">
|
||||
<button
|
||||
className="rounded-md bg-black/50 px-3 py-1.5 text-xs backdrop-blur transition-colors hover:bg-black/70 hover:text-white"
|
||||
style={{
|
||||
color: activeTrack !== -1 ? "white" : undefined,
|
||||
borderBottom:
|
||||
activeTrack !== -1 ? "2px solid white" : "2px solid transparent",
|
||||
}}
|
||||
onClick={onToggle}
|
||||
>
|
||||
CC
|
||||
</button>
|
||||
{show && (
|
||||
<div className="absolute right-0 top-9 z-30 min-w-[10rem] overflow-hidden rounded-md border border-zinc-700 bg-zinc-900/95 py-1 shadow-xl backdrop-blur">
|
||||
<button
|
||||
className={`w-full px-3 py-1.5 text-left text-xs transition-colors hover:bg-zinc-700 ${activeTrack === -1 ? "text-white" : "text-zinc-400"}`}
|
||||
onClick={() => onChangeTrack(-1)}
|
||||
>
|
||||
Off
|
||||
</button>
|
||||
{tracks.map((track) => (
|
||||
<button
|
||||
key={track.id}
|
||||
className={`w-full px-3 py-1.5 text-left text-xs transition-colors hover:bg-zinc-700 ${activeTrack === track.id ? "text-white" : "text-zinc-400"}`}
|
||||
onClick={() => onChangeTrack(track.id)}
|
||||
>
|
||||
{track.name || track.lang || `Track ${track.id + 1}`}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
156
k-tv-frontend/app/(main)/tv/components/top-control-bar.tsx
Normal file
156
k-tv-frontend/app/(main)/tv/components/top-control-bar.tsx
Normal file
@@ -0,0 +1,156 @@
|
||||
import { Cast, Info, Maximize2, Minimize2 } from "lucide-react";
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
import { SubtitlePicker } from "./subtitle-picker";
|
||||
import { VolumeControl } from "./volume-control";
|
||||
import { QualityPicker } from "./quality-picker";
|
||||
import type { SubtitleTrack } from "./video-player";
|
||||
|
||||
interface TopControlBarProps {
|
||||
// Subtitles
|
||||
subtitleTracks: SubtitleTrack[];
|
||||
activeSubtitleTrack: number;
|
||||
showSubtitlePicker: boolean;
|
||||
onToggleSubtitlePicker: () => void;
|
||||
onChangeSubtitleTrack: (id: number) => void;
|
||||
// Volume
|
||||
volume: number;
|
||||
isMuted: boolean;
|
||||
VolumeIcon: LucideIcon;
|
||||
showVolumeSlider: boolean;
|
||||
onToggleVolumeSlider: () => void;
|
||||
onToggleMute: () => void;
|
||||
onVolumeChange: (v: number) => void;
|
||||
// Fullscreen
|
||||
isFullscreen: boolean;
|
||||
onToggleFullscreen: () => void;
|
||||
// Cast
|
||||
castAvailable: boolean;
|
||||
isCasting: boolean;
|
||||
castDeviceName?: string | null;
|
||||
streamUrl?: string | null;
|
||||
onRequestCast: (url: string) => void;
|
||||
onStopCasting: () => void;
|
||||
// Quality
|
||||
quality: string;
|
||||
qualityOptions: { value: string; label: string }[];
|
||||
showQualityPicker: boolean;
|
||||
onToggleQualityPicker: () => void;
|
||||
onChangeQuality: (q: string) => void;
|
||||
// Stats & guide
|
||||
showStats: boolean;
|
||||
onToggleStats: () => void;
|
||||
showSchedule: boolean;
|
||||
onToggleSchedule: () => void;
|
||||
}
|
||||
|
||||
export function TopControlBar({
|
||||
subtitleTracks,
|
||||
activeSubtitleTrack,
|
||||
showSubtitlePicker,
|
||||
onToggleSubtitlePicker,
|
||||
onChangeSubtitleTrack,
|
||||
volume,
|
||||
isMuted,
|
||||
VolumeIcon,
|
||||
showVolumeSlider,
|
||||
onToggleVolumeSlider,
|
||||
onToggleMute,
|
||||
onVolumeChange,
|
||||
isFullscreen,
|
||||
onToggleFullscreen,
|
||||
castAvailable,
|
||||
isCasting,
|
||||
castDeviceName,
|
||||
streamUrl,
|
||||
onRequestCast,
|
||||
onStopCasting,
|
||||
quality,
|
||||
qualityOptions,
|
||||
showQualityPicker,
|
||||
onToggleQualityPicker,
|
||||
onChangeQuality,
|
||||
showStats,
|
||||
onToggleStats,
|
||||
showSchedule,
|
||||
onToggleSchedule,
|
||||
}: TopControlBarProps) {
|
||||
return (
|
||||
<div className="flex justify-end gap-2 p-4">
|
||||
<SubtitlePicker
|
||||
tracks={subtitleTracks}
|
||||
activeTrack={activeSubtitleTrack}
|
||||
show={showSubtitlePicker}
|
||||
onToggle={onToggleSubtitlePicker}
|
||||
onChangeTrack={(id) => {
|
||||
onChangeSubtitleTrack(id);
|
||||
if (id !== activeSubtitleTrack) onToggleSubtitlePicker();
|
||||
}}
|
||||
/>
|
||||
|
||||
<VolumeControl
|
||||
volume={volume}
|
||||
isMuted={isMuted}
|
||||
VolumeIcon={VolumeIcon}
|
||||
showSlider={showVolumeSlider}
|
||||
onToggleSlider={onToggleVolumeSlider}
|
||||
onToggleMute={onToggleMute}
|
||||
onVolumeChange={(v) => {
|
||||
onVolumeChange(v);
|
||||
}}
|
||||
/>
|
||||
|
||||
<button
|
||||
className="pointer-events-auto rounded-md bg-black/50 p-1.5 text-zinc-400 backdrop-blur transition-colors hover:bg-black/70 hover:text-white"
|
||||
onClick={onToggleFullscreen}
|
||||
title={isFullscreen ? "Exit fullscreen [F]" : "Fullscreen [F]"}
|
||||
>
|
||||
{isFullscreen ? (
|
||||
<Minimize2 className="h-4 w-4" />
|
||||
) : (
|
||||
<Maximize2 className="h-4 w-4" />
|
||||
)}
|
||||
</button>
|
||||
|
||||
{castAvailable && (
|
||||
<button
|
||||
className={`pointer-events-auto rounded-md bg-black/50 p-1.5 backdrop-blur transition-colors hover:bg-black/70 hover:text-white ${
|
||||
isCasting ? "text-blue-400" : "text-zinc-400"
|
||||
}`}
|
||||
onClick={() =>
|
||||
isCasting ? onStopCasting() : streamUrl && onRequestCast(streamUrl)
|
||||
}
|
||||
title={
|
||||
isCasting
|
||||
? `Stop casting to ${castDeviceName ?? "TV"}`
|
||||
: "Cast to TV"
|
||||
}
|
||||
>
|
||||
<Cast className="h-4 w-4" />
|
||||
</button>
|
||||
)}
|
||||
|
||||
<QualityPicker
|
||||
quality={quality}
|
||||
options={qualityOptions}
|
||||
showPicker={showQualityPicker}
|
||||
onToggle={onToggleQualityPicker}
|
||||
onChangeQuality={onChangeQuality}
|
||||
/>
|
||||
|
||||
<button
|
||||
className={`pointer-events-auto rounded-md bg-black/50 p-1.5 backdrop-blur transition-colors hover:bg-black/70 hover:text-white ${showStats ? "text-violet-400" : "text-zinc-400"}`}
|
||||
onClick={onToggleStats}
|
||||
title="Stats for nerds [S]"
|
||||
>
|
||||
<Info className="h-4 w-4" />
|
||||
</button>
|
||||
|
||||
<button
|
||||
className="pointer-events-auto rounded-md bg-black/50 px-3 py-1.5 text-xs text-zinc-400 backdrop-blur transition-colors hover:bg-black/70 hover:text-white"
|
||||
onClick={onToggleSchedule}
|
||||
>
|
||||
{showSchedule ? "Hide guide" : "Guide [G]"}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
140
k-tv-frontend/app/(main)/tv/components/tv-base-layer.tsx
Normal file
140
k-tv-frontend/app/(main)/tv/components/tv-base-layer.tsx
Normal file
@@ -0,0 +1,140 @@
|
||||
import { RefObject } from "react";
|
||||
import type { CurrentBroadcastResponse } from "@/lib/types";
|
||||
import { VideoPlayer } from "./video-player";
|
||||
import { NoSignal } from "./no-signal";
|
||||
import { calcOffsetSecs } from "@/hooks/use-tv";
|
||||
import type { SubtitleTrack } from "./video-player";
|
||||
|
||||
interface TvBaseLayerProps {
|
||||
isLoadingChannels: boolean;
|
||||
hasChannels: boolean;
|
||||
broadcastError: unknown;
|
||||
isLoadingBroadcast: boolean;
|
||||
hasBroadcast: boolean;
|
||||
streamUrlError: unknown;
|
||||
streamError: boolean;
|
||||
videoEnded: boolean;
|
||||
streamUrl?: string | null;
|
||||
broadcast?: CurrentBroadcastResponse | null;
|
||||
activeSubtitleTrack: number;
|
||||
isMuted: boolean;
|
||||
showStats: boolean;
|
||||
videoRef: RefObject<HTMLVideoElement | null>;
|
||||
onSubtitleTracksChange: (tracks: SubtitleTrack[]) => void;
|
||||
onStreamError: () => void;
|
||||
onEnded: () => void;
|
||||
onNeedsInteraction: () => void;
|
||||
onRetry: () => void;
|
||||
}
|
||||
|
||||
export function TvBaseLayer({
|
||||
isLoadingChannels,
|
||||
hasChannels,
|
||||
broadcastError,
|
||||
isLoadingBroadcast,
|
||||
hasBroadcast,
|
||||
streamUrlError,
|
||||
streamError,
|
||||
videoEnded,
|
||||
streamUrl,
|
||||
broadcast,
|
||||
activeSubtitleTrack,
|
||||
isMuted,
|
||||
showStats,
|
||||
videoRef,
|
||||
onSubtitleTracksChange,
|
||||
onStreamError,
|
||||
onEnded,
|
||||
onNeedsInteraction,
|
||||
onRetry,
|
||||
}: TvBaseLayerProps) {
|
||||
if (isLoadingChannels) {
|
||||
return <NoSignal variant="loading" message="Tuning in…" />;
|
||||
}
|
||||
if (!hasChannels) {
|
||||
return (
|
||||
<NoSignal
|
||||
variant="no-signal"
|
||||
message="No channels configured. Visit the Dashboard to create one."
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const broadcastErrMsg = (broadcastError as Error)?.message;
|
||||
if (broadcastErrMsg === "auth_required") {
|
||||
return (
|
||||
<NoSignal variant="locked" message="Sign in to watch this channel." />
|
||||
);
|
||||
}
|
||||
if (
|
||||
broadcastErrMsg &&
|
||||
broadcastError &&
|
||||
(broadcastError as { status?: number }).status === 403
|
||||
) {
|
||||
return (
|
||||
<NoSignal variant="locked" message="This channel is owner-only." />
|
||||
);
|
||||
}
|
||||
|
||||
if (isLoadingBroadcast) {
|
||||
return <NoSignal variant="loading" message="Tuning in…" />;
|
||||
}
|
||||
if (!hasBroadcast) {
|
||||
return (
|
||||
<NoSignal
|
||||
variant="no-signal"
|
||||
message="Nothing is scheduled right now. Check back later."
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const streamErrMsg = (streamUrlError as Error)?.message;
|
||||
if (streamErrMsg === "auth_required") {
|
||||
return (
|
||||
<NoSignal variant="locked" message="Sign in to watch this block." />
|
||||
);
|
||||
}
|
||||
if (
|
||||
streamUrlError &&
|
||||
(streamUrlError as { status?: number }).status === 403
|
||||
) {
|
||||
return <NoSignal variant="locked" message="This block is owner-only." />;
|
||||
}
|
||||
|
||||
if (streamError) {
|
||||
return (
|
||||
<NoSignal variant="error" message="Stream failed to load.">
|
||||
<button
|
||||
onClick={onRetry}
|
||||
className="mt-2 rounded-md border border-zinc-700 bg-zinc-800/80 px-4 py-2 text-xs text-zinc-300 transition-colors hover:bg-zinc-700 hover:text-zinc-100"
|
||||
>
|
||||
Retry
|
||||
</button>
|
||||
</NoSignal>
|
||||
);
|
||||
}
|
||||
if (videoEnded) {
|
||||
return <NoSignal variant="loading" message="Up next, stay tuned…" />;
|
||||
}
|
||||
if (streamUrl) {
|
||||
return (
|
||||
<VideoPlayer
|
||||
ref={videoRef}
|
||||
src={streamUrl}
|
||||
className="absolute inset-0 h-full w-full"
|
||||
initialOffset={
|
||||
broadcast ? calcOffsetSecs(broadcast.slot.start_at) : 0
|
||||
}
|
||||
subtitleTrack={activeSubtitleTrack}
|
||||
muted={isMuted}
|
||||
showStats={showStats}
|
||||
broadcast={broadcast ?? undefined}
|
||||
onSubtitleTracksChange={onSubtitleTracksChange}
|
||||
onStreamError={onStreamError}
|
||||
onEnded={onEnded}
|
||||
onNeedsInteraction={onNeedsInteraction}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return <NoSignal variant="loading" message="Loading stream…" />;
|
||||
}
|
||||
59
k-tv-frontend/app/(main)/tv/components/volume-control.tsx
Normal file
59
k-tv-frontend/app/(main)/tv/components/volume-control.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import type { LucideIcon } from "lucide-react";
|
||||
|
||||
interface VolumeControlProps {
|
||||
volume: number;
|
||||
isMuted: boolean;
|
||||
VolumeIcon: LucideIcon;
|
||||
showSlider: boolean;
|
||||
onToggleSlider: () => void;
|
||||
onToggleMute: () => void;
|
||||
onVolumeChange: (v: number) => void;
|
||||
}
|
||||
|
||||
export function VolumeControl({
|
||||
volume,
|
||||
isMuted,
|
||||
VolumeIcon,
|
||||
showSlider,
|
||||
onToggleSlider,
|
||||
onToggleMute,
|
||||
onVolumeChange,
|
||||
}: VolumeControlProps) {
|
||||
return (
|
||||
<div className="pointer-events-auto relative">
|
||||
<button
|
||||
className="rounded-md bg-black/50 p-1.5 text-zinc-400 backdrop-blur transition-colors hover:bg-black/70 hover:text-white"
|
||||
onClick={onToggleSlider}
|
||||
title="Volume"
|
||||
>
|
||||
<VolumeIcon className="h-4 w-4" />
|
||||
</button>
|
||||
{showSlider && (
|
||||
<div className="absolute right-0 top-9 z-30 w-36 rounded-lg border border-zinc-700 bg-zinc-900/95 p-3 shadow-xl backdrop-blur">
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={100}
|
||||
value={isMuted ? 0 : Math.round(volume * 100)}
|
||||
onChange={(e) => {
|
||||
const v = Number(e.target.value) / 100;
|
||||
onVolumeChange(v);
|
||||
}}
|
||||
className="w-full accent-white"
|
||||
/>
|
||||
<div className="mt-1.5 flex items-center justify-between">
|
||||
<button
|
||||
onClick={onToggleMute}
|
||||
className="text-[10px] text-zinc-500 hover:text-zinc-300"
|
||||
>
|
||||
{isMuted ? "Unmute [M]" : "Mute [M]"}
|
||||
</button>
|
||||
<span className="font-mono text-[10px] text-zinc-500">
|
||||
{isMuted ? "0" : Math.round(volume * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user