]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Check latest plugins version with config interval
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
CommitLineData
6702a1b2 1import { Component, OnInit } from '@angular/core'
89c344db 2import { Notifier, ServerService } from '@app/core'
6702a1b2 3import { ConfirmService } from '../../../core'
d00dc28d 4import { I18n } from '@ngx-translate/i18n-polyfill'
d00dc28d
C
5import { PluginType } from '@shared/models/plugins/plugin.type'
6import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
6702a1b2
C
7import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
8import { ActivatedRoute, Router } from '@angular/router'
9import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
10import { Subject } from 'rxjs'
11import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
d00dc28d
C
12
13@Component({
14 selector: 'my-plugin-search',
15 templateUrl: './plugin-search.component.html',
dba85a1e
C
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
6702a1b2 18 '../shared/plugin-list.component.scss',
dba85a1e
C
19 './plugin-search.component.scss'
20 ]
d00dc28d
C
21})
22export class PluginSearchComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
6702a1b2
C
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10
29 }
30 sort = '-popularity'
31
32 search = ''
33 isSearching = false
34
35 plugins: PeerTubePluginIndex[] = []
36 installing: { [name: string]: boolean } = {}
89c344db 37 pluginInstalled = false
6702a1b2
C
38
39 private searchSubject = new Subject<string>()
d00dc28d
C
40
41 constructor (
89c344db 42 private server: ServerService,
d00dc28d 43 private i18n: I18n,
6702a1b2
C
44 private pluginService: PluginApiService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private router: Router,
48 private route: ActivatedRoute
d00dc28d
C
49 ) {
50 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
51 }
52
53 ngOnInit () {
6702a1b2
C
54 const query = this.route.snapshot.queryParams
55 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56
57 this.searchSubject.asObservable()
58 .pipe(
59 debounceTime(400),
60 distinctUntilChanged()
61 )
62 .subscribe(search => {
63 this.search = search
64 this.reloadPlugins()
65 })
66
67 this.reloadPlugins()
68 }
69
70 onSearchChange (search: string) {
71 this.searchSubject.next(search)
72 }
73
74 reloadPlugins () {
75 this.pagination.currentPage = 1
76 this.plugins = []
77
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
79
80 this.loadMorePlugins()
81 }
82
83 loadMorePlugins () {
84 this.isSearching = true
85
86 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
87 .subscribe(
88 res => {
89 this.isSearching = false
90
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
93 },
94
95 err => this.notifier.error(err.message)
96 )
97 }
98
99 onNearOfBottom () {
100 if (!hasMoreItems(this.pagination)) return
101
102 this.pagination.currentPage += 1
103
104 this.loadMorePlugins()
105 }
106
107 isInstalling (plugin: PeerTubePluginIndex) {
108 return !!this.installing[plugin.npmName]
109 }
110
111 async install (plugin: PeerTubePluginIndex) {
112 if (this.installing[plugin.npmName]) return
113
114 const res = await this.confirmService.confirm(
115 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
116 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
117 )
118 if (res === false) return
119
120 this.installing[plugin.npmName] = true
121
122 this.pluginService.install(plugin.npmName)
123 .subscribe(
124 () => {
125 this.installing[plugin.npmName] = false
89c344db 126 this.pluginInstalled = true
6702a1b2
C
127
128 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
129
130 plugin.installed = true
131 },
132
133 err => this.notifier.error(err.message)
134 )
d00dc28d
C
135 }
136}