"use client";
import { useSettings } from "@/features/settings/store/SettingsStore";
import { useStagedSettings } from "@/features/settings/store/useStagedSettings";
import { useEffect, useState } from "react";
import { CheckCircle2, Loader2, Server, Youtube } from "lucide-react";
import { useTranslation } from "react-i18next";
import { SettingsPageHeader } from "@/components/settings/shared";
import {
getVideoLearningSettings,
testInvidious,
type VideoLearningSettings,
} from "@/lib/video-learning-api";
const DEFAULTS: VideoLearningSettings = {
version: 1,
default_provider: "youtube",
youtube: { transcript_provider: "youtube_transcript_api" },
invidious: { api_base_url: "", public_base_url: "" },
};
export default function VideoLearningSettingsPage() {
const { t } = useTranslation();
const { draftRevision } = useSettings();
const [live, setLive] = useState(DEFAULTS);
const [settings, setSettings] = useStagedSettings("video-learning", live, setLive);
const [loading, setLoading] = useState(true);
const [message, setMessage] = useState("");
useEffect(() => {
void getVideoLearningSettings()
.then(setLive)
.catch((error) =>
setMessage(
error instanceof Error
? error.message
: t("Settings could not be loaded."),
),
)
.finally(() => setLoading(false));
}, [t, draftRevision]);
const test = async () => {
setMessage(t("Testing Invidious…"));
try {
const result = await testInvidious(settings);
setMessage(
result.ok
? t("Invidious connected ({{message}}).", result)
: t("Invidious unavailable: {{message}}", result),
);
} catch (error) {
setMessage(
error instanceof Error ? error.message : t("Connection test failed."),
);
}
};
return (
{loading ? (
) : (
<>
{(
[
[
"youtube",
Youtube,
t(
"Official privacy-enhanced IFrame Player. No media is proxied or stored.",
),
],
[
"invidious",
Server,
t(
"HTML5 playback through your configured Invidious instance.",
),
],
] as const
).map(([provider, Icon, description]) => (
setSettings((current) => ({
...current,
default_provider: provider,
}))
}
className={`rounded-xl border p-4 text-left ${settings.default_provider === provider ? "border-[var(--primary)] ring-1 ring-[var(--primary)]" : "border-[var(--border)]"}`}
>
{provider === "youtube"
? t("Native YouTube")
: t("Self-hosted Invidious")}
{description}
))}
{t("Invidious connection")}
{t("Backend API origin")}
setSettings((current) => ({
...current,
invidious: {
...current.invidious,
api_base_url: event.target.value,
},
}))
}
placeholder={t("Invidious API URL")}
className="w-full rounded-lg border border-[var(--border)] bg-transparent px-3 py-2 font-mono"
/>
{t("Browser-facing origin (optional)")}
setSettings((current) => ({
...current,
invidious: {
...current.invidious,
public_base_url: event.target.value,
},
}))
}
placeholder={t("Defaults to the API origin")}
className="w-full rounded-lg border border-[var(--border)] bg-transparent px-3 py-2 font-mono"
/>
void test()}
className="rounded-lg border border-[var(--border)] px-3 py-2 text-sm"
>
{t("Test connection")}
{t("YouTube transcript adapter")}
{t(
"Optional captions; playback remains available when the package or captions are unavailable.",
)}
setSettings((current) => ({
...current,
youtube: {
transcript_provider: event.target.value as
| "youtube_transcript_api"
| "none",
},
}))
}
className="rounded-lg border border-[var(--border)] bg-transparent px-3 py-2"
>
{t("youtube-transcript-api")}
{t("Disabled")}
{message && (
{message}
)}
>
)}
);
}