1 import { Component, OnInit } from '@angular/core'
2 import { PluginType } from '@shared/models/plugins/plugin.type'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
6 import { ConfirmService, Notifier } from '@app/core'
7 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
8 import { ActivatedRoute, Router } from '@angular/router'
9 import { compareSemVer } from '@shared/core-utils/miscs/miscs'
10 import { PluginService } from '@app/core/plugins/plugin.service'
11 import { Subject } from 'rxjs'
14 selector: 'my-plugin-list-installed',
15 templateUrl: './plugin-list-installed.component.html',
17 '../shared/toggle-plugin-type.scss',
18 '../shared/plugin-list.component.scss',
19 './plugin-list-installed.component.scss'
22 export class PluginListInstalledComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
26 pagination: ComponentPagination = {
32 plugins: PeerTubePlugin[] = []
33 updating: { [name: string]: boolean } = {}
35 PluginType = PluginType
37 onDataSubject = new Subject<any[]>()
41 private pluginService: PluginService,
42 private pluginApiService: PluginApiService,
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private router: Router,
46 private route: ActivatedRoute
48 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
52 const query = this.route.snapshot.queryParams
53 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
59 this.pagination.currentPage = 1
62 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
64 this.loadMorePlugins()
68 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
71 this.plugins = this.plugins.concat(res.data)
72 this.pagination.totalItems = res.total
74 this.onDataSubject.next(res.data)
77 err => this.notifier.error(err.message)
82 if (!hasMoreItems(this.pagination)) return
84 this.pagination.currentPage += 1
86 this.loadMorePlugins()
89 getNoResultMessage () {
90 if (this.pluginType === PluginType.PLUGIN) {
91 return this.i18n('You don\'t have plugins installed yet.')
94 return this.i18n('You don\'t have themes installed yet.')
97 isUpdateAvailable (plugin: PeerTubePlugin) {
98 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
101 getUpdateLabel (plugin: PeerTubePlugin) {
102 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
105 isUpdating (plugin: PeerTubePlugin) {
106 return !!this.updating[this.getUpdatingKey(plugin)]
109 async uninstall (plugin: PeerTubePlugin) {
110 const res = await this.confirmService.confirm(
111 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
112 this.i18n('Uninstall')
114 if (res === false) return
116 this.pluginApiService.uninstall(plugin.name, plugin.type)
119 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
121 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
122 this.pagination.totalItems--
125 err => this.notifier.error(err.message)
129 async update (plugin: PeerTubePlugin) {
130 const updatingKey = this.getUpdatingKey(plugin)
131 if (this.updating[updatingKey]) return
133 this.updating[updatingKey] = true
135 this.pluginApiService.update(plugin.name, plugin.type)
139 this.updating[updatingKey] = false
141 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
143 Object.assign(plugin, res)
146 err => this.notifier.error(err.message)
150 getShowRouterLink (plugin: PeerTubePlugin) {
151 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
154 private getUpdatingKey (plugin: PeerTubePlugin) {
155 return plugin.name + plugin.type