Files
AFFiNE/libs/components/editor-plugins/src/comment/use-add-comment.ts
zuomeng wang b35c9b2c14 Feat/land page (#391)
* feat: update landing page

* feat: update landing page

* fix: react warning

* feat(route): rename variables

* feat(route): better refresh

* fix: update logo and i18n

* feat(code): remove unused comment

* refactor(workspace): rename workspaceId

* refactor(page): rename pageId

* feat(edit): more robust editing experience

* fix: landing page mobile

* fix: landing page mobile

Co-authored-by: tzhangchi <terry.zhangchi@outlook.com>
Co-authored-by: DarkSky <25152247+darkskygit@users.noreply.github.com>
2022-09-09 02:56:10 +00:00

79 lines
2.3 KiB
TypeScript

import { services } from '@toeverything/datasource/db-service';
import { useCallback, useState } from 'react';
import { useParams } from 'react-router-dom';
import { WithEditorSelectionType } from '../menu/inline-menu/types';
export const useAddComment = ({
editor,
selectionInfo,
setShow,
}: WithEditorSelectionType) => {
const { workspaceId, pageId } = useParams();
const [currentComment, setCurrentComment] = useState('');
const createComment = useCallback(async (): Promise<{
commentsId: string;
}> => {
const selectedBlockId = selectionInfo?.anchorNode?.id;
if (!currentComment || !currentComment.trim()) {
throw new Error(
'Comment content must not be empty before creating in db. '
);
}
if (!selectedBlockId) {
throw new Error(
'Commented block id must not be empty before creating in db. '
);
}
const created = await services.api.commentService.createComment({
workspace: workspaceId,
pageId: pageId,
attachedToBlocksIds: [selectionInfo.anchorNode.id],
quote: {
value: [
{
text: editor.blockHelper.getBlockTextBetweenSelection(
selectedBlockId
),
},
],
},
content: {
value: [{ text: currentComment }],
},
});
return created;
}, [
currentComment,
editor.blockHelper,
pageId,
selectionInfo?.anchorNode?.id,
workspaceId,
]);
const handleSubmitCurrentComment = useCallback(async () => {
const textBlockId = selectionInfo.anchorNode.id;
if (!textBlockId) return;
const created = await createComment();
const commentId = created?.commentsId;
if (commentId) {
editor.blockHelper.addComment(textBlockId, commentId);
setShow(false);
}
}, [
createComment,
editor.blockHelper,
selectionInfo.anchorNode.id,
setShow,
]);
return {
currentComment,
setCurrentComment,
createComment,
handleSubmitCurrentComment,
};
};