fix minifying and reuse code
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { escapeHtml, formatBytes } from "./format.js";
|
||||
|
||||
const clientList = document.getElementById("client-list");
|
||||
const clientSearch = document.getElementById("client-search");
|
||||
const osFilter = document.getElementById("os-filter");
|
||||
@@ -275,20 +277,6 @@ function setOsBadge(os) {
|
||||
osBadge.classList.add("border-emerald-600", "text-emerald-300");
|
||||
}
|
||||
|
||||
function formatBytes(bytes) {
|
||||
const value = Number(bytes) || 0;
|
||||
if (value < 1024) return `${value} B`;
|
||||
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`;
|
||||
if (value < 1024 * 1024 * 1024) return `${(value / 1024 / 1024).toFixed(2)} MB`;
|
||||
return `${(value / 1024 / 1024 / 1024).toFixed(2)} GB`;
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
clientSearch.addEventListener("input", filterAndRenderClients);
|
||||
osFilter.addEventListener("change", filterAndRenderClients);
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { escapeHtml, formatBytes, timeAgo } from "./format.js";
|
||||
|
||||
const fileList = document.getElementById("file-list");
|
||||
const uploadSection = document.getElementById("upload-section");
|
||||
const uploadForm = document.getElementById("upload-form");
|
||||
@@ -6,32 +8,6 @@ const uploadMaxDownloads = document.getElementById("upload-max-downloads");
|
||||
|
||||
let currentUser = null;
|
||||
|
||||
function formatBytes(bytes) {
|
||||
if (bytes === 0) return "0 B";
|
||||
const k = 1024;
|
||||
const sizes = ["B", "KB", "MB", "GB", "TB"];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
|
||||
}
|
||||
|
||||
function timeAgo(ts) {
|
||||
const seconds = Math.floor((Date.now() - ts) / 1000);
|
||||
if (seconds < 60) return "just now";
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
if (hours < 24) return `${hours}h ago`;
|
||||
const days = Math.floor(hours / 24);
|
||||
if (days < 30) return `${days}d ago`;
|
||||
return new Date(ts).toLocaleDateString();
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = str;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
function getDownloadUrl(fileId) {
|
||||
return `${window.location.origin}/api/file-share/${fileId}/download`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
export function escapeHtml(value) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = value == null ? "" : String(value);
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
export function formatBytes(bytes, fractionDigits = 2) {
|
||||
const value = Number(bytes) || 0;
|
||||
if (value === 0) return "0 B";
|
||||
const units = ["B", "KB", "MB", "GB", "TB"];
|
||||
const unitIndex = Math.min(units.length - 1, Math.floor(Math.log(Math.abs(value)) / Math.log(1024)));
|
||||
const scaled = value / Math.pow(1024, unitIndex);
|
||||
return `${parseFloat(scaled.toFixed(fractionDigits))} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
export function formatDate(timestamp, empty = "") {
|
||||
const ts = Number(timestamp) || 0;
|
||||
if (!ts) return empty;
|
||||
return new Date(ts).toLocaleString();
|
||||
}
|
||||
|
||||
export function timeAgo(timestamp) {
|
||||
const seconds = Math.floor((Date.now() - Number(timestamp || 0)) / 1000);
|
||||
if (seconds < 60) return "just now";
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
if (hours < 24) return `${hours}h ago`;
|
||||
const days = Math.floor(hours / 24);
|
||||
if (days < 30) return `${days}d ago`;
|
||||
return new Date(timestamp).toLocaleDateString();
|
||||
}
|
||||
@@ -2047,6 +2047,14 @@ loadCurrentUser();
|
||||
|
||||
// console easter egg
|
||||
(function () {
|
||||
const runWhenIdle = (fn) => {
|
||||
if ("requestIdleCallback" in window) {
|
||||
window.requestIdleCallback(fn, { timeout: 5000 });
|
||||
return;
|
||||
}
|
||||
window.addEventListener("load", () => setTimeout(fn, 1500), { once: true });
|
||||
};
|
||||
|
||||
const consoleimg = {
|
||||
load: function (src, { size = 320, color = "transparent" } = {}) {
|
||||
const reader = new FileReader();
|
||||
@@ -2072,5 +2080,5 @@ loadCurrentUser();
|
||||
.catch((err) => console.warn(err.message));
|
||||
},
|
||||
};
|
||||
consoleimg.load("/assets/console.gif", { size: 320, color: "transparent" });
|
||||
runWhenIdle(() => consoleimg.load("/assets/console.gif", { size: 320, color: "transparent" }));
|
||||
})();
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
setDesktopNotificationsEnabled,
|
||||
requestDesktopNotificationPermission,
|
||||
} from "./notify-client.js";
|
||||
import { escapeHtml, formatBytes as formatSharedBytes, formatDate as formatSharedDate } from "./format.js";
|
||||
|
||||
const PREF_REFRESH_KEY = "overlord_refresh_interval_seconds";
|
||||
const NAV_MODE_KEY = "sb_mode";
|
||||
@@ -119,16 +120,8 @@ function showMessage(text, type = "ok") {
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = text == null ? "" : String(text);
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
function formatDate(timestamp) {
|
||||
const ts = Number(timestamp) || 0;
|
||||
if (!ts) return "-";
|
||||
return new Date(ts).toLocaleString();
|
||||
return formatSharedDate(timestamp, "-");
|
||||
}
|
||||
|
||||
function canManageClientBans(role) {
|
||||
@@ -2047,11 +2040,7 @@ function initThumbnailHandlers() {
|
||||
// ── Server Health ─────────────────────────────────────────────────────────
|
||||
|
||||
function formatBytes(bytes) {
|
||||
if (!bytes || bytes <= 0) return "0 B";
|
||||
const k = 1024;
|
||||
const sizes = ["B", "KB", "MB", "GB", "TB"];
|
||||
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
|
||||
return `${(bytes / Math.pow(k, i)).toFixed(i > 0 ? 1 : 0)} ${sizes[i]}`;
|
||||
return formatSharedBytes(bytes, 1);
|
||||
}
|
||||
|
||||
function formatUptime(totalSeconds) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
let currentUser = null;
|
||||
let users = [];
|
||||
|
||||
import { escapeHtml, formatDate } from "./format.js";
|
||||
|
||||
const usersTableBody = document.getElementById("users-table-body");
|
||||
const addUserBtn = document.getElementById("add-user-btn");
|
||||
const userModal = document.getElementById("user-modal");
|
||||
@@ -350,17 +352,6 @@ function attachActionListeners() {
|
||||
usersTableBody._actionListener = listener;
|
||||
}
|
||||
|
||||
function formatDate(timestamp) {
|
||||
const date = new Date(timestamp);
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement("div");
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
function showModal(title) {
|
||||
modalTitle.textContent = title;
|
||||
userModal.classList.remove("hidden");
|
||||
|
||||
Reference in New Issue
Block a user