import {LayoutConfig} from "../types/LayoutConfig"; const NO_LAYOUT_CONFIG: LayoutConfig = { id: "", schedule: { always: true }, panels: [] } export class LayoutService { static configs: LayoutConfig[] = []; static async init(): Promise<void> { try { const activeConfigs = await fetch("/activeConfigs.json").then(content => content.json()); const configFetches = (activeConfigs as string[]) .map(configPath => fetch(configPath).then(content => content.json())); LayoutService.configs = await Promise.all(configFetches); } catch (e) { console.error("LayoutService could not init", e) } } static getActiveLayout(): LayoutConfig { const now = new Date(); const activeConfigs = this.configs.filter(config => { if(config.schedule.always) { return true; } return config.schedule.times.reduce((accu, curr) => { if(accu) return true; return (new Date(curr.from) <= now && new Date(curr.to) >= now); }, false); }); console.log(activeConfigs) /* ToDo: This is not great, as it assumes there is always an active layout. If you don't configure this correctly, consider yourself warned now and don't blame me */ return activeConfigs.at(0) ?? NO_LAYOUT_CONFIG; } }