1 import { Component, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { ConfirmService } from '../../../core'
4 import { I18n } from '@ngx-translate/i18n-polyfill'
5 import { PluginType } from '@shared/models/plugins/plugin.type'
6 import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
7 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
8 import { ActivatedRoute, Router } from '@angular/router'
9 import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
10 import { Subject } from 'rxjs'
11 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
14 selector: 'my-plugin-search',
15 templateUrl: './plugin-search.component.html',
17 '../shared/toggle-plugin-type.scss',
18 '../shared/plugin-list.component.scss',
19 './plugin-search.component.scss'
22 export class PluginSearchComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
26 pagination: ComponentPagination = {
35 plugins: PeerTubePluginIndex[] = []
36 installing: { [name: string]: boolean } = {}
38 private searchSubject = new Subject<string>()
42 private pluginService: PluginApiService,
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private router: Router,
46 private route: ActivatedRoute
48 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
52 const query = this.route.snapshot.queryParams
53 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
55 this.searchSubject.asObservable()
58 distinctUntilChanged()
60 .subscribe(search => {
68 onSearchChange (search: string) {
69 this.searchSubject.next(search)
73 this.pagination.currentPage = 1
76 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
78 this.loadMorePlugins()
82 this.isSearching = true
84 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
87 this.isSearching = false
89 this.plugins = this.plugins.concat(res.data)
90 this.pagination.totalItems = res.total
93 err => this.notifier.error(err.message)
98 if (!hasMoreItems(this.pagination)) return
100 this.pagination.currentPage += 1
102 this.loadMorePlugins()
105 isInstalling (plugin: PeerTubePluginIndex) {
106 return !!this.installing[plugin.npmName]
109 async install (plugin: PeerTubePluginIndex) {
110 if (this.installing[plugin.npmName]) return
112 const res = await this.confirmService.confirm(
113 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
114 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
116 if (res === false) return
118 this.installing[plugin.npmName] = true
120 this.pluginService.install(plugin.npmName)
123 this.installing[plugin.npmName] = false
125 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
127 plugin.installed = true
130 err => this.notifier.error(err.message)