Refactors loading function

* Initialize service component and handler in hooks
* Dynamically watch the config instead of a reload on each page
* Uses derived store for public server config
* Moves service data in its own page data property
* Reverts to a array to store group/services
This commit is contained in:
2023-08-13 13:59:31 +02:00
parent f23f268b60
commit edbbd2f0f6
13 changed files with 280 additions and 248 deletions

View File

@@ -1,99 +0,0 @@
import configData from '../config.yml';
export interface BrandConfig {
logo?: string;
icon?: string;
usemask?: boolean;
}
export interface SectionConfig extends BrandConfig {
title: string;
subtitle?: string;
}
export interface ServiceConfig extends SectionConfig {
url: string;
target?: string;
type?: string;
data?: Record<string, unknown>;
[x: string]: unknown;
}
export interface ServiceGroupConfig extends SectionConfig {
items: Record<string, ServiceConfig>;
[x: string]: unknown;
}
export interface Config extends SectionConfig {
services: Record<string, ServiceGroupConfig>;
[x: string]: unknown;
}
type DeepRequired<T> = T extends object
? {
[P in keyof T]: DeepRequired<T[P]>;
}
: T;
const requiredConfig: DeepRequired<Config> = {
title: '',
subtitle: '',
services: {
default: {
title: '',
items: {
default: {
title: '',
url: ''
}
}
}
}
};
export function mergeConfig(a: Config, b: any): Config {
return { ...a, ...b };
}
export const defaultConfig: Config = {
title: 'Flanders',
services: {}
};
type SPOJO = Record<string, unknown>;
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
const res: Type = { ...toStrip };
const referenceNames = Object.entries(reference).map(([key, value]) => key);
const allowAny: boolean = referenceNames.length == 1 && referenceNames[0] == 'default';
for (const [key, value] of Object.entries(res)) {
if (referenceNames.includes(key) == false && allowAny == false) {
// remove the object
delete res[key];
continue;
}
if (typeof value != 'object') {
continue;
}
// it is a child object, we strip it further
const stripped: SPOJO = {};
const childReference: SPOJO = reference[allowAny ? 'default' : key] as SPOJO;
stripped[key] = strip(value as SPOJO, childReference);
Object.assign(res, stripped);
}
return res;
}
export function stripPrivateFields(config: Config): Config {
return strip(config, requiredConfig);
}
export const config: Config = mergeConfig(defaultConfig, configData);
export const clientConfig: Config = stripPrivateFields(config);