import { UIPlugin, Uppy } from '@uppy/core' import { ProviderViews } from '@uppy/provider-views' import { Provider, tokenStorage, getAllowedHosts, type CompanionPluginOptions, } from '@uppy/companion-client' import { h, type ComponentChild } from 'preact' import type { UppyFile, Body, Meta, AsyncStore, UnknownProviderPlugin, UnknownProviderPluginState, } from '@uppy/core' // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore We don't want TS to generate types for the package.json import packageJson from '../package.json' import locale from './locale.js' export type GooglePhotosOptions = CompanionPluginOptions export default class GooglePhotos extends UIPlugin implements UnknownProviderPlugin { static VERSION = packageJson.version icon: () => h.JSX.Element provider: Provider view!: ProviderViews storage: AsyncStore files: UppyFile[] rootFolderId: string | null = null constructor(uppy: Uppy, opts: GooglePhotosOptions) { super(uppy, opts) this.type = 'acquirer' this.storage = this.opts.storage || tokenStorage this.files = [] this.id = this.opts.id || 'GooglePhotos' this.icon = () => ( ) this.opts.companionAllowedHosts = getAllowedHosts( this.opts.companionAllowedHosts, this.opts.companionUrl, ) this.provider = new Provider(uppy, { companionUrl: this.opts.companionUrl, companionHeaders: this.opts.companionHeaders, companionKeysParams: this.opts.companionKeysParams, companionCookiesRule: this.opts.companionCookiesRule, provider: 'googlephotos', pluginId: this.id, supportsRefreshToken: true, }) this.defaultLocale = locale this.i18nInit() this.title = this.i18n('pluginNameGooglePhotos') this.render = this.render.bind(this) } install(): void { this.view = new ProviderViews(this, { provider: this.provider, loadAllFiles: true, }) const { target } = this.opts if (target) { this.mount(target, this) } } uninstall(): void { this.view.tearDown() this.unmount() } render(state: unknown): ComponentChild { const { partialTree, currentFolderId } = this.getPluginState() const foldersInThisFolder = partialTree.filter( (i) => i.type === 'folder' && i.parentId === currentFolderId, ) if (foldersInThisFolder.length === 0) { return this.view.render(state, { viewType: 'grid', showFilter: false, showTitles: false, }) } return this.view.render(state) } }