|
@@ -0,0 +1,127 @@
|
|
|
+import {
|
|
|
+ Provider,
|
|
|
+ getAllowedHosts,
|
|
|
+ tokenStorage,
|
|
|
+ type CompanionPluginOptions,
|
|
|
+} from '@uppy/companion-client'
|
|
|
+import { UIPlugin, Uppy } from '@uppy/core'
|
|
|
+import { ProviderViews } from '@uppy/provider-views'
|
|
|
+import { h, type ComponentChild } from 'preact'
|
|
|
+
|
|
|
+import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile'
|
|
|
+import type { UnknownProviderPluginState } from '@uppy/core/lib/Uppy.ts'
|
|
|
+import locale from './locale.ts'
|
|
|
+// 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'
|
|
|
+
|
|
|
+export type FacebookOptions = CompanionPluginOptions
|
|
|
+
|
|
|
+export default class Facebook<M extends Meta, B extends Body> extends UIPlugin<
|
|
|
+ FacebookOptions,
|
|
|
+ M,
|
|
|
+ B,
|
|
|
+ UnknownProviderPluginState
|
|
|
+> {
|
|
|
+ static VERSION = packageJson.version
|
|
|
+
|
|
|
+ icon: () => JSX.Element
|
|
|
+
|
|
|
+ provider: Provider<M, B>
|
|
|
+
|
|
|
+ view: ProviderViews<M, B>
|
|
|
+
|
|
|
+ storage: typeof tokenStorage
|
|
|
+
|
|
|
+ files: UppyFile<M, B>[]
|
|
|
+
|
|
|
+ constructor(uppy: Uppy<M, B>, opts: FacebookOptions) {
|
|
|
+ super(uppy, opts)
|
|
|
+ this.id = this.opts.id || 'Facebook'
|
|
|
+ this.type = 'acquirer'
|
|
|
+ this.storage = this.opts.storage || tokenStorage
|
|
|
+ this.files = []
|
|
|
+ this.icon = () => (
|
|
|
+ <svg
|
|
|
+ aria-hidden="true"
|
|
|
+ focusable="false"
|
|
|
+ width="32"
|
|
|
+ height="32"
|
|
|
+ viewBox="0 0 32 32"
|
|
|
+ >
|
|
|
+ <g fill="none" fillRule="evenodd">
|
|
|
+ <path
|
|
|
+ d="M27 16c0-6.075-4.925-11-11-11S5 9.925 5 16c0 5.49 4.023 10.041 9.281 10.866V19.18h-2.793V16h2.793v-2.423c0-2.757 1.642-4.28 4.155-4.28 1.204 0 2.462.215 2.462.215v2.707h-1.387c-1.366 0-1.792.848-1.792 1.718V16h3.05l-.487 3.18h-2.563v7.686C22.977 26.041 27 21.49 27 16"
|
|
|
+ fill="#1777F2"
|
|
|
+ />
|
|
|
+ <path
|
|
|
+ d="M20.282 19.18L20.77 16h-3.051v-2.063c0-.87.426-1.718 1.792-1.718h1.387V9.512s-1.258-.215-2.462-.215c-2.513 0-4.155 1.523-4.155 4.28V16h-2.793v3.18h2.793v7.686a11.082 11.082 0 003.438 0V19.18h2.563"
|
|
|
+ fill="#FFFFFE"
|
|
|
+ />
|
|
|
+ </g>
|
|
|
+ </svg>
|
|
|
+ )
|
|
|
+
|
|
|
+ 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: 'facebook',
|
|
|
+ pluginId: this.id,
|
|
|
+ supportsRefreshToken: false,
|
|
|
+ })
|
|
|
+
|
|
|
+ this.defaultLocale = locale
|
|
|
+
|
|
|
+ this.i18nInit()
|
|
|
+ this.title = this.i18n('pluginNameFacebook')
|
|
|
+
|
|
|
+ this.onFirstRender = this.onFirstRender.bind(this)
|
|
|
+ this.render = this.render.bind(this)
|
|
|
+ }
|
|
|
+
|
|
|
+ install(): void {
|
|
|
+ this.view = new ProviderViews(this, {
|
|
|
+ provider: this.provider,
|
|
|
+ })
|
|
|
+
|
|
|
+ const { target } = this.opts
|
|
|
+ if (target) {
|
|
|
+ this.mount(target, this)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ uninstall(): void {
|
|
|
+ this.view.tearDown()
|
|
|
+ this.unmount()
|
|
|
+ }
|
|
|
+
|
|
|
+ async onFirstRender(): Promise<void> {
|
|
|
+ await Promise.all([
|
|
|
+ this.provider.fetchPreAuthToken(),
|
|
|
+ this.view.getFolder(),
|
|
|
+ ])
|
|
|
+ }
|
|
|
+
|
|
|
+ render(state: unknown): ComponentChild {
|
|
|
+ const viewOptions: {
|
|
|
+ viewType?: string
|
|
|
+ showFilter?: boolean
|
|
|
+ showTitles?: boolean
|
|
|
+ } = {}
|
|
|
+ if (
|
|
|
+ this.getPluginState().files.length &&
|
|
|
+ !this.getPluginState().folders.length
|
|
|
+ ) {
|
|
|
+ viewOptions.viewType = 'grid'
|
|
|
+ viewOptions.showFilter = false
|
|
|
+ viewOptions.showTitles = false
|
|
|
+ }
|
|
|
+ return this.view.render(state, viewOptions)
|
|
|
+ }
|
|
|
+}
|