Sfoglia il codice sorgente

corrections from FB

jam411 3 anni fa
parent
commit
dacea2174d

+ 13 - 4
packages/app/src/components/Comments.tsx

@@ -1,22 +1,26 @@
 import React from 'react';
 import React from 'react';
 
 
+import { IRevisionHasId } from '@growi/core';
+
 import { PageComment } from '~/components/PageComment';
 import { PageComment } from '~/components/PageComment';
 import { useCommentPreviewOptions } from '~/stores/renderer';
 import { useCommentPreviewOptions } from '~/stores/renderer';
 
 
-import { useIsTrashPage } from '../stores/context';
+import { useIsTrashPage, useCurrentUser } from '../stores/context';
 
 
 import { CommentEditorLazyRenderer } from './PageComment/CommentEditorLazyRenderer';
 import { CommentEditorLazyRenderer } from './PageComment/CommentEditorLazyRenderer';
 
 
 type CommentsProps = {
 type CommentsProps = {
-  pageId?: string,
+  pageId: string,
+  revision: IRevisionHasId,
 }
 }
 
 
 export const Comments = (props: CommentsProps): JSX.Element => {
 export const Comments = (props: CommentsProps): JSX.Element => {
 
 
-  const { pageId } = props;
+  const { pageId, revision } = props;
 
 
   const { data: rendererOptions } = useCommentPreviewOptions();
   const { data: rendererOptions } = useCommentPreviewOptions();
   const { data: isDeleted } = useIsTrashPage();
   const { data: isDeleted } = useIsTrashPage();
+  const { data: currentUser } = useCurrentUser();
 
 
   // TODO: Implement or refactor Skelton if server-side rendering
   // TODO: Implement or refactor Skelton if server-side rendering
   if (rendererOptions == null || isDeleted == null) {
   if (rendererOptions == null || isDeleted == null) {
@@ -31,6 +35,8 @@ export const Comments = (props: CommentsProps): JSX.Element => {
           <div id="page-comments-list" className="page-comments-list">
           <div id="page-comments-list" className="page-comments-list">
             <PageComment
             <PageComment
               pageId={pageId}
               pageId={pageId}
+              revision={revision}
+              currentUser={currentUser}
               isReadOnly={false}
               isReadOnly={false}
               titleAlign="left"
               titleAlign="left"
               hideIfEmpty={false}
               hideIfEmpty={false}
@@ -38,7 +44,10 @@ export const Comments = (props: CommentsProps): JSX.Element => {
           </div>
           </div>
           { !isDeleted && (
           { !isDeleted && (
             <div id="page-comment-write">
             <div id="page-comment-write">
-              <CommentEditorLazyRenderer pageId={pageId} rendererOptions={rendererOptions} />
+              <CommentEditorLazyRenderer
+                pageId={pageId}
+                rendererOptions={rendererOptions}
+              />
             </div>
             </div>
           )}
           )}
         </div>
         </div>

+ 18 - 2
packages/app/src/components/PageComment.tsx

@@ -2,6 +2,7 @@ import React, {
   FC, useEffect, useState, useMemo, memo, useCallback,
   FC, useEffect, useState, useMemo, memo, useCallback,
 } from 'react';
 } from 'react';
 
 
+import { IRevisionHasId } from '@growi/core';
 import dynamic from 'next/dynamic';
 import dynamic from 'next/dynamic';
 import { Button } from 'reactstrap';
 import { Button } from 'reactstrap';
 
 
@@ -25,9 +26,10 @@ const DeleteCommentModal = dynamic<DeleteCommentModalProps>(
   () => import('./PageComment/DeleteCommentModal').then(mod => mod.DeleteCommentModal), { ssr: false },
   () => import('./PageComment/DeleteCommentModal').then(mod => mod.DeleteCommentModal), { ssr: false },
 );
 );
 
 
-
 export type PageCommentProps = {
 export type PageCommentProps = {
   pageId?: string,
   pageId?: string,
+  revision: string | IRevisionHasId,
+  currentUser: any,
   isReadOnly: boolean,
   isReadOnly: boolean,
   titleAlign?: 'center' | 'left' | 'right',
   titleAlign?: 'center' | 'left' | 'right',
   highlightKeywords?: string[],
   highlightKeywords?: string[],
@@ -37,7 +39,7 @@ export type PageCommentProps = {
 export const PageComment: FC<PageCommentProps> = memo((props:PageCommentProps): JSX.Element => {
 export const PageComment: FC<PageCommentProps> = memo((props:PageCommentProps): JSX.Element => {
 
 
   const {
   const {
-    pageId, highlightKeywords, isReadOnly, titleAlign, hideIfEmpty,
+    pageId, revision, currentUser, highlightKeywords, isReadOnly, titleAlign, hideIfEmpty,
   } = props;
   } = props;
 
 
   const { data: comments, mutate } = useSWRxPageComment(pageId);
   const { data: comments, mutate } = useSWRxPageComment(pageId);
@@ -137,9 +139,20 @@ export const PageComment: FC<PageCommentProps> = memo((props:PageCommentProps):
     );
     );
   }
   }
 
 
+  // Conditional for called PageComment from SearchResultContext
+  let revisionId = revision;
+  let revisionCreatedAt: Date;
+  if (typeof (revision) !== 'string') {
+    revisionId = revision._id;
+    revisionCreatedAt = revision.createdAt;
+  }
+
   const generateCommentElement = (comment: ICommentHasId) => (
   const generateCommentElement = (comment: ICommentHasId) => (
     <Comment
     <Comment
       comment={comment}
       comment={comment}
+      revisionId={revisionId as string}
+      revisionCreatedAt={revisionCreatedAt}
+      currentUser={currentUser}
       isReadOnly={isReadOnly}
       isReadOnly={isReadOnly}
       deleteBtnClicked={onClickDeleteButton}
       deleteBtnClicked={onClickDeleteButton}
       onComment={mutate}
       onComment={mutate}
@@ -150,6 +163,9 @@ export const PageComment: FC<PageCommentProps> = memo((props:PageCommentProps):
   const generateReplyCommentsElement = (replyComments: ICommentHasIdList) => (
   const generateReplyCommentsElement = (replyComments: ICommentHasIdList) => (
     <ReplyComments
     <ReplyComments
       isReadOnly={isReadOnly}
       isReadOnly={isReadOnly}
+      revisionId={revisionId as string}
+      revisionCreatedAt={revisionCreatedAt}
+      currentUser={currentUser}
       replyList={replyComments}
       replyList={replyComments}
       deleteBtnClicked={onClickDeleteButton}
       deleteBtnClicked={onClickDeleteButton}
       onComment={mutate}
       onComment={mutate}

+ 15 - 27
packages/app/src/components/PageComment/Comment.tsx

@@ -7,10 +7,9 @@ import dynamic from 'next/dynamic';
 import { UncontrolledTooltip } from 'reactstrap';
 import { UncontrolledTooltip } from 'reactstrap';
 
 
 import { RendererOptions } from '~/services/renderer/renderer';
 import { RendererOptions } from '~/services/renderer/renderer';
-import { useCurrentUser } from '~/stores/context';
-import { useSWRxCurrentPage } from '~/stores/page';
 
 
 import { ICommentHasId } from '../../interfaces/comment';
 import { ICommentHasId } from '../../interfaces/comment';
+import { IUser } from '../../interfaces/user';
 import FormattedDistanceDate from '../FormattedDistanceDate';
 import FormattedDistanceDate from '../FormattedDistanceDate';
 import HistoryIcon from '../Icons/HistoryIcon';
 import HistoryIcon from '../Icons/HistoryIcon';
 import RevisionRenderer from '../Page/RevisionRenderer';
 import RevisionRenderer from '../Page/RevisionRenderer';
@@ -23,26 +22,24 @@ import styles from './Comment.module.scss';
 
 
 const CommentEditor = dynamic<CommentEditorProps>(() => import('./CommentEditor').then(mod => mod.CommentEditor), { ssr: false });
 const CommentEditor = dynamic<CommentEditorProps>(() => import('./CommentEditor').then(mod => mod.CommentEditor), { ssr: false });
 
 
-
 type CommentProps = {
 type CommentProps = {
   comment: ICommentHasId,
   comment: ICommentHasId,
+  revisionId: string,
+  revisionCreatedAt: Date,
+  currentUser: IUser,
   isReadOnly: boolean,
   isReadOnly: boolean,
   deleteBtnClicked: (comment: ICommentHasId) => void,
   deleteBtnClicked: (comment: ICommentHasId) => void,
   onComment: () => void,
   onComment: () => void,
   rendererOptions: RendererOptions,
   rendererOptions: RendererOptions,
-  currentRevisionId?: string,
-  currentRevisionCreatedAt?: Date,
 }
 }
 
 
 export const Comment = (props: CommentProps): JSX.Element => {
 export const Comment = (props: CommentProps): JSX.Element => {
 
 
   const {
   const {
-    comment, isReadOnly, deleteBtnClicked, onComment, rendererOptions,
+    comment, revisionId, revisionCreatedAt, currentUser, isReadOnly, deleteBtnClicked, onComment, rendererOptions,
   } = props;
   } = props;
 
 
   const { t } = useTranslation();
   const { t } = useTranslation();
-  const { data: currentUser } = useCurrentUser();
-  const { data: currentPage } = useSWRxCurrentPage();
 
 
   const [markdown, setMarkdown] = useState('');
   const [markdown, setMarkdown] = useState('');
   const [isReEdit, setIsReEdit] = useState(false);
   const [isReEdit, setIsReEdit] = useState(false);
@@ -53,22 +50,17 @@ export const Comment = (props: CommentProps): JSX.Element => {
   const createdAt = new Date(comment.createdAt);
   const createdAt = new Date(comment.createdAt);
   const updatedAt = new Date(comment.updatedAt);
   const updatedAt = new Date(comment.updatedAt);
   const isEdited = createdAt < updatedAt;
   const isEdited = createdAt < updatedAt;
-  const currentRevisionId = currentPage?.revision._id;
-  const currentRevisionCreatedAt = currentPage?.revision.createdAt;
 
 
   useEffect(() => {
   useEffect(() => {
     setMarkdown(comment.comment);
     setMarkdown(comment.comment);
-
     const isCurrentRevision = () => {
     const isCurrentRevision = () => {
-      return comment.revision === currentRevisionId;
+      return comment.revision === revisionId;
     };
     };
     isCurrentRevision();
     isCurrentRevision();
-
-  }, [comment, currentRevisionId]);
+  }, [comment, revisionId]);
 
 
   const isCurrentUserEqualsToAuthor = () => {
   const isCurrentUserEqualsToAuthor = () => {
     const { creator }: any = comment;
     const { creator }: any = comment;
-
     if (creator == null || currentUser == null) {
     if (creator == null || currentUser == null) {
       return false;
       return false;
     }
     }
@@ -79,11 +71,11 @@ export const Comment = (props: CommentProps): JSX.Element => {
     let className = 'page-comment flex-column';
     let className = 'page-comment flex-column';
 
 
     // Conditional for called from SearchResultContext
     // Conditional for called from SearchResultContext
-    if (currentRevisionId != null && currentRevisionCreatedAt != null) {
-      if (comment.revision === currentRevisionId) {
+    if (revisionCreatedAt != null) {
+      if (comment.revision === revisionId) {
         className += ' page-comment-current';
         className += ' page-comment-current';
       }
       }
-      else if (comment.createdAt.getTime() > currentRevisionCreatedAt.getTime()) {
+      else if (comment.createdAt.getTime() > revisionCreatedAt.getTime()) {
         className += ' page-comment-newer';
         className += ' page-comment-newer';
       }
       }
       else {
       else {
@@ -119,15 +111,12 @@ export const Comment = (props: CommentProps): JSX.Element => {
   const rootClassName = getRootClassName(comment);
   const rootClassName = getRootClassName(comment);
   const commentBody = isMarkdown ? renderRevisionBody() : renderText(comment.comment);
   const commentBody = isMarkdown ? renderRevisionBody() : renderText(comment.comment);
   const revHref = `?revision=${comment.revision}`;
   const revHref = `?revision=${comment.revision}`;
-
   const editedDateId = `editedDate-${comment._id}`;
   const editedDateId = `editedDate-${comment._id}`;
-  const editedDateFormatted = isEdited
-    ? format(updatedAt, 'yyyy/MM/dd HH:mm')
-    : null;
+  const editedDateFormatted = isEdited ? format(updatedAt, 'yyyy/MM/dd HH:mm') : null;
 
 
   return (
   return (
     <div className={`${styles['comment-styles']}`}>
     <div className={`${styles['comment-styles']}`}>
-      {(isReEdit && !isReadOnly) ? (
+      { (isReEdit && !isReadOnly) ? (
         <CommentEditor
         <CommentEditor
           rendererOptions={rendererOptions}
           rendererOptions={rendererOptions}
           replyTo={undefined}
           replyTo={undefined}
@@ -158,7 +147,7 @@ export const Comment = (props: CommentProps): JSX.Element => {
                   <span id={editedDateId}>&nbsp;(edited)</span>
                   <span id={editedDateId}>&nbsp;(edited)</span>
                   <UncontrolledTooltip placement="bottom" fade={false} target={editedDateId}>{editedDateFormatted}</UncontrolledTooltip>
                   <UncontrolledTooltip placement="bottom" fade={false} target={editedDateId}>{editedDateFormatted}</UncontrolledTooltip>
                 </>
                 </>
-              )}
+              ) }
               <span className="ml-2">
               <span className="ml-2">
                 <a id={`page-comment-revision-${commentId}`} className="page-comment-revision" href={revHref}>
                 <a id={`page-comment-revision-${commentId}`} className="page-comment-revision" href={revHref}>
                   <HistoryIcon />
                   <HistoryIcon />
@@ -168,7 +157,7 @@ export const Comment = (props: CommentProps): JSX.Element => {
                 </UncontrolledTooltip>
                 </UncontrolledTooltip>
               </span>
               </span>
             </div>
             </div>
-            {(isCurrentUserEqualsToAuthor() && !isReadOnly) && (
+            { (isCurrentUserEqualsToAuthor() && !isReadOnly) && (
               <CommentControl
               <CommentControl
                 onClickDeleteBtn={deleteBtnClickedHandler}
                 onClickDeleteBtn={deleteBtnClickedHandler}
                 onClickEditBtn={() => setIsReEdit(true)}
                 onClickEditBtn={() => setIsReEdit(true)}
@@ -176,8 +165,7 @@ export const Comment = (props: CommentProps): JSX.Element => {
             ) }
             ) }
           </div>
           </div>
         </div>
         </div>
-      )
-      }
+      ) }
     </div>
     </div>
   );
   );
 };
 };

+ 8 - 1
packages/app/src/components/PageComment/ReplyComments.tsx

@@ -6,6 +6,7 @@ import { Collapse } from 'reactstrap';
 import { RendererOptions } from '~/services/renderer/renderer';
 import { RendererOptions } from '~/services/renderer/renderer';
 
 
 import { ICommentHasId, ICommentHasIdList } from '../../interfaces/comment';
 import { ICommentHasId, ICommentHasIdList } from '../../interfaces/comment';
+import { IUser } from '../../interfaces/user';
 import { useIsAllReplyShown } from '../../stores/context';
 import { useIsAllReplyShown } from '../../stores/context';
 
 
 import { Comment } from './Comment';
 import { Comment } from './Comment';
@@ -15,6 +16,9 @@ import styles from './ReplyComments.module.scss';
 
 
 type ReplycommentsProps = {
 type ReplycommentsProps = {
   isReadOnly: boolean,
   isReadOnly: boolean,
+  revisionId: string,
+  revisionCreatedAt: Date,
+  currentUser: IUser,
   replyList: ICommentHasIdList,
   replyList: ICommentHasIdList,
   deleteBtnClicked: (comment: ICommentHasId) => void,
   deleteBtnClicked: (comment: ICommentHasId) => void,
   onComment: () => void,
   onComment: () => void,
@@ -24,7 +28,7 @@ type ReplycommentsProps = {
 export const ReplyComments = (props: ReplycommentsProps): JSX.Element => {
 export const ReplyComments = (props: ReplycommentsProps): JSX.Element => {
 
 
   const {
   const {
-    isReadOnly, replyList, deleteBtnClicked, onComment, rendererOptions,
+    isReadOnly, revisionId, revisionCreatedAt, currentUser, replyList, deleteBtnClicked, onComment, rendererOptions,
   } = props;
   } = props;
 
 
   const { data: isAllReplyShown } = useIsAllReplyShown();
   const { data: isAllReplyShown } = useIsAllReplyShown();
@@ -36,6 +40,9 @@ export const ReplyComments = (props: ReplycommentsProps): JSX.Element => {
       <div key={reply._id} className={`${styles['page-comment-reply']} ml-4 ml-sm-5 mr-3`}>
       <div key={reply._id} className={`${styles['page-comment-reply']} ml-4 ml-sm-5 mr-3`}>
         <Comment
         <Comment
           comment={reply}
           comment={reply}
+          revisionId={revisionId}
+          revisionCreatedAt={revisionCreatedAt}
+          currentUser={currentUser}
           isReadOnly={isReadOnly}
           isReadOnly={isReadOnly}
           deleteBtnClicked={deleteBtnClicked}
           deleteBtnClicked={deleteBtnClicked}
           onComment={onComment}
           onComment={onComment}

+ 5 - 3
packages/app/src/components/SearchPage/SearchResultContent.tsx

@@ -12,6 +12,7 @@ import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
 import { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '~/interfaces/page';
 import { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '~/interfaces/page';
 import { IPageWithSearchMeta } from '~/interfaces/search';
 import { IPageWithSearchMeta } from '~/interfaces/search';
 import { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
 import { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
+import { useCurrentUser } from '~/stores/context';
 import {
 import {
   usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
   usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
 } from '~/stores/modal';
 } from '~/stores/modal';
@@ -31,7 +32,6 @@ const RevisionLoader = dynamic(() => import('../Page/RevisionLoader'), { ssr: fa
 const PageComment = dynamic<PageCommentProps>(() => import('../PageComment').then(mod => mod.PageComment), { ssr: false });
 const PageComment = dynamic<PageCommentProps>(() => import('../PageComment').then(mod => mod.PageComment), { ssr: false });
 const PageContentFooter = dynamic<PageContentFooterProps>(() => import('../PageContentFooter').then(mod => mod.PageContentFooter), { ssr: false });
 const PageContentFooter = dynamic<PageContentFooterProps>(() => import('../PageContentFooter').then(mod => mod.PageContentFooter), { ssr: false });
 
 
-
 type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
 type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
   pageId: string,
   pageId: string,
   revisionId: string,
   revisionId: string,
@@ -122,8 +122,8 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
   const { open: openDuplicateModal } = usePageDuplicateModal();
   const { open: openDuplicateModal } = usePageDuplicateModal();
   const { open: openRenameModal } = usePageRenameModal();
   const { open: openRenameModal } = usePageRenameModal();
   const { open: openDeleteModal } = usePageDeleteModal();
   const { open: openDeleteModal } = usePageDeleteModal();
-
   const { data: rendererOptions } = useSearchResultOptions();
   const { data: rendererOptions } = useSearchResultOptions();
+  const { data: currentUser } = useCurrentUser();
 
 
   const duplicateItemClickedHandler = useCallback(async(pageToDuplicate) => {
   const duplicateItemClickedHandler = useCallback(async(pageToDuplicate) => {
     // eslint-disable-next-line @typescript-eslint/no-unused-vars
     // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -194,7 +194,7 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
         />
         />
       </div>
       </div>
     );
     );
-  }, [page, SubNavButtons, showPageControlDropdown, forceHideMenuItems, duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler]);
+  }, [page, showPageControlDropdown, forceHideMenuItems, duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler]);
 
 
   // return if page or growiRenderer is null
   // return if page or growiRenderer is null
   if (page == null || rendererOptions == null) return <></>;
   if (page == null || rendererOptions == null) return <></>;
@@ -219,6 +219,8 @@ export const SearchResultContent: FC<Props> = (props: Props) => {
         />
         />
         <PageComment
         <PageComment
           pageId={page._id}
           pageId={page._id}
+          revision={page.revision}
+          currentUser={currentUser}
           highlightKeywords={highlightKeywords}
           highlightKeywords={highlightKeywords}
           isReadOnly
           isReadOnly
           hideIfEmpty
           hideIfEmpty

+ 1 - 1
packages/app/src/pages/[[...path]].page.tsx

@@ -327,7 +327,7 @@ const GrowiPage: NextPage<Props> = (props: Props) => {
           </div>
           </div>
           { !props.isIdenticalPathPage && !props.isNotFound && (
           { !props.isIdenticalPathPage && !props.isNotFound && (
             <footer className="footer d-edit-none">
             <footer className="footer d-edit-none">
-              { !isTopPagePath && (<Comments pageId={pageId} />) }
+              { !isTopPagePath && (<Comments pageId={pageId} revision={pageWithMeta?.data.revision} />) }
               { (pageWithMeta != null && isUsersHomePage(pageWithMeta.data.path)) && (
               { (pageWithMeta != null && isUsersHomePage(pageWithMeta.data.path)) && (
                 <UsersHomePageFooter creatorId={pageWithMeta.data.creator._id}/>
                 <UsersHomePageFooter creatorId={pageWithMeta.data.creator._id}/>
               ) }
               ) }