Unsplash.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {
  2. getAllowedHosts,
  3. tokenStorage,
  4. type CompanionPluginOptions,
  5. SearchProvider,
  6. } from '@uppy/companion-client'
  7. import { UIPlugin, Uppy } from '@uppy/core'
  8. import { SearchProviderViews } 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 { UnknownSearchProviderPluginState } 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. export type UnsplashOptions = CompanionPluginOptions
  16. export default class Unsplash<M extends Meta, B extends Body> extends UIPlugin<
  17. UnsplashOptions,
  18. M,
  19. B,
  20. UnknownSearchProviderPluginState
  21. > {
  22. static VERSION = packageJson.version
  23. icon: () => JSX.Element
  24. provider: SearchProvider<M, B>
  25. view: SearchProviderViews<M, B>
  26. storage: typeof tokenStorage
  27. files: UppyFile<M, B>[]
  28. hostname: string
  29. constructor(uppy: Uppy<M, B>, opts: UnsplashOptions) {
  30. super(uppy, opts)
  31. this.type = 'acquirer'
  32. this.files = []
  33. this.storage = this.opts.storage || tokenStorage
  34. this.id = this.opts.id || 'Unsplash'
  35. this.title = this.opts.title || 'Unsplash'
  36. this.icon = () => (
  37. <svg
  38. className="uppy-DashboardTab-iconUnsplash"
  39. viewBox="0 0 32 32"
  40. height="32"
  41. width="32"
  42. aria-hidden="true"
  43. >
  44. <g fill="currentcolor">
  45. <path d="M46.575 10.883v-9h12v9zm12 5h10v18h-32v-18h10v9h12z" />
  46. <path d="M13 12.5V8h6v4.5zm6 2.5h5v9H8v-9h5v4.5h6z" />
  47. </g>
  48. </svg>
  49. )
  50. if (!this.opts.companionUrl) {
  51. throw new Error(
  52. 'Companion hostname is required, please consult https://uppy.io/docs/companion',
  53. )
  54. }
  55. this.hostname = this.opts.companionUrl
  56. this.opts.companionAllowedHosts = getAllowedHosts(
  57. this.opts.companionAllowedHosts,
  58. this.opts.companionUrl,
  59. )
  60. this.provider = new SearchProvider(uppy, {
  61. companionUrl: this.opts.companionUrl,
  62. companionHeaders: this.opts.companionHeaders,
  63. companionCookiesRule: this.opts.companionCookiesRule,
  64. provider: 'unsplash',
  65. pluginId: this.id,
  66. })
  67. }
  68. install(): void {
  69. this.view = new SearchProviderViews(this, {
  70. provider: this.provider,
  71. viewType: 'unsplash',
  72. showFilter: true,
  73. })
  74. const { target } = this.opts
  75. if (target) {
  76. this.mount(target, this)
  77. }
  78. }
  79. // eslint-disable-next-line class-methods-use-this
  80. async onFirstRender(): Promise<void> {
  81. // do nothing
  82. }
  83. render(state: unknown): ComponentChild {
  84. return this.view.render(state)
  85. }
  86. uninstall(): void {
  87. this.unmount()
  88. }
  89. }