1 import { Subject } from 'rxjs'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5 import { ComponentPagination, ConfirmService, hasMoreItems, Notifier } from '@app/core'
6 import { PluginService } from '@app/core/plugins/plugin.service'
7 import { compareSemVer } from '@shared/core-utils/miscs/miscs'
8 import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
9 import { PluginType } from '@shared/models/plugins/plugin.type'
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 = {
31 plugins: PeerTubePlugin[] = []
32 updating: { [name: string]: boolean } = {}
34 PluginType = PluginType
36 onDataSubject = new Subject<any[]>()
39 private pluginService: PluginService,
40 private pluginApiService: PluginApiService,
41 private notifier: Notifier,
42 private confirmService: ConfirmService,
43 private router: Router,
44 private route: ActivatedRoute
46 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
50 const query = this.route.snapshot.queryParams
51 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
57 this.pagination.currentPage = 1
60 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
62 this.loadMorePlugins()
66 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
69 this.plugins = this.plugins.concat(res.data)
70 this.pagination.totalItems = res.total
72 this.onDataSubject.next(res.data)
75 err => this.notifier.error(err.message)
80 if (!hasMoreItems(this.pagination)) return
82 this.pagination.currentPage += 1
84 this.loadMorePlugins()
87 getNoResultMessage () {
88 if (this.pluginType === PluginType.PLUGIN) {
89 return $localize`You don't have plugins installed yet.`
92 return $localize`You don't have themes installed yet.`
95 isUpdateAvailable (plugin: PeerTubePlugin) {
96 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
99 getUpdateLabel (plugin: PeerTubePlugin) {
100 return $localize`Update to ${plugin.latestVersion}`
103 isUpdating (plugin: PeerTubePlugin) {
104 return !!this.updating[this.getUpdatingKey(plugin)]
107 async uninstall (plugin: PeerTubePlugin) {
108 const res = await this.confirmService.confirm(
109 $localize`Do you really want to uninstall ${plugin.name}?`,
112 if (res === false) return
114 this.pluginApiService.uninstall(plugin.name, plugin.type)
117 this.notifier.success($localize`${plugin.name} uninstalled.`)
119 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
120 this.pagination.totalItems--
123 err => this.notifier.error(err.message)
127 async update (plugin: PeerTubePlugin) {
128 const updatingKey = this.getUpdatingKey(plugin)
129 if (this.updating[updatingKey]) return
131 this.updating[updatingKey] = true
133 this.pluginApiService.update(plugin.name, plugin.type)
137 this.updating[updatingKey] = false
139 this.notifier.success($localize`${plugin.name} updated.`)
141 Object.assign(plugin, res)
144 err => this.notifier.error(err.message)
148 getShowRouterLink (plugin: PeerTubePlugin) {
149 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
152 private getUpdatingKey (plugin: PeerTubePlugin) {
153 return plugin.name + plugin.type