Files
Compass/web/components/editor/upload-extension.tsx
Martin Braquet ba9b3cfb06 Add pretty formatting (#29)
* Test

* Add pretty formatting

* Fix Tests

* Fix Tests

* Fix Tests

* Fix

* Add pretty formatting fix

* Fix

* Test

* Fix tests

* Clean typeckech

* Add prettier check

* Fix api tsconfig

* Fix api tsconfig

* Fix tsconfig

* Fix

* Fix

* Prettier
2026-02-20 17:32:27 +01:00

34 lines
961 B
TypeScript

import {Editor, Extension} from '@tiptap/core'
import toast from 'react-hot-toast'
import {useMutation} from 'web/hooks/use-mutation'
import {uploadImage} from 'web/lib/firebase/storage'
export const Upload = Extension.create({
name: 'upload',
addStorage: () => ({mutation: {}}),
})
export const useUploadMutation = (editor: Editor | null) =>
useMutation(
(files: File[]) =>
// TODO: Images should be uploaded under a particular username
Promise.all(files.map((file) => uploadImage('default', file))),
{
onSuccess(urls) {
if (!editor || !urls.length) return
let trans = editor.chain().focus()
urls.forEach((src) => {
trans = trans.setImage({src})
trans = trans.createParagraphNear()
})
trans.run()
},
onError(error: any) {
toast.error(error.message ?? error)
},
},
)
export type UploadMutation = ReturnType<typeof useUploadMutation>