Files
ani-cli-web/config_page.py
T

1026 lines
34 KiB
Python

#!/usr/bin/env python3
"""Config page template for ani-cli-web."""
from app_support import DISCORD_WEBHOOK_EVENT_CHOICES, DISCORD_WEBHOOK_EVENT_LABELS
from template_helpers import BROWSER_API_HELPER, render_page_links, render_sidebar_brand
WEBHOOK_EVENT_OPTIONS = "\n".join(
(
f' <label class="check-item"><input class="webhook-event" type="checkbox" value="{event_name}"> '
f"<span>{DISCORD_WEBHOOK_EVENT_LABELS[event_name]}</span></label>"
)
for event_name in DISCORD_WEBHOOK_EVENT_CHOICES
)
CONFIG_HTML = r"""<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ani-cli web - config</title>
<style>
:root {
color-scheme: dark;
--bg: #090611;
--bg-2: #140d21;
--panel: rgba(25, 18, 41, 0.82);
--panel-strong: rgba(18, 13, 31, 0.92);
--panel-2: rgba(157, 123, 255, 0.12);
--line: rgba(255, 255, 255, 0.08);
--line-strong: rgba(157, 123, 255, 0.26);
--text: #f6f2ff;
--muted: #b8b0cf;
--accent: #9d7bff;
--accent-strong: #7b5cff;
--accent-soft: rgba(157, 123, 255, 0.14);
--focus: #b296ff;
--shadow: 0 24px 60px rgba(5, 2, 15, 0.42);
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at top left, rgba(157, 123, 255, 0.18), transparent 28%),
radial-gradient(circle at top right, rgba(117, 201, 255, 0.12), transparent 24%),
linear-gradient(180deg, #120c1d 0%, #0b0813 56%, #07050d 100%);
color: var(--text);
font: 15px/1.5 "Segoe UI", Inter, system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background: linear-gradient(180deg, rgba(255, 255, 255, 0.03), transparent 24%);
opacity: 0.5;
}
button, input, select, textarea {
font: inherit;
}
button {
border: 1px solid var(--line);
background: var(--panel-2);
color: var(--text);
min-height: 40px;
border-radius: 14px;
padding: 0 14px;
cursor: pointer;
transition: transform 0.16s ease, border-color 0.16s ease, background 0.16s ease, box-shadow 0.16s ease;
}
button:hover {
border-color: var(--line-strong);
background: rgba(157, 123, 255, 0.18);
transform: translateY(-1px);
}
button.primary {
background: linear-gradient(135deg, var(--accent), var(--accent-strong));
border-color: transparent;
color: #fcfaff;
font-weight: 700;
box-shadow: 0 16px 32px rgba(123, 92, 255, 0.26);
}
input, select, textarea {
width: 100%;
min-height: 42px;
border-radius: 14px;
border: 1px solid var(--line);
background: rgba(10, 7, 18, 0.82);
color: var(--text);
padding: 0 13px;
outline: none;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
textarea {
min-height: 108px;
padding: 12px 13px;
resize: vertical;
}
input:focus, select:focus, textarea:focus {
border-color: var(--focus);
box-shadow: 0 0 0 3px rgba(178, 150, 255, 0.12);
}
label {
display: grid;
gap: 7px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.app {
display: grid;
grid-template-columns: minmax(320px, 380px) 1fr;
min-height: 100vh;
}
aside, main {
padding: 22px;
}
aside {
border-right: 1px solid var(--line);
background: linear-gradient(180deg, rgba(25, 18, 41, 0.94), rgba(14, 10, 24, 0.9));
display: grid;
align-content: start;
gap: 20px;
backdrop-filter: blur(18px) saturate(170%);
}
main {
display: grid;
gap: 20px;
min-width: 0;
align-content: start;
justify-items: stretch;
}
h1, h2, p { margin: 0; }
h1 {
font-size: 29px;
line-height: 1.05;
letter-spacing: -0.04em;
}
h2 {
font-size: 17px;
letter-spacing: -0.02em;
}
.topline, .toolbar, .row {
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
}
.topline, .toolbar {
justify-content: space-between;
}
.brand-wrap {
display: grid;
gap: 6px;
}
.brand-word {
color: var(--accent);
}
.shell-note {
color: var(--muted);
font-size: 13px;
max-width: 28ch;
}
.row > * { flex: 1; }
.page-links {
display: grid;
gap: 8px;
padding: 6px;
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(255, 255, 255, 0.03);
}
.page-link {
display: flex;
align-items: center;
justify-content: flex-start;
width: 100%;
min-height: 40px;
border: 0;
border-radius: 12px;
padding: 0 14px;
color: var(--muted);
background: transparent;
font-weight: 700;
text-decoration: none;
transition: background 0.16s ease, color 0.16s ease, transform 0.16s ease;
}
.page-link.active {
background: var(--accent-soft);
color: var(--text);
box-shadow: inset 0 0 0 1px rgba(157, 123, 255, 0.22);
}
.page-link:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.05);
}
.settings, .deps {
border: 1px solid var(--line);
border-radius: 22px;
background: linear-gradient(180deg, rgba(29, 22, 47, 0.88), rgba(17, 13, 29, 0.94));
padding: 18px;
display: grid;
gap: 14px;
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.muted {
color: var(--muted);
font-size: 13px;
}
.notice {
min-height: 20px;
color: #ddd0ff;
font-size: 13px;
}
.stack {
display: grid;
gap: 10px;
}
.wide-button {
width: 100%;
}
.chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.badge {
border: 1px solid var(--line);
border-radius: 999px;
padding: 6px 10px;
color: var(--muted);
background: rgba(255, 255, 255, 0.04);
font-size: 12px;
white-space: nowrap;
}
.badge.ok { color: #95e4ba; border-color: rgba(149, 228, 186, 0.24); }
.badge.bad { color: #ffd0da; border-color: rgba(255, 144, 164, 0.22); }
.field-hint {
color: var(--muted);
font-size: 12px;
}
.path-input {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 10px;
align-items: center;
}
.form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.check-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.check-item {
display: flex;
align-items: center;
gap: 10px;
min-height: 48px;
border: 1px solid var(--line);
border-radius: 16px;
padding: 10px 12px;
background: rgba(255, 255, 255, 0.03);
color: var(--text);
text-transform: none;
letter-spacing: 0;
font-size: 13px;
font-weight: 600;
}
.check-item input {
width: 18px;
min-height: 18px;
margin: 0;
accent-color: var(--accent);
}
.inline-actions {
display: flex;
gap: 10px;
flex-wrap: wrap;
align-items: center;
}
.deps {
justify-items: center;
text-align: center;
}
.deps .chips {
justify-content: center;
}
.settings-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 18px;
align-items: start;
width: 100%;
}
.settings-main, .deps {
width: 100%;
}
.overlay[hidden] {
display: none !important;
}
.overlay {
position: fixed;
inset: 0;
z-index: 40;
display: grid;
place-items: center;
padding: 22px;
background: rgba(4, 3, 10, 0.78);
backdrop-filter: blur(10px);
}
.browser-modal {
width: min(100%, 860px);
max-height: min(84vh, 760px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 22px;
background: linear-gradient(180deg, rgba(29, 22, 47, 0.96), rgba(17, 13, 29, 0.98));
box-shadow: var(--shadow);
display: grid;
grid-template-rows: auto auto auto 1fr auto;
gap: 12px;
padding: 18px;
}
.browser-header,
.browser-actions,
.browser-footer {
display: flex;
gap: 10px;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
}
.browser-path {
min-height: 42px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(10, 7, 18, 0.82);
color: var(--text);
padding: 10px 13px;
overflow-wrap: anywhere;
}
.browser-list {
overflow: auto;
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(255, 255, 255, 0.03);
padding: 8px;
display: grid;
gap: 8px;
min-height: 220px;
}
.browser-roots {
display: flex;
flex-wrap: wrap;
gap: 8px;
padding-bottom: 4px;
border-bottom: 1px solid var(--line);
}
.browser-roots .ghost.active {
background: rgba(255, 255, 255, 0.08);
border-color: var(--line-strong);
color: var(--text);
}
.browser-entry {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
min-height: 44px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.03);
color: var(--text);
text-align: left;
}
.browser-entry .muted {
font-size: 12px;
}
.browser-entry-name {
overflow-wrap: anywhere;
}
.changelog-modal {
width: min(100%, 900px);
max-height: min(84vh, 820px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 22px;
background: linear-gradient(180deg, rgba(29, 22, 47, 0.96), rgba(17, 13, 29, 0.98));
box-shadow: var(--shadow);
display: grid;
grid-template-rows: auto 1fr;
gap: 12px;
padding: 18px;
}
.changelog-header {
display: flex;
gap: 10px;
align-items: center;
justify-content: space-between;
}
.changelog-content {
overflow: auto;
border: 1px solid var(--line);
border-radius: 18px;
background: rgba(255, 255, 255, 0.03);
padding: 16px;
}
.changelog-content pre {
margin: 0;
white-space: pre-wrap;
overflow-wrap: anywhere;
color: var(--text);
font: 13px/1.55 "Cascadia Code", "Fira Code", Consolas, monospace;
}
@media (max-width: 900px) {
.app { grid-template-columns: 1fr; }
aside { border-right: 0; border-bottom: 1px solid var(--line); }
.toolbar, .row { align-items: stretch; flex-direction: column; }
}
@media (max-width: 1080px) {
.settings-grid {
grid-template-columns: 1fr;
}
}
@media (max-width: 640px) {
.form-grid, .check-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="app">
<aside>
""" + render_sidebar_brand(
"Tsuki-inspired settings view for saved defaults and runtime checks.",
"Config",
) + render_page_links("config") + r"""
<section class="settings">
<h2>Saved defaults</h2>
<div class="stack muted">
<p>These values preload the Search page and act as the fallback folder for new queue jobs.</p>
<p>Mode and quality can still be changed on the Search page before queueing a download.</p>
</div>
</section>
<section class="settings">
<h2>Runtime info</h2>
<div class="stack muted">
<p id="versionLine">Loading version...</p>
<p id="aniCliVersionLine">Detecting ani-cli version...</p>
<p>Saved settings live in `.ani-cli-web/config.json` inside this project.</p>
</div>
<button class="ghost wide-button" id="openChangelogBtn" type="button">Changelog</button>
</section>
</aside>
<main>
<section class="deps" id="deps"></section>
<div class="notice" id="notice"></div>
<div class="settings-grid">
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Defaults</h2>
<p class="muted">Manage the folder, mode, and quality that the Search page starts with.</p>
</div>
<button class="primary" id="saveConfigBtn" type="button">Save</button>
</div>
<div class="stack">
<label>Default folder
<div class="path-input">
<input id="downloadDir" placeholder="/home/me/Downloads/Anime">
<button class="ghost browse-path-btn" type="button" data-target="downloadDir" data-mode="dir">Browse</button>
</div>
</label>
</div>
<div class="form-grid">
<label>Default mode
<select id="configMode">
<option value="sub">Subtitled (Sub)</option>
<option value="dub">Dubbed (Dub)</option>
</select>
</label>
<label>Default quality
<select id="configQuality">
<option value="best">Best</option>
<option value="1080">1080p</option>
<option value="720">720p</option>
<option value="480">480p</option>
</select>
</label>
</div>
<div class="field-hint">Tip: these are the saved defaults only. The active search form can still be adjusted per download.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Watchlist schedule</h2>
<p class="muted">Periodically queue a background watchlist refresh to check for newly available episodes.</p>
</div>
</div>
<div class="form-grid">
<label>Automatic refresh
<select id="watchlistAutoRefreshEnabled">
<option value="false">Disabled</option>
<option value="true">Enabled</option>
</select>
</label>
<label>Refresh period (minutes)
<input id="watchlistAutoRefreshMinutes" type="number" min="1" max="10080" step="1" value="60">
</label>
<label>Delay between series (seconds)
<input id="watchlistRefreshDelaySeconds" type="number" min="0" max="300" step="1" value="5">
</label>
</div>
<div class="field-hint">When enabled, the server periodically starts the existing background “Refresh All” job using this interval. Bulk refreshes now wait this many seconds between shows to avoid flooding upstream sources.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Auto-download</h2>
<p class="muted">Automatically queue newly discovered episodes for shows in the `Watching` category after manual or scheduled refreshes.</p>
</div>
</div>
<div class="form-grid">
<label>Automatic queueing
<select id="autoDownloadEnabled">
<option value="false">Disabled</option>
<option value="true">Enabled</option>
</select>
</label>
<label>Default language
<select id="autoDownloadMode">
<option value="dub">Dubbed (Dub)</option>
<option value="sub">Subtitled (Sub)</option>
</select>
</label>
<label>Default quality
<select id="autoDownloadQuality">
<option value="best">Best</option>
<option value="1080">1080p</option>
<option value="720">720p</option>
<option value="480">480p</option>
</select>
</label>
</div>
<div class="field-hint">Adding a new watchlist entry does not auto-download anything. Auto-download only reacts to later manual refreshes or scheduled refreshes that find new episodes.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Jellyfin handoff</h2>
<p class="muted">Move fully completed anime libraries from the default download area into the folders that Jellyfin scans.</p>
</div>
<button id="runJellyfinSyncBtn" type="button">Run now</button>
</div>
<div class="form-grid">
<label>Automatic handoff
<select id="jellyfinSyncEnabled">
<option value="false">Disabled</option>
<option value="true">Enabled</option>
</select>
</label>
<label>Jellyfin TV library
<div class="path-input">
<input id="jellyfinTvDir" placeholder="/srv/media/anime-tv">
<button class="ghost browse-path-btn" type="button" data-target="jellyfinTvDir" data-mode="dir">Browse</button>
</div>
</label>
<label>Jellyfin movie library
<div class="path-input">
<input id="jellyfinMovieDir" placeholder="/srv/media/anime-movies">
<button class="ghost browse-path-btn" type="button" data-target="jellyfinMovieDir" data-mode="dir">Browse</button>
</div>
</label>
</div>
<div class="field-hint">TV entries move only after the show is marked `Finished` and at least one downloaded language covers the expected episode count. Movie entries move after they are downloaded. The manual trigger scans the current watchlist and moves anything already eligible.</div>
<div class="muted" id="jellyfinSyncStatus">No recent Jellyfin handoff job.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Discord notifications</h2>
<p class="muted">Send webhook notifications for refresh discoveries, refresh problems, and unexpected runtime errors.</p>
</div>
<button id="testWebhookBtn" type="button">Test webhook</button>
</div>
<div class="stack">
<label>Discord webhook URL
<textarea id="discordWebhookUrl" placeholder="https://discord.com/api/webhooks/..."></textarea>
</label>
<div class="field-hint">The new-episodes notification includes one card per anime, with local cached thumbnails attached to the webhook when available.</div>
</div>
<div class="stack">
<label>Notify for</label>
<div class="check-grid">
""" + WEBHOOK_EVENT_OPTIONS + r"""
</div>
</div>
</section>
</div>
</main>
</div>
<div class="overlay" id="pathBrowserOverlay" hidden>
<div class="browser-modal" role="dialog" aria-modal="true" aria-labelledby="pathBrowserTitle">
<div class="browser-header">
<div>
<h2 id="pathBrowserTitle">Choose path</h2>
<p class="muted" id="pathBrowserSubtitle">Browse folders on the host and select the destination path.</p>
</div>
<button class="ghost" id="pathBrowserCloseBtn" type="button" aria-label="Close path browser">&times;</button>
</div>
<div class="browser-path" id="pathBrowserCurrentPath"></div>
<div class="browser-actions">
<div class="inline-actions">
<button class="ghost" id="pathBrowserHomeBtn" type="button">Home</button>
<button class="ghost" id="pathBrowserUpBtn" type="button">Up</button>
<button class="ghost" id="pathBrowserRefreshBtn" type="button">Refresh</button>
</div>
<div class="muted" id="pathBrowserStatus"></div>
</div>
<div class="browser-list" id="pathBrowserList"></div>
<div class="browser-footer">
<button class="ghost" id="pathBrowserCancelBtn" type="button">Cancel</button>
<button class="primary" id="pathBrowserSelectBtn" type="button">Use this path</button>
</div>
</div>
</div>
<div class="overlay" id="changelogOverlay" hidden>
<div class="changelog-modal" role="dialog" aria-modal="true" aria-labelledby="changelogTitle">
<div class="changelog-header">
<div>
<h2 id="changelogTitle">Changelog</h2>
<p class="muted">Recent release notes loaded from the project's `CHANGELOG.md` file.</p>
</div>
<button class="ghost" id="closeChangelogBtn" type="button" aria-label="Close changelog">&times;</button>
</div>
<div class="changelog-content">
<pre id="changelogContent">Loading changelog...</pre>
</div>
</div>
</div>
<script>
const state = {
config: {
mode: "sub",
quality: "best",
download_dir: "",
watchlist_auto_refresh_enabled: false,
watchlist_auto_refresh_minutes: 60,
watchlist_refresh_delay_seconds: 5,
auto_download_enabled: false,
auto_download_mode: "dub",
auto_download_quality: "best",
jellyfin_sync_enabled: false,
jellyfin_tv_dir: "",
jellyfin_movie_dir: "",
discord_webhook_url: "",
discord_webhook_events: []
},
pathBrowser: {
targetId: "",
mode: "dir",
path: "",
parent_path: "",
home_path: ""
},
jellyfinSyncRunning: false,
jellyfinSyncJobId: null
};
const $ = (id) => document.getElementById(id);
""" + BROWSER_API_HELPER + r"""
const pathBrowserOverlay = $("pathBrowserOverlay");
const pathBrowserCurrentPath = $("pathBrowserCurrentPath");
const pathBrowserList = $("pathBrowserList");
const pathBrowserStatus = $("pathBrowserStatus");
const changelogOverlay = $("changelogOverlay");
const changelogContent = $("changelogContent");
function setNotice(text) {
$("notice").textContent = text || "";
}
function setJellyfinSyncButton(running) {
$("runJellyfinSyncBtn").disabled = !!running;
$("runJellyfinSyncBtn").textContent = running ? "Running..." : "Run now";
}
function setJellyfinSyncStatus(job) {
const el = $("jellyfinSyncStatus");
if (!job) {
el.textContent = "No recent Jellyfin handoff job.";
return;
}
if (job.status === "running") {
const title = job.current_title ? ` · ${job.current_title}` : "";
const file = job.current_file ? ` · ${job.current_file}` : "";
el.textContent = `${job.completed || 0}/${job.total || 0} processed · ${job.moved || 0} moved · ${job.moved_files || 0} files${title}${file}`;
return;
}
el.textContent = job.message || "Jellyfin handoff idle.";
}
function selectedWebhookEvents() {
return [...document.querySelectorAll(".webhook-event:checked")].map((input) => input.value);
}
function formConfigPayload() {
return {
download_dir: $("downloadDir").value,
mode: $("configMode").value,
quality: $("configQuality").value,
watchlist_auto_refresh_enabled: $("watchlistAutoRefreshEnabled").value === "true",
watchlist_auto_refresh_minutes: $("watchlistAutoRefreshMinutes").value,
watchlist_refresh_delay_seconds: $("watchlistRefreshDelaySeconds").value,
auto_download_enabled: $("autoDownloadEnabled").value === "true",
auto_download_mode: $("autoDownloadMode").value,
auto_download_quality: $("autoDownloadQuality").value,
jellyfin_sync_enabled: $("jellyfinSyncEnabled").value === "true",
jellyfin_tv_dir: $("jellyfinTvDir").value,
jellyfin_movie_dir: $("jellyfinMovieDir").value,
discord_webhook_url: $("discordWebhookUrl").value,
discord_webhook_events: selectedWebhookEvents()
};
}
function applyConfig(data) {
state.config = data;
$("downloadDir").value = data.download_dir;
$("configMode").value = data.mode;
$("configQuality").value = data.quality;
$("watchlistAutoRefreshEnabled").value = String(Boolean(data.watchlist_auto_refresh_enabled));
$("watchlistAutoRefreshMinutes").value = data.watchlist_auto_refresh_minutes;
$("watchlistRefreshDelaySeconds").value = data.watchlist_refresh_delay_seconds;
$("autoDownloadEnabled").value = String(Boolean(data.auto_download_enabled));
$("autoDownloadMode").value = data.auto_download_mode;
$("autoDownloadQuality").value = data.auto_download_quality;
$("jellyfinSyncEnabled").value = String(Boolean(data.jellyfin_sync_enabled));
$("jellyfinTvDir").value = data.jellyfin_tv_dir || "";
$("jellyfinMovieDir").value = data.jellyfin_movie_dir || "";
$("discordWebhookUrl").value = data.discord_webhook_url || "";
const selected = new Set(data.discord_webhook_events || []);
for (const input of document.querySelectorAll(".webhook-event")) {
input.checked = selected.has(input.value);
}
}
async function loadConfig() {
applyConfig(await api("/api/config"));
}
async function saveConfig() {
try {
applyConfig(await api("/api/config", {
method: "POST",
body: JSON.stringify(formConfigPayload())
}));
setNotice("Defaults saved.");
} catch (error) {
setNotice(error.message);
}
}
async function testWebhook() {
try {
const data = await api("/api/config/webhook/test", {
method: "POST",
body: JSON.stringify(formConfigPayload())
});
setNotice(data.message || "Discord webhook test notification sent.");
} catch (error) {
setNotice(error.message);
}
}
async function pollJellyfinSyncStatus(notifyOnComplete = true) {
try {
const data = await api("/api/config/jellyfin/sync-status");
const job = data.job || null;
const running = !!data.running && !!job;
const wasRunning = state.jellyfinSyncRunning;
state.jellyfinSyncRunning = running;
state.jellyfinSyncJobId = job ? job.id : null;
setJellyfinSyncButton(running);
setJellyfinSyncStatus(job);
if (running) {
setNotice(job.message || "Jellyfin handoff running...");
} else if (job && wasRunning && notifyOnComplete) {
setNotice(job.message || "Jellyfin handoff completed.");
}
} catch (error) {
setNotice(error.message);
}
}
async function runJellyfinSync() {
try {
const data = await api("/api/config/jellyfin/sync", {
method: "POST",
body: JSON.stringify(formConfigPayload())
});
const job = data.job || null;
state.jellyfinSyncRunning = !!job && (job.status === "pending" || job.status === "running");
state.jellyfinSyncJobId = job ? job.id : null;
setJellyfinSyncButton(state.jellyfinSyncRunning);
setJellyfinSyncStatus(job);
setNotice(data.message || "Jellyfin handoff started.");
} catch (error) {
setNotice(error.message);
}
}
function closePathBrowser() {
state.pathBrowser = {
targetId: "",
mode: "dir",
path: "",
parent_path: "",
home_path: "",
root_paths: []
};
pathBrowserOverlay.hidden = true;
pathBrowserList.innerHTML = "";
pathBrowserStatus.textContent = "";
}
function closeChangelog() {
changelogOverlay.hidden = true;
}
function renderPathBrowser(data) {
state.pathBrowser = {
...state.pathBrowser,
...data
};
pathBrowserCurrentPath.textContent = data.path || "";
pathBrowserStatus.textContent = "";
pathBrowserList.innerHTML = "";
const roots = Array.isArray(data.root_paths) ? data.root_paths : [];
if (roots.length > 1) {
const rootGroup = document.createElement("div");
rootGroup.className = "browser-roots";
for (const rootPath of roots) {
const active = data.path === rootPath || String(data.path || "").startsWith(`${rootPath}/`);
const button = document.createElement("button");
button.type = "button";
button.className = `ghost${active ? " active" : ""}`;
button.textContent = rootPath;
button.addEventListener("click", () => loadPathBrowser(rootPath));
rootGroup.appendChild(button);
}
pathBrowserList.appendChild(rootGroup);
}
if (!Array.isArray(data.entries) || !data.entries.length) {
const empty = document.createElement("div");
empty.className = "muted";
empty.textContent = "No entries found in this folder.";
pathBrowserList.appendChild(empty);
return;
}
for (const entry of data.entries) {
const button = document.createElement("button");
button.type = "button";
button.className = "browser-entry";
button.innerHTML = `
<span class="browser-entry-name"></span>
<span class="muted"></span>
`;
button.querySelector(".browser-entry-name").textContent = entry.name;
button.querySelector(".muted").textContent = entry.is_dir ? "Folder" : "File";
button.addEventListener("click", async () => {
if (entry.is_dir) {
await loadPathBrowser(entry.path);
return;
}
state.pathBrowser.path = entry.path;
pathBrowserCurrentPath.textContent = entry.path;
});
pathBrowserList.appendChild(button);
}
}
async function loadPathBrowser(path = "") {
try {
pathBrowserStatus.textContent = "Loading...";
const data = await api(`/api/fs/browse?path=${encodeURIComponent(path || state.pathBrowser.path || "")}&mode=${encodeURIComponent(state.pathBrowser.mode || "dir")}`);
renderPathBrowser(data);
} catch (error) {
pathBrowserStatus.textContent = error.message;
}
}
async function openPathBrowser(targetId, mode = "dir") {
state.pathBrowser = {
targetId,
mode,
path: $(targetId).value.trim() || "",
parent_path: "",
home_path: "",
root_paths: []
};
pathBrowserOverlay.hidden = false;
await loadPathBrowser(state.pathBrowser.path);
}
async function browseParentPath() {
if (!state.pathBrowser.parent_path) return;
await loadPathBrowser(state.pathBrowser.parent_path);
}
async function browseHomePath() {
if (!state.pathBrowser.home_path) return;
await loadPathBrowser(state.pathBrowser.home_path);
}
function selectCurrentBrowserPath() {
if (!state.pathBrowser.targetId) return;
$(state.pathBrowser.targetId).value = state.pathBrowser.path || "";
closePathBrowser();
}
async function loadDeps() {
const data = await api("/api/dependencies");
const el = $("deps");
el.innerHTML = "<h2>Dependencies</h2>";
const wrap = document.createElement("div");
wrap.className = "chips";
for (const [name, ok] of Object.entries(data)) {
const span = document.createElement("span");
span.className = `badge ${ok ? "ok" : "bad"}`;
span.textContent = `${name} ${ok ? "ok" : "missing"}`;
wrap.appendChild(span);
}
el.appendChild(wrap);
}
async function loadVersion() {
const data = await api("/api/version");
$("versionLine").textContent = `${data.name} ${data.version}`;
$("aniCliVersionLine").textContent = `ani-cli ${data.ani_cli_version || "Unavailable"}`;
}
async function openChangelog() {
try {
changelogContent.textContent = "Loading changelog...";
changelogOverlay.hidden = false;
const data = await api("/api/changelog");
changelogContent.textContent = data.content || "Changelog unavailable.";
} catch (error) {
changelogContent.textContent = error.message || "Changelog unavailable.";
}
}
$("saveConfigBtn").addEventListener("click", saveConfig);
$("testWebhookBtn").addEventListener("click", testWebhook);
$("runJellyfinSyncBtn").addEventListener("click", runJellyfinSync);
$("openChangelogBtn").addEventListener("click", openChangelog);
$("closeChangelogBtn").addEventListener("click", closeChangelog);
$("pathBrowserCloseBtn").addEventListener("click", closePathBrowser);
$("pathBrowserCancelBtn").addEventListener("click", closePathBrowser);
$("pathBrowserSelectBtn").addEventListener("click", selectCurrentBrowserPath);
$("pathBrowserUpBtn").addEventListener("click", browseParentPath);
$("pathBrowserHomeBtn").addEventListener("click", browseHomePath);
$("pathBrowserRefreshBtn").addEventListener("click", () => loadPathBrowser());
pathBrowserOverlay.addEventListener("click", (event) => {
if (event.target === pathBrowserOverlay) {
closePathBrowser();
}
});
changelogOverlay.addEventListener("click", (event) => {
if (event.target === changelogOverlay) {
closeChangelog();
}
});
window.addEventListener("keydown", (event) => {
if (event.key === "Escape" && !pathBrowserOverlay.hidden) {
closePathBrowser();
}
if (event.key === "Escape" && !changelogOverlay.hidden) {
closeChangelog();
}
});
for (const button of document.querySelectorAll(".browse-path-btn")) {
button.addEventListener("click", () => openPathBrowser(button.dataset.target || "", button.dataset.mode || "dir"));
}
(async function init() {
try {
await loadConfig();
await pollJellyfinSyncStatus(false);
await loadDeps();
await loadVersion();
window.setInterval(() => {
pollJellyfinSyncStatus();
}, 2500);
} catch (error) {
setNotice(error.message);
}
})();
</script>
</body>
</html>
"""