import { type CompanionPluginOptions, getAllowedHosts, SearchProvider, tokenStorage, } from '@uppy/companion-client' import type { AsyncStore, Body, Meta, UnknownSearchProviderPlugin, UnknownSearchProviderPluginState, UppyFile, } from '@uppy/core' import { UIPlugin, type Uppy } from '@uppy/core' import { SearchProviderViews } from '@uppy/provider-views' import type { LocaleStrings } from '@uppy/utils' // biome-ignore lint/style/useImportType: h is not a type import { type ComponentChild, h } from 'preact' import packageJson from '../package.json' with { type: 'json' } import locale from './locale.js' export type UnsplashOptions = { utmSource?: string locale?: LocaleStrings } & CompanionPluginOptions export default class Unsplash extends UIPlugin implements UnknownSearchProviderPlugin { static VERSION = packageJson.version icon: () => h.JSX.Element provider: SearchProvider view!: SearchProviderViews storage: AsyncStore files: UppyFile[] hostname: string constructor(uppy: Uppy, opts: UnsplashOptions) { super(uppy, opts) this.type = 'acquirer' this.files = [] this.storage = this.opts.storage || tokenStorage this.id = this.opts.id || 'Unsplash' this.defaultLocale = locale this.i18nInit() this.title = this.i18n('pluginNameUnsplash') this.icon = () => ( ) if (!this.opts.companionUrl) { throw new Error( 'Companion hostname is required, please consult https://uppy.io/docs/companion', ) } this.hostname = this.opts.companionUrl this.opts.companionAllowedHosts = getAllowedHosts( this.opts.companionAllowedHosts, this.opts.companionUrl, ) this.provider = new SearchProvider(uppy, { companionUrl: this.opts.companionUrl, companionHeaders: this.opts.companionHeaders, companionCookiesRule: this.opts.companionCookiesRule, provider: 'unsplash', pluginId: this.id, }) } install(): void { this.view = new SearchProviderViews(this, { provider: this.provider, viewType: 'unsplash', showFilter: true, utmSource: this.opts.utmSource, }) const { target } = this.opts if (target) { this.mount(target, this) } } render(state: unknown): ComponentChild { return this.view.render(state) } uninstall(): void { this.unmount() } } declare module '@uppy/core' { export interface PluginTypeRegistry { Unsplash: Unsplash } }