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 private searchSubject = new Subject<string>()
42 private server: ServerService,
44 private pluginService: PluginApiService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private router: Router,
48 private route: ActivatedRoute
50 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
54 const query = this.route.snapshot.queryParams
55 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
57 this.searchSubject.asObservable()
60 distinctUntilChanged()
62 .subscribe(search => {
70 onSearchChange (search: string) {
71 this.searchSubject.next(search)
75 this.pagination.currentPage = 1
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
80 this.loadMorePlugins()
84 this.isSearching = true
86 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
89 this.isSearching = false
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
98 const message = this.i18n('The plugin index is not available. Please retry later.')
99 this.notifier.error(message)
105 if (!hasMoreItems(this.pagination)) return
107 this.pagination.currentPage += 1
109 this.loadMorePlugins()
112 isInstalling (plugin: PeerTubePluginIndex) {
113 return !!this.installing[plugin.npmName]
116 async install (plugin: PeerTubePluginIndex) {
117 if (this.installing[plugin.npmName]) return
119 const res = await this.confirmService.confirm(
120 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
121 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
123 if (res === false) return
125 this.installing[plugin.npmName] = true
127 this.pluginService.install(plugin.npmName)
130 this.installing[plugin.npmName] = false
131 this.pluginInstalled = true
133 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
135 plugin.installed = true
138 err => this.notifier.error(err.message)