]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-list-installed/plugin-list-installed.component.ts
Fix theme npm link
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-list-installed / plugin-list-installed.component.ts
CommitLineData
67ed6552 1import { Subject } from 'rxjs'
d00dc28d 2import { Component, OnInit } from '@angular/core'
dba85a1e 3import { ActivatedRoute, Router } from '@angular/router'
67ed6552
C
4import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
5import { ComponentPagination, ConfirmService, hasMoreItems, Notifier } from '@app/core'
23bdacf8 6import { PluginService } from '@app/core/plugins/plugin.service'
67ed6552
C
7import { compareSemVer } from '@shared/core-utils/miscs/miscs'
8import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model'
9import { PluginType } from '@shared/models/plugins/plugin.type'
d00dc28d
C
10
11@Component({
12 selector: 'my-plugin-list-installed',
13 templateUrl: './plugin-list-installed.component.html',
dba85a1e
C
14 styleUrls: [
15 '../shared/toggle-plugin-type.scss',
6702a1b2 16 '../shared/plugin-list.component.scss',
dba85a1e
C
17 './plugin-list-installed.component.scss'
18 ]
d00dc28d
C
19})
20export class PluginListInstalledComponent implements OnInit {
21 pluginTypeOptions: { label: string, value: PluginType }[] = []
22 pluginType: PluginType = PluginType.PLUGIN
23
24 pagination: ComponentPagination = {
25 currentPage: 1,
440d39c5
C
26 itemsPerPage: 10,
27 totalItems: null
d00dc28d
C
28 }
29 sort = 'name'
30
31 plugins: PeerTubePlugin[] = []
b5f919ac
C
32 updating: { [name: string]: boolean } = {}
33
34 PluginType = PluginType
d00dc28d 35
ad453580
C
36 onDataSubject = new Subject<any[]>()
37
d00dc28d 38 constructor (
23bdacf8
C
39 private pluginService: PluginService,
40 private pluginApiService: PluginApiService,
dba85a1e
C
41 private notifier: Notifier,
42 private confirmService: ConfirmService,
43 private router: Router,
44 private route: ActivatedRoute
d00dc28d 45 ) {
23bdacf8 46 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
d00dc28d
C
47 }
48
49 ngOnInit () {
dba85a1e
C
50 const query = this.route.snapshot.queryParams
51 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
52
d00dc28d
C
53 this.reloadPlugins()
54 }
55
56 reloadPlugins () {
57 this.pagination.currentPage = 1
58 this.plugins = []
59
b5f919ac 60 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
dba85a1e 61
d00dc28d
C
62 this.loadMorePlugins()
63 }
64
65 loadMorePlugins () {
23bdacf8 66 this.pluginApiService.getPlugins(this.pluginType, this.pagination, this.sort)
d00dc28d
C
67 .subscribe(
68 res => {
69 this.plugins = this.plugins.concat(res.data)
70 this.pagination.totalItems = res.total
ad453580
C
71
72 this.onDataSubject.next(res.data)
d00dc28d
C
73 },
74
75 err => this.notifier.error(err.message)
76 )
77 }
78
79 onNearOfBottom () {
80 if (!hasMoreItems(this.pagination)) return
81
82 this.pagination.currentPage += 1
83
84 this.loadMorePlugins()
85 }
86
87 getNoResultMessage () {
88 if (this.pluginType === PluginType.PLUGIN) {
66357162 89 return $localize`You don't have plugins installed yet.`
d00dc28d
C
90 }
91
66357162 92 return $localize`You don't have themes installed yet.`
d00dc28d 93 }
dba85a1e 94
b5f919ac
C
95 isUpdateAvailable (plugin: PeerTubePlugin) {
96 return plugin.latestVersion && compareSemVer(plugin.latestVersion, plugin.version) > 0
97 }
98
99 getUpdateLabel (plugin: PeerTubePlugin) {
66357162 100 return $localize`Update to ${plugin.latestVersion}`
b5f919ac
C
101 }
102
103 isUpdating (plugin: PeerTubePlugin) {
104 return !!this.updating[this.getUpdatingKey(plugin)]
105 }
106
dba85a1e
C
107 async uninstall (plugin: PeerTubePlugin) {
108 const res = await this.confirmService.confirm(
66357162
C
109 $localize`Do you really want to uninstall ${plugin.name}?`,
110 $localize`Uninstall`
dba85a1e
C
111 )
112 if (res === false) return
113
23bdacf8 114 this.pluginApiService.uninstall(plugin.name, plugin.type)
dba85a1e
C
115 .subscribe(
116 () => {
66357162 117 this.notifier.success($localize`${plugin.name} uninstalled.`)
dba85a1e
C
118
119 this.plugins = this.plugins.filter(p => p.name !== plugin.name)
120 this.pagination.totalItems--
121 },
122
123 err => this.notifier.error(err.message)
124 )
125 }
126
b5f919ac
C
127 async update (plugin: PeerTubePlugin) {
128 const updatingKey = this.getUpdatingKey(plugin)
129 if (this.updating[updatingKey]) return
130
131 this.updating[updatingKey] = true
132
23bdacf8 133 this.pluginApiService.update(plugin.name, plugin.type)
b5f919ac
C
134 .pipe()
135 .subscribe(
136 res => {
137 this.updating[updatingKey] = false
138
66357162 139 this.notifier.success($localize`${plugin.name} updated.`)
b5f919ac
C
140
141 Object.assign(plugin, res)
142 },
143
144 err => this.notifier.error(err.message)
145 )
146 }
147
dba85a1e
C
148 getShowRouterLink (plugin: PeerTubePlugin) {
149 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, plugin.type) ]
150 }
b5f919ac 151
078b4716
C
152 getPluginOrThemeHref (name: string) {
153 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
154 }
155
b5f919ac
C
156 private getUpdatingKey (plugin: PeerTubePlugin) {
157 return plugin.name + plugin.type
158 }
d00dc28d 159}