| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- import type { ReactNode } from 'react';
- import React, {
- useCallback, useState, useEffect,
- useMemo,
- } from 'react';
- import { GlobalCodeMirrorEditorKey } from '@growi/editor';
- import {
- CodeMirrorEditorComment, useCodeMirrorEditorIsolated, useResolvedThemeForEditor,
- } from '@growi/editor/dist/client';
- import { UserPicture } from '@growi/ui/dist/components';
- import { useTranslation } from 'next-i18next';
- import dynamic from 'next/dynamic';
- import {
- TabContent, TabPane,
- } from 'reactstrap';
- import { uploadAttachments } from '~/client/services/upload-attachments';
- import { toastError } from '~/client/util/toastr';
- import { useSWRxPageComment } from '~/stores/comment';
- import {
- useCurrentUser, useIsSlackConfigured, useAcceptedUploadFileType,
- } from '~/stores/context';
- import {
- useSWRxSlackChannels, useIsSlackEnabled, useIsEnabledUnsavedWarning, useEditorSettings,
- } from '~/stores/editor';
- import { useCurrentPagePath } from '~/stores/page';
- import { useCommentEditorDirtyMap } from '~/stores/ui';
- import { useNextThemes } from '~/stores/use-next-themes';
- import loggerFactory from '~/utils/logger';
- import { NotAvailableForGuest } from '../NotAvailableForGuest';
- import { NotAvailableForReadOnlyUser } from '../NotAvailableForReadOnlyUser';
- import { CommentPreview } from './CommentPreview';
- import { SwitchingButtonGroup } from './SwitchingButtonGroup';
- import '@growi/editor/dist/style.css';
- import styles from './CommentEditor.module.scss';
- const logger = loggerFactory('growi:components:CommentEditor');
- const SlackNotification = dynamic(() => import('../SlackNotification').then(mod => mod.SlackNotification), { ssr: false });
- const CommentEditorLayout = ({ children }: { children: ReactNode }): JSX.Element => {
- return (
- <div className={`${styles['comment-editor-styles']} form`}>
- <div className="comment-form">
- <div className="bg-comment rounded">
- {children}
- </div>
- </div>
- </div>
- );
- };
- type CommentEditorProps = {
- pageId: string,
- replyTo?: string,
- revisionId: string,
- currentCommentId?: string,
- commentBody?: string,
- onCanceled?: () => void,
- onCommented?: () => void,
- }
- export const CommentEditor = (props: CommentEditorProps): JSX.Element => {
- const {
- pageId, replyTo, revisionId,
- currentCommentId, commentBody, onCanceled, onCommented,
- } = props;
- const { data: currentUser } = useCurrentUser();
- const { data: currentPagePath } = useCurrentPagePath();
- const { update: updateComment, post: postComment } = useSWRxPageComment(pageId);
- const { data: isSlackEnabled, mutate: mutateIsSlackEnabled } = useIsSlackEnabled();
- const { data: acceptedUploadFileType } = useAcceptedUploadFileType();
- const { data: slackChannelsData } = useSWRxSlackChannels(currentPagePath);
- const { data: isSlackConfigured } = useIsSlackConfigured();
- const { data: editorSettings } = useEditorSettings();
- const { mutate: mutateIsEnabledUnsavedWarning } = useIsEnabledUnsavedWarning();
- const {
- evaluate: evaluateEditorDirtyMap,
- clean: cleanEditorDirtyMap,
- } = useCommentEditorDirtyMap();
- const { mutate: mutateResolvedTheme } = useResolvedThemeForEditor();
- const { resolvedTheme } = useNextThemes();
- mutateResolvedTheme({ themeData: resolvedTheme });
- const editorKey = useMemo(() => {
- if (replyTo != null) {
- return `comment_replyTo_${replyTo}`;
- }
- if (currentCommentId != null) {
- return `comment_edit_${currentCommentId}`;
- }
- return GlobalCodeMirrorEditorKey.COMMENT_NEW;
- }, [currentCommentId, replyTo]);
- const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(editorKey);
- const [showPreview, setShowPreview] = useState(false);
- const [error, setError] = useState();
- const [slackChannels, setSlackChannels] = useState<string>('');
- const { t } = useTranslation('');
- const handleSelect = useCallback((showPreview: boolean) => {
- setShowPreview(showPreview);
- }, []);
- // DO NOT dependent on slackChannelsData directly: https://github.com/weseek/growi/pull/7332
- const slackChannelsDataString = slackChannelsData?.toString();
- const initializeSlackEnabled = useCallback(() => {
- setSlackChannels(slackChannelsDataString ?? '');
- mutateIsSlackEnabled(false);
- }, [mutateIsSlackEnabled, slackChannelsDataString]);
- useEffect(() => {
- initializeSlackEnabled();
- }, [initializeSlackEnabled]);
- const isSlackEnabledToggleHandler = (isSlackEnabled: boolean) => {
- mutateIsSlackEnabled(isSlackEnabled, false);
- };
- const slackChannelsChangedHandler = useCallback((slackChannels: string) => {
- setSlackChannels(slackChannels);
- }, []);
- const initializeEditor = useCallback(async() => {
- const dirtyNum = await cleanEditorDirtyMap(editorKey);
- mutateIsEnabledUnsavedWarning(dirtyNum > 0);
- setShowPreview(false);
- setError(undefined);
- initializeSlackEnabled();
- }, [editorKey, cleanEditorDirtyMap, mutateIsEnabledUnsavedWarning, initializeSlackEnabled]);
- const cancelButtonClickedHandler = useCallback(() => {
- initializeEditor();
- onCanceled?.();
- }, [onCanceled, initializeEditor]);
- const postCommentHandler = useCallback(async() => {
- const commentBodyToPost = codeMirrorEditor?.getDoc() ?? '';
- try {
- if (currentCommentId != null) {
- // update current comment
- await updateComment(commentBodyToPost, revisionId, currentCommentId);
- }
- else {
- // post new comment
- const postCommentArgs = {
- commentForm: {
- comment: commentBodyToPost,
- revisionId,
- replyTo,
- },
- slackNotificationForm: {
- isSlackEnabled,
- slackChannels,
- },
- };
- await postComment(postCommentArgs);
- }
- initializeEditor();
- onCommented?.();
- // Insert empty string as new comment editor is opened after comment
- codeMirrorEditor?.initDoc('');
- }
- catch (err) {
- const errorMessage = err.message || 'An unknown error occured when posting comment';
- setError(errorMessage);
- }
- // eslint-disable-next-line max-len
- }, [currentCommentId, initializeEditor, onCommented, codeMirrorEditor, updateComment, revisionId, replyTo, isSlackEnabled, slackChannels, postComment]);
- // the upload event handler
- const uploadHandler = useCallback((files: File[]) => {
- uploadAttachments(pageId, files, {
- onUploaded: (attachment) => {
- const fileName = attachment.originalName;
- const prefix = attachment.fileFormat.startsWith('image/')
- ? '!' // use "" syntax when image
- : '';
- const insertText = `${prefix}[${fileName}](${attachment.filePathProxied})\n`;
- codeMirrorEditor?.insertText(insertText);
- },
- onError: (error) => {
- toastError(error);
- },
- });
- }, [codeMirrorEditor, pageId]);
- const onChangeHandler = useCallback(async(value: string) => {
- const dirtyNum = await evaluateEditorDirtyMap(editorKey, value);
- mutateIsEnabledUnsavedWarning(dirtyNum > 0);
- }, [editorKey, evaluateEditorDirtyMap, mutateIsEnabledUnsavedWarning]);
- // initialize CodeMirrorEditor
- useEffect(() => {
- if (commentBody == null) {
- return;
- }
- codeMirrorEditor?.initDoc(commentBody);
- }, [codeMirrorEditor, commentBody]);
- const errorMessage = useMemo(() => <span className="text-danger text-end me-2">{error}</span>, [error]);
- const cancelButton = useMemo(() => (
- <button
- type="button"
- className="btn btn-outline-neutral-secondary"
- onClick={cancelButtonClickedHandler}
- >
- {t('Cancel')}
- </button>
- ), [cancelButtonClickedHandler, t]);
- const submitButton = useMemo(() => {
- return (
- <button
- type="button"
- data-testid="comment-submit-button"
- className="btn btn-primary"
- onClick={postCommentHandler}
- >
- {t('page_comment.comment')}
- </button>
- );
- }, [postCommentHandler, t]);
- return (
- <CommentEditorLayout>
- <div className="px-4 pt-3 pb-1">
- <div className="d-flex justify-content-between align-items-center mb-2">
- <div className="d-flex">
- <UserPicture user={currentUser} noLink noTooltip />
- <p className="ms-2 mb-0">{t('page_comment.add_a_comment')}</p>
- </div>
- <SwitchingButtonGroup showPreview={showPreview} onSelected={handleSelect} />
- </div>
- <TabContent activeTab={showPreview ? 'comment_preview' : 'comment_editor'}>
- <TabPane tabId="comment_editor">
- <CodeMirrorEditorComment
- editorKey={editorKey}
- acceptedUploadFileType={acceptedUploadFileType}
- onChange={onChangeHandler}
- onSave={postCommentHandler}
- onUpload={uploadHandler}
- editorSettings={editorSettings}
- />
- </TabPane>
- <TabPane tabId="comment_preview">
- <div className="comment-preview-container">
- <CommentPreview markdown={codeMirrorEditor?.getDoc() ?? ''} />
- </div>
- </TabPane>
- </TabContent>
- </div>
- <div className="comment-submit px-4 pb-3 mb-2">
- <div className="d-flex">
- <span className="flex-grow-1" />
- <span className="d-none d-sm-inline">{errorMessage && errorMessage}</span>
- {isSlackConfigured && isSlackEnabled != null
- && (
- <div className="align-self-center me-md-3">
- <SlackNotification
- isSlackEnabled={isSlackEnabled}
- slackChannels={slackChannels}
- onEnabledFlagChange={isSlackEnabledToggleHandler}
- onChannelChange={slackChannelsChangedHandler}
- id="idForComment"
- />
- </div>
- )
- }
- <div className="d-none d-sm-block">
- <span className="me-2">{cancelButton}</span><span>{submitButton}</span>
- </div>
- </div>
- <div className="d-block d-sm-none mt-2">
- <div className="d-flex justify-content-end">
- {error && errorMessage}
- <span className="me-2">{cancelButton}</span><span>{submitButton}</span>
- </div>
- </div>
- </div>
- </CommentEditorLayout>
- );
- };
- export const CommentEditorPre = (props: CommentEditorProps): JSX.Element => {
- const { onCommented, onCanceled, ...rest } = props;
- const { data: currentUser } = useCurrentUser();
- const { mutate: mutateResolvedTheme } = useResolvedThemeForEditor();
- const { resolvedTheme } = useNextThemes();
- mutateResolvedTheme({ themeData: resolvedTheme });
- const [isReadyToUse, setIsReadyToUse] = useState(false);
- const { t } = useTranslation('');
- const render = useCallback((): JSX.Element => {
- return (
- <CommentEditorLayout>
- <NotAvailableForGuest>
- <NotAvailableForReadOnlyUser>
- <button
- type="button"
- className="btn btn-outline-primary w-100 text-start py-3"
- onClick={() => setIsReadyToUse(true)}
- data-testid="open-comment-editor-button"
- >
- <UserPicture user={currentUser} noLink noTooltip additionalClassName="me-3" />
- <span className="material-symbols-outlined me-1 fs-5">add_comment</span>
- <small>{t('page_comment.add_a_comment')}...</small>
- </button>
- </NotAvailableForReadOnlyUser>
- </NotAvailableForGuest>
- </CommentEditorLayout>
- );
- }, [currentUser, t]);
- return isReadyToUse
- ? (
- <CommentEditor
- onCommented={() => {
- onCommented?.();
- setIsReadyToUse(false);
- }}
- onCanceled={() => {
- onCanceled?.();
- setIsReadyToUse(false);
- }}
- {...rest}
- />
- )
- : render();
- };
|