* Added Overview page * Revised Getting Started page * Minor revision * Edited readme, minor modifications to docs * Removed sweep.yaml, .devcontainer, .ergomake * Moved security.md to .github, added contributing.md * changes as per code review * updated contributing.md * fixed broken links & added missing links in doc, improved structure * fixed link in wsl setup * fixed server link, added https cloning in yarn-setup * removed package-lock.json * added doc card, admonitions * removed underline from nav buttons * refactoring modules/ui * refactoring modules/ui * Change folder case * Fix theme location * Fix case 2 * Fix storybook --------- Co-authored-by: Nimra Ahmed <nimra1408@gmail.com> Co-authored-by: Nimra Ahmed <50912134+nimraahmed@users.noreply.github.com>
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react';
|
|
import { useApolloClient } from '@apollo/client';
|
|
import { BlockNoteEditor } from '@blocknote/core';
|
|
import { useBlockNote } from '@blocknote/react';
|
|
import styled from '@emotion/styled';
|
|
import debounce from 'lodash.debounce';
|
|
|
|
import { BlockEditor } from '@/ui/input/editor/components/BlockEditor';
|
|
import { Activity, useUpdateActivityMutation } from '~/generated/graphql';
|
|
|
|
import { ACTIVITY_UPDATE_FRAGMENT } from '../graphql/fragments/activityUpdateFragment';
|
|
|
|
const StyledBlockNoteStyledContainer = styled.div`
|
|
width: 100%;
|
|
`;
|
|
|
|
type ActivityBodyEditorProps = {
|
|
activity: Pick<Activity, 'id' | 'body'>;
|
|
onChange?: (activityBody: string) => void;
|
|
};
|
|
|
|
export const ActivityBodyEditor = ({
|
|
activity,
|
|
onChange,
|
|
}: ActivityBodyEditorProps) => {
|
|
const [updateActivityMutation] = useUpdateActivityMutation();
|
|
|
|
const client = useApolloClient();
|
|
const cachedActivity = client.readFragment({
|
|
id: `Activity:${activity.id}`,
|
|
fragment: ACTIVITY_UPDATE_FRAGMENT,
|
|
});
|
|
|
|
const [body, setBody] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (body) {
|
|
onChange?.(body);
|
|
}
|
|
}, [body, onChange]);
|
|
|
|
const debounceOnChange = useMemo(() => {
|
|
const onInternalChange = (activityBody: string) => {
|
|
setBody(activityBody);
|
|
updateActivityMutation({
|
|
variables: {
|
|
where: {
|
|
id: activity.id,
|
|
},
|
|
data: {
|
|
body: activityBody,
|
|
},
|
|
},
|
|
optimisticResponse: {
|
|
__typename: 'Mutation',
|
|
updateOneActivity: {
|
|
...cachedActivity,
|
|
body: activityBody,
|
|
},
|
|
},
|
|
});
|
|
};
|
|
|
|
return debounce(onInternalChange, 200);
|
|
}, [updateActivityMutation, activity.id, cachedActivity]);
|
|
|
|
const editor: BlockNoteEditor | null = useBlockNote({
|
|
initialContent: activity.body ? JSON.parse(activity.body) : undefined,
|
|
editorDOMAttributes: { class: 'editor' },
|
|
onEditorContentChange: (editor) => {
|
|
debounceOnChange(JSON.stringify(editor.topLevelBlocks) ?? '');
|
|
},
|
|
});
|
|
|
|
return (
|
|
<StyledBlockNoteStyledContainer>
|
|
<BlockEditor editor={editor} />
|
|
</StyledBlockNoteStyledContainer>
|
|
);
|
|
};
|