diff --git a/src/lib/components/partials/login.svelte b/src/lib/components/partials/login.svelte index d235bf9..0ca9eb2 100644 --- a/src/lib/components/partials/login.svelte +++ b/src/lib/components/partials/login.svelte @@ -43,7 +43,7 @@ {#if browser && window?.nostr}
- {#if mrp?.nostr && (typeof mrp?.nostr?.user === 'undefined' || mrp?.nostr?.user === null)} + {#if mrp?.nostr && (typeof mrp?.nostr?.user === 'undefined' || mrp?.nostr?.user == null)} {:else if mrp?.nostr?.user?.profile}
diff --git a/src/lib/core/MRPBlockLoader.ts b/src/lib/core/MRPBlockLoader.ts index ba24067..b98947c 100644 --- a/src/lib/core/MRPBlockLoader.ts +++ b/src/lib/core/MRPBlockLoader.ts @@ -1,7 +1,8 @@ import type { R } from "vitest/dist/reporters-P7C2ytIv.js" -import type { MRPConfig } from "./MRPConfig" +import type { MRPConfig, ConfigBlocks } from "./MRPConfig" import type { MRPState } from "./MRP" import { MRPData } from "./MRPData" +import { AppConfig } from "./kinds/app-config" type Components = { [key: string]: string | boolean @@ -27,11 +28,10 @@ export class BlockLoader extends MRPData { // 'feed': 'feed', } private $: MRPState - private _componentsDef: Components = {} private _components: Record = {} - private _config: MRPConfig | undefined + private _config: MRPConfig - constructor($state: MRPState, config: MRPConfig | undefined){ + constructor($state: MRPState, config: MRPConfig){ super($state.signal, 'blockLoader') this.$ = $state this._config = config @@ -39,27 +39,31 @@ export class BlockLoader extends MRPData { async init(){ this.begin() - if(this.config?.componentVisible && Object.keys(this.config?.componentVisible)?.length){ - this.config = { ...config.componentVisible, ...this.defaultComponents } - } - else { - console.log('loading default components') - this._componentsDef = this.defaultComponents - } await this.loadNativeComponents().catch(BlockLoader.errorHandler) } - private set componentDef( component: ComponentOption ){ - if(!component?.url){ - this._componentsDef[component.name] = this.nativeComponentPath(component.name) - } - else { - this._componentsDef[component.name] = component.url + async loadAllComponents(){ + await this.loadNativeComponents().catch(BlockLoader.errorHandler) + await this.loadRemoteComponents().catch(BlockLoader.errorHandler) + } + + async loadNativeComponents(){ + console.log(this.config.event.blocks) + for(let name in this.config.event.blocks){ + console.log(`tryinng to load`, name) + if(!this.config.event.blocks[name].enabled || this.config.event.blocks[name].custom === true) continue; + await this.loadNativeComponent(name).catch(BlockLoader.errorHandler) } } - private set component( component: $Component ){ - this._components[component.name] = component.module + async loadRemoteComponents(){ + for(let name in this.config.event.blocks){ + const block = this.config.event.blocks[name] + if(!block.enabled || block.custom === false) + continue; + if(block.customType === 'url') + await this.loadRemoteComponent(name, block.customUrl).catch(BlockLoader.errorHandler) + } } nativeComponentPath(name: string): string { @@ -67,56 +71,44 @@ export class BlockLoader extends MRPData { } async addComponent(name: string, url?: string, native: boolean = true){ - this.componentDef = { - name: name, - url: url - } as ComponentOption if(native) await this.loadNativeComponent(name).catch(BlockLoader.errorHandler) else if(url) await this.loadRemoteComponent(name, url).catch(BlockLoader.errorHandler) + this.config.event.setBlock(name, { enabled: true, order: Object.keys(this.config.event.blocks).length, options: {} }) } private async loadNativeComponent(name: string): Promise { + const block = this.config.event.blocks?.[name] + console.log('block', block) let $component; - if(this._componentsDef[name]){ - $component = await import(`../components/blocks/${this._componentsDef[name]}/${this._componentsDef[name]}.svelte`).catch(BlockLoader.errorHandler) + if(block){ + console.log('loading native component:', name) + $component = await import(`../components/blocks/${name}/${name}.svelte`).catch(BlockLoader.errorHandler) this._components[name] = $component?.default? $component.default: $component } return $component } private async loadRemoteComponent(name: string, url: string): Promise { + const block = this.config.event.blocks?.[name] let $component; - if(this._componentsDef[name]){ + if(block){ $component = await import(/* @vite-ignore */ url).catch(BlockLoader.errorHandler) this._components[name] = $component?.default? $component.default: $component } return $component } - async loadAllComponents(){ - await this.loadNativeComponents().catch(BlockLoader.errorHandler) - await this.loadRemoteComponents().catch(BlockLoader.errorHandler) - } - async loadNativeComponents(){ - for(let name in this._componentsDef){ - await this.loadNativeComponent(name).catch(BlockLoader.errorHandler) - } - } - async loadRemoteComponents(){ - for(let name in this._componentsDef){ - await this.loadNativeComponent(name).catch(BlockLoader.errorHandler) - } - } + static errorHandler(error: Error){ console.error(`BlockLoader: ${error}`) } - get config(): MRPConfig | undefined { + get config(): MRPConfig { return this._config } diff --git a/src/lib/core/MRPConfig.ts b/src/lib/core/MRPConfig.ts index 47037f6..39f2feb 100644 --- a/src/lib/core/MRPConfig.ts +++ b/src/lib/core/MRPConfig.ts @@ -8,37 +8,37 @@ import { MRPUser } from "./MRPUser"; export class MRPConfig extends MRPData { - private readonly kind: NDKKind.AppSpecificData; private readonly namespace: `myrelay.page`; private _$: MRPState; private _type: string; //operator, user - private _event: AppConfig | null; - private _data: any; - private _pubkey: string | undefined; - private _relay: string | undefined + private pubkey: string | undefined; + private relay: string | undefined + + public event: AppConfig; constructor($state: MRPState, relay?: string, pubkey?: string){ super($state.signal, 'config') this._$ = $state - this._pubkey = pubkey - this._relay = relay + this.pubkey = pubkey + this.relay = relay } async init(){ this.begin() console.log('fetching config') - const result = await this.fetch() + let result = await this.fetch() console.log('fetched config', result) - if(result === null) return this.complete(false) + if(this.event == null) result = this.create() + this.event = result as AppConfig + console.log(this.event) this.complete(true) } - create(){ - if(this?.event) return - if(!this.$?.ndk) return - this.event = new AppConfig(this.$.ndk) + create(): AppConfig { + this.event = new AppConfig(this.$.ndk as NDK) this.$.signal.emit('config:created', this.event) + return this.event } async fetch(): Promise { @@ -49,7 +49,7 @@ export class MRPConfig extends MRPData { .fetchEvent( { kinds: [NDKKind.AppSpecificData], authors: [this.pubkey], '#d': [this.configKey()] }, undefined, - this._relay? NDKRelaySet.fromRelayUrls([this._relay], this?.$?.ndk as NDK): undefined + this.relay? NDKRelaySet.fromRelayUrls([this.relay], this?.$?.ndk as NDK): undefined ) ) as AppConfig if(config === null) return null @@ -58,18 +58,6 @@ export class MRPConfig extends MRPData { return this.event } - update(key: string, config: Record): boolean{ - if(!this?.event) return false - this.data[key] = config; - this.event.set(key, this.data[key]) - this.$.signal.emit(`config:updated`, this.type, key, this.event) - return true - } - - get(key: string): any { - return this.event?.get(key) - } - configKey(): string { return `${this?.relay}@myrelay.page` } @@ -86,11 +74,7 @@ export class MRPConfig extends MRPData { formatType(): string { return `${this.type.toLowerCase()}@${this.namespace}`.toLowerCase() } - - get relay(): string | undefined { - return this._relay - } - + get type(){ return this._type } @@ -99,14 +83,6 @@ export class MRPConfig extends MRPData { this._type = type } - get event(): AppConfig | null { - return this._event - } - - private set event(event: AppConfig){ - this._event = event - } - private get $(): MRPState{ return this._$ } @@ -115,19 +91,5 @@ export class MRPConfig extends MRPData { this._$ = $ } - get data(){ - return this._data - } - private set data(data: any){ - this._data = data - } - - get pubkey(): string | undefined{ - return this._pubkey - } - - private set pubkey(pubkey: string){ - this._pubkey = pubkey - } } \ No newline at end of file diff --git a/src/lib/core/MRPRelay.ts b/src/lib/core/MRPRelay.ts index 74858f5..9522c36 100644 --- a/src/lib/core/MRPRelay.ts +++ b/src/lib/core/MRPRelay.ts @@ -37,10 +37,10 @@ export class MRPRelay extends MRPData { } private async initOwner(stage: MRPStage, status: MRPStatus){ - if(MRPStatus.Failure === status || this.config?.get('relay-feed')?.enabled) { + if(MRPStatus.Failure === status || this.config.event.isBlockEnabled('relay-feed')) { this.fetchRelayNotes() } - if(MRPStatus.Failure === status || this.config?.get('owner')?.enabled) { + if(MRPStatus.Failure === status || this.config.event.isBlockEnabled('operator-profile')) { this.owner = new MRPUser(this.$, this.info.pubkey, 'operator') await this.owner.init() await this.owner.fetchNotes() diff --git a/src/lib/core/config/config.ts b/src/lib/core/config/config.ts index d290204..ad80640 100644 --- a/src/lib/core/config/config.ts +++ b/src/lib/core/config/config.ts @@ -25,7 +25,7 @@ export class MRPConfig extends MRPData { async init(){ this.begin() const result = await this.fetch() - if(result === null) return this.complete(false) + if(result == null) return this.complete(false) this.complete(true) } @@ -40,7 +40,7 @@ export class MRPConfig extends MRPData { if(!this.$?.ndk) return null if(!this.pubkey) return null const config: AppConfig | null = (await this.$.ndk.fetchEvent({ kinds: [this.kind], authors: [this.pubkey] })) as AppConfig - if(config === null) return null + if(config == null) return null this.event = new AppConfig(this.$.ndk, config?.rawEvent()) this.$.signal.emit(`config:fetched`, this.type, this.event) return this.event diff --git a/src/lib/core/kinds/app-config.ts b/src/lib/core/kinds/app-config.ts index e97708c..bda9076 100644 --- a/src/lib/core/kinds/app-config.ts +++ b/src/lib/core/kinds/app-config.ts @@ -25,25 +25,29 @@ type Json = { type Theme = string -type ConfigTheme = { +export type ConfigTheme = { native: Theme, custom: boolean, customUrl: string, - customType: 'url' + customType: string } - -type ConfigBlock = { +export type ConfigBlock = { enabled: boolean, order: number, - options?: any + options?: { + [key: string]: any + }, + custom?: boolean, + customUrl?: string, + customType?: string } -type ConfigBlocks = { +export type ConfigBlocks = { [key: string]: ConfigBlock } -type Config = { +export type ConfigObj = { general: { login: { enabled: boolean, @@ -67,7 +71,7 @@ const defaults = { theme: { native: 'classic', custom: false, - customType: 'url', + customType: '', customUrl: '', } }, @@ -92,39 +96,34 @@ export class AppConfig extends NDKEvent { constructor( ndk: NDK, event?: NostrEvent ){ super(ndk, event) - this.kind ?? NDKKind.AppSpecificData + this.kind = NDKKind.AppSpecificData console.log('AppConfig', this.content) - if(!this?.content || this.content.length === 0) return - this._config = { ...defaults, ...this.parseConfig() } + this.config = { ...defaults, ...this.parseConfig() } + console.log('parsed', this.config) } static from( ndk: NDK, rawEvent: NostrEvent ){ return new AppConfig(ndk, rawEvent) } - private parseConfig(): Json { - let json: Json; - if(this?.content?.length === 0) { - json = {} + private parseConfig(): ConfigObj { + let json: ConfigObj = defaults + try { + json = jsonpack.unpack(this.content) } - else { - try { - json = jsonpack.unpack(this.content) - } - catch(e){ - console.error(`Error parsing content: ${e}`) - } + catch(e){ + console.error(`Error parsing content: ${e}`) } return json } - set config(config: Config) { + set config(config: ConfigObj) { this._config = this.deepProxy(config, () => { this.content = jsonpack.pack(config); }); } - get config(): Config { + get config(): ConfigObj { return this._config; } @@ -140,8 +139,8 @@ export class AppConfig extends NDKEvent { return this.config.general } - private deepProxy(obj: Config, callback: () => void): Config { - const handler: ProxyHandler = { + private deepProxy(obj: ConfigObj, callback: () => void): ConfigObj { + const handler: ProxyHandler = { set: (target, property, value) => { target[property] = (value && typeof value === 'object') ? this.deepProxy(value, callback) : value; callback(); // Call the callback whenever a property is set diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 45dd966..dd4219b 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -41,7 +41,7 @@ const _mrp = new MyRelayPage(url); _mrp.$.signal.on('state:changed', function(){ - console.log('state:changed', ...arguments) + // console.log('state:changed', ...arguments) mrp.set(_mrp); }); mrp.set(_mrp);