GooglePhotos.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { UIPlugin, Uppy } from '@uppy/core'
  2. import { ProviderViews } from '@uppy/provider-views'
  3. import {
  4. Provider,
  5. tokenStorage,
  6. getAllowedHosts,
  7. type CompanionPluginOptions,
  8. } from '@uppy/companion-client'
  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. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  13. // @ts-ignore We don't want TS to generate types for the package.json
  14. import packageJson from '../package.json'
  15. import locale from './locale.ts'
  16. export type GooglePhotosOptions = CompanionPluginOptions
  17. export default class GooglePhotos<
  18. M extends Meta,
  19. B extends Body,
  20. > extends UIPlugin<GooglePhotosOptions, M, B, UnknownProviderPluginState> {
  21. static VERSION = packageJson.version
  22. icon: () => h.JSX.Element
  23. provider: Provider<M, B>
  24. view: ProviderViews<M, B>
  25. storage: typeof tokenStorage
  26. files: UppyFile<M, B>[]
  27. constructor(uppy: Uppy<M, B>, opts: GooglePhotosOptions) {
  28. super(uppy, opts)
  29. this.type = 'acquirer'
  30. this.storage = this.opts.storage || tokenStorage
  31. this.files = []
  32. this.id = this.opts.id || 'GooglePhotos'
  33. this.icon = () => (
  34. <svg
  35. aria-hidden="true"
  36. focusable="false"
  37. width="32"
  38. height="32"
  39. viewBox="-7 -7 73 73"
  40. >
  41. <g fill="none" fill-rule="evenodd">
  42. <path d="M-3-3h64v64H-3z" />
  43. <g fill-rule="nonzero">
  44. <path
  45. fill="#FBBC04"
  46. d="M14.8 13.4c8.1 0 14.7 6.6 14.7 14.8v1.3H1.3c-.7 0-1.3-.6-1.3-1.3C0 20 6.6 13.4 14.8 13.4z"
  47. />
  48. <path
  49. fill="#EA4335"
  50. d="M45.6 14.8c0 8.1-6.6 14.7-14.8 14.7h-1.3V1.3c0-.7.6-1.3 1.3-1.3C39 0 45.6 6.6 45.6 14.8z"
  51. />
  52. <path
  53. fill="#4285F4"
  54. d="M44.3 45.6c-8.2 0-14.8-6.6-14.8-14.8v-1.3h28.2c.7 0 1.3.6 1.3 1.3 0 8.2-6.6 14.8-14.8 14.8z"
  55. />
  56. <path
  57. fill="#34A853"
  58. d="M13.4 44.3c0-8.2 6.6-14.8 14.8-14.8h1.3v28.2c0 .7-.6 1.3-1.3 1.3-8.2 0-14.8-6.6-14.8-14.8z"
  59. />
  60. </g>
  61. </g>
  62. </svg>
  63. )
  64. this.opts.companionAllowedHosts = getAllowedHosts(
  65. this.opts.companionAllowedHosts,
  66. this.opts.companionUrl,
  67. )
  68. this.provider = new Provider(uppy, {
  69. companionUrl: this.opts.companionUrl,
  70. companionHeaders: this.opts.companionHeaders,
  71. companionKeysParams: this.opts.companionKeysParams,
  72. companionCookiesRule: this.opts.companionCookiesRule,
  73. provider: 'googlephotos',
  74. pluginId: this.id,
  75. supportsRefreshToken: true,
  76. })
  77. this.defaultLocale = locale
  78. this.i18nInit()
  79. this.title = this.i18n('pluginNameGooglePhotos')
  80. this.onFirstRender = this.onFirstRender.bind(this)
  81. this.render = this.render.bind(this)
  82. }
  83. install(): void {
  84. this.view = new ProviderViews(this, {
  85. provider: this.provider,
  86. loadAllFiles: true,
  87. })
  88. const { target } = this.opts
  89. if (target) {
  90. this.mount(target, this)
  91. }
  92. }
  93. uninstall(): void {
  94. this.view.tearDown()
  95. this.unmount()
  96. }
  97. async onFirstRender(): Promise<void> {
  98. await Promise.all([
  99. this.provider.fetchPreAuthToken(),
  100. this.view.getFolder(),
  101. ])
  102. }
  103. render(state: unknown): ComponentChild {
  104. if (
  105. this.getPluginState().files.length &&
  106. !this.getPluginState().folders.length
  107. ) {
  108. return this.view.render(state, {
  109. viewType: 'grid',
  110. showFilter: false,
  111. showTitles: false,
  112. })
  113. }
  114. return this.view.render(state)
  115. }
  116. }