Refactoring: simplifing the peertube context.

This commit is contained in:
John Livingston
2024-06-12 16:26:35 +02:00
parent 268c60d699
commit 2c3739f633
13 changed files with 73 additions and 157 deletions

View File

@ -3,7 +3,6 @@
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { ChannelConfiguration } from 'shared/lib/types'
import { TemplateResult, html, nothing } from 'lit'
import { customElement, property, state } from 'lit/decorators.js'
@ -12,17 +11,12 @@ import { Task } from '@lit/task'
import { ChannelDetailsService } from '../services/channel-details'
import { provide } from '@lit/context'
import { channelConfigurationContext, channelDetailsServiceContext } from '../contexts/channel'
import { registerClientOptionsContext } from '../../lib/contexts/peertube'
import { LivechatElement } from '../../lib/elements/livechat'
import { ValidationError, ValidationErrorType } from '../../lib/models/validation'
import { classMap } from 'lit/directives/class-map.js'
@customElement('livechat-channel-configuration')
export class ChannelConfigurationElement extends LivechatElement {
@provide({ context: registerClientOptionsContext })
@property({ attribute: false })
public registerClientOptions?: RegisterClientOptions
@property({ attribute: false })
public channelId?: number
@ -48,14 +42,12 @@ export class ChannelConfigurationElement extends LivechatElement {
protected _initTask (): Task {
return new Task(this, {
task: async ([registerClientOptions]) => {
if (registerClientOptions) {
this._channelDetailsService = new ChannelDetailsService(registerClientOptions)
this._channelConfiguration = await this._channelDetailsService.fetchConfiguration(this.channelId ?? 0)
this._actionDisabled = false // in case of reset
}
task: async () => {
this._channelDetailsService = new ChannelDetailsService(this.ptOptions)
this._channelConfiguration = await this._channelDetailsService.fetchConfiguration(this.channelId ?? 0)
this._actionDisabled = false // in case of reset
},
args: () => [this.registerClientOptions]
args: () => []
})
}
@ -74,10 +66,9 @@ export class ChannelConfigurationElement extends LivechatElement {
this._channelConfiguration.configuration)
.then(() => {
this._validationError = undefined
this.registerClientOptions?.peertubeHelpers.translate(LOC_SUCCESSFULLY_SAVED).then((msg) => {
this.registerClientOptions
?.peertubeHelpers.notifier.info(msg)
})
this.ptTranslate(LOC_SUCCESSFULLY_SAVED).then((msg) => {
this.ptNotifier.info(msg)
}, () => {})
this.requestUpdate('_validationError')
})
.catch(async (error: Error) => {
@ -86,10 +77,10 @@ export class ChannelConfigurationElement extends LivechatElement {
this._validationError = error
}
console.warn(`A validation error occurred in saving configuration. ${error.name}: ${error.message}`)
this.registerClientOptions?.peertubeHelpers.notifier.error(
this.ptNotifier.error(
error.message
? error.message
: await this.registerClientOptions.peertubeHelpers.translate('error')
: await this.ptTranslate(LOC_ERROR)
)
this.requestUpdate('_validationError')
})

View File

@ -2,11 +2,9 @@
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { ChannelEmojisConfiguration } from 'shared/lib/types'
import type { DynamicFormHeader, DynamicFormSchema } from '../../lib/elements/dynamic-table-form'
import { LivechatElement } from '../../lib/elements/livechat'
import { registerClientOptionsContext } from '../../lib/contexts/peertube'
import { ChannelDetailsService } from '../services/channel-details'
import { channelDetailsServiceContext } from '../contexts/channel'
import { maxEmojisPerChannel } from 'shared/lib/emojis'
@ -22,10 +20,6 @@ import { html } from 'lit'
*/
@customElement('livechat-channel-emojis')
export class ChannelEmojisElement extends LivechatElement {
@provide({ context: registerClientOptionsContext })
@property({ attribute: false })
public registerClientOptions?: RegisterClientOptions
@property({ attribute: false })
public channelId?: number
@ -159,13 +153,10 @@ export class ChannelEmojisElement extends LivechatElement {
protected _initTask (): Task {
return new Task(this, {
task: async () => {
if (!this.registerClientOptions) {
throw new Error('Missing client options')
}
if (!this.channelId) {
throw new Error('Missing channelId')
}
this._channelDetailsService = new ChannelDetailsService(this.registerClientOptions)
this._channelDetailsService = new ChannelDetailsService(this.ptOptions)
this._channelEmojisConfiguration = await this._channelDetailsService.fetchEmojisConfiguration(this.channelId)
this._actionDisabled = false // in case of reset
},
@ -182,11 +173,9 @@ export class ChannelEmojisElement extends LivechatElement {
private async _saveEmojis (ev?: Event): Promise<void> {
ev?.preventDefault()
const peertubeHelpers = this.registerClientOptions?.peertubeHelpers
if (!peertubeHelpers) { return } // Should not happen
if (!this._channelDetailsService || !this._channelEmojisConfiguration || !this.channelId) {
peertubeHelpers.notifier.error(await peertubeHelpers.translate(LOC_ERROR))
this.ptNotifier.error(await this.ptTranslate(LOC_ERROR))
return
}
@ -194,7 +183,7 @@ export class ChannelEmojisElement extends LivechatElement {
this._actionDisabled = true
await this._channelDetailsService.saveEmojisConfiguration(this.channelId, this._channelEmojisConfiguration.emojis)
this._validationError = undefined
peertubeHelpers.notifier.info(await peertubeHelpers.translate(LOC_SUCCESSFULLY_SAVED))
this.ptNotifier.info(await this.ptTranslate(LOC_SUCCESSFULLY_SAVED))
this.requestUpdate('_validationError')
} catch (error) {
this._validationError = undefined
@ -205,8 +194,8 @@ export class ChannelEmojisElement extends LivechatElement {
msg = error.message
}
}
msg ??= await peertubeHelpers.translate(LOC_ERROR)
peertubeHelpers.notifier.error(msg)
msg ??= await this.ptTranslate(LOC_ERROR)
this.ptNotifier.error(msg)
this.requestUpdate('_validationError')
} finally {
this._actionDisabled = false
@ -282,11 +271,11 @@ export class ChannelEmojisElement extends LivechatElement {
this.requestUpdate('_channelEmojisConfiguration')
this.registerClientOptions?.peertubeHelpers.notifier.info(
await this.registerClientOptions?.peertubeHelpers.translate(LOC_ACTION_IMPORT_EMOJIS_INFO)
this.ptNotifier.info(
await this.ptTranslate(LOC_ACTION_IMPORT_EMOJIS_INFO)
)
} catch (err: any) {
this.registerClientOptions?.peertubeHelpers.notifier.error(err.toString())
this.ptNotifier.error(err.toString())
} finally {
this._actionDisabled = false
}
@ -322,7 +311,7 @@ export class ChannelEmojisElement extends LivechatElement {
a.remove()
} catch (err: any) {
console.error(err)
this.registerClientOptions?.peertubeHelpers.notifier.error(err.toString())
this.ptNotifier.error(err.toString())
} finally {
this._actionDisabled = false
}

View File

@ -2,24 +2,18 @@
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import { html } from 'lit'
import { customElement, property, state } from 'lit/decorators.js'
import { customElement, state } from 'lit/decorators.js'
import { ptTr } from '../../lib/directives/translation'
import { Task } from '@lit/task'
import type { ChannelLiveChatInfos } from 'shared/lib/types'
import { ChannelDetailsService } from '../services/channel-details'
import { provide } from '@lit/context'
import { channelDetailsServiceContext } from '../contexts/channel'
import { registerClientOptionsContext } from '../../lib/contexts/peertube'
import { LivechatElement } from '../../lib/elements/livechat'
@customElement('livechat-channel-home')
export class ChannelHomeElement extends LivechatElement {
@provide({ context: registerClientOptionsContext })
@property({ attribute: false })
public registerClientOptions?: RegisterClientOptions
@state()
public _channels?: ChannelLiveChatInfos[]
@ -30,19 +24,17 @@ export class ChannelHomeElement extends LivechatElement {
public _formStatus: boolean | any = undefined
private readonly _asyncTaskRender = new Task(this, {
task: async ([registerClientOptions]) => {
task: async () => {
// Getting the current username in localStorage. Don't know any cleaner way to do.
const username = window.localStorage.getItem('username')
if (!username) {
throw new Error('Can\'t get the current username.')
}
if (registerClientOptions) {
this._channelDetailsService = new ChannelDetailsService(registerClientOptions)
this._channels = await this._channelDetailsService.fetchUserChannels(username)
}
this._channelDetailsService = new ChannelDetailsService(this.ptOptions)
this._channels = await this._channelDetailsService.fetchUserChannels(username)
},
args: () => [this.registerClientOptions]
args: () => []
})
protected override render = (): unknown => {

View File

@ -2,19 +2,13 @@
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import { registerClientOptionsContext } from '../../lib/contexts/peertube'
import { LivechatElement } from '../../lib/elements/livechat'
import { ptTr } from '../../lib/directives/translation'
import { html, TemplateResult } from 'lit'
import { customElement, property } from 'lit/decorators.js'
import { consume } from '@lit/context'
@customElement('livechat-channel-tabs')
export class ChannelHomeElement extends LivechatElement {
@consume({ context: registerClientOptionsContext, subscribe: true })
public registerClientOptions?: RegisterClientOptions
@property({ attribute: false })
public channelId?: number