Box.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {
  2. Provider,
  3. getAllowedHosts,
  4. tokenStorage,
  5. type CompanionPluginOptions,
  6. } from '@uppy/companion-client'
  7. import { UIPlugin, Uppy } from '@uppy/core'
  8. import { ProviderViews } from '@uppy/provider-views'
  9. import { h, type ComponentChild } from 'preact'
  10. import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile'
  11. import type { UnknownProviderPluginState } from '@uppy/core/lib/Uppy.ts'
  12. import locale from './locale.ts'
  13. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  14. // @ts-ignore We don't want TS to generate types for the package.json
  15. import packageJson from '../package.json'
  16. export type BoxOptions = CompanionPluginOptions
  17. export default class Box<M extends Meta, B extends Body> extends UIPlugin<
  18. BoxOptions,
  19. M,
  20. B,
  21. UnknownProviderPluginState
  22. > {
  23. static VERSION = packageJson.version
  24. icon: () => JSX.Element
  25. provider: Provider<M, B>
  26. view: ProviderViews<M, B>
  27. storage: typeof tokenStorage
  28. files: UppyFile<M, B>[]
  29. constructor(uppy: Uppy<M, B>, opts: BoxOptions) {
  30. super(uppy, opts)
  31. this.id = this.opts.id || 'Box'
  32. this.type = 'acquirer'
  33. this.storage = this.opts.storage || tokenStorage
  34. this.files = []
  35. this.icon = () => (
  36. <svg
  37. className="uppy-DashboardTab-iconBox"
  38. aria-hidden="true"
  39. focusable="false"
  40. width="32"
  41. height="32"
  42. viewBox="0 0 32 32"
  43. >
  44. <g fill="currentcolor" fillRule="nonzero">
  45. <path d="m16.4 13.5c-1.6 0-3 0.9-3.7 2.2-0.7-1.3-2.1-2.2-3.7-2.2-1 0-1.8 0.3-2.5 0.8v-3.6c-0.1-0.3-0.5-0.7-1-0.7s-0.8 0.4-0.8 0.8v7c0 2.3 1.9 4.2 4.2 4.2 1.6 0 3-0.9 3.7-2.2 0.7 1.3 2.1 2.2 3.7 2.2 2.3 0 4.2-1.9 4.2-4.2 0.1-2.4-1.8-4.3-4.1-4.3m-7.5 6.8c-1.4 0-2.5-1.1-2.5-2.5s1.1-2.5 2.5-2.5 2.5 1.1 2.5 2.5-1.1 2.5-2.5 2.5m7.5 0c-1.4 0-2.5-1.1-2.5-2.5s1.1-2.5 2.5-2.5 2.5 1.1 2.5 2.5-1.1 2.5-2.5 2.5" />
  46. <path d="m27.2 20.6l-2.3-2.8 2.3-2.8c0.3-0.4 0.2-0.9-0.2-1.2s-1-0.2-1.3 0.2l-2 2.4-2-2.4c-0.3-0.4-0.9-0.4-1.3-0.2-0.4 0.3-0.5 0.8-0.2 1.2l2.3 2.8-2.3 2.8c-0.3 0.4-0.2 0.9 0.2 1.2s1 0.2 1.3-0.2l2-2.4 2 2.4c0.3 0.4 0.9 0.4 1.3 0.2 0.4-0.3 0.4-0.8 0.2-1.2" />
  47. </g>
  48. </svg>
  49. )
  50. this.opts.companionAllowedHosts = getAllowedHosts(
  51. this.opts.companionAllowedHosts,
  52. this.opts.companionUrl,
  53. )
  54. this.provider = new Provider(uppy, {
  55. companionUrl: this.opts.companionUrl,
  56. companionHeaders: this.opts.companionHeaders,
  57. companionKeysParams: this.opts.companionKeysParams,
  58. companionCookiesRule: this.opts.companionCookiesRule,
  59. provider: 'box',
  60. pluginId: this.id,
  61. supportsRefreshToken: false,
  62. })
  63. this.defaultLocale = locale
  64. this.i18nInit()
  65. this.title = this.i18n('pluginNameBox')
  66. this.onFirstRender = this.onFirstRender.bind(this)
  67. this.render = this.render.bind(this)
  68. }
  69. install(): void {
  70. this.view = new ProviderViews(this, {
  71. provider: this.provider,
  72. loadAllFiles: true,
  73. })
  74. const { target } = this.opts
  75. if (target) {
  76. this.mount(target, this)
  77. }
  78. }
  79. uninstall(): void {
  80. this.view.tearDown()
  81. this.unmount()
  82. }
  83. onFirstRender(): void {
  84. Promise.all([this.provider.fetchPreAuthToken(), this.view.getFolder()])
  85. }
  86. render(state: unknown): ComponentChild {
  87. return this.view.render(state)
  88. }
  89. }