Skip to content

Commit

Permalink
@uppy/unsplash: refactor to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Murderlon authored and aduh95 committed Mar 10, 2024
1 parent 1723666 commit 9f95053
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 70 deletions.
1 change: 1 addition & 0 deletions packages/@uppy/unsplash/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tsconfig.*
69 changes: 0 additions & 69 deletions packages/@uppy/unsplash/src/Unsplash.jsx

This file was deleted.

108 changes: 108 additions & 0 deletions packages/@uppy/unsplash/src/Unsplash.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
getAllowedHosts,
tokenStorage,
type CompanionPluginOptions,
SearchProvider,
} from '@uppy/companion-client'
import { UIPlugin, Uppy } from '@uppy/core'
import { SearchProviderViews } from '@uppy/provider-views'
import { h, type ComponentChild } from 'preact'

import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile'
import type { UnknownSearchProviderPluginState } from '@uppy/core/lib/Uppy.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 UnsplashOptions = CompanionPluginOptions

export default class Unsplash<M extends Meta, B extends Body> extends UIPlugin<
UnsplashOptions,
M,
B,
UnknownSearchProviderPluginState
> {
static VERSION = packageJson.version

icon: () => JSX.Element

provider: SearchProvider<M, B>

view: SearchProviderViews<M, B>

storage: typeof tokenStorage

files: UppyFile<M, B>[]

hostname: string

constructor(uppy: Uppy<M, B>, opts: UnsplashOptions) {
super(uppy, opts)
this.type = 'acquirer'
this.files = []
this.storage = this.opts.storage || tokenStorage
this.id = this.opts.id || 'Unsplash'
this.title = this.opts.title || 'Unsplash'

this.icon = () => (
<svg
className="uppy-DashboardTab-iconUnsplash"
viewBox="0 0 32 32"
height="32"
width="32"
aria-hidden="true"
>
<g fill="currentcolor">
<path d="M46.575 10.883v-9h12v9zm12 5h10v18h-32v-18h10v9h12z" />
<path d="M13 12.5V8h6v4.5zm6 2.5h5v9H8v-9h5v4.5h6z" />
</g>
</svg>
)

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,
})

const { target } = this.opts
if (target) {
this.mount(target, this)
}
}

// eslint-disable-next-line class-methods-use-this
async onFirstRender(): Promise<void> {
// do nothing
}

render(state: unknown): ComponentChild {
return this.view.render(state)
}

uninstall(): void {
this.unmount()
}
}
1 change: 0 additions & 1 deletion packages/@uppy/unsplash/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/@uppy/unsplash/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Unsplash.tsx'
35 changes: 35 additions & 0 deletions packages/@uppy/unsplash/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"noImplicitAny": false,
"outDir": "./lib",
"paths": {
"@uppy/companion-client": ["../companion-client/src/index.js"],
"@uppy/companion-client/lib/*": ["../companion-client/src/*"],
"@uppy/provider-views": ["../provider-views/src/index.js"],
"@uppy/provider-views/lib/*": ["../provider-views/src/*"],
"@uppy/utils/lib/*": ["../utils/src/*"],
"@uppy/core": ["../core/src/index.js"],
"@uppy/core/lib/*": ["../core/src/*"]
},
"resolveJsonModule": false,
"rootDir": "./src",
"skipLibCheck": true
},
"include": ["./src/**/*.*"],
"exclude": ["./src/**/*.test.ts"],
"references": [
{
"path": "../companion-client/tsconfig.build.json"
},
{
"path": "../provider-views/tsconfig.build.json"
},
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../core/tsconfig.build.json"
}
]
}
31 changes: 31 additions & 0 deletions packages/@uppy/unsplash/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true,
"paths": {
"@uppy/companion-client": ["../companion-client/src/index.js"],
"@uppy/companion-client/lib/*": ["../companion-client/src/*"],
"@uppy/provider-views": ["../provider-views/src/index.js"],
"@uppy/provider-views/lib/*": ["../provider-views/src/*"],
"@uppy/utils/lib/*": ["../utils/src/*"],
"@uppy/core": ["../core/src/index.js"],
"@uppy/core/lib/*": ["../core/src/*"],
},
},
"include": ["./package.json", "./src/**/*.*"],
"references": [
{
"path": "../companion-client/tsconfig.build.json",
},
{
"path": "../provider-views/tsconfig.build.json",
},
{
"path": "../utils/tsconfig.build.json",
},
{
"path": "../core/tsconfig.build.json",
},
],
}

0 comments on commit 9f95053

Please sign in to comment.