]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-show-installed/plugin-show-installed.component.ts
Translate plugin settings
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-show-installed / plugin-show-installed.component.ts
CommitLineData
dba85a1e
C
1import { Subscription } from 'rxjs'
2import { map, switchMap } from 'rxjs/operators'
67ed6552
C
3import { Component, OnDestroy, OnInit } from '@angular/core'
4import { ActivatedRoute } from '@angular/router'
0ea9f463 5import { HooksService, Notifier, PluginService } from '@app/core'
7ed1edbb
C
6import { BuildFormArgument } from '@app/shared/form-validators'
7import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
67ed6552
C
8import { PeerTubePlugin, RegisterServerSettingOptions } from '@shared/models'
9import { PluginApiService } from '../shared/plugin-api.service'
d00dc28d
C
10
11@Component({
12 selector: 'my-plugin-show-installed',
13 templateUrl: './plugin-show-installed.component.html',
14 styleUrls: [ './plugin-show-installed.component.scss' ]
15})
109d893f 16export class PluginShowInstalledComponent extends FormReactive implements OnInit, OnDestroy {
dba85a1e 17 plugin: PeerTubePlugin
9ae88819 18 registeredSettings: RegisterServerSettingOptions[] = []
dba85a1e
C
19 pluginTypeLabel: string
20
21 private sub: Subscription
3c47fa3b 22 private npmName: string
dba85a1e
C
23
24 constructor (
25 protected formValidatorService: FormValidatorService,
c713017f
C
26 private pluginService: PluginService,
27 private pluginAPIService: PluginApiService,
dba85a1e 28 private notifier: Notifier,
0ea9f463 29 private hooks: HooksService,
dba85a1e
C
30 private route: ActivatedRoute
31 ) {
32 super()
33 }
d00dc28d
C
34
35 ngOnInit () {
dba85a1e
C
36 this.sub = this.route.params.subscribe(
37 routeParams => {
3c47fa3b 38 this.npmName = routeParams['npmName']
dba85a1e 39
3c47fa3b 40 this.loadPlugin(this.npmName)
dba85a1e
C
41 }
42 )
43 }
44
45 ngOnDestroy () {
46 if (this.sub) this.sub.unsubscribe()
47 }
48
49 formValidated () {
50 const settings = this.form.value
51
c713017f 52 this.pluginAPIService.updatePluginSettings(this.plugin.name, this.plugin.type, settings)
1378c0d3
C
53 .subscribe({
54 next: () => {
66357162 55 this.notifier.success($localize`Settings updated.`)
dba85a1e
C
56 },
57
1378c0d3
C
58 error: err => this.notifier.error(err.message)
59 })
dba85a1e
C
60 }
61
62 hasRegisteredSettings () {
63 return Array.isArray(this.registeredSettings) && this.registeredSettings.length !== 0
64 }
65
c713017f 66 isSettingHidden (setting: RegisterServerSettingOptions) {
3c47fa3b
C
67 const script = this.pluginService.getRegisteredSettingsScript(this.npmName)
68
69 if (!script?.isSettingHidden) return false
70
71 return script.isSettingHidden({ setting, formValues: this.form.value })
c713017f
C
72 }
73
0ea9f463
C
74 getWrapperId (setting: RegisterServerSettingOptions) {
75 if (!setting.name) return
76
77 return setting.name + '-wrapper'
78 }
79
dba85a1e 80 private loadPlugin (npmName: string) {
c713017f 81 this.pluginAPIService.getPlugin(npmName)
dba85a1e 82 .pipe(switchMap(plugin => {
c713017f 83 return this.pluginAPIService.getPluginRegisteredSettings(plugin.name, plugin.type)
ba211e73 84 .pipe(map(data => ({ plugin, registeredSettings: data.registeredSettings })))
dba85a1e 85 }))
1378c0d3
C
86 .subscribe({
87 next: async ({ plugin, registeredSettings }) => {
dba85a1e 88 this.plugin = plugin
3c47fa3b 89
c713017f 90 this.registeredSettings = await this.translateSettings(registeredSettings)
dba85a1e 91
c713017f 92 this.pluginTypeLabel = this.pluginAPIService.getPluginTypeLabel(this.plugin.type)
dba85a1e
C
93
94 this.buildSettingsForm()
95 },
96
1378c0d3
C
97 error: err => this.notifier.error(err.message)
98 })
dba85a1e
C
99 }
100
101 private buildSettingsForm () {
dba85a1e
C
102 const buildOptions: BuildFormArgument = {}
103 const settingsValues: any = {}
104
105 for (const setting of this.registeredSettings) {
9df52d66
C
106 buildOptions[setting.name] = null
107 settingsValues[setting.name] = this.getSetting(setting.name)
dba85a1e
C
108 }
109
110 this.buildForm(buildOptions)
111
112 this.form.patchValue(settingsValues)
0ea9f463
C
113
114 setTimeout(() => this.hooks.runAction('action:admin-plugin-settings.init', 'admin-plugin', { npmName: this.npmName }))
dba85a1e
C
115 }
116
117 private getSetting (name: string) {
118 const settings = this.plugin.settings
119
9df52d66 120 if (settings?.[name] !== undefined) return settings[name]
dba85a1e
C
121
122 const registered = this.registeredSettings.find(r => r.name === name)
d00dc28d 123
dba85a1e 124 return registered.default
d00dc28d
C
125 }
126
c713017f 127 private async translateSettings (settings: RegisterServerSettingOptions[]) {
c713017f 128 for (const setting of settings) {
fb3c9e2b 129 await this.pluginService.translateSetting(this.npmName, setting)
c713017f
C
130 }
131
132 return settings
133 }
d00dc28d 134}