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 = {
36 plugins: PeerTubePluginIndex[] = []
37 installing: { [name: string]: boolean } = {}
38 pluginInstalled = false
40 onDataSubject = new Subject<any[]>()
42 private searchSubject = new Subject<string>()
45 private server: ServerService,
47 private pluginService: PluginApiService,
48 private notifier: Notifier,
49 private confirmService: ConfirmService,
50 private router: Router,
51 private route: ActivatedRoute
53 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
57 const query = this.route.snapshot.queryParams
58 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
60 this.searchSubject.asObservable()
63 distinctUntilChanged()
65 .subscribe(search => {
73 onSearchChange (event: Event) {
74 const target = event.target as HTMLInputElement
76 this.searchSubject.next(target.value)
80 this.pagination.currentPage = 1
83 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
85 this.loadMorePlugins()
89 this.isSearching = true
91 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
94 this.isSearching = false
96 this.plugins = this.plugins.concat(res.data)
97 this.pagination.totalItems = res.total
99 this.onDataSubject.next(res.data)
105 const message = this.i18n('The plugin index is not available. Please retry later.')
106 this.notifier.error(message)
112 if (!hasMoreItems(this.pagination)) return
114 this.pagination.currentPage += 1
116 this.loadMorePlugins()
119 isInstalling (plugin: PeerTubePluginIndex) {
120 return !!this.installing[plugin.npmName]
123 async install (plugin: PeerTubePluginIndex) {
124 if (this.installing[plugin.npmName]) return
126 const res = await this.confirmService.confirm(
127 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
128 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
130 if (res === false) return
132 this.installing[plugin.npmName] = true
134 this.pluginService.install(plugin.npmName)
137 this.installing[plugin.npmName] = false
138 this.pluginInstalled = true
140 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
142 plugin.installed = true
145 err => this.notifier.error(err.message)