]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / plugins / plugin-search / plugin-search.component.ts
CommitLineData
67ed6552
C
1import { Subject } from 'rxjs'
2import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
6702a1b2 3import { Component, OnInit } from '@angular/core'
6702a1b2 4import { ActivatedRoute, Router } from '@angular/router'
67ed6552 5import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
c96e457b 6import { ComponentPagination, ConfirmService, hasMoreItems, Notifier, PluginService } from '@app/core'
42b40636 7import { logger } from '@root-helpers/logger'
428ccb8b 8import { PeerTubePluginIndex, PluginType } from '@shared/models'
d00dc28d
C
9
10@Component({
11 selector: 'my-plugin-search',
12 templateUrl: './plugin-search.component.html',
2accfdd8 13 styleUrls: [ './plugin-search.component.scss' ]
d00dc28d
C
14})
15export class PluginSearchComponent implements OnInit {
2accfdd8 16 pluginType: PluginType
6702a1b2
C
17
18 pagination: ComponentPagination = {
19 currentPage: 1,
440d39c5
C
20 itemsPerPage: 10,
21 totalItems: null
6702a1b2
C
22 }
23 sort = '-popularity'
24
25 search = ''
26 isSearching = false
27
28 plugins: PeerTubePluginIndex[] = []
29 installing: { [name: string]: boolean } = {}
89c344db 30 pluginInstalled = false
6702a1b2 31
ad453580
C
32 onDataSubject = new Subject<any[]>()
33
6702a1b2 34 private searchSubject = new Subject<string>()
d00dc28d
C
35
36 constructor (
c96e457b 37 private pluginService: PluginService,
078b4716 38 private pluginApiService: PluginApiService,
6702a1b2
C
39 private notifier: Notifier,
40 private confirmService: ConfirmService,
41 private router: Router,
42 private route: ActivatedRoute
d00dc28d 43 ) {
d00dc28d
C
44 }
45
46 ngOnInit () {
2accfdd8
C
47 if (!this.route.snapshot.queryParams['pluginType']) {
48 const queryParams = { pluginType: PluginType.PLUGIN }
49
50 this.router.navigate([], { queryParams })
51 }
52
53 this.route.queryParams.subscribe(query => {
54 if (!query['pluginType']) return
55
56 this.pluginType = parseInt(query['pluginType'], 10)
57 this.search = query['search'] || ''
58
59 this.reloadPlugins()
60 })
6702a1b2
C
61
62 this.searchSubject.asObservable()
63 .pipe(
64 debounceTime(400),
65 distinctUntilChanged()
66 )
2accfdd8 67 .subscribe(search => this.router.navigate([], { queryParams: { search }, queryParamsHandling: 'merge' }))
6702a1b2
C
68 }
69
be27ef3b
C
70 onSearchChange (event: Event) {
71 const target = event.target as HTMLInputElement
72
73 this.searchSubject.next(target.value)
6702a1b2
C
74 }
75
76 reloadPlugins () {
77 this.pagination.currentPage = 1
78 this.plugins = []
79
6702a1b2
C
80 this.loadMorePlugins()
81 }
82
83 loadMorePlugins () {
84 this.isSearching = true
85
078b4716 86 this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
1378c0d3
C
87 .subscribe({
88 next: res => {
6702a1b2
C
89 this.isSearching = false
90
91 this.plugins = this.plugins.concat(res.data)
92 this.pagination.totalItems = res.total
ad453580
C
93
94 this.onDataSubject.next(res.data)
6702a1b2
C
95 },
96
1378c0d3 97 error: err => {
42b40636 98 logger.error(err)
f0c5e8b6 99
66357162 100 const message = $localize`The plugin index is not available. Please retry later.`
f0c5e8b6
C
101 this.notifier.error(message)
102 }
1378c0d3 103 })
6702a1b2
C
104 }
105
106 onNearOfBottom () {
107 if (!hasMoreItems(this.pagination)) return
108
109 this.pagination.currentPage += 1
110
111 this.loadMorePlugins()
112 }
113
114 isInstalling (plugin: PeerTubePluginIndex) {
115 return !!this.installing[plugin.npmName]
116 }
117
c96e457b
C
118 getShowRouterLink (plugin: PeerTubePluginIndex) {
119 return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ]
120 }
121
122 isThemeSearch () {
123 return this.pluginType === PluginType.THEME
124 }
125
6702a1b2
C
126 async install (plugin: PeerTubePluginIndex) {
127 if (this.installing[plugin.npmName]) return
128
129 const res = await this.confirmService.confirm(
66357162
C
130 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
131 $localize`Install ${plugin.name}?`
6702a1b2
C
132 )
133 if (res === false) return
134
135 this.installing[plugin.npmName] = true
136
078b4716 137 this.pluginApiService.install(plugin.npmName)
1378c0d3
C
138 .subscribe({
139 next: () => {
6702a1b2 140 this.installing[plugin.npmName] = false
89c344db 141 this.pluginInstalled = true
6702a1b2 142
66357162 143 this.notifier.success($localize`${plugin.name} installed.`)
6702a1b2
C
144
145 plugin.installed = true
146 },
147
81ed2de8
C
148 error: err => {
149 this.installing[plugin.npmName] = false
150
151 this.notifier.error(err.message)
152 }
1378c0d3 153 })
d00dc28d
C
154 }
155}