feat: add global sidebar links management and settings UI

This commit is contained in:
jhartworks committed 2026-11-25 09:38:54 +01:00
1 parent 3203577a9d
commit f6f3c402cc
10 files changed
+463 -40

No files matched your search

+10
View File
@@ -26,6 +26,7 @@ import {
import {
deleteSelection,
getDashboard,
getGlobalSidebarLinks,
getPreferences,
getRoleMap,
getSelection,
@@ -33,6 +34,7 @@ import {
listSelections,
listUnits,
saveDashboard,
saveGlobalSidebarLinks,
savePreferences,
saveRole,
saveSelection,
@@ -969,6 +971,14 @@ app.put("/api/preferences", authenticateToken, wrap(async (req, res) => {
res.json(preferences);
}));
app.get("/api/settings/sidebar-links", wrap(async (_req, res) => {
res.json({ links: await getGlobalSidebarLinks(redis) });
}));
app.put("/api/settings/sidebar-links", authenticateToken, requireRoles("admin"), wrap(async (req, res) => {
res.json({ links: await saveGlobalSidebarLinks(redis, req.body?.links) });
}));
app.use((error, _req, res, _next) => {
console.error(error);
res.status(400).json({ error: error.message || "Unexpected error." });
+34
View File
@@ -1,10 +1,12 @@
const SELECTION_SET_KEY = "selection:ids";
import { DEFAULT_UNITS as DEFAULT_UNIT_DEFINITIONS, normalizeUnitSymbol as canonicalUnitSymbol } from "../../../shared/engineering-units.mjs";
import { DEFAULT_MAX_TREND_SERIES, normalizeMaxTrendSeries } from "../../../shared/trend-preferences.mjs";
import { normalizeSidebarLink } from "../../../shared/sidebar-links.mjs";
const SELECTION_SEQ_KEY = "selection:seq";
const ROLE_SET_KEY = "role:names";
const UNIT_SET_KEY = "unit:symbols";
const GLOBAL_SIDEBAR_LINKS_KEY = "settings:sidebar-links";
export const DEFAULT_ROLE_DEFINITIONS = [
{
@@ -248,6 +250,35 @@ export async function saveDashboard(redis, userId, dashboard) {
return payload;
}
function readSidebarLinks(raw) {
const links = [];
for (const link of Array.isArray(raw) ? raw : []) {
try {
links.push(normalizeSidebarLink(link));
} catch {
// Ignore invalid links saved by older versions.
}
}
return links;
}
function validateSidebarLinks(links) {
if (!Array.isArray(links)) {
throw new Error("Seitenleisten-Links müssen als Liste übergeben werden.");
}
return links.map(normalizeSidebarLink);
}
export async function getGlobalSidebarLinks(redis) {
return readSidebarLinks(safeParse(await redis.get(GLOBAL_SIDEBAR_LINKS_KEY)));
}
export async function saveGlobalSidebarLinks(redis, input) {
const links = validateSidebarLinks(input);
await redis.set(GLOBAL_SIDEBAR_LINKS_KEY, JSON.stringify(links));
return links;
}
export async function getPreferences(redis, userId) {
const stored = userId == null ? null : safeParse(await redis.get(`preferences:${userId}`));
return {
@@ -257,10 +288,12 @@ export async function getPreferences(redis, userId) {
trendLineWidth: 1.8,
...stored,
maxTrendSeries: normalizeMaxTrendSeries(stored?.maxTrendSeries, DEFAULT_MAX_TREND_SERIES),
sidebarLinks: readSidebarLinks(stored?.sidebarLinks),
};
}
export async function savePreferences(redis, userId, preferences) {
const sidebarLinks = preferences.sidebarLinks === undefined ? undefined : validateSidebarLinks(preferences.sidebarLinks);
const current = await getPreferences(redis, userId);
const payload = {
...current,
@@ -270,6 +303,7 @@ export async function savePreferences(redis, userId, preferences) {
sidebarPinned: typeof preferences.sidebarPinned === "boolean" ? preferences.sidebarPinned : current.sidebarPinned,
trendLineWidth: Number.isFinite(Number(preferences.trendLineWidth)) ? Number(preferences.trendLineWidth) : Number(current.trendLineWidth || 1.8),
maxTrendSeries: normalizeMaxTrendSeries(preferences.maxTrendSeries, current.maxTrendSeries),
sidebarLinks: sidebarLinks ?? current.sidebarLinks,
updatedAt: new Date().toISOString(),
};
await redis.set(`preferences:${userId}`, JSON.stringify(payload));