import { RegisterClientOptions } from '@peertube/peertube-types/client' import { css, html, LitElement } from 'lit' import { repeat } from 'lit-html/directives/repeat.js' import { customElement, property, state } from 'lit/decorators.js' import { ptTr } from './TranslationDirective' import { localizedHelpUrl } from '../../../utils/help' import './DynamicTableFormElement' import './PluginConfigurationRow' import './HelpButtonElement' import { until } from 'async' import { Task } from '@lit/task'; import { ChannelConfiguration } from 'shared/lib/types' import { ChannelConfigurationService } from './ChannelConfigurationService' import { createContext, provide } from '@lit/context' import { getGlobalStyleSheets } from '../../global-styles' export const registerClientOptionsContext = createContext(Symbol('register-client-options')); export const channelConfigurationContext = createContext(Symbol('channel-configuration')); export const channelConfigurationServiceContext = createContext(Symbol('channel-configuration-service')); @customElement('channel-configuration') export class ChannelConfigurationElement extends LitElement { @provide({ context: registerClientOptionsContext }) @property({ attribute: false }) public registerClientOptions: RegisterClientOptions | undefined @property({ attribute: false }) public channelId: number | undefined @provide({ context: channelConfigurationContext }) @state() public _channelConfiguration: ChannelConfiguration | undefined @provide({ context: channelConfigurationServiceContext }) private _configurationService: ChannelConfigurationService | undefined static styles = [ ...getGlobalStyleSheets() ]; @state() public _formStatus: boolean | any = undefined private _asyncTaskRender = new Task(this, { task: async ([registerClientOptions], {signal}) => { if (this.registerClientOptions) { this._configurationService = new ChannelConfigurationService(this.registerClientOptions) this._channelConfiguration = await this._configurationService.fetchConfiguration(this.channelId ?? 0) } }, args: () => [this.registerClientOptions] }); private _saveConfig = () => { if(this._configurationService && this._channelConfiguration) { this._configurationService.saveOptions(this._channelConfiguration.channel.id, this._channelConfiguration.configuration) .then((value) => { this._formStatus = { success: true } console.log(`Configuration has been updated`) this.requestUpdate('_formStatus') }) .catch((error) => { this._formStatus = error console.log(`An error occurred : ${JSON.stringify(this._formStatus)}`) this.requestUpdate('_formStatus') }); } } render = () => { let tableHeaderList = { forbiddenWords: { entries: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_DESC2) }, regex: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REGEXP_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REGEXP_DESC) }, applyToModerators: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_APPLYTOMODERATORS_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_APPLYTOMODERATORS_DESC) }, label: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_LABEL_DESC) }, reason: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REASON_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_REASON_DESC) }, comments: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_COMMENTS_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_FORBIDDEN_WORDS_COMMENTS_DESC) } }, quotes: { messages: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_LABEL2), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DESC2) }, delay: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DELAY_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_QUOTE_DELAY_DESC) } }, commands: { command: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_CMD_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_CMD_DESC) }, message: { colName: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_MESSAGE_LABEL), description: ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_COMMAND_MESSAGE_DESC) } } } let tableSchema = { forbiddenWords: { entries: { inputType: 'textarea', default: ['helloqwesad'], separator: '\n', }, regex: { inputType: 'text', default: 'helloaxzca', }, applyToModerators: { inputType: 'checkbox', default: true }, label: { inputType: 'text', default: 'helloasx' }, reason: { inputType: 'text', default: 'transphobia', datalist: ['Racism', 'Sexism', 'Transphobia', 'Bigotry'] }, comments: { inputType: 'textarea', default: `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.` }, }, quotes: { messages: { inputType: 'textarea', default: ['default message'], separator: '\n', }, delay: { inputType: 'number', default: 100, } }, commands: { command: { inputType: 'text', default: 'default command', }, message: { inputType: 'text', default: 'default message', } } } return this._asyncTaskRender.render({ complete: () => html`

${ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_TITLE)}: ${this._channelConfiguration?.channel.displayName} ${this._channelConfiguration?.channel.name}

${ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_DESC)}

${this._channelConfiguration?.configuration.bot.enabled ? html`
{ if (event?.target && this._channelConfiguration) this._channelConfiguration.configuration.bot.nickname = (event.target as HTMLInputElement).value this.requestUpdate('_channelConfiguration') } } value="${this._channelConfiguration?.configuration.bot.nickname}" />
` : '' }
${this._channelConfiguration?.configuration.bot.enabled ? html` { if (this._channelConfiguration) this._channelConfiguration.configuration.bot.forbiddenWords = e.detail this.requestUpdate('_channelConfiguration') } } .formName=${'forbidden-words'}> { if (this._channelConfiguration) this._channelConfiguration.configuration.bot.quotes = e.detail this.requestUpdate('_channelConfiguration') } } .formName=${'quote'}> { if (this._channelConfiguration) this._channelConfiguration.configuration.bot.commands = e.detail this.requestUpdate('_channelConfiguration') } } .formName=${'command'}> ` : '' }
${(this._formStatus && this._formStatus.success === undefined) ? html`` : '' } ${(this._formStatus && this._formStatus.success === true) ? html`` : '' }
${JSON.stringify(this._channelConfiguration)}` }) } }