]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Subject } from 'rxjs' |
2 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' | |
6702a1b2 | 3 | import { Component, OnInit } from '@angular/core' |
6702a1b2 | 4 | import { ActivatedRoute, Router } from '@angular/router' |
67ed6552 | 5 | import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service' |
66357162 | 6 | import { ComponentPagination, ConfirmService, hasMoreItems, Notifier } from '@app/core' |
6702a1b2 | 7 | import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model' |
67ed6552 | 8 | import { 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 | }) |
19 | export 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 ( | |
6702a1b2 C |
42 | private pluginService: PluginApiService, |
43 | private notifier: Notifier, | |
44 | private confirmService: ConfirmService, | |
45 | private router: Router, | |
46 | private route: ActivatedRoute | |
d00dc28d C |
47 | ) { |
48 | this.pluginTypeOptions = this.pluginService.getPluginTypeOptions() | |
49 | } | |
50 | ||
51 | ngOnInit () { | |
6702a1b2 C |
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 | ||
be27ef3b C |
68 | onSearchChange (event: Event) { |
69 | const target = event.target as HTMLInputElement | |
70 | ||
71 | this.searchSubject.next(target.value) | |
6702a1b2 C |
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 | |
ad453580 C |
93 | |
94 | this.onDataSubject.next(res.data) | |
6702a1b2 C |
95 | }, |
96 | ||
f0c5e8b6 C |
97 | err => { |
98 | console.error(err) | |
99 | ||
66357162 | 100 | const message = $localize`The plugin index is not available. Please retry later.` |
f0c5e8b6 C |
101 | this.notifier.error(message) |
102 | } | |
6702a1b2 C |
103 | ) |
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 | ||
118 | async install (plugin: PeerTubePluginIndex) { | |
119 | if (this.installing[plugin.npmName]) return | |
120 | ||
121 | const res = await this.confirmService.confirm( | |
66357162 C |
122 | $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`, |
123 | $localize`Install ${plugin.name}?` | |
6702a1b2 C |
124 | ) |
125 | if (res === false) return | |
126 | ||
127 | this.installing[plugin.npmName] = true | |
128 | ||
129 | this.pluginService.install(plugin.npmName) | |
130 | .subscribe( | |
131 | () => { | |
132 | this.installing[plugin.npmName] = false | |
89c344db | 133 | this.pluginInstalled = true |
6702a1b2 | 134 | |
66357162 | 135 | this.notifier.success($localize`${plugin.name} installed.`) |
6702a1b2 C |
136 | |
137 | plugin.installed = true | |
138 | }, | |
139 | ||
140 | err => this.notifier.error(err.message) | |
141 | ) | |
d00dc28d C |
142 | } |
143 | } |