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'
13 selector: 'my-plugin-list-installed',
14 templateUrl: './plugin-list-installed.component.html',
16 '../shared/toggle-plugin-type.scss',
17 '../shared/plugin-list.component.scss',
18 './plugin-list-installed.component.scss'
21 export class PluginListInstalledComponent implements OnInit {
22 pluginTypeOptions: { label: string, value: PluginType }[] = []
23 pluginType: PluginType = PluginType.PLUGIN
25 pagination: ComponentPagination = {
31 plugins: PeerTubePlugin[] = []
32 updating: { [name: string]: boolean } = {}
34 PluginType = PluginType
38 private pluginService: PluginService,
39 private pluginApiService: PluginApiService,
40 private notifier: Notifier,
41 private confirmService: ConfirmService,
42 private router: Router,
43 private route: ActivatedRoute
45 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
49 const query = this.route.snapshot.queryParams
50 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56 this.pagination.currentPage = 1
59 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
61 this.loadMorePlugins()
65 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
68 this.plugins = this.plugins.concat(res.data)
69 this.pagination.totalItems = res.total
72 err => this.notifier.error(err.message)
77 if (!hasMoreItems(this.pagination)) return
79 this.pagination.currentPage += 1
81 this.loadMorePlugins()
84 getNoResultMessage () {
85 if (this.pluginType === PluginType.PLUGIN) {
86 return this.i18n('You don\'t have plugins installed yet.')
89 return this.i18n('You don\'t have themes installed yet.')
92 isUpdateAvailable (plugin: PeerTubePlugin) {
93 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
96 getUpdateLabel (plugin: PeerTubePlugin) {
97 return this.i18n('Update to {{version}}', { version: plugin.latestVersion })
100 isUpdating (plugin: PeerTubePlugin) {
101 return !!this.updating[this.getUpdatingKey(plugin)]
104 async uninstall (plugin: PeerTubePlugin) {
105 const res = await this.confirmService.confirm(
106 this.i18n('Do you really want to uninstall {{pluginName}}?', { pluginName: plugin.name }),
107 this.i18n('Uninstall')
109 if (res === false) return
111 this.pluginApiService.uninstall(plugin.name, plugin.type)
114 this.notifier.success(this.i18n('{{pluginName}} uninstalled.', { pluginName: plugin.name }))
116 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
117 this.pagination.totalItems--
120 err => this.notifier.error(err.message)
124 async update (plugin: PeerTubePlugin) {
125 const updatingKey = this.getUpdatingKey(plugin)
126 if (this.updating[updatingKey]) return
128 this.updating[updatingKey] = true
130 this.pluginApiService.update(plugin.name, plugin.type)
134 this.updating[updatingKey] = false
136 this.notifier.success(this.i18n('{{pluginName}} updated.', { pluginName: plugin.name }))
138 Object.assign(plugin, res)
141 err => this.notifier.error(err.message)
145 getShowRouterLink (plugin: PeerTubePlugin) {
146 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
149 private getUpdatingKey (plugin: PeerTubePlugin) {
150 return plugin.name + plugin.type