plugin-search.component.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { Component, OnInit } from '@angular/core'
  2. import { Notifier } from '@app/core'
  3. import { ConfirmService } from '../../../core'
  4. import { I18n } from '@ngx-translate/i18n-polyfill'
  5. import { PluginType } from '@shared/models/plugins/plugin.type'
  6. import { PluginApiService } from '@app/+admin/plugins/shared/plugin-api.service'
  7. import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
  8. import { ActivatedRoute, Router } from '@angular/router'
  9. import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model'
  10. import { Subject } from 'rxjs'
  11. import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
  12. @Component({
  13. selector: 'my-plugin-search',
  14. templateUrl: './plugin-search.component.html',
  15. styleUrls: [
  16. '../shared/toggle-plugin-type.scss',
  17. '../shared/plugin-list.component.scss',
  18. './plugin-search.component.scss'
  19. ]
  20. })
  21. export class PluginSearchComponent implements OnInit {
  22. pluginTypeOptions: { label: string, value: PluginType }[] = []
  23. pluginType: PluginType = PluginType.PLUGIN
  24. pagination: ComponentPagination = {
  25. currentPage: 1,
  26. itemsPerPage: 10
  27. }
  28. sort = '-popularity'
  29. search = ''
  30. isSearching = false
  31. plugins: PeerTubePluginIndex[] = []
  32. installing: { [name: string]: boolean } = {}
  33. private searchSubject = new Subject<string>()
  34. constructor (
  35. private i18n: I18n,
  36. private pluginService: PluginApiService,
  37. private notifier: Notifier,
  38. private confirmService: ConfirmService,
  39. private router: Router,
  40. private route: ActivatedRoute
  41. ) {
  42. this.pluginTypeOptions = this.pluginService.getPluginTypeOptions()
  43. }
  44. ngOnInit () {
  45. const query = this.route.snapshot.queryParams
  46. if (query['pluginType']) this.pluginType = parseInt(query['pluginType'], 10)
  47. this.searchSubject.asObservable()
  48. .pipe(
  49. debounceTime(400),
  50. distinctUntilChanged()
  51. )
  52. .subscribe(search => {
  53. this.search = search
  54. this.reloadPlugins()
  55. })
  56. this.reloadPlugins()
  57. }
  58. onSearchChange (search: string) {
  59. this.searchSubject.next(search)
  60. }
  61. reloadPlugins () {
  62. this.pagination.currentPage = 1
  63. this.plugins = []
  64. this.router.navigate([], { queryParams: { pluginType: this.pluginType } })
  65. this.loadMorePlugins()
  66. }
  67. loadMorePlugins () {
  68. this.isSearching = true
  69. this.pluginService.searchAvailablePlugins(this.pluginType, this.pagination, this.sort, this.search)
  70. .subscribe(
  71. res => {
  72. this.isSearching = false
  73. this.plugins = this.plugins.concat(res.data)
  74. this.pagination.totalItems = res.total
  75. },
  76. err => this.notifier.error(err.message)
  77. )
  78. }
  79. onNearOfBottom () {
  80. if (!hasMoreItems(this.pagination)) return
  81. this.pagination.currentPage += 1
  82. this.loadMorePlugins()
  83. }
  84. isInstalling (plugin: PeerTubePluginIndex) {
  85. return !!this.installing[plugin.npmName]
  86. }
  87. async install (plugin: PeerTubePluginIndex) {
  88. if (this.installing[plugin.npmName]) return
  89. const res = await this.confirmService.confirm(
  90. this.i18n('Please only install plugins or themes you trust, since they can execute any code on your instance.'),
  91. this.i18n('Install {{pluginName}}?', { pluginName: plugin.name })
  92. )
  93. if (res === false) return
  94. this.installing[plugin.npmName] = true
  95. this.pluginService.install(plugin.npmName)
  96. .subscribe(
  97. () => {
  98. this.installing[plugin.npmName] = false
  99. this.notifier.success(this.i18n('{{pluginName}} installed.', { pluginName: plugin.name }))
  100. plugin.installed = true
  101. },
  102. err => this.notifier.error(err.message)
  103. )
  104. }
  105. }