feat: add global sidebar links management and settings UI
This commit is contained in:
1 parent
3203577a9d
commit
f6f3c402cc
10 files changed
+463
-40
No files matched your search
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user