]>
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' |
c96e457b | 6 | import { ComponentPagination, ConfirmService, hasMoreItems, Notifier, PluginService } from '@app/core' |
428ccb8b | 7 | import { PeerTubePluginIndex, PluginType } from '@shared/models' |
d00dc28d C |
8 | |
9 | @Component({ | |
10 | selector: 'my-plugin-search', | |
11 | templateUrl: './plugin-search.component.html', | |
dba85a1e C |
12 | styleUrls: [ |
13 | '../shared/toggle-plugin-type.scss', | |
6702a1b2 | 14 | '../shared/plugin-list.component.scss', |
dba85a1e C |
15 | './plugin-search.component.scss' |
16 | ] | |
d00dc28d C |
17 | }) |
18 | export class PluginSearchComponent implements OnInit { | |
19 | pluginTypeOptions: { label: string, value: PluginType }[] = [] | |
6702a1b2 C |
20 | pluginType: PluginType = PluginType.PLUGIN |
21 | ||
22 | pagination: ComponentPagination = { | |
23 | currentPage: 1, | |
440d39c5 C |
24 | itemsPerPage: 10, |
25 | totalItems: null | |
6702a1b2 C |
26 | } |
27 | sort = '-popularity' | |
28 | ||
29 | search = '' | |
30 | isSearching = false | |
31 | ||
32 | plugins: PeerTubePluginIndex[] = [] | |
33 | installing: { [name: string]: boolean } = {} | |
89c344db | 34 | pluginInstalled = false |
6702a1b2 | 35 | |
ad453580 C |
36 | onDataSubject = new Subject<any[]>() |
37 | ||
6702a1b2 | 38 | private searchSubject = new Subject<string>() |
d00dc28d C |
39 | |
40 | constructor ( | |
c96e457b | 41 | private pluginService: PluginService, |
078b4716 | 42 | private pluginApiService: PluginApiService, |
6702a1b2 C |
43 | private notifier: Notifier, |
44 | private confirmService: ConfirmService, | |
45 | private router: Router, | |
46 | private route: ActivatedRoute | |
d00dc28d | 47 | ) { |
078b4716 | 48 | this.pluginTypeOptions = this.pluginApiService.getPluginTypeOptions() |
d00dc28d C |
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 | ||
078b4716 | 86 | this.pluginApiService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search) |
6702a1b2 C |
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 | ||
078b4716 C |
118 | getPluginOrThemeHref (name: string) { |
119 | return this.pluginApiService.getPluginOrThemeHref(this.pluginType, name) | |
120 | } | |
121 | ||
c96e457b C |
122 | getShowRouterLink (plugin: PeerTubePluginIndex) { |
123 | return [ '/admin', 'plugins', 'show', this.pluginService.nameToNpmName(plugin.name, this.pluginType) ] | |
124 | } | |
125 | ||
126 | isThemeSearch () { | |
127 | return this.pluginType === PluginType.THEME | |
128 | } | |
129 | ||
6702a1b2 C |
130 | async install (plugin: PeerTubePluginIndex) { |
131 | if (this.installing[plugin.npmName]) return | |
132 | ||
133 | const res = await this.confirmService.confirm( | |
66357162 C |
134 | $localize`Please only install plugins or themes you trust, since they can execute any code on your instance.`, |
135 | $localize`Install ${plugin.name}?` | |
6702a1b2 C |
136 | ) |
137 | if (res === false) return | |
138 | ||
139 | this.installing[plugin.npmName] = true | |
140 | ||
078b4716 | 141 | this.pluginApiService.install(plugin.npmName) |
6702a1b2 C |
142 | .subscribe( |
143 | () => { | |
144 | this.installing[plugin.npmName] = false | |
89c344db | 145 | this.pluginInstalled = true |
6702a1b2 | 146 | |
66357162 | 147 | this.notifier.success($localize`${plugin.name} installed.`) |
6702a1b2 C |
148 | |
149 | plugin.installed = true | |
150 | }, | |
151 | ||
152 | err => this.notifier.error(err.message) | |
153 | ) | |
d00dc28d C |
154 | } |
155 | } |