]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
935e113620dea24b101a92cc5b18579bd6cfb418
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Notifier } 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'
12
13 @Component({
14 selector: 'my-plugin-search',
15 templateUrl: './plugin-search.component.html',
16 styleUrls: [
17 '../shared/toggle-plugin-type.scss',
18 '../shared/plugin-list.component.scss',
19 './plugin-search.component.scss'
20 ]
21 })
22 export class PluginSearchComponent implements OnInit {
23 pluginTypeOptions: { label: string, value: PluginType }[] = []
24 pluginType: PluginType = PluginType.PLUGIN
25
26 pagination: ComponentPagination = {
27 currentPage: 1,
28 itemsPerPage: 10
29 }
30 sort = '-popularity'
31
32 search = ''
33 isSearching = false
34
35 plugins: PeerTubePluginIndex[] = []
36 installing: { [name: string]: boolean } = {}
37
38 private searchSubject = new Subject<string>()
39
40 constructor (
41 private i18n: I18n,
42 private pluginService: PluginApiService,
43 private notifier: Notifier,
44 private confirmService: ConfirmService,
45 private router: Router,
46 private route: ActivatedRoute
47 ) {
48 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
49 }
50
51 ngOnInit () {
52 const query = this.route.snapshot.queryParams
53 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
54
55 this.searchSubject.asObservable()
56 .pipe(
57 debounceTime(400),
58 distinctUntilChanged()
59 )
60 .subscribe(search => {
61 this.search = search
62 this.reloadPlugins()
63 })
64
65 this.reloadPlugins()
66 }
67
68 onSearchChange (search: string) {
69 this.searchSubject.next(search)
70 }
71
72 reloadPlugins () {
73 this.pagination.currentPage = 1
74 this.plugins = []
75
76 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
77
78 this.loadMorePlugins()
79 }
80
81 loadMorePlugins () {
82 this.isSearching = true
83
84 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
85 .subscribe(
86 res => {
87 this.isSearching = false
88
89 this.plugins = this.plugins.concat(res.data)
90 this.pagination.totalItems = res.total
91 },
92
93 err => this.notifier.error(err.message)
94 )
95 }
96
97 onNearOfBottom () {
98 if (!hasMoreItems(this.pagination)) return
99
100 this.pagination.currentPage += 1
101
102 this.loadMorePlugins()
103 }
104
105 isInstalling (plugin: PeerTubePluginIndex) {
106 return !!this.installing[plugin.npmName]
107 }
108
109 async install (plugin: PeerTubePluginIndex) {
110 if (this.installing[plugin.npmName]) return
111
112 const res = await this.confirmService.confirm(
113 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
114 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
115 )
116 if (res === false) return
117
118 this.installing[plugin.npmName] = true
119
120 this.pluginService.install(plugin.npmName)
121 .subscribe(
122 () => {
123 this.installing[plugin.npmName] = false
124
125 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
126
127 plugin.installed = true
128 },
129
130 err => this.notifier.error(err.message)
131 )
132 }
133 }