1 import { Subject } from 'rxjs'
2 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
3 import { Component, OnInit } from '@angular/core'
4 import { ActivatedRoute, Router } from '@angular/router'
5 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
6 import { ComponentPagination, ConfirmService, hasMoreItems, Notifier, ServerService } from '@app/core'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
9 import { PluginType } from '@shared/models/plugins/plugin.type'
12 selector: 'my-plugin-search',
13 templateUrl: './plugin-search.component.html',
15 '../shared/toggle-plugin-type.scss',
16 '../shared/plugin-list.component.scss',
17 './plugin-search.component.scss'
20 export class PluginSearchComponent implements OnInit {
21 pluginTypeOptions: { label: string, value: PluginType }[] = []
22 pluginType: PluginType = PluginType.PLUGIN
24 pagination: ComponentPagination = {
34 plugins: PeerTubePluginIndex[] = []
35 installing: { [name: string]: boolean } = {}
36 pluginInstalled = false
38 onDataSubject = new Subject<any[]>()
40 private searchSubject = new Subject<string>()
43 private server: ServerService,
45 private pluginService: PluginApiService,
46 private notifier: Notifier,
47 private confirmService: ConfirmService,
48 private router: Router,
49 private route: ActivatedRoute
51 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
55 const query = this.route.snapshot.queryParams
56 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
58 this.searchSubject.asObservable()
61 distinctUntilChanged()
63 .subscribe(search => {
71 onSearchChange (event: Event) {
72 const target = event.target as HTMLInputElement
74 this.searchSubject.next(target.value)
78 this.pagination.currentPage = 1
81 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
83 this.loadMorePlugins()
87 this.isSearching = true
89 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
92 this.isSearching = false
94 this.plugins = this.plugins.concat(res.data)
95 this.pagination.totalItems = res.total
97 this.onDataSubject.next(res.data)
103 const message = this.i18n('The plugin index is not available. Please retry later.')
104 this.notifier.error(message)
110 if (!hasMoreItems(this.pagination)) return
112 this.pagination.currentPage += 1
114 this.loadMorePlugins()
117 isInstalling (plugin: PeerTubePluginIndex) {
118 return !!this.installing[plugin.npmName]
121 async install (plugin: PeerTubePluginIndex) {
122 if (this.installing[plugin.npmName]) return
124 const res = await this.confirmService.confirm(
125 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
126 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
128 if (res === false) return
130 this.installing[plugin.npmName] = true
132 this.pluginService.install(plugin.npmName)
135 this.installing[plugin.npmName] = false
136 this.pluginInstalled = true
138 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
140 plugin.installed = true
143 err => this.notifier.error(err.message)