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, PluginService } from '@app/core'
7 import { PeerTubePluginIndex, PluginType } from '@shared/models'
10 selector: 'my-plugin-search',
11 templateUrl: './plugin-search.component.html',
12 styleUrls: [ './plugin-search.component.scss' ]
14 export class PluginSearchComponent implements OnInit {
15 pluginType: PluginType
17 pagination: ComponentPagination = {
27 plugins: PeerTubePluginIndex[] = []
28 installing: { [name: string]: boolean } = {}
29 pluginInstalled = false
31 onDataSubject = new Subject<any[]>()
33 private searchSubject = new Subject<string>()
36 private pluginService: PluginService,
37 private pluginApiService: PluginApiService,
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private router: Router,
41 private route: ActivatedRoute
46 if (!this.route.snapshot.queryParams['pluginType']) {
47 const queryParams = { pluginType: PluginType.PLUGIN }
49 this.router.navigate([], { queryParams })
52 this.route.queryParams.subscribe(query => {
53 if (!query['pluginType']) return
55 this.pluginType = parseInt(query['pluginType'], 10)
56 this.search = query['search'] || ''
61 this.searchSubject.asObservable()
64 distinctUntilChanged()
66 .subscribe(search => this.router.navigate([], { queryParams: { search }, queryParamsHandling: 'merge' }))
69 onSearchChange (event: Event) {
70 const target = event.target as HTMLInputElement
72 this.searchSubject.next(target.value)
76 this.pagination.currentPage = 1
79 this.loadMorePlugins()
83 this.isSearching = true
85 this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
88 this.isSearching = false
90 this.plugins = this.plugins.concat(res.data)
91 this.pagination.totalItems = res.total
93 this.onDataSubject.next(res.data)
99 const message = $localize`The plugin index is not available. Please retry later.`
100 this.notifier.error(message)
106 if (!hasMoreItems(this.pagination)) return
108 this.pagination.currentPage += 1
110 this.loadMorePlugins()
113 isInstalling (plugin: PeerTubePluginIndex) {
114 return !!this.installing[plugin.npmName]
117 getShowRouterLink (plugin: PeerTubePluginIndex) {
118 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
122 return this.pluginType === PluginType.THEME
125 async install (plugin: PeerTubePluginIndex) {
126 if (this.installing[plugin.npmName]) return
128 const res = await this.confirmService.confirm(
129 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
130 $localize`Install ${plugin.name}?`
132 if (res === false) return
134 this.installing[plugin.npmName] = true
136 this.pluginApiService.install(plugin.npmName)
139 this.installing[plugin.npmName] = false
140 this.pluginInstalled = true
142 this.notifier.success($localize`${plugin.name} installed.`)
144 plugin.installed = true
147 error: err => this.notifier.error(err.message)