1 import { Component, OnInit } from '@angular/core'
2 import { Notifier, ServerService } 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 } = {}
37 pluginInstalled = false
39 onDataSubject = new Subject<any[]>()
41 private searchSubject = new Subject<string>()
44 private server: ServerService,
46 private pluginService: PluginApiService,
47 private notifier: Notifier,
48 private confirmService: ConfirmService,
49 private router: Router,
50 private route: ActivatedRoute
52 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
56 const query = this.route.snapshot.queryParams
57 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
59 this.searchSubject.asObservable()
62 distinctUntilChanged()
64 .subscribe(search => {
72 onSearchChange (search: string) {
73 this.searchSubject.next(search)
77 this.pagination.currentPage = 1
80 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
82 this.loadMorePlugins()
86 this.isSearching = true
88 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
91 this.isSearching = false
93 this.plugins = this.plugins.concat(res.data)
94 this.pagination.totalItems = res.total
96 this.onDataSubject.next(res.data)
102 const message = this.i18n('The plugin index is not available. Please retry later.')
103 this.notifier.error(message)
109 if (!hasMoreItems(this.pagination)) return
111 this.pagination.currentPage += 1
113 this.loadMorePlugins()
116 isInstalling (plugin: PeerTubePluginIndex) {
117 return !!this.installing[plugin.npmName]
120 async install (plugin: PeerTubePluginIndex) {
121 if (this.installing[plugin.npmName]) return
123 const res = await this.confirmService.confirm(
124 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
125 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
127 if (res === false) return
129 this.installing[plugin.npmName] = true
131 this.pluginService.install(plugin.npmName)
134 this.installing[plugin.npmName] = false
135 this.pluginInstalled = true
137 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
139 plugin.installed = true
142 err => this.notifier.error(err.message)