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 = {
33 plugins: PeerTubePlugin[] = []
34 updating: { [name: string]: boolean } = {}
36 PluginType = PluginType
38 onDataSubject = new Subject<any[]>()
42 private pluginService: PluginService,
43 private pluginApiService: PluginApiService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private router: Router,
47 private route: ActivatedRoute
49 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
53 const query = this.route.snapshot.queryParams
54 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
60 this.pagination.currentPage = 1
63 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
65 this.loadMorePlugins()
69 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
72 this.plugins = this.plugins.concat(res.data)
73 this.pagination.totalItems = res.total
75 this.onDataSubject.next(res.data)
78 err => this.notifier.error(err.message)
83 if (!hasMoreItems(this.pagination)) return
85 this.pagination.currentPage += 1
87 this.loadMorePlugins()
90 getNoResultMessage () {
91 if (this.pluginType === PluginType.PLUGIN) {
92 return this.i18n('You don\'t have plugins installed yet.')
95 return this.i18n('You don\'t have themes installed yet.')
98 isUpdateAvailable (plugin: PeerTubePlugin) {
99 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
102 getUpdateLabel (plugin: PeerTubePlugin) {
103 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
106 isUpdating (plugin: PeerTubePlugin) {
107 return !!this.updating[this.getUpdatingKey(plugin)]
110 async uninstall (plugin: PeerTubePlugin) {
111 const res = await this.confirmService.confirm(
112 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
113 this.i18n('Uninstall')
115 if (res === false) return
117 this.pluginApiService.uninstall(plugin.name, plugin.type)
120 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
122 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
123 this.pagination.totalItems--
126 err => this.notifier.error(err.message)
130 async update (plugin: PeerTubePlugin) {
131 const updatingKey = this.getUpdatingKey(plugin)
132 if (this.updating[updatingKey]) return
134 this.updating[updatingKey] = true
136 this.pluginApiService.update(plugin.name, plugin.type)
140 this.updating[updatingKey] = false
142 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
144 Object.assign(plugin, res)
147 err => this.notifier.error(err.message)
151 getShowRouterLink (plugin: PeerTubePlugin) {
152 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
155 private getUpdatingKey (plugin: PeerTubePlugin) {
156 return plugin.name + plugin.type