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',
13 '../shared/toggle-plugin-type.scss',
14 '../shared/plugin-list.component.scss',
15 './plugin-search.component.scss'
18 export class PluginSearchComponent implements OnInit {
19 pluginTypeOptions: { label: string, value: PluginType }[] = []
20 pluginType: PluginType = PluginType.PLUGIN
22 pagination: ComponentPagination = {
32 plugins: PeerTubePluginIndex[] = []
33 installing: { [name: string]: boolean } = {}
34 pluginInstalled = false
36 onDataSubject = new Subject<any[]>()
38 private searchSubject = new Subject<string>()
41 private pluginService: PluginService,
42 private pluginApiService: PluginApiService,
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private router: Router,
46 private route: ActivatedRoute
48 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
52 const query = this.route.snapshot.queryParams
53 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
55 this.searchSubject.asObservable()
58 distinctUntilChanged()
60 .subscribe(search => {
68 onSearchChange (event: Event) {
69 const target = event.target as HTMLInputElement
71 this.searchSubject.next(target.value)
75 this.pagination.currentPage = 1
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
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 getPluginOrThemeHref (name: string) {
119 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
122 getShowRouterLink (plugin: PeerTubePluginIndex) {
123 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
127 return this.pluginType === PluginType.THEME
130 async install (plugin: PeerTubePluginIndex) {
131 if (this.installing[plugin.npmName]) return
133 const res = await this.confirmService.confirm(
134 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
135 $localize`Install ${plugin.name}?`
137 if (res === false) return
139 this.installing[plugin.npmName] = true
141 this.pluginApiService.install(plugin.npmName)
144 this.installing[plugin.npmName] = false
145 this.pluginInstalled = true
147 this.notifier.success($localize`${plugin.name} installed.`)
149 plugin.installed = true
152 error: err => this.notifier.error(err.message)