1215 lines
36 KiB
HTML
1215 lines
36 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Archive Bot Dashboard</title>
|
|
<style>
|
|
:root {
|
|
color-scheme: dark;
|
|
--bg: #111214;
|
|
--panel: #1b1d21;
|
|
--panel-2: #202328;
|
|
--line: #30343b;
|
|
--line-strong: #3d424b;
|
|
--text: #f1f2f4;
|
|
--muted: #a2a8b3;
|
|
--faint: #757d8a;
|
|
--ok: #10b981;
|
|
--warn: #f59e0b;
|
|
--bad: #ef4444;
|
|
--action: #e7e9ed;
|
|
--action-text: #15171a;
|
|
}
|
|
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
margin: 0;
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Helvetica Neue", sans-serif;
|
|
}
|
|
|
|
button,
|
|
input,
|
|
select {
|
|
font: inherit;
|
|
}
|
|
|
|
button {
|
|
border: 1px solid var(--line-strong);
|
|
border-radius: 6px;
|
|
background: var(--panel-2);
|
|
color: var(--text);
|
|
padding: 8px 10px;
|
|
font-weight: 650;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button:hover {
|
|
border-color: #5a606b;
|
|
}
|
|
|
|
button.primary {
|
|
background: var(--action);
|
|
border-color: var(--action);
|
|
color: var(--action-text);
|
|
}
|
|
|
|
button.danger {
|
|
color: #fecaca;
|
|
}
|
|
|
|
input,
|
|
select {
|
|
width: 100%;
|
|
border: 1px solid var(--line);
|
|
border-radius: 6px;
|
|
background: #14161a;
|
|
color: var(--text);
|
|
padding: 8px 9px;
|
|
min-height: 36px;
|
|
}
|
|
|
|
input:focus,
|
|
select:focus {
|
|
outline: 2px solid #5f6875;
|
|
outline-offset: 1px;
|
|
}
|
|
|
|
.app {
|
|
min-height: 100vh;
|
|
display: grid;
|
|
grid-template-columns: 240px 1fr;
|
|
}
|
|
|
|
aside {
|
|
border-right: 1px solid var(--line);
|
|
background: #15171a;
|
|
padding: 18px 14px;
|
|
}
|
|
|
|
.brand {
|
|
margin: 0 0 20px;
|
|
font-size: 15px;
|
|
font-weight: 750;
|
|
}
|
|
|
|
nav {
|
|
display: grid;
|
|
gap: 4px;
|
|
}
|
|
|
|
.nav-item {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
gap: 12px;
|
|
width: 100%;
|
|
border: 0;
|
|
border-radius: 6px;
|
|
background: transparent;
|
|
padding: 8px 9px;
|
|
color: var(--muted);
|
|
font-weight: 650;
|
|
text-align: left;
|
|
}
|
|
|
|
.nav-item:hover {
|
|
background: #1b1d21;
|
|
border-color: transparent;
|
|
}
|
|
|
|
.nav-item.active {
|
|
background: var(--panel);
|
|
color: var(--text);
|
|
}
|
|
|
|
.nav-item.disabled {
|
|
color: var(--faint);
|
|
}
|
|
|
|
main {
|
|
min-width: 0;
|
|
padding: 22px 24px 36px;
|
|
}
|
|
|
|
.topbar {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 16px;
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
h1 {
|
|
margin: 0;
|
|
font-size: 20px;
|
|
letter-spacing: 0;
|
|
}
|
|
|
|
.actions {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 8px;
|
|
justify-content: flex-end;
|
|
}
|
|
|
|
.panel {
|
|
border: 1px solid var(--line);
|
|
border-radius: 8px;
|
|
background: var(--panel);
|
|
}
|
|
|
|
.panel-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
gap: 12px;
|
|
padding: 13px 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
.panel-title {
|
|
font-weight: 700;
|
|
}
|
|
|
|
.summary {
|
|
display: grid;
|
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
|
gap: 1px;
|
|
overflow: hidden;
|
|
margin-bottom: 16px;
|
|
background: var(--line);
|
|
}
|
|
|
|
.summary-item {
|
|
background: var(--panel);
|
|
padding: 14px;
|
|
}
|
|
|
|
.summary-item span {
|
|
display: block;
|
|
color: var(--muted);
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.summary-item strong {
|
|
font-size: 18px;
|
|
}
|
|
|
|
.service-list {
|
|
display: grid;
|
|
gap: 1px;
|
|
background: var(--line);
|
|
}
|
|
|
|
.service-row {
|
|
display: grid;
|
|
grid-template-columns: 24px minmax(100px, 0.6fr) minmax(120px, 0.8fr) minmax(200px, 1.5fr) minmax(160px, 1.2fr) 100px 80px 110px 65px;
|
|
gap: 10px;
|
|
align-items: end;
|
|
background: var(--panel);
|
|
padding: 12px 14px;
|
|
border-top: 2px solid transparent;
|
|
border-bottom: 2px solid transparent;
|
|
}
|
|
|
|
.service-row.drag-over {
|
|
border-top-color: var(--ok);
|
|
}
|
|
|
|
.drag-handle {
|
|
cursor: grab;
|
|
color: var(--faint);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
align-self: center;
|
|
height: 100%;
|
|
font-size: 18px;
|
|
}
|
|
|
|
.drag-handle:active {
|
|
cursor: grabbing;
|
|
}
|
|
|
|
.field label {
|
|
display: block;
|
|
color: var(--muted);
|
|
font-size: 12px;
|
|
font-weight: 650;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.row-status {
|
|
align-self: center;
|
|
color: var(--muted);
|
|
font-size: 12px;
|
|
line-height: 1.35;
|
|
}
|
|
|
|
.state {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.state.ok {
|
|
color: var(--ok);
|
|
}
|
|
|
|
.state.bad {
|
|
color: var(--bad);
|
|
}
|
|
|
|
.message {
|
|
margin-top: 12px;
|
|
min-height: 20px;
|
|
color: var(--muted);
|
|
}
|
|
|
|
.message.error {
|
|
color: #fca5a5;
|
|
}
|
|
|
|
.view[hidden] {
|
|
display: none;
|
|
}
|
|
|
|
.media-form {
|
|
display: grid;
|
|
grid-template-columns: minmax(220px, 0.8fr) minmax(220px, 1fr) minmax(220px, 1fr);
|
|
gap: 12px;
|
|
padding: 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
.channel-form {
|
|
display: grid;
|
|
grid-template-columns: minmax(220px, 360px);
|
|
gap: 12px;
|
|
padding: 14px;
|
|
margin-bottom: 16px;
|
|
}
|
|
|
|
.media-field label {
|
|
display: block;
|
|
color: var(--muted);
|
|
font-size: 12px;
|
|
font-weight: 650;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.media-field input[type="file"] {
|
|
padding: 7px;
|
|
}
|
|
|
|
.media-status {
|
|
display: grid;
|
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
|
gap: 1px;
|
|
background: var(--line);
|
|
}
|
|
|
|
.media-status div {
|
|
background: var(--panel);
|
|
padding: 14px;
|
|
min-height: 74px;
|
|
}
|
|
|
|
.media-status span {
|
|
display: block;
|
|
color: var(--muted);
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
.media-status strong {
|
|
display: block;
|
|
font-size: 16px;
|
|
overflow-wrap: anywhere;
|
|
}
|
|
|
|
.media-editor {
|
|
margin-top: 16px;
|
|
}
|
|
|
|
.media-toolbar {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 8px;
|
|
padding: 12px 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
.media-tabs {
|
|
display: flex;
|
|
gap: 6px;
|
|
padding: 10px 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
.media-tab {
|
|
min-width: 86px;
|
|
}
|
|
|
|
.media-tab.active {
|
|
background: var(--action);
|
|
border-color: var(--action);
|
|
color: var(--action-text);
|
|
}
|
|
|
|
.library-list {
|
|
display: grid;
|
|
gap: 1px;
|
|
background: var(--line);
|
|
}
|
|
|
|
.library-row {
|
|
display: grid;
|
|
grid-template-columns: minmax(170px, 1fr) 82px minmax(130px, 0.9fr) 90px 96px 96px minmax(220px, 1.3fr) 70px;
|
|
gap: 10px;
|
|
align-items: end;
|
|
background: var(--panel);
|
|
padding: 12px 14px;
|
|
}
|
|
|
|
.library-row.movies {
|
|
grid-template-columns: minmax(170px, 1fr) 82px minmax(130px, 0.9fr) 90px 96px minmax(220px, 1.3fr) 70px;
|
|
}
|
|
|
|
.empty-library {
|
|
background: var(--panel);
|
|
color: var(--muted);
|
|
padding: 18px 14px;
|
|
}
|
|
|
|
.token-screen {
|
|
max-width: 420px;
|
|
margin: 80px auto;
|
|
padding: 18px;
|
|
}
|
|
|
|
.token-screen p {
|
|
color: var(--muted);
|
|
margin: 8px 0 16px;
|
|
}
|
|
|
|
.token-screen input + input {
|
|
margin-top: 8px;
|
|
}
|
|
|
|
.token-actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
margin-top: 10px;
|
|
}
|
|
|
|
@media (max-width: 1100px) {
|
|
.app {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
aside {
|
|
border-right: 0;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
nav {
|
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
|
}
|
|
|
|
.service-row,
|
|
.channel-form,
|
|
.library-row,
|
|
.library-row.movies,
|
|
.media-form {
|
|
grid-template-columns: 1fr 1fr;
|
|
}
|
|
}
|
|
|
|
@media (max-width: 680px) {
|
|
main {
|
|
padding: 18px 14px 28px;
|
|
}
|
|
|
|
.topbar,
|
|
.panel-header {
|
|
align-items: flex-start;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.summary {
|
|
grid-template-columns: 1fr 1fr;
|
|
}
|
|
|
|
nav,
|
|
.service-row,
|
|
.channel-form,
|
|
.library-row,
|
|
.library-row.movies,
|
|
.media-form,
|
|
.media-status {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="login" class="token-screen panel" hidden>
|
|
<h1>Archive Bot</h1>
|
|
<p>Sign in with the dashboard account configured on the bot.</p>
|
|
<input id="username" type="text" autocomplete="username" placeholder="Username">
|
|
<input id="password" type="password" autocomplete="current-password" placeholder="Password">
|
|
<div class="token-actions">
|
|
<button id="loginButton" class="primary" type="button">Sign in</button>
|
|
</div>
|
|
<div id="loginMessage" class="message error"></div>
|
|
</div>
|
|
|
|
<div id="app" class="app" hidden>
|
|
<aside>
|
|
<div class="brand">Archive Bot</div>
|
|
<nav aria-label="Bot modules">
|
|
<button class="nav-item active" type="button" data-view="status"><span>Status</span><span>Ready</span></button>
|
|
<button class="nav-item" type="button" data-view="media"><span>Media</span><span>CSV</span></button>
|
|
<div class="nav-item disabled"><span>Polls</span><span>Later</span></div>
|
|
<div class="nav-item disabled"><span>Automations</span><span>Later</span></div>
|
|
</nav>
|
|
</aside>
|
|
|
|
<main>
|
|
<section id="statusView" class="view">
|
|
<div class="topbar">
|
|
<h1>Status Services</h1>
|
|
<div class="actions">
|
|
<button id="refresh" type="button">Refresh</button>
|
|
<button id="checkNow" type="button">Check now</button>
|
|
<button id="addService" type="button">Add service</button>
|
|
<button id="save" class="primary" type="button">Save and update Discord</button>
|
|
<button id="logout" type="button">Logout</button>
|
|
</div>
|
|
</div>
|
|
|
|
<section class="panel channel-form" aria-label="Status channel">
|
|
<div class="media-field">
|
|
<label for="statusChannelId">Status Channel ID</label>
|
|
<input id="statusChannelId" inputmode="numeric" placeholder="Discord channel ID">
|
|
</div>
|
|
</section>
|
|
|
|
<section class="summary panel" aria-label="Current status summary">
|
|
<div class="summary-item">
|
|
<span>Services</span>
|
|
<strong id="serviceCount">0</strong>
|
|
</div>
|
|
<div class="summary-item">
|
|
<span>Online</span>
|
|
<strong id="onlineCount">0</strong>
|
|
</div>
|
|
<div class="summary-item">
|
|
<span>Issues</span>
|
|
<strong id="issueCount">0</strong>
|
|
</div>
|
|
<div class="summary-item">
|
|
<span>Last check</span>
|
|
<strong id="lastCheck">Never</strong>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="panel">
|
|
<div class="panel-header">
|
|
<div class="panel-title">Monitored Links</div>
|
|
<div id="message" class="message"></div>
|
|
</div>
|
|
<div id="services" class="service-list"></div>
|
|
</section>
|
|
</section>
|
|
|
|
<section id="mediaView" class="view" hidden>
|
|
<div class="topbar">
|
|
<h1>Media Catalog</h1>
|
|
<div class="actions">
|
|
<button id="mediaRefresh" type="button">Refresh</button>
|
|
<button id="saveMediaLibrary" type="button">Save library</button>
|
|
<button id="publishMedia" class="primary" type="button">Publish to Discord</button>
|
|
</div>
|
|
</div>
|
|
|
|
<section class="panel">
|
|
<div class="panel-header">
|
|
<div class="panel-title">CSV Upload</div>
|
|
<div id="mediaMessage" class="message"></div>
|
|
</div>
|
|
<div class="media-form">
|
|
<div class="media-field">
|
|
<label for="mediaChannelId">Channel ID</label>
|
|
<input id="mediaChannelId" inputmode="numeric" placeholder="Discord channel ID">
|
|
</div>
|
|
<div class="media-field">
|
|
<label for="moviesCsv">Movies.csv</label>
|
|
<input id="moviesCsv" type="file" accept=".csv,text/csv">
|
|
</div>
|
|
<div class="media-field">
|
|
<label for="showsCsv">Shows.csv</label>
|
|
<input id="showsCsv" type="file" accept=".csv,text/csv">
|
|
</div>
|
|
</div>
|
|
<div class="media-toolbar">
|
|
<button id="importMediaCsv" type="button">Import CSV</button>
|
|
<button id="addMovie" type="button">Add movie</button>
|
|
<button id="addShow" type="button">Add show</button>
|
|
</div>
|
|
<div class="media-status" aria-label="Media catalog status">
|
|
<div>
|
|
<span>Movies</span>
|
|
<strong id="mediaMovieCount">Not published</strong>
|
|
</div>
|
|
<div>
|
|
<span>Shows</span>
|
|
<strong id="mediaShowCount">Not published</strong>
|
|
</div>
|
|
<div>
|
|
<span>Messages</span>
|
|
<strong id="mediaMessageCount">0</strong>
|
|
</div>
|
|
<div>
|
|
<span>Published</span>
|
|
<strong id="mediaPublishedAt">Never</strong>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="panel media-editor">
|
|
<div class="panel-header">
|
|
<div class="panel-title">Library Editor</div>
|
|
<div id="libraryCount" class="message"></div>
|
|
</div>
|
|
<div class="media-tabs">
|
|
<button id="moviesTab" class="media-tab active" type="button" data-media-tab="movies">Movies</button>
|
|
<button id="showsTab" class="media-tab" type="button" data-media-tab="shows">Shows</button>
|
|
</div>
|
|
<div id="mediaLibrary" class="library-list"></div>
|
|
</section>
|
|
</section>
|
|
</main>
|
|
</div>
|
|
|
|
<script>
|
|
const appEl = document.querySelector("#app");
|
|
const loginEl = document.querySelector("#login");
|
|
const statusViewEl = document.querySelector("#statusView");
|
|
const mediaViewEl = document.querySelector("#mediaView");
|
|
const servicesEl = document.querySelector("#services");
|
|
const mediaLibraryEl = document.querySelector("#mediaLibrary");
|
|
const libraryCountEl = document.querySelector("#libraryCount");
|
|
const messageEl = document.querySelector("#message");
|
|
const mediaMessageEl = document.querySelector("#mediaMessage");
|
|
const loginMessageEl = document.querySelector("#loginMessage");
|
|
|
|
let services = [];
|
|
let results = new Map();
|
|
let csrfToken = "";
|
|
let channels = { statusChannelId: "", mediaChannelId: "" };
|
|
let mediaLibrary = { movies: [], shows: [] };
|
|
let activeMediaTab = "movies";
|
|
|
|
function headers(method = "GET") {
|
|
const base = { "Content-Type": "application/json" };
|
|
if (method !== "GET") {
|
|
base["X-CSRF-Token"] = csrfToken;
|
|
}
|
|
return base;
|
|
}
|
|
|
|
async function api(path, options = {}) {
|
|
const method = options.method || "GET";
|
|
const response = await fetch(path, {
|
|
...options,
|
|
credentials: "same-origin",
|
|
headers: { ...headers(method), ...(options.headers || {}) }
|
|
});
|
|
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
const error = new Error(data.error || `Request failed: ${response.status}`);
|
|
error.status = response.status;
|
|
throw error;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
function setMessage(text, isError = false) {
|
|
messageEl.textContent = text;
|
|
messageEl.classList.toggle("error", isError);
|
|
}
|
|
|
|
function setMediaMessage(text, isError = false) {
|
|
mediaMessageEl.textContent = text;
|
|
mediaMessageEl.classList.toggle("error", isError);
|
|
}
|
|
|
|
function showView(name) {
|
|
statusViewEl.hidden = name !== "status";
|
|
mediaViewEl.hidden = name !== "media";
|
|
document.querySelectorAll("[data-view]").forEach((item) => {
|
|
item.classList.toggle("active", item.dataset.view === name);
|
|
});
|
|
}
|
|
|
|
function showLogin(message = "") {
|
|
appEl.hidden = true;
|
|
loginEl.hidden = false;
|
|
loginMessageEl.textContent = message;
|
|
document.querySelector("#password").value = "";
|
|
}
|
|
|
|
function showApp() {
|
|
loginEl.hidden = true;
|
|
appEl.hidden = false;
|
|
}
|
|
|
|
function normalizeService(service = {}) {
|
|
return {
|
|
name: service.name || "",
|
|
group: service.group || "Main Services",
|
|
url: service.url || "",
|
|
displayUrl: service.displayUrl || service.url || "",
|
|
method: service.method || "GET",
|
|
timeoutSeconds: service.timeoutSeconds || 10,
|
|
expectedStatuses: Array.isArray(service.expectedStatuses) ? service.expectedStatuses : ["200-399"],
|
|
keyword: service.keyword || ""
|
|
};
|
|
}
|
|
|
|
function statusFor(service) {
|
|
return results.get(service.name) || null;
|
|
}
|
|
|
|
function renderSummary(payload) {
|
|
channels = payload.channels || {
|
|
statusChannelId: payload.channelId || channels.statusChannelId || "",
|
|
mediaChannelId: channels.mediaChannelId || payload.channelId || ""
|
|
};
|
|
document.querySelector("#statusChannelId").value = channels.statusChannelId || "";
|
|
const online = payload.results.filter((result) => result.ok).length;
|
|
document.querySelector("#serviceCount").textContent = payload.services.length;
|
|
document.querySelector("#onlineCount").textContent = online;
|
|
document.querySelector("#issueCount").textContent = Math.max(payload.results.length - online, 0);
|
|
document.querySelector("#lastCheck").textContent = payload.lastCheckedAt
|
|
? new Date(payload.lastCheckedAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })
|
|
: "Never";
|
|
}
|
|
|
|
function serviceRow(service, index) {
|
|
const result = statusFor(service);
|
|
const row = document.createElement("div");
|
|
row.className = "service-row";
|
|
row.dataset.index = String(index);
|
|
row.draggable = true;
|
|
|
|
row.innerHTML = `
|
|
<div class="drag-handle" title="Drag to reorder">≡</div>
|
|
<div class="field">
|
|
<label>Group</label>
|
|
<input data-key="group" value="${escapeAttr(service.group)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Name</label>
|
|
<input data-key="name" value="${escapeAttr(service.name)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Check URL</label>
|
|
<input data-key="url" value="${escapeAttr(service.url)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Display URL</label>
|
|
<input data-key="displayUrl" value="${escapeAttr(service.displayUrl)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Expected</label>
|
|
<input data-key="expectedStatuses" value="${escapeAttr(service.expectedStatuses.join(", "))}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Timeout</label>
|
|
<input data-key="timeoutSeconds" type="number" min="1" max="60" value="${escapeAttr(service.timeoutSeconds)}">
|
|
</div>
|
|
<div class="row-status">
|
|
${renderResult(result)}
|
|
</div>
|
|
<button class="danger" type="button" data-remove="${index}" title="Remove service">Del</button>
|
|
`;
|
|
|
|
return row;
|
|
}
|
|
|
|
function renderResult(result) {
|
|
if (!result) {
|
|
return `<span class="state">Unchecked</span>`;
|
|
}
|
|
|
|
const stateClass = result.ok ? "ok" : "bad";
|
|
const label = result.ok ? "Online" : "Issue";
|
|
const status = result.status ? `HTTP ${result.status}` : "No response";
|
|
const latency = result.latencyMs == null ? "n/a" : `${result.latencyMs} ms`;
|
|
return `<span class="state ${stateClass}">${label}</span><br>${status}<br>${latency}`;
|
|
}
|
|
|
|
function escapeAttr(value) {
|
|
return String(value ?? "")
|
|
.replace(/&/g, "&")
|
|
.replace(/"/g, """)
|
|
.replace(/</g, "<")
|
|
.replace(/>/g, ">");
|
|
}
|
|
|
|
function renderServices() {
|
|
servicesEl.innerHTML = "";
|
|
services.forEach((service, index) => {
|
|
servicesEl.append(serviceRow(service, index));
|
|
});
|
|
}
|
|
|
|
function collectServices() {
|
|
return [...servicesEl.querySelectorAll(".service-row")].map((row) => {
|
|
const item = {};
|
|
row.querySelectorAll("[data-key]").forEach((input) => {
|
|
const key = input.dataset.key;
|
|
if (key === "expectedStatuses") {
|
|
item[key] = input.value.split(",").map((part) => part.trim()).filter(Boolean);
|
|
} else if (key === "timeoutSeconds") {
|
|
item[key] = Number(input.value || 10);
|
|
} else {
|
|
item[key] = input.value.trim();
|
|
}
|
|
});
|
|
item.method = "GET";
|
|
if (!item.displayUrl) item.displayUrl = item.url;
|
|
return item;
|
|
});
|
|
}
|
|
|
|
async function loadStatus() {
|
|
const payload = await api("/api/status");
|
|
services = payload.services.map(normalizeService);
|
|
results = new Map(payload.results.map((result) => [result.name, result]));
|
|
renderSummary(payload);
|
|
renderServices();
|
|
setMessage(payload.lastError ? `Last error: ${payload.lastError}` : "");
|
|
showApp();
|
|
}
|
|
|
|
function renderMediaStatus(payload) {
|
|
channels.mediaChannelId = payload.channelId || channels.mediaChannelId || "";
|
|
document.querySelector("#mediaChannelId").value = channels.mediaChannelId;
|
|
if (payload.library) {
|
|
mediaLibrary = normalizeMediaLibrary(payload.library);
|
|
renderMediaLibrary();
|
|
}
|
|
document.querySelector("#mediaMovieCount").textContent = payload.movieCount == null ? "Not published" : payload.movieCount;
|
|
document.querySelector("#mediaShowCount").textContent = payload.showCount == null ? "Not published" : payload.showCount;
|
|
document.querySelector("#mediaMessageCount").textContent = Array.isArray(payload.messageIds) ? payload.messageIds.length : 0;
|
|
document.querySelector("#mediaPublishedAt").textContent = payload.publishedAt
|
|
? new Date(payload.publishedAt).toLocaleString([], { dateStyle: "short", timeStyle: "short" })
|
|
: "Never";
|
|
}
|
|
|
|
async function loadMediaStatus() {
|
|
const payload = await api("/api/media");
|
|
renderMediaStatus(payload);
|
|
return payload;
|
|
}
|
|
|
|
function normalizeMediaLibrary(library = {}) {
|
|
return {
|
|
movies: Array.isArray(library.movies) ? library.movies.map((item) => normalizeMediaItem(item, "movie")) : [],
|
|
shows: Array.isArray(library.shows) ? library.shows.map((item) => normalizeMediaItem(item, "show")) : []
|
|
};
|
|
}
|
|
|
|
function normalizeMediaItem(item = {}, mediaType = "movie") {
|
|
return {
|
|
title: item.title || "",
|
|
mediaType,
|
|
year: item.year || "",
|
|
genres: item.genres || "",
|
|
rating: item.rating || "",
|
|
runtime: mediaType === "movie" ? item.runtime || "" : "",
|
|
seasons: mediaType === "show" ? item.seasons || "" : "",
|
|
episodes: mediaType === "show" ? item.episodes || "" : "",
|
|
summary: item.summary || ""
|
|
};
|
|
}
|
|
|
|
function collectMediaLibrary() {
|
|
const currentItems = [...mediaLibraryEl.querySelectorAll(".library-row")].map((row) => {
|
|
const item = {};
|
|
row.querySelectorAll("[data-media-key]").forEach((input) => {
|
|
item[input.dataset.mediaKey] = input.value.trim();
|
|
});
|
|
return normalizeMediaItem(item, activeMediaTab === "movies" ? "movie" : "show");
|
|
});
|
|
mediaLibrary[activeMediaTab] = currentItems;
|
|
return mediaLibrary;
|
|
}
|
|
|
|
function mediaRow(item, index) {
|
|
const type = activeMediaTab === "movies" ? "movie" : "show";
|
|
const row = document.createElement("div");
|
|
row.className = `library-row ${activeMediaTab}`;
|
|
row.dataset.index = String(index);
|
|
const countFields = type === "show"
|
|
? `
|
|
<div class="field">
|
|
<label>Seasons</label>
|
|
<input data-media-key="seasons" type="number" min="0" value="${escapeAttr(item.seasons)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Episodes</label>
|
|
<input data-media-key="episodes" type="number" min="0" value="${escapeAttr(item.episodes)}">
|
|
</div>
|
|
`
|
|
: `
|
|
<div class="field">
|
|
<label>Runtime</label>
|
|
<input data-media-key="runtime" value="${escapeAttr(item.runtime)}">
|
|
</div>
|
|
`;
|
|
|
|
row.innerHTML = `
|
|
<div class="field">
|
|
<label>Title</label>
|
|
<input data-media-key="title" value="${escapeAttr(item.title)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Year</label>
|
|
<input data-media-key="year" inputmode="numeric" value="${escapeAttr(item.year)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Genres</label>
|
|
<input data-media-key="genres" value="${escapeAttr(item.genres)}">
|
|
</div>
|
|
<div class="field">
|
|
<label>Rating</label>
|
|
<input data-media-key="rating" value="${escapeAttr(item.rating)}">
|
|
</div>
|
|
${countFields}
|
|
<div class="field">
|
|
<label>Summary</label>
|
|
<input data-media-key="summary" value="${escapeAttr(item.summary)}">
|
|
</div>
|
|
<button class="danger" type="button" data-remove-media="${index}">Del</button>
|
|
`;
|
|
return row;
|
|
}
|
|
|
|
function renderMediaLibrary() {
|
|
mediaLibraryEl.innerHTML = "";
|
|
const items = mediaLibrary[activeMediaTab] || [];
|
|
libraryCountEl.textContent = `${mediaLibrary.movies.length} movies · ${mediaLibrary.shows.length} shows`;
|
|
document.querySelectorAll("[data-media-tab]").forEach((button) => {
|
|
button.classList.toggle("active", button.dataset.mediaTab === activeMediaTab);
|
|
});
|
|
if (!items.length) {
|
|
const empty = document.createElement("div");
|
|
empty.className = "empty-library";
|
|
empty.textContent = activeMediaTab === "movies" ? "No movies loaded." : "No shows loaded.";
|
|
mediaLibraryEl.append(empty);
|
|
return;
|
|
}
|
|
items.forEach((item, index) => mediaLibraryEl.append(mediaRow(item, index)));
|
|
}
|
|
|
|
function currentChannelSettings() {
|
|
const statusChannelId = document.querySelector("#statusChannelId").value.trim() || channels.statusChannelId || "";
|
|
const mediaChannelId = document.querySelector("#mediaChannelId").value.trim() || channels.mediaChannelId || statusChannelId;
|
|
return { statusChannelId, mediaChannelId };
|
|
}
|
|
|
|
async function saveChannelSettings() {
|
|
const payload = await api("/api/settings", {
|
|
method: "POST",
|
|
body: JSON.stringify({ channels: currentChannelSettings() })
|
|
});
|
|
channels = payload.channels || currentChannelSettings();
|
|
document.querySelector("#statusChannelId").value = channels.statusChannelId || "";
|
|
document.querySelector("#mediaChannelId").value = channels.mediaChannelId || "";
|
|
return channels;
|
|
}
|
|
|
|
function readFileText(input) {
|
|
const file = input.files && input.files[0];
|
|
if (!file) return Promise.resolve({ name: "", text: "" });
|
|
return file.text().then((text) => ({ name: file.name, text }));
|
|
}
|
|
|
|
async function importMediaCsv() {
|
|
const moviesInput = document.querySelector("#moviesCsv");
|
|
const showsInput = document.querySelector("#showsCsv");
|
|
const [movies, shows] = await Promise.all([
|
|
readFileText(moviesInput),
|
|
readFileText(showsInput)
|
|
]);
|
|
|
|
if (!movies.text.trim() && !shows.text.trim()) {
|
|
throw new Error("Choose a Movies.csv or Shows.csv file before importing.");
|
|
}
|
|
|
|
setMediaMessage("Importing CSV files...");
|
|
const payload = await api("/api/media/import", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
moviesCsv: movies.text,
|
|
showsCsv: shows.text,
|
|
movieFileName: movies.name || "Movies.csv",
|
|
showFileName: shows.name || "Shows.csv"
|
|
})
|
|
});
|
|
mediaLibrary = normalizeMediaLibrary(payload.library);
|
|
renderMediaLibrary();
|
|
document.querySelector("#mediaMovieCount").textContent = payload.movieCount;
|
|
document.querySelector("#mediaShowCount").textContent = payload.showCount;
|
|
setMediaMessage(`Imported ${payload.movieCount} movies and ${payload.showCount} shows.`);
|
|
}
|
|
|
|
async function saveMediaLibrary() {
|
|
collectMediaLibrary();
|
|
const payload = await api("/api/media/library", {
|
|
method: "POST",
|
|
body: JSON.stringify(mediaLibrary)
|
|
});
|
|
mediaLibrary = normalizeMediaLibrary(payload.library);
|
|
renderMediaLibrary();
|
|
document.querySelector("#mediaMovieCount").textContent = payload.movieCount;
|
|
document.querySelector("#mediaShowCount").textContent = payload.showCount;
|
|
setMediaMessage("Saved library edits.");
|
|
return payload;
|
|
}
|
|
|
|
async function publishMedia() {
|
|
collectMediaLibrary();
|
|
setMediaMessage("Publishing library...");
|
|
const payload = await api("/api/media", {
|
|
method: "POST",
|
|
body: JSON.stringify({
|
|
channelId: document.querySelector("#mediaChannelId").value.trim() || channels.mediaChannelId,
|
|
movies: mediaLibrary.movies,
|
|
shows: mediaLibrary.shows
|
|
})
|
|
});
|
|
|
|
renderMediaStatus({
|
|
channelId: payload.channelId,
|
|
messageIds: payload.messageIds,
|
|
movieCount: payload.movieCount,
|
|
showCount: payload.showCount,
|
|
publishedAt: new Date().toISOString(),
|
|
library: mediaLibrary
|
|
});
|
|
setMediaMessage(`Published ${payload.movieCount} movies and ${payload.showCount} shows.`);
|
|
}
|
|
|
|
async function loadSession() {
|
|
const session = await api("/api/session");
|
|
csrfToken = session.csrfToken || "";
|
|
return session;
|
|
}
|
|
|
|
async function login() {
|
|
const username = document.querySelector("#username").value.trim();
|
|
const password = document.querySelector("#password").value;
|
|
const response = await fetch("/api/login", {
|
|
method: "POST",
|
|
credentials: "same-origin",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
const error = new Error(data.error || `Login failed: ${response.status}`);
|
|
error.status = response.status;
|
|
throw error;
|
|
}
|
|
|
|
csrfToken = data.csrfToken || "";
|
|
await loadStatus();
|
|
}
|
|
|
|
async function logout() {
|
|
await api("/api/logout", { method: "POST", body: "{}" }).catch(() => {});
|
|
csrfToken = "";
|
|
showLogin();
|
|
}
|
|
|
|
async function saveServices() {
|
|
services = collectServices().map(normalizeService);
|
|
await saveChannelSettings();
|
|
const payload = await api("/api/services", {
|
|
method: "POST",
|
|
body: JSON.stringify({ services })
|
|
});
|
|
results = new Map(payload.results.map((result) => [result.name, result]));
|
|
renderSummary({ services, results: payload.results, lastCheckedAt: new Date().toISOString() });
|
|
renderServices();
|
|
setMessage("Saved and updated Discord.");
|
|
}
|
|
|
|
async function checkNow() {
|
|
await saveChannelSettings();
|
|
const payload = await api("/api/check", { method: "POST", body: "{}" });
|
|
results = new Map(payload.results.map((result) => [result.name, result]));
|
|
renderSummary({ services, results: payload.results, lastCheckedAt: new Date().toISOString() });
|
|
renderServices();
|
|
setMessage("Checked services and updated Discord.");
|
|
}
|
|
|
|
document.querySelector("#loginButton").addEventListener("click", async () => {
|
|
try {
|
|
await login();
|
|
} catch (error) {
|
|
showLogin(error.message);
|
|
}
|
|
});
|
|
|
|
document.querySelector("#password").addEventListener("keydown", (event) => {
|
|
if (event.key === "Enter") {
|
|
document.querySelector("#loginButton").click();
|
|
}
|
|
});
|
|
|
|
document.querySelector("#refresh").addEventListener("click", () => {
|
|
loadStatus().catch((error) => setMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#save").addEventListener("click", () => {
|
|
saveServices().catch((error) => setMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#checkNow").addEventListener("click", () => {
|
|
checkNow().catch((error) => setMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#mediaRefresh").addEventListener("click", () => {
|
|
loadMediaStatus()
|
|
.then(() => setMediaMessage(""))
|
|
.catch((error) => setMediaMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#importMediaCsv").addEventListener("click", () => {
|
|
importMediaCsv().catch((error) => setMediaMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#saveMediaLibrary").addEventListener("click", () => {
|
|
saveMediaLibrary().catch((error) => setMediaMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#publishMedia").addEventListener("click", () => {
|
|
publishMedia().catch((error) => setMediaMessage(error.message, true));
|
|
});
|
|
|
|
document.querySelector("#logout").addEventListener("click", () => {
|
|
logout();
|
|
});
|
|
|
|
document.querySelectorAll("[data-view]").forEach((item) => {
|
|
item.addEventListener("click", () => {
|
|
showView(item.dataset.view);
|
|
if (item.dataset.view === "media") {
|
|
loadMediaStatus().catch((error) => setMediaMessage(error.message, true));
|
|
}
|
|
});
|
|
});
|
|
|
|
document.querySelectorAll("[data-media-tab]").forEach((button) => {
|
|
button.addEventListener("click", () => {
|
|
collectMediaLibrary();
|
|
activeMediaTab = button.dataset.mediaTab;
|
|
renderMediaLibrary();
|
|
});
|
|
});
|
|
|
|
document.querySelector("#addMovie").addEventListener("click", () => {
|
|
collectMediaLibrary();
|
|
activeMediaTab = "movies";
|
|
mediaLibrary.movies.push(normalizeMediaItem({ title: "New Movie" }, "movie"));
|
|
renderMediaLibrary();
|
|
});
|
|
|
|
document.querySelector("#addShow").addEventListener("click", () => {
|
|
collectMediaLibrary();
|
|
activeMediaTab = "shows";
|
|
mediaLibrary.shows.push(normalizeMediaItem({ title: "New Show" }, "show"));
|
|
renderMediaLibrary();
|
|
});
|
|
|
|
document.querySelector("#addService").addEventListener("click", () => {
|
|
services.push(normalizeService({ name: "New Service", url: "https://example.com" }));
|
|
renderServices();
|
|
});
|
|
|
|
servicesEl.addEventListener("click", (event) => {
|
|
const button = event.target.closest("[data-remove]");
|
|
if (!button) return;
|
|
services.splice(Number(button.dataset.remove), 1);
|
|
renderServices();
|
|
});
|
|
|
|
mediaLibraryEl.addEventListener("click", (event) => {
|
|
const button = event.target.closest("[data-remove-media]");
|
|
if (!button) return;
|
|
collectMediaLibrary();
|
|
mediaLibrary[activeMediaTab].splice(Number(button.dataset.removeMedia), 1);
|
|
renderMediaLibrary();
|
|
});
|
|
|
|
let draggedRow = null;
|
|
|
|
servicesEl.addEventListener("dragstart", (e) => {
|
|
const row = e.target.closest(".service-row");
|
|
if (!row) return;
|
|
draggedRow = row;
|
|
e.dataTransfer.effectAllowed = "move";
|
|
// Save current input values back to the services array before moving
|
|
services = collectServices();
|
|
});
|
|
|
|
servicesEl.addEventListener("dragover", (e) => {
|
|
e.preventDefault();
|
|
const row = e.target.closest(".service-row");
|
|
if (!row || row === draggedRow) return;
|
|
e.dataTransfer.dropEffect = "move";
|
|
row.classList.add("drag-over");
|
|
});
|
|
|
|
servicesEl.addEventListener("dragleave", (e) => {
|
|
const row = e.target.closest(".service-row");
|
|
if (row) row.classList.remove("drag-over");
|
|
});
|
|
|
|
servicesEl.addEventListener("drop", (e) => {
|
|
e.preventDefault();
|
|
const targetRow = e.target.closest(".service-row");
|
|
if (targetRow) targetRow.classList.remove("drag-over");
|
|
if (!draggedRow || !targetRow || draggedRow === targetRow) return;
|
|
|
|
const fromIndex = parseInt(draggedRow.dataset.index, 10);
|
|
const toIndex = parseInt(targetRow.dataset.index, 10);
|
|
|
|
const item = services.splice(fromIndex, 1)[0];
|
|
services.splice(toIndex, 0, item);
|
|
renderServices();
|
|
});
|
|
|
|
loadSession().then(loadStatus).catch((error) => {
|
|
if (error.status === 401) {
|
|
showLogin();
|
|
} else {
|
|
showLogin(error.message);
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|