]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Cleanup plugins admin CSS
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
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'
9
10 @Component({
11 selector: 'my-plugin-search',
12 templateUrl: './plugin-search.component.html',
13 styleUrls: [
14 '../shared/toggle-plugin-type.scss',
15 '../shared/plugin-list.component.scss',
16 './plugin-search.component.scss'
17 ]
18 })
19 export class PluginSearchComponent implements OnInit {
20 pluginTypeOptions: { label: string, value: PluginType }[] = []
21 pluginType: PluginType = PluginType.PLUGIN
22
23 pagination: ComponentPagination = {
24 currentPage: 1,
25 itemsPerPage: 10,
26 totalItems: null
27 }
28 sort = '-popularity'
29
30 search = ''
31 isSearching = false
32
33 plugins: PeerTubePluginIndex[] = []
34 installing: { [name: string]: boolean } = {}
35 pluginInstalled = false
36
37 onDataSubject = new Subject<any[]>()
38
39 private searchSubject = new Subject<string>()
40
41 constructor (
42 private pluginService: PluginService,
43 private pluginApiService: PluginApiService,
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private router: Router,
47 private route: ActivatedRoute
48 ) {
49 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
50 }
51
52 ngOnInit () {
53 const query = this.route.snapshot.queryParams
54 if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
55
56 this.searchSubject.asObservable()
57 .pipe(
58 debounceTime(400),
59 distinctUntilChanged()
60 )
61 .subscribe(search => {
62 this.search = search
63 this.reloadPlugins()
64 })
65
66 this.reloadPlugins()
67 }
68
69 onSearchChange (event: Event) {
70 const target = event.target as HTMLInputElement
71
72 this.searchSubject.next(target.value)
73 }
74
75 reloadPlugins () {
76 this.pagination.currentPage = 1
77 this.plugins = []
78
79 this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
80
81 this.loadMorePlugins()
82 }
83
84 loadMorePlugins () {
85 this.isSearching = true
86
87 this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
88 .subscribe(
89 res => {
90 this.isSearching = false
91
92 this.plugins = this.plugins.concat(res.data)
93 this.pagination.totalItems = res.total
94
95 this.onDataSubject.next(res.data)
96 },
97
98 err => {
99 console.error(err)
100
101 const message = $localize`The plugin index is not available. Please retry later.`
102 this.notifier.error(message)
103 }
104 )
105 }
106
107 onNearOfBottom () {
108 if (!hasMoreItems(this.pagination)) return
109
110 this.pagination.currentPage += 1
111
112 this.loadMorePlugins()
113 }
114
115 isInstalling (plugin: PeerTubePluginIndex) {
116 return !!this.installing[plugin.npmName]
117 }
118
119 getPluginOrThemeHref (name: string) {
120 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
121 }
122
123 getShowRouterLink (plugin: PeerTubePluginIndex) {
124 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
125 }
126
127 isThemeSearch () {
128 return this.pluginType === PluginType.THEME
129 }
130
131 async install (plugin: PeerTubePluginIndex) {
132 if (this.installing[plugin.npmName]) return
133
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}?`
137 )
138 if (res === false) return
139
140 this.installing[plugin.npmName] = true
141
142 this.pluginApiService.install(plugin.npmName)
143 .subscribe(
144 () => {
145 this.installing[plugin.npmName] = false
146 this.pluginInstalled = true
147
148 this.notifier.success($localize`${plugin.name} installed.`)
149
150 plugin.installed = true
151 },
152
153 err => this.notifier.error(err.message)
154 )
155 }
156 }