♻️(frontend) add hooks useUploadFile
Move upload file logic to hooks useUploadFile. It will be more readable and easy to reuse.
This commit is contained in:
@@ -4,16 +4,14 @@ import { BlockNoteView } from '@blocknote/mantine';
|
|||||||
import '@blocknote/mantine/style.css';
|
import '@blocknote/mantine/style.css';
|
||||||
import { useCreateBlockNote } from '@blocknote/react';
|
import { useCreateBlockNote } from '@blocknote/react';
|
||||||
import { HocuspocusProvider } from '@hocuspocus/provider';
|
import { HocuspocusProvider } from '@hocuspocus/provider';
|
||||||
import { t } from 'i18next';
|
import React, { useEffect } from 'react';
|
||||||
import React, { useCallback, useEffect } from 'react';
|
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { Box, TextErrors } from '@/components';
|
import { Box, TextErrors } from '@/components';
|
||||||
import { useAuthStore } from '@/core/auth';
|
import { useAuthStore } from '@/core/auth';
|
||||||
import { useMediaUrl } from '@/core/config';
|
|
||||||
import { Doc } from '@/features/docs/doc-management';
|
import { Doc } from '@/features/docs/doc-management';
|
||||||
|
|
||||||
import { useCreateDocAttachment } from '../api/useCreateDocUpload';
|
import { useUploadFile } from '../hook';
|
||||||
import useSaveDoc from '../hook/useSaveDoc';
|
import useSaveDoc from '../hook/useSaveDoc';
|
||||||
import { useEditorStore, useHeadingStore } from '../stores';
|
import { useEditorStore, useHeadingStore } from '../stores';
|
||||||
import { randomColor } from '../utils';
|
import { randomColor } from '../utils';
|
||||||
@@ -87,33 +85,14 @@ export const BlockNoteEditor = ({
|
|||||||
const isVersion = doc.id !== storeId;
|
const isVersion = doc.id !== storeId;
|
||||||
const { userData } = useAuthStore();
|
const { userData } = useAuthStore();
|
||||||
const { setEditor } = useEditorStore();
|
const { setEditor } = useEditorStore();
|
||||||
const mediaUrl = useMediaUrl();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const readOnly = !doc.abilities.partial_update || isVersion;
|
const readOnly = !doc.abilities.partial_update || isVersion;
|
||||||
useSaveDoc(doc.id, provider.document, !readOnly);
|
useSaveDoc(doc.id, provider.document, !readOnly);
|
||||||
const {
|
|
||||||
mutateAsync: createDocAttachment,
|
|
||||||
isError: isErrorAttachment,
|
|
||||||
error: errorAttachment,
|
|
||||||
} = useCreateDocAttachment();
|
|
||||||
const { setHeadings, resetHeadings } = useHeadingStore();
|
const { setHeadings, resetHeadings } = useHeadingStore();
|
||||||
const { i18n } = useTranslation();
|
const { i18n } = useTranslation();
|
||||||
const lang = i18n.language;
|
const lang = i18n.language;
|
||||||
|
|
||||||
const uploadFile = useCallback(
|
const { uploadFile, errorAttachment } = useUploadFile(doc.id);
|
||||||
async (file: File) => {
|
|
||||||
const body = new FormData();
|
|
||||||
body.append('file', file);
|
|
||||||
|
|
||||||
const ret = await createDocAttachment({
|
|
||||||
docId: doc.id,
|
|
||||||
body,
|
|
||||||
});
|
|
||||||
|
|
||||||
return `${mediaUrl}${ret.file}`;
|
|
||||||
},
|
|
||||||
[createDocAttachment, doc.id, mediaUrl],
|
|
||||||
);
|
|
||||||
|
|
||||||
const editor = useCreateBlockNote(
|
const editor = useCreateBlockNote(
|
||||||
{
|
{
|
||||||
@@ -153,7 +132,7 @@ export const BlockNoteEditor = ({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Box $css={cssEditor(readOnly)}>
|
<Box $css={cssEditor(readOnly)}>
|
||||||
{isErrorAttachment && (
|
{errorAttachment && (
|
||||||
<Box $margin={{ bottom: 'big' }}>
|
<Box $margin={{ bottom: 'big' }}>
|
||||||
<TextErrors
|
<TextErrors
|
||||||
causes={errorAttachment.cause}
|
causes={errorAttachment.cause}
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './useSaveDoc';
|
||||||
|
export * from './useUploadFile';
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
|
import { useMediaUrl } from '@/core/config';
|
||||||
|
|
||||||
|
import { useCreateDocAttachment } from '../api';
|
||||||
|
|
||||||
|
export const useUploadFile = (docId: string) => {
|
||||||
|
const mediaUrl = useMediaUrl();
|
||||||
|
const {
|
||||||
|
mutateAsync: createDocAttachment,
|
||||||
|
isError: isErrorAttachment,
|
||||||
|
error: errorAttachment,
|
||||||
|
} = useCreateDocAttachment();
|
||||||
|
|
||||||
|
const uploadFile = useCallback(
|
||||||
|
async (file: File) => {
|
||||||
|
const body = new FormData();
|
||||||
|
body.append('file', file);
|
||||||
|
|
||||||
|
const ret = await createDocAttachment({
|
||||||
|
docId,
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
|
||||||
|
return `${mediaUrl}${ret.file}`;
|
||||||
|
},
|
||||||
|
[createDocAttachment, docId, mediaUrl],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
uploadFile,
|
||||||
|
isErrorAttachment,
|
||||||
|
errorAttachment,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user