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 { logger } from '@root-helpers/logger'
8 import { PeerTubePluginIndex, PluginType } from '@shared/models'
11 selector: 'my-plugin-search',
12 templateUrl: './plugin-search.component.html',
13 styleUrls: [ './plugin-search.component.scss' ]
15 export class PluginSearchComponent implements OnInit {
16 pluginType: PluginType
18 pagination: ComponentPagination = {
28 plugins: PeerTubePluginIndex[] = []
29 installing: { [name: string]: boolean } = {}
30 pluginInstalled = false
32 onDataSubject = new Subject<any[]>()
34 private searchSubject = new Subject<string>()
37 private pluginService: PluginService,
38 private pluginApiService: PluginApiService,
39 private notifier: Notifier,
40 private confirmService: ConfirmService,
41 private router: Router,
42 private route: ActivatedRoute
47 if (!this.route.snapshot.queryParams['pluginType']) {
48 const queryParams = { pluginType: PluginType.PLUGIN }
50 this.router.navigate([], { queryParams })
53 this.route.queryParams.subscribe(query => {
54 if (!query['pluginType']) return
56 this.pluginType = parseInt(query['pluginType'], 10)
57 this.search = query['search'] || ''
62 this.searchSubject.asObservable()
65 distinctUntilChanged()
67 .subscribe(search => this.router.navigate([], { queryParams: { search }, queryParamsHandling: 'merge' }))
70 onSearchChange (event: Event) {
71 const target = event.target as HTMLInputElement
73 this.searchSubject.next(target.value)
77 this.pagination.currentPage = 1
80 this.loadMorePlugins()
84 this.isSearching = true
86 this.pluginApiService.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
94 this.onDataSubject.next(res.data)
100 const message = $localize`The plugin index is not available. Please retry later.`
101 this.notifier.error(message)
107 if (!hasMoreItems(this.pagination)) return
109 this.pagination.currentPage += 1
111 this.loadMorePlugins()
114 isInstalling (plugin: PeerTubePluginIndex) {
115 return !!this.installing[plugin.npmName]
118 getShowRouterLink (plugin: PeerTubePluginIndex) {
119 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
123 return this.pluginType === PluginType.THEME
126 async install (plugin: PeerTubePluginIndex) {
127 if (this.installing[plugin.npmName]) return
129 const res = await this.confirmService.confirm(
130 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
131 $localize`Install ${plugin.name}?`
133 if (res === false) return
135 this.installing[plugin.npmName] = true
137 this.pluginApiService.install(plugin.npmName)
140 this.installing[plugin.npmName] = false
141 this.pluginInstalled = true
143 this.notifier.success($localize`${plugin.name} installed.`)
145 plugin.installed = true
149 this.installing[plugin.npmName] = false
151 this.notifier.error(err.message)