App.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* eslint-disable */
  2. import React from 'react'
  3. import { createRoot } from 'react-dom/client'
  4. import Uppy, {
  5. UIPlugin,
  6. type Meta,
  7. type Body,
  8. type UIPluginOptions,
  9. type State,
  10. } from '@uppy/core'
  11. import Tus from '@uppy/tus'
  12. import Webcam from '@uppy/webcam'
  13. import { Dashboard, useUppyState } from '@uppy/react'
  14. import '@uppy/core/dist/style.css'
  15. import '@uppy/dashboard/dist/style.css'
  16. import '@uppy/webcam/dist/style.css'
  17. interface MyPluginOptions extends UIPluginOptions {}
  18. interface MyPluginState extends Record<string, unknown> {}
  19. // Custom plugin example inside React
  20. class MyPlugin<M extends Meta, B extends Body> extends UIPlugin<
  21. MyPluginOptions,
  22. M,
  23. B,
  24. MyPluginState
  25. > {
  26. container!: HTMLElement
  27. constructor(uppy: Uppy<M, B>, opts?: MyPluginOptions) {
  28. super(uppy, opts)
  29. this.type = 'acquirer'
  30. this.id = this.opts.id || 'TEST'
  31. this.title = 'Test'
  32. }
  33. override install() {
  34. const { target } = this.opts
  35. if (target) {
  36. this.mount(target, this)
  37. }
  38. }
  39. override uninstall() {
  40. this.unmount()
  41. }
  42. override render(state: State<M, B>, container: HTMLElement) {
  43. // Important: during the initial render is not defined. Safely return.
  44. if (!container) return
  45. createRoot(container).render(
  46. <h2>React component inside Uppy's Preact UI</h2>,
  47. )
  48. }
  49. }
  50. const metaFields = [
  51. { id: 'license', name: 'License', placeholder: 'specify license' },
  52. ]
  53. function createUppy() {
  54. return new Uppy({ restrictions: { requiredMetaFields: ['license'] } })
  55. .use(Tus, { endpoint: 'https://tusd.tusdemo.net/files/' })
  56. .use(Webcam)
  57. .use(MyPlugin)
  58. }
  59. export default function App() {
  60. // IMPORTANT: passing an initaliser function to useState
  61. // to prevent creating a new Uppy instance on every render.
  62. // useMemo is a performance hint, not a guarantee.
  63. const [uppy] = React.useState(createUppy)
  64. // You can access state reactively with useUppyState
  65. const fileCount = useUppyState(
  66. uppy,
  67. (state) => Object.keys(state.files).length,
  68. )
  69. const totalProgress = useUppyState(uppy, (state) => state.totalProgress)
  70. // Also possible to get the state of all plugins.
  71. const plugins = useUppyState(uppy, (state) => state.plugins)
  72. return (
  73. <>
  74. <p>File count: {fileCount}</p>
  75. <p>Total progress: {totalProgress}</p>
  76. <Dashboard uppy={uppy} metaFields={metaFields} />
  77. </>
  78. )
  79. }