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'
12 selector: 'my-plugin-list-installed',
13 templateUrl: './plugin-list-installed.component.html',
15 '../shared/toggle-plugin-type.scss',
16 '../shared/plugin-list.component.scss',
17 './plugin-list-installed.component.scss'
20 export class PluginListInstalledComponent implements OnInit {
21 pluginTypeOptions: { label: string, value: PluginType }[] = []
22 pluginType: PluginType = PluginType.PLUGIN
24 pagination: ComponentPagination = {
30 plugins: PeerTubePlugin[] = []
31 updating: { [name: string]: boolean } = {}
33 PluginType = PluginType
37 private pluginService: PluginApiService,
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private router: Router,
41 private route: ActivatedRoute
43 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
47 const query = this.route.snapshot.queryParams
48 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
54 this.pagination.currentPage = 1
57 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
59 this.loadMorePlugins()
63 this.pluginService.getPlugins(this.pluginType, this.pagination, this.sort)
66 this.plugins = this.plugins.concat(res.data)
67 this.pagination.totalItems = res.total
70 err => this.notifier.error(err.message)
75 if (!hasMoreItems(this.pagination)) return
77 this.pagination.currentPage += 1
79 this.loadMorePlugins()
82 getNoResultMessage () {
83 if (this.pluginType === PluginType.PLUGIN) {
84 return this.i18n('You don\'t have plugins installed yet.')
87 return this.i18n('You don\'t have themes installed yet.')
90 isUpdateAvailable (plugin: PeerTubePlugin) {
91 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
94 getUpdateLabel (plugin: PeerTubePlugin) {
95 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
98 isUpdating (plugin: PeerTubePlugin) {
99 return !!this.updating[this.getUpdatingKey(plugin)]
102 async uninstall (plugin: PeerTubePlugin) {
103 const res = await this.confirmService.confirm(
104 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
105 this.i18n('Uninstall')
107 if (res === false) return
109 this.pluginService.uninstall(plugin.name, plugin.type)
112 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
114 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
115 this.pagination.totalItems--
118 err => this.notifier.error(err.message)
122 async update (plugin: PeerTubePlugin) {
123 const updatingKey = this.getUpdatingKey(plugin)
124 if (this.updating[updatingKey]) return
126 this.updating[updatingKey] = true
128 this.pluginService.update(plugin.name, plugin.type)
132 this.updating[updatingKey] = false
134 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
136 Object.assign(plugin, res)
139 err => this.notifier.error(err.message)
143 getShowRouterLink (plugin: PeerTubePlugin) {
144 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
147 private getUpdatingKey (plugin: PeerTubePlugin) {
148 return plugin.name + plugin.type