]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/plugins/plugin-search/plugin-search.component.ts
Add settings button after plugin install
[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'
6702a1b2 7import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
67ed6552 8import { PluginType } from '@shared/models/plugins/plugin.type'
d00dc28d
C
9
10@Component({
11 selector: 'my-plugin-search',
12 templateUrl: './plugin-search.component.html',
dba85a1e
C
13 styleUrls: [
14 '../shared/toggle-plugin-type.scss',
6702a1b2 15 '../shared/plugin-list.component.scss',
dba85a1e
C
16 './plugin-search.component.scss'
17 ]
d00dc28d
C
18})
19export class PluginSearchComponent implements OnInit {
20 pluginTypeOptions: { label: string, value: PluginType }[] = []
6702a1b2
C
21 pluginType: PluginType = PluginType.PLUGIN
22
23 pagination: ComponentPagination = {
24 currentPage: 1,
440d39c5
C
25 itemsPerPage: 10,
26 totalItems: null
6702a1b2
C
27 }
28 sort = '-popularity'
29
30 search = ''
31 isSearching = false
32
33 plugins: PeerTubePluginIndex[] = []
34 installing: { [name: string]: boolean } = {}
89c344db 35 pluginInstalled = false
6702a1b2 36
ad453580
C
37 onDataSubject = new Subject<any[]>()
38
6702a1b2 39 private searchSubject = new Subject<string>()
d00dc28d
C
40
41 constructor (
c96e457b 42 private pluginService: PluginService,
078b4716 43 private pluginApiService: PluginApiService,
6702a1b2
C
44 private notifier: Notifier,
45 private confirmService: ConfirmService,
46 private router: Router,
47 private route: ActivatedRoute
d00dc28d 48 ) {
078b4716 49 this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions()
d00dc28d
C
50 }
51
52 ngOnInit () {
6702a1b2
C
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
be27ef3b
C
69 onSearchChange (event: Event) {
70 const target = event.target as HTMLInputElement
71
72 this.searchSubject.next(target.value)
6702a1b2
C
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
078b4716 87 this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
6702a1b2
C
88 .subscribe(
89 res => {
90 this.isSearching = false
91
92 this.plugins = this.plugins.concat(res.data)
93 this.pagination.totalItems = res.total
ad453580
C
94
95 this.onDataSubject.next(res.data)
6702a1b2
C
96 },
97
f0c5e8b6
C
98 err => {
99 console.error(err)
100
66357162 101 const message = $localize`The plugin index is not available. Please retry later.`
f0c5e8b6
C
102 this.notifier.error(message)
103 }
6702a1b2
C
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
078b4716
C
119 getPluginOrThemeHref (name: string) {
120 return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name)
121 }
122
c96e457b
C
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
6702a1b2
C
131 async install (plugin: PeerTubePluginIndex) {
132 if (this.installing[plugin.npmName]) return
133
134 const res = await this.confirmService.confirm(
66357162
C
135 $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`,
136 $localize`Install ${plugin.name}?`
6702a1b2
C
137 )
138 if (res === false) return
139
140 this.installing[plugin.npmName] = true
141
078b4716 142 this.pluginApiService.install(plugin.npmName)
6702a1b2
C
143 .subscribe(
144 () => {
145 this.installing[plugin.npmName] = false
89c344db 146 this.pluginInstalled = true
6702a1b2 147
66357162 148 this.notifier.success($localize`${plugin.name} installed.`)
6702a1b2
C
149
150 plugin.installed = true
151 },
152
153 err => this.notifier.error(err.message)
154 )
d00dc28d
C
155 }
156}