]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Add client helpers to plugins
[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, ServerService } 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 pluginInstalled = false
38
39 private searchSubject = new Subject<string>()
40
41 constructor (
42 private server: ServerService,
43 private i18n: I18n,
44 private pluginService: PluginApiService,
45 private notifier: Notifier,
46 private confirmService: ConfirmService,
47 private router: Router,
48 private route: ActivatedRoute
49 ) {
50 this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
51 }
52
53 ngOnInit () {
54 const query = this.route.snapshot.queryParams
55 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
56
57 this.searchSubject.asObservable()
58 .pipe(
59 debounceTime(400),
60 distinctUntilChanged()
61 )
62 .subscribe(search => {
63 this.search = search
64 this.reloadPlugins()
65 })
66
67 this.reloadPlugins()
68 }
69
70 onSearchChange (search: string) {
71 this.searchSubject.next(search)
72 }
73
74 reloadPlugins () {
75 this.pagination.currentPage = 1
76 this.plugins = []
77
78 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
79
80 this.loadMorePlugins()
81 }
82
83 loadMorePlugins () {
84 this.isSearching = true
85
86 this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
87 .subscribe(
88 res => {
89 this.isSearching = false
90
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
93 },
94
95 err => {
96 console.error(err)
97
98 const message = this.i18n('The plugin index is not available. Please retry later.')
99 this.notifier.error(message)
100 }
101 )
102 }
103
104 onNearOfBottom () {
105 if (!hasMoreItems(this.pagination)) return
106
107 this.pagination.currentPage += 1
108
109 this.loadMorePlugins()
110 }
111
112 isInstalling (plugin: PeerTubePluginIndex) {
113 return !!this.installing[plugin.npmName]
114 }
115
116 async install (plugin: PeerTubePluginIndex) {
117 if (this.installing[plugin.npmName]) return
118
119 const res = await this.confirmService.confirm(
120 this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
121 this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
122 )
123 if (res === false) return
124
125 this.installing[plugin.npmName] = true
126
127 this.pluginService.install(plugin.npmName)
128 .subscribe(
129 () => {
130 this.installing[plugin.npmName] = false
131 this.pluginInstalled = true
132
133 this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
134
135 plugin.installed = true
136 },
137
138 err => this.notifier.error(err.message)
139 )
140 }
141 }