Dropbox.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 {
  12. AsyncStore,
  13. UnknownProviderPlugin,
  14. UnknownProviderPluginState,
  15. } from '@uppy/core/lib/Uppy.js'
  16. import locale from './locale.ts'
  17. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  18. // @ts-ignore We don't want TS to generate types for the package.json
  19. import packageJson from '../package.json'
  20. export type DropboxOptions = CompanionPluginOptions
  21. export default class Dropbox<M extends Meta, B extends Body>
  22. extends UIPlugin<DropboxOptions, M, B, UnknownProviderPluginState>
  23. implements UnknownProviderPlugin<M, B>
  24. {
  25. static VERSION = packageJson.version
  26. icon: () => h.JSX.Element
  27. provider: Provider<M, B>
  28. view!: ProviderViews<M, B>
  29. storage: AsyncStore
  30. files: UppyFile<M, B>[]
  31. rootFolderId: string | null = null
  32. constructor(uppy: Uppy<M, B>, opts: DropboxOptions) {
  33. super(uppy, opts)
  34. this.id = this.opts.id || 'Dropbox'
  35. this.type = 'acquirer'
  36. this.storage = this.opts.storage || tokenStorage
  37. this.files = []
  38. this.icon = () => (
  39. <svg
  40. className="uppy-DashboardTab-iconDropbox"
  41. aria-hidden="true"
  42. focusable="false"
  43. width="32"
  44. height="32"
  45. viewBox="0 0 32 32"
  46. >
  47. <path
  48. d="M10.5 7.5L5 10.955l5.5 3.454 5.5-3.454 5.5 3.454 5.5-3.454L21.5 7.5 16 10.955zM10.5 21.319L5 17.864l5.5-3.455 5.5 3.455zM16 17.864l5.5-3.455 5.5 3.455-5.5 3.455zM16 25.925l-5.5-3.455 5.5-3.454 5.5 3.454z"
  49. fill="currentcolor"
  50. fillRule="nonzero"
  51. />
  52. </svg>
  53. )
  54. this.opts.companionAllowedHosts = getAllowedHosts(
  55. this.opts.companionAllowedHosts,
  56. this.opts.companionUrl,
  57. )
  58. this.provider = new Provider(uppy, {
  59. companionUrl: this.opts.companionUrl,
  60. companionHeaders: this.opts.companionHeaders,
  61. companionKeysParams: this.opts.companionKeysParams,
  62. companionCookiesRule: this.opts.companionCookiesRule,
  63. provider: 'dropbox',
  64. pluginId: this.id,
  65. supportsRefreshToken: true,
  66. })
  67. this.defaultLocale = locale
  68. this.i18nInit()
  69. this.title = this.i18n('pluginNameDropbox')
  70. this.render = this.render.bind(this)
  71. }
  72. install(): void {
  73. this.view = new ProviderViews(this, {
  74. provider: this.provider,
  75. loadAllFiles: true,
  76. virtualList: true,
  77. })
  78. const { target } = this.opts
  79. if (target) {
  80. this.mount(target, this)
  81. }
  82. }
  83. uninstall(): void {
  84. this.view.tearDown()
  85. this.unmount()
  86. }
  87. render(state: unknown): ComponentChild {
  88. return this.view.render(state)
  89. }
  90. }