Pagination just landed hardcoded to 20 rows everywhere; add a way to change that instead of leaving it fixed for every table in the app. pageSize joins dateFormat/timeFormat on the existing display settings object (server-side default 20, 5-500 range enforced by the PUT schema) rather than becoming its own settings section, since it's the same kind of thing -- an admin-configured, globally-applied display preference read by every signed-in role via the already-public GET /api/settings/display endpoint, same as the date/time format already works. Renamed the "Date & Time" settings tab/page/route to "Display" (still just one component, now covering both date/time format and table pagination) since its scope no longer matches the old name -- kept Settings.tsx's usual pattern of one page per concern rather than adding a second, oddly-scoped tab just for one number field. New web/src/utils/pageSize.ts mirrors utils/date.ts's existing module-level "set once at startup, read anywhere without prop- drilling" pattern; usePagination()'s pageSize parameter now defaults to getPageSize() instead of a literal 20, evaluated fresh on every call so it picks up a saved change without touching any of the ten pages already using the hook. Verified server-side against a temp SQLite DB: pageSize defaults to 20, a partial update sets it without disturbing dateFormat/timeFormat and vice versa, and it persists across a fresh settings read. Also checked the default-parameter mechanics directly (re-evaluates the global value on every call rather than capturing it once, and an explicit override still wins). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
143 lines
4.0 KiB
TypeScript
143 lines
4.0 KiB
TypeScript
import { sql } from "drizzle-orm";
|
|
import { db } from "../db/client.js";
|
|
import { settings } from "../db/schema.js";
|
|
|
|
export interface GotifySettings {
|
|
enabled: boolean;
|
|
url: string;
|
|
token: string;
|
|
priority: number;
|
|
}
|
|
|
|
export interface NtfySettings {
|
|
enabled: boolean;
|
|
url: string;
|
|
topic: string;
|
|
token: string;
|
|
priority: number;
|
|
}
|
|
|
|
export interface SmtpSettings {
|
|
enabled: boolean;
|
|
host: string;
|
|
port: number;
|
|
secure: boolean;
|
|
username: string;
|
|
password: string;
|
|
from: string;
|
|
to: string;
|
|
}
|
|
|
|
export interface WebhookSettings {
|
|
enabled: boolean;
|
|
url: string;
|
|
secret: string;
|
|
}
|
|
|
|
export interface NotificationEvents {
|
|
dnsAdd: boolean;
|
|
dnsUpdate: boolean;
|
|
dnsDelete: boolean;
|
|
secretCheck: boolean;
|
|
tailscaleKeyCheck: boolean;
|
|
secretCheckTime: string; // "HH:MM" — shared by the secret-expiry and Tailscale key-expiry checks
|
|
timezone: string;
|
|
}
|
|
|
|
export type ProviderColors = Record<string, string>;
|
|
export type IntegrationColors = Record<string, string>;
|
|
|
|
export type DateFormat = "ymd" | "dmy" | "mdy";
|
|
export type TimeFormat = "24h" | "12h";
|
|
|
|
export interface DisplaySettings {
|
|
dateFormat: DateFormat;
|
|
timeFormat: TimeFormat;
|
|
/** Rows per page for every paginated table in the app. */
|
|
pageSize: number;
|
|
}
|
|
|
|
export interface AppSettings {
|
|
gotify: GotifySettings;
|
|
ntfy: NtfySettings;
|
|
smtp: SmtpSettings;
|
|
webhook: WebhookSettings;
|
|
notifications: NotificationEvents;
|
|
providerColors: ProviderColors;
|
|
integrationColors: IntegrationColors;
|
|
display: DisplaySettings;
|
|
}
|
|
|
|
const DEFAULTS: AppSettings = {
|
|
gotify: { enabled: false, url: "", token: "", priority: 5 },
|
|
ntfy: { enabled: false, url: "https://ntfy.sh", topic: "", token: "", priority: 3 },
|
|
smtp: { enabled: false, host: "", port: 587, secure: false, username: "", password: "", from: "", to: "" },
|
|
webhook: { enabled: false, url: "", secret: "" },
|
|
notifications: {
|
|
dnsAdd: true,
|
|
dnsUpdate: true,
|
|
dnsDelete: true,
|
|
secretCheck: true,
|
|
tailscaleKeyCheck: true,
|
|
secretCheckTime: "08:00",
|
|
timezone: "UTC",
|
|
},
|
|
providerColors: {},
|
|
integrationColors: {},
|
|
display: { dateFormat: "ymd", timeFormat: "24h", pageSize: 20 },
|
|
};
|
|
|
|
const KEYS = Object.keys(DEFAULTS) as (keyof AppSettings)[];
|
|
|
|
export async function getSettings(): Promise<AppSettings> {
|
|
const rows = await db.select().from(settings);
|
|
const byKey = new Map(rows.map((r) => [r.key, r.value]));
|
|
|
|
const result = {} as AppSettings;
|
|
for (const key of KEYS) {
|
|
const raw = byKey.get(key);
|
|
let parsed: Record<string, unknown> = {};
|
|
if (raw) {
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch {
|
|
parsed = {};
|
|
}
|
|
}
|
|
(result[key] as Record<string, unknown>) = { ...(DEFAULTS[key] as object), ...parsed };
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export type AppSettingsPatch = { [K in keyof AppSettings]?: Partial<AppSettings[K]> };
|
|
|
|
export async function updateSettings(partial: AppSettingsPatch): Promise<AppSettings> {
|
|
const current = await getSettings();
|
|
|
|
for (const key of Object.keys(partial) as (keyof AppSettings)[]) {
|
|
if (!KEYS.includes(key)) continue;
|
|
const merged = { ...(current[key] as object), ...(partial[key] as object) };
|
|
const value = JSON.stringify(merged);
|
|
await db
|
|
.insert(settings)
|
|
.values({ key, value })
|
|
.onConflictDoUpdate({ target: settings.key, set: { value, updatedAt: sql`(current_timestamp)` } });
|
|
}
|
|
|
|
return getSettings();
|
|
}
|
|
|
|
/** Small internal key/value slot for scheduler bookkeeping, outside the AppSettings shape. */
|
|
export async function getInternalFlag(key: string): Promise<string | null> {
|
|
const [row] = await db.select().from(settings).where(sql`${settings.key} = ${`_internal:${key}`}`).limit(1);
|
|
return row?.value ?? null;
|
|
}
|
|
|
|
export async function setInternalFlag(key: string, value: string): Promise<void> {
|
|
const fullKey = `_internal:${key}`;
|
|
await db
|
|
.insert(settings)
|
|
.values({ key: fullKey, value })
|
|
.onConflictDoUpdate({ target: settings.key, set: { value, updatedAt: sql`(current_timestamp)` } });
|
|
}
|