2024-05-28 15:56:24 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
2024-06-04 14:39:25 +00:00
|
|
|
import type { ChannelEmojisConfiguration } from 'shared/lib/types'
|
2024-06-05 13:56:03 +00:00
|
|
|
import type { DynamicFormHeader, DynamicFormSchema } from '../../lib/elements/dynamic-table-form'
|
2024-05-28 15:56:24 +00:00
|
|
|
import { LivechatElement } from '../../lib/elements/livechat'
|
|
|
|
import { registerClientOptionsContext } from '../../lib/contexts/peertube'
|
|
|
|
import { ChannelDetailsService } from '../services/channel-details'
|
|
|
|
import { channelDetailsServiceContext } from '../contexts/channel'
|
2024-06-06 09:36:07 +00:00
|
|
|
import { maxEmojisPerChannel } from 'shared/lib/emojis'
|
2024-06-04 14:39:25 +00:00
|
|
|
import { ptTr } from '../../lib/directives/translation'
|
2024-06-05 13:56:03 +00:00
|
|
|
import { ValidationError } from '../../lib/models/validation'
|
2024-05-28 15:56:24 +00:00
|
|
|
import { Task } from '@lit/task'
|
2024-06-05 13:56:03 +00:00
|
|
|
import { customElement, property, state } from 'lit/decorators.js'
|
2024-05-28 15:56:24 +00:00
|
|
|
import { provide } from '@lit/context'
|
2024-06-04 14:39:25 +00:00
|
|
|
import { html } from 'lit'
|
2024-05-28 15:56:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Channel emojis configuration page.
|
|
|
|
*/
|
|
|
|
@customElement('livechat-channel-emojis')
|
|
|
|
export class ChannelEmojisElement extends LivechatElement {
|
|
|
|
@provide({ context: registerClientOptionsContext })
|
|
|
|
@property({ attribute: false })
|
|
|
|
public registerClientOptions?: RegisterClientOptions
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
public channelId?: number
|
|
|
|
|
2024-06-04 14:39:25 +00:00
|
|
|
private _channelEmojisConfiguration?: ChannelEmojisConfiguration
|
2024-05-28 15:56:24 +00:00
|
|
|
|
|
|
|
@provide({ context: channelDetailsServiceContext })
|
|
|
|
private _channelDetailsService?: ChannelDetailsService
|
|
|
|
|
2024-06-05 13:56:03 +00:00
|
|
|
@state()
|
|
|
|
private _validationError?: ValidationError
|
|
|
|
|
2024-06-04 14:39:25 +00:00
|
|
|
protected override render = (): unknown => {
|
2024-06-05 13:56:03 +00:00
|
|
|
const tableHeaderList: DynamicFormHeader = {
|
2024-06-05 16:27:57 +00:00
|
|
|
sn: {
|
2024-06-05 13:56:03 +00:00
|
|
|
colName: ptTr(LOC_LIVECHAT_EMOJIS_SHORTNAME),
|
|
|
|
description: ptTr(LOC_LIVECHAT_EMOJIS_SHORTNAME_DESC)
|
|
|
|
},
|
2024-06-05 16:27:57 +00:00
|
|
|
url: {
|
2024-06-05 13:56:03 +00:00
|
|
|
colName: ptTr(LOC_LIVECHAT_EMOJIS_FILE),
|
|
|
|
description: ptTr(LOC_LIVECHAT_EMOJIS_FILE_DESC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const tableSchema: DynamicFormSchema = {
|
2024-06-05 16:27:57 +00:00
|
|
|
sn: {
|
2024-06-05 13:56:03 +00:00
|
|
|
inputType: 'text',
|
|
|
|
default: ''
|
|
|
|
},
|
2024-06-05 16:27:57 +00:00
|
|
|
url: {
|
2024-06-05 13:56:03 +00:00
|
|
|
inputType: 'image-file',
|
|
|
|
default: ''
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 15:56:24 +00:00
|
|
|
return this._asyncTaskRender.render({
|
|
|
|
pending: () => {},
|
2024-06-04 14:39:25 +00:00
|
|
|
complete: () => html`
|
|
|
|
<div
|
|
|
|
class="margin-content peertube-plugin-livechat-configuration peertube-plugin-livechat-configuration-channel"
|
|
|
|
>
|
|
|
|
<h1>
|
|
|
|
${ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_EMOJIS_TITLE)}:
|
|
|
|
<span class="peertube-plugin-livechat-configuration-channel-info">
|
|
|
|
<span>${this._channelEmojisConfiguration?.channel.displayName}</span>
|
|
|
|
<span>${this._channelEmojisConfiguration?.channel.name}</span>
|
|
|
|
</span>
|
|
|
|
<livechat-help-button .page=${'documentation/user/streamers/emojis'}>
|
|
|
|
</livechat-help-button>
|
|
|
|
FIXME: help url OK?
|
|
|
|
</h1>
|
|
|
|
<form role="form" @submit=${this._saveEmojis}>
|
2024-06-05 13:56:03 +00:00
|
|
|
<div class="row mt-5">
|
|
|
|
<livechat-dynamic-table-form
|
|
|
|
.header=${tableHeaderList}
|
|
|
|
.schema=${tableSchema}
|
2024-06-06 09:36:07 +00:00
|
|
|
.maxLines=${maxEmojisPerChannel}
|
2024-06-05 13:56:03 +00:00
|
|
|
.validation=${this._validationError?.properties}
|
|
|
|
.validationPrefix=${'emojis'}
|
|
|
|
.rows=${this._channelEmojisConfiguration?.emojis.customEmojis}
|
2024-06-05 16:27:57 +00:00
|
|
|
@update=${(e: CustomEvent) => {
|
|
|
|
if (this._channelEmojisConfiguration) {
|
|
|
|
this._channelEmojisConfiguration.emojis.customEmojis = e.detail
|
2024-06-06 09:36:07 +00:00
|
|
|
// Fixing missing ':' for shortnames:
|
|
|
|
for (const desc of this._channelEmojisConfiguration.emojis.customEmojis) {
|
|
|
|
if (desc.sn === '') { continue }
|
|
|
|
if (!desc.sn.startsWith(':')) { desc.sn = ':' + desc.sn }
|
|
|
|
if (!desc.sn.endsWith(':')) { desc.sn += ':' }
|
|
|
|
}
|
2024-06-05 16:27:57 +00:00
|
|
|
this.requestUpdate('_channelEmojisConfiguration')
|
|
|
|
}
|
2024-06-05 13:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
></livechat-dynamic-table-form>
|
|
|
|
</div>
|
2024-06-04 14:39:25 +00:00
|
|
|
<div class="form-group mt-5">
|
|
|
|
<button type="submit" class="peertube-button-link orange-button">
|
|
|
|
${ptTr(LOC_SAVE)}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
`,
|
2024-05-28 15:56:24 +00:00
|
|
|
error: (err: any) => {
|
|
|
|
this.registerClientOptions?.peertubeHelpers.notifier.error(err.toString())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly _asyncTaskRender = 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)
|
2024-06-04 14:39:25 +00:00
|
|
|
this._channelEmojisConfiguration = await this._channelDetailsService.fetchEmojisConfiguration(this.channelId)
|
2024-05-28 15:56:24 +00:00
|
|
|
},
|
|
|
|
args: () => []
|
|
|
|
})
|
2024-06-04 14:39:25 +00:00
|
|
|
|
2024-06-05 16:27:57 +00:00
|
|
|
private async _saveEmojis (ev?: Event): Promise<void> {
|
2024-06-04 14:39:25 +00:00
|
|
|
ev?.preventDefault()
|
2024-06-05 16:27:57 +00:00
|
|
|
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))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._channelDetailsService.saveEmojisConfiguration(this.channelId, this._channelEmojisConfiguration.emojis)
|
|
|
|
this._validationError = undefined
|
2024-06-06 09:36:07 +00:00
|
|
|
peertubeHelpers.notifier.info(await peertubeHelpers.translate(LOC_SUCCESSFULLY_SAVED))
|
2024-06-05 16:27:57 +00:00
|
|
|
this.requestUpdate('_validationError')
|
|
|
|
} catch (error) {
|
|
|
|
this._validationError = undefined
|
|
|
|
let msg: string
|
|
|
|
if ((error instanceof ValidationError)) {
|
|
|
|
this._validationError = error
|
|
|
|
if (error.message) {
|
|
|
|
msg = error.message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg ??= await peertubeHelpers.translate(LOC_ERROR)
|
|
|
|
peertubeHelpers.notifier.error(msg)
|
|
|
|
this.requestUpdate('_validationError')
|
|
|
|
}
|
2024-06-04 14:39:25 +00:00
|
|
|
}
|
2024-05-28 15:56:24 +00:00
|
|
|
}
|