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, PluginType } from '@shared/models'
11 selector: 'my-plugin-list-installed',
12 templateUrl: './plugin-list-installed.component.html',
14 '../shared/toggle-plugin-type.scss',
15 '../shared/plugin-list.component.scss',
16 './plugin-list-installed.component.scss'
19 export class PluginListInstalledComponent implements OnInit {
20 pluginTypeOptions: { label: string, value: PluginType }[] = []
21 pluginType: PluginType = PluginType.PLUGIN
23 pagination: ComponentPagination = {
30 plugins: PeerTubePlugin[] = []
31 updating: { [name: string]: boolean } = {}
33 onDataSubject = new Subject<any[]>()
36 private pluginService: PluginService,
37 private pluginApiService: PluginApiService,
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private router: Router,
41 private route: ActivatedRoute
43 this.pluginTypeOptions = this.pluginApiService.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.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
66 this.plugins = this.plugins.concat(res.data)
67 this.pagination.totalItems = res.total
69 this.onDataSubject.next(res.data)
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 $localize`You don't have plugins installed yet.`
89 return $localize`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 $localize`Update to ${plugin.latestVersion}`
100 isUpdating (plugin: PeerTubePlugin) {
101 return !!this.updating[this.getUpdatingKey(plugin)]
104 isTheme (plugin: PeerTubePlugin) {
105 return plugin.type === PluginType.THEME
108 async uninstall (plugin: PeerTubePlugin) {
109 const res = await this.confirmService.confirm(
110 $localize`Do you really want to uninstall ${plugin.name}?`,
113 if (res === false) return
115 this.pluginApiService.uninstall(plugin.name, plugin.type)
118 this.notifier.success($localize`${plugin.name} uninstalled.`)
120 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
121 this.pagination.totalItems--
124 err => this.notifier.error(err.message)
128 async update (plugin: PeerTubePlugin) {
129 const updatingKey = this.getUpdatingKey(plugin)
130 if (this.updating[updatingKey]) return
132 if (this.isMajorUpgrade(plugin)) {
133 const res = await this.confirmService.confirm(
134 $localize`This is a major plugin upgrade. Please go on the plugin homepage to check potential release notes.`,
136 $localize`Proceed upgrade`
139 if (res === false) return
142 this.updating[updatingKey] = true
144 this.pluginApiService.update(plugin.name, plugin.type)
148 this.updating[updatingKey] = false
150 this.notifier.success($localize`${plugin.name} updated.`)
152 Object.assign(plugin, res)
155 err => this.notifier.error(err.message)
159 getShowRouterLink (plugin: PeerTubePlugin) {
160 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
163 getPluginOrThemeHref (name: string) {
164 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
167 private getUpdatingKey (plugin: PeerTubePlugin) {
168 return plugin.name + plugin.type
171 private isMajorUpgrade (plugin: PeerTubePlugin) {
172 if (!plugin.latestVersion) return false
174 const latestMajor = plugin.latestVersion.split('.')[0]
175 const currentMajor = plugin.version.split('.')[0]
177 return latestMajor > currentMajor