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 } from '@shared/models/plugins/peertube-plugin-index.model'
8 import { PluginType } from '@shared/models/plugins/plugin.type'
11 selector: 'my-plugin-search',
12 templateUrl: './plugin-search.component.html',
14 '../shared/toggle-plugin-type.scss',
15 '../shared/plugin-list.component.scss',
16 './plugin-search.component.scss'
19 export class PluginSearchComponent implements OnInit {
20 pluginTypeOptions: { label: string, value: PluginType }[] = []
21 pluginType: PluginType = PluginType.PLUGIN
23 pagination: ComponentPagination = {
33 plugins: PeerTubePluginIndex[] = []
34 installing: { [name: string]: boolean } = {}
35 pluginInstalled = false
37 onDataSubject = new Subject<any[]>()
39 private searchSubject = new Subject<string>()
42 private pluginService: PluginService,
43 private pluginApiService: PluginApiService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private router: Router,
47 private route: ActivatedRoute
49 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
53 const query = this.route.snapshot.queryParams
54 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56 this.searchSubject.asObservable()
59 distinctUntilChanged()
61 .subscribe(search => {
69 onSearchChange (event: Event) {
70 const target = event.target as HTMLInputElement
72 this.searchSubject.next(target.value)
76 this.pagination.currentPage = 1
79 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
81 this.loadMorePlugins()
85 this.isSearching = true
87 this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
90 this.isSearching = false
92 this.plugins = this.plugins.concat(res.data)
93 this.pagination.totalItems = res.total
95 this.onDataSubject.next(res.data)
101 const message = $localize`The plugin index is not available. Please retry later.`
102 this.notifier.error(message)
108 if (!hasMoreItems(this.pagination)) return
110 this.pagination.currentPage += 1
112 this.loadMorePlugins()
115 isInstalling (plugin: PeerTubePluginIndex) {
116 return !!this.installing[plugin.npmName]
119 getPluginOrThemeHref (name: string) {
120 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
123 getShowRouterLink (plugin: PeerTubePluginIndex) {
124 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
128 return this.pluginType === PluginType.THEME
131 async install (plugin: PeerTubePluginIndex) {
132 if (this.installing[plugin.npmName]) return
134 const res = await this.confirmService.confirm(
135 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
136 $localize`Install ${plugin.name}?`
138 if (res === false) return
140 this.installing[plugin.npmName] = true
142 this.pluginApiService.install(plugin.npmName)
145 this.installing[plugin.npmName] = false
146 this.pluginInstalled = true
148 this.notifier.success($localize`${plugin.name} installed.`)
150 plugin.installed = true
153 err => this.notifier.error(err.message)