Connect, collaborate, and grow with a community of TypeScript developers
Fix uploadthing (#1404)
Sean Boult12750a0a0840
diff --git a/apps/web/package.json b/apps/web/package.jsonindex b68f4dd8..b6430e4a 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -40,7 +40,7 @@"@tanstack/react-query-devtools": "^5.8.1","@tanstack/react-table": "^8.9.3","@uiw/react-md-editor": "^3.23.3",- "@uploadthing/react": "^5.5.0",+ "@uploadthing/react": "^6.2.2","@vercel/analytics": "^1.1.1","@vercel/edge-config": "^0.2.1","@vercel/toolbar": "^0.1.1",@@ -76,7 +76,7 @@"tailwindcss-animate": "^1.0.6","unified": "^10.1.2","unist-util-visit": "^5.0.0",- "uploadthing": "^5.5.2",+ "uploadthing": "6.3.1","zod": "^3.21.4","zustand": "^4.3.9"},diff --git a/apps/web/src/app/api/uploadthing/core.ts b/apps/web/src/app/api/uploadthing/core.tsindex e31cbbea..010a2468 100644--- a/apps/web/src/app/api/uploadthing/core.ts+++ b/apps/web/src/app/api/uploadthing/core.ts@@ -2,7 +2,18 @@ import { createUploadthing, type FileRouter } from 'uploadthing/next';import { prisma } from '@repo/db';import { auth } from '@repo/auth/server';-const f = createUploadthing();+const f = createUploadthing({+ /**+ * Log out more information about the error, but don't return it to the client+ * @see https://docs.uploadthing.com/errors#error-formatting+ */+ errorFormatter: (err) => {+ console.log('Error uploading file', err.message);+ console.log(' - Above error caused by:', err.cause);++ return { message: err.message };+ },+});type ValidFileTypes = 'audio' | 'blob' | 'image' | 'video';type FileRouterInput =diff --git a/apps/web/src/components/rich-markdown-editor.tsx b/apps/web/src/components/rich-markdown-editor.tsxindex 0504a8d3..4be7f62f 100644--- a/apps/web/src/components/rich-markdown-editor.tsx+++ b/apps/web/src/components/rich-markdown-editor.tsx@@ -94,12 +94,13 @@ export function RichMarkdownEditor({const { startUpload } = useUploadThing('imageUploader', {onClientUploadComplete: (res) => {if (!res) return;- // fist file in the array- const uploadedFile = res[0];+ // first file in the array+ const [uploadedFile] = res;// insert string at cursor position by calling on change+ const markdownImageEmbed = ``;insertText(- ``,+ markdownImageEmbed,// @ts-expect-erroreditorRef.current.textarea,);@@ -142,24 +143,23 @@ export function RichMarkdownEditor({}, [startUpload, allowImageUpload]);const handlePasta = async (event: React.ClipboardEvent<HTMLDivElement>) => {+ event.preventDefault();+// only allow image if it's enabledif (!allowImageUpload) return;- const items = event.clipboardData.items;- if (!items) return;- // upload the image to and endpoint- for (const item of items) {- if (item.kind === 'file') {- const blob = item.getAsFile();- if (!blob) return;+ const [firstImage] = event.clipboardData.items;+ if (!firstImage) return;+ if (firstImage.kind !== 'file') return;- // update state for the loading spinner- setIsImageUploading(true);+ const blob = firstImage.getAsFile();+ if (!blob) return;- // upload to the endpoint- await startUpload([blob]);- }- }+ // update state for the loading spinner+ setIsImageUploading(true);++ // upload to the endpoint+ await startUpload([blob]);};return (diff --git a/apps/web/src/utils/domUtils.ts b/apps/web/src/utils/domUtils.tsindex 7d16d0d4..81184439 100644--- a/apps/web/src/utils/domUtils.ts+++ b/apps/web/src/utils/domUtils.ts@@ -1,23 +1,29 @@/**- * This will paste the given text into the textarea element+ * This will insert the given text at the caret position into the textarea element*/export function insertText(newText: string, textarea: HTMLTextAreaElement) {- // make sure it's selected when we go to pasta- textarea.focus();+ // @ts-ignore - i don't care that you error+ const nativeInputValueSetter = Object.getOwnPropertyDescriptor(+ window.HTMLTextAreaElement.prototype,+ 'value',+ ).set;++ if (!nativeInputValueSetter) return;++ // get left value of the textarea+ const left = textarea.value.substring(0, textarea.selectionStart);- let pasted = true;- try {- if (!document.execCommand('insertText', false, newText)) {- pasted = false;- }- } catch (e) {- console.error('error caught:', e);- pasted = false;- }+ // get right value of the textarea+ const right = textarea.value.substring(textarea.selectionEnd, textarea.value.length);+ nativeInputValueSetter.call(textarea, left + newText + right);- if (!pasted) {- console.error('paste unsuccessful, execCommand not supported');- }+ // set the caret position to the end of the new text+ const newCaretPosition = left.length + newText.length;++ // make sure to keep focus on the textarea+ textarea.focus();+ textarea.setSelectionRange(newCaretPosition, newCaretPosition);- return pasted;+ const inputEvent = new Event('input', { bubbles: true });+ textarea.dispatchEvent(inputEvent);}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 8c456149..860e2b36 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -305,8 +305,8 @@ importers:specifier: ^3.23.3'@uploadthing/react':- specifier: ^5.5.0+ specifier: ^6.2.2'@vercel/analytics':specifier: ^1.1.1version: 1.1.1@@ -413,8 +413,8 @@ importers:specifier: ^5.0.0version: 5.0.0uploadthing:- specifier: ^5.5.2- version: 5.5.2([email protected])+ specifier: 6.3.1+ version: 6.3.1zod:specifier: ^3.21.4version: 3.21.4@@ -764,7 +764,7 @@ importers:dependencies:'@next/eslint-plugin-next':specifier: canary- version: 14.0.5-canary.35+ version: 14.0.5-canary.52'@repo/tsconfig':specifier: workspace:*version: link:../config-typescript@@ -776,7 +776,7 @@ importers:version: 5.62.0([email protected])([email protected])'@vercel/style-guide':specifier: ^4.0.2eslint:specifier: ^8.45.0version: 8.45.0@@ -2814,8 +2814,8 @@ packages:glob: 7.1.7dev: true- /@next/[email protected]:- resolution: {integrity: sha512-E49+IC/7hwVFl9Z5bdPvS95MYtqAR6Zsza39vGGtDrZi3uxRe4b1DXdTlLuYpIT+LOLrZLYx/j9hoiViYmfGgQ==}+ /@next/[email protected]:+ resolution: {integrity: sha512-jdYG/tA65388cqi8fhx4eIm/qxUNh+v7Qvx578+BaZyv72RPuw74eReAfbJoidv5YAnJ5upXrJcXxO/9ir2xGQ==}dependencies:glob: 7.1.7dev: false@@ -3432,7 +3432,7 @@ packages:is-glob: 4.0.3open: 9.1.0picocolors: 1.0.0- tslib: 2.6.0+ tslib: 2.6.2/@playwright/[email protected]:resolution: {integrity: sha512-xis/RXXsLxwThKnlIXouxmIvvT3zvQj1JE39GsNieMUrMpb3/GySHDh2j8itCG22qKVD4MYLBp7xB73cUW/UUw==}@@ -5091,7 +5091,7 @@ packages:'@sentry/core': 7.73.0'@sentry/types': 7.73.0'@sentry/utils': 7.73.0- tslib: 2.6.0+ tslib: 2.6.2dev: false/@sentry/[email protected]:@@ -5103,7 +5103,7 @@ packages:'@sentry/replay': 7.73.0'@sentry/types': 7.73.0'@sentry/utils': 7.73.0- tslib: 2.6.0+ tslib: 2.6.2dev: false/@sentry/[email protected]:@@ -5898,38 +5898,49 @@ packages:/@ungap/[email protected]:resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}- /@uploadthing/[email protected]:- resolution: {integrity: sha512-UDySsPbdi7wp52WG7mp5CiZwaTvokDubqhm1xhFm73Bqhp0PsGbwSSF5PdCXH4fe8Y4EKGrMyWQ4i1fF9+DVVQ==}+ /@uploadthing/[email protected]:+ resolution: {integrity: sha512-ohlnFQfNr/UmImfipVo4uiBj1HQL7dp/sWBD96iAajc6RaQuYnHATGaSaISpCjZTeoLkksuu4/LR8vU9nSzpvQ==}+ dependencies:+ attr-accept: 2.2.2+ file-selector: 0.6.0+ dev: false++ /@uploadthing/[email protected]:+ resolution: {integrity: sha512-ZUo1JHOPPMZDsUw1mOhhVDIvJGlsjj6T0xJ/YJtulyJwL43S9B5pxg1cHcRuTEgjaxj7B55jiqQ6r9mDrrjH9A==}dev: false- /@uploadthing/[email protected]([email protected])([email protected])([email protected])([email protected]):- resolution: {integrity: sha512-50VduVEqDJwEyT9GDxCAzI5V28/5tD9Rddsmwt/ciCJG8J+sg3C9oHAvhEm9MWYf3h0YVi/fgtpYLg64Y8PSAA==}+ resolution: {integrity: sha512-fDfVooLTbC5/FM1o/chPDf5i8DRxCOeRSuRl0R6yh/jIHTdDO2kwXeM5DTaBazJmaEabh5GB+I9Rwg8nU0QdVA==}peerDependencies:+ next: '*'react: ^17.0.2 || ^18.0.0- react-dropzone: ^14.2.3- uploadthing: ^5.0.0+ uploadthing: ^6.0.0+ peerDependenciesMeta:+ next:+ optional: truedependencies:- '@uploadthing/shared': 5.2.1(@uploadthing/[email protected])([email protected])+ '@uploadthing/dropzone': 0.1.1+ '@uploadthing/shared': 6.2.0(@uploadthing/[email protected])+ attr-accept: 2.2.2+ file-selector: 0.6.0react: 18.2.0- react-dropzone: 14.2.3([email protected])tailwind-merge: 1.14.0- uploadthing: 5.5.2([email protected])+ uploadthing: 6.3.1transitivePeerDependencies:- '@uploadthing/mime-types'- - zoddev: false- resolution: {integrity: sha512-ISb78wuHqU5F/7TVw/3eO0VHQdSx3qdN7I4R4knHynuWFjgJjfUsc+zAAAtpo0UZlwfjIQIv/WtSwxdlNxRPGw==}+ /@uploadthing/[email protected](@uploadthing/[email protected]):+ resolution: {integrity: sha512-GMC1gwZa9X48RZwuY7gGse7E8TXYMAMUNaYWEQL2ctVhMZv7viCPyMyZ0yMqBsQmOeGfbmycPnUeNE9IuesJGg==}peerDependencies:- '@uploadthing/mime-types': ^0.2.0- zod: ^3.21.4+ '@uploadthing/mime-types': ^0.2.2peerDependenciesMeta:'@uploadthing/mime-types':optional: truedependencies:- '@uploadthing/mime-types': 0.2.1- zod: 3.21.4+ '@uploadthing/mime-types': 0.2.2+ std-env: 3.7.0dev: false/@vercel/[email protected]:@@ -5979,7 +5990,7 @@ packages:yoga-wasm-web: 0.3.0dev: false- /@vercel/[email protected](@next/[email protected])([email protected])([email protected])([email protected]):+ /@vercel/[email protected](@next/[email protected])([email protected])([email protected])([email protected]):resolution: {integrity: sha512-FroL+oOePzhw7n/I+f7zr4WNroGHT/+2TlW6WH9+CVSjMNsEyu7Qstj2mI5gWIBjT1Y2ZImKPppCzI2cIYmNZw==}engines: {node: '>=16'}peerDependencies:@@ -5999,7 +6010,7 @@ packages:dependencies:'@babel/core': 7.22.10'@babel/eslint-parser': 7.22.10(@babel/[email protected])([email protected])- '@next/eslint-plugin-next': 14.0.5-canary.35+ '@next/eslint-plugin-next': 14.0.5-canary.52'@rushstack/eslint-patch': 1.3.2'@typescript-eslint/eslint-plugin': 5.59.1(@typescript-eslint/[email protected])([email protected])([email protected])'@typescript-eslint/parser': 5.62.0([email protected])([email protected])@@ -7111,7 +7122,6 @@ packages:resolution: {integrity: sha512-I5qxpzLv+sJhTVEoLYNcTW+bThDCPsit0vLNKShZx6rLtpilNpmmeTPaeqJb9ZE9dV3DGaeby6Vuhrw38WjeyQ==}engines: {node: ^14.18.0 || >=16.10.0}- dev: trueresolution: {integrity: sha512-ty/fTekppD2fIwRvnZAVdeOiGd1c7YXEixbgJTNzqcxJWKQnjJ/V1bNEEE6hygpM3WjwHFUVK6HTjWSzV4a8sQ==}@@ -12394,18 +12404,6 @@ packages:scheduler: 0.23.0dev: false- resolution: {integrity: sha512-O3om8I+PkFKbxCukfIR3QAGftYXDZfOE2N1mr/7qebQJHs7U+/RSL/9xomJNpRg9kM5h9soQSdf0Gc7OHF5Fug==}- engines: {node: '>= 10.13'}- peerDependencies:- react: '>= 16.8 || 18.0.0'- dependencies:- attr-accept: 2.2.2- file-selector: 0.6.0- prop-types: 15.8.1- react: 18.2.0- dev: false-resolution: {integrity: sha512-pD+uVG6GoJR03Kzkk5KHPlc/TtUZ/UYs/hfakeRP6Ma/Oja0fOw4gjYGXQo0zwwqEM5OdLQOdS3qBCJyRYrqVw==}engines: {node: '>=16.0.0'}@@ -12537,7 +12535,7 @@ packages:'@types/react': 18.2.20react: 18.2.0react-style-singleton: 2.2.1(@types/[email protected])([email protected])- tslib: 2.6.0+ tslib: 2.6.2dev: false@@ -12554,7 +12552,7 @@ packages:react: 18.2.0react-remove-scroll-bar: 2.3.4(@types/[email protected])([email protected])react-style-singleton: 2.2.1(@types/[email protected])([email protected])- tslib: 2.6.0+ tslib: 2.6.2use-callback-ref: 1.3.0(@types/[email protected])([email protected])use-sidecar: 1.1.2(@types/[email protected])([email protected])dev: false@@ -12613,7 +12611,7 @@ packages:get-nonce: 1.0.1invariant: 2.2.4react: 18.2.0- tslib: 2.6.0+ tslib: 2.6.2dev: false@@ -13407,7 +13405,6 @@ packages:resolution: {integrity: sha512-JPbdCEQLj1w5GilpiHAx3qJvFndqybBysA3qUOnznweH4QbNYUsW/ea8QzSrnh0vNsezMMw5bcVool8lM0gwzg==}- dev: trueresolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==}@@ -13873,7 +13870,6 @@ packages:resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==}- dev: falseresolution: {integrity: sha512-FGZZNikQYGlEPtKEnz04hEniEO9ctP0rfWOdDeJCpFY4sqQe+TE90YNsv/1g0FshXoGYAOTFDYAVHg6Ovh003Q==}@@ -14411,13 +14407,14 @@ packages:escalade: 3.1.1picocolors: 1.0.0- resolution: {integrity: sha512-PvCPzXHr1xMFh5qN9ef9gL1KOeQhKLII58bOEwT6+DaN8nWmFtXRd66PDMkoEInYuC7U1Cacuvc1oniEQF5pAw==}+ resolution: {integrity: sha512-eqbUpxSYtofT/aqqXHlaNJhpK94st6rGeEr8d2QmL0gHWKauS9w7J4jQ5Q7Jfmb2kMJj+pXW3duydkev/5G6Mw==}+ engines: {node: '>=18.13.0'}dependencies:- '@uploadthing/mime-types': 0.2.1- '@uploadthing/shared': 5.2.1(@uploadthing/[email protected])([email protected])- transitivePeerDependencies:- - zod+ '@uploadthing/mime-types': 0.2.2+ '@uploadthing/shared': 6.2.0(@uploadthing/[email protected])+ consola: 3.2.3+ std-env: 3.7.0dev: false@@ -14445,7 +14442,7 @@ packages:dependencies:'@types/react': 18.2.20react: 18.2.0- tslib: 2.6.0+ tslib: 2.6.2dev: false@@ -14474,7 +14471,7 @@ packages:'@types/react': 18.2.20detect-node-es: 1.1.0react: 18.2.0- tslib: 2.6.0+ tslib: 2.6.2dev: false