ContentLinkButtons.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { useMemo } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { pagePathUtils } from '@growi/core';
  4. import AppContainer from '~/client/services/AppContainer';
  5. import PageContainer from '~/client/services/PageContainer';
  6. import { withUnstatedContainers } from './UnstatedUtils';
  7. import RecentlyCreatedIcon from './Icons/RecentlyCreatedIcon';
  8. import { smoothScrollIntoView } from '~/client/util/smooth-scroll';
  9. const { isTopPage } = pagePathUtils;
  10. const WIKI_HEADER_LINK = 120;
  11. /**
  12. * @author Yuki Takei <yuki@weseek.co.jp>
  13. *
  14. */
  15. const ContentLinkButtons = (props) => {
  16. const { appContainer, pageContainer } = props;
  17. const { pageUser, path } = pageContainer.state;
  18. const { isPageExist } = pageContainer.state;
  19. const { isSharedUser } = appContainer;
  20. const isTopPagePath = isTopPage(path);
  21. // get element for smoothScroll
  22. const getCommentListDom = useMemo(() => { return document.getElementById('page-comments-list') }, []);
  23. const getBookMarkListHeaderDom = useMemo(() => { return document.getElementById('bookmarks-list') }, []);
  24. const getRecentlyCreatedListHeaderDom = useMemo(() => { return document.getElementById('recently-created-list') }, []);
  25. const CommentLinkButton = () => {
  26. return (
  27. <div className="mt-3">
  28. <button
  29. type="button"
  30. className="btn btn-outline-secondary btn-sm btn-block"
  31. onClick={() => smoothScrollIntoView(getCommentListDom, WIKI_HEADER_LINK)}
  32. >
  33. <i className="mr-2 icon-fw icon-bubbles"></i>
  34. <span>Comments</span>
  35. </button>
  36. </div>
  37. );
  38. };
  39. const BookMarkLinkButton = () => {
  40. return (
  41. <button
  42. type="button"
  43. className="btn btn-outline-secondary btn-sm px-2"
  44. onClick={() => smoothScrollIntoView(getBookMarkListHeaderDom, WIKI_HEADER_LINK)}
  45. >
  46. <i className="mr-2 icon-star"></i>
  47. <span>Bookmarks</span>
  48. </button>
  49. );
  50. };
  51. const RecentlyCreatedLinkButton = () => {
  52. return (
  53. <button
  54. type="button"
  55. className="btn btn-outline-secondary btn-sm px-3"
  56. onClick={() => smoothScrollIntoView(getRecentlyCreatedListHeaderDom, WIKI_HEADER_LINK)}
  57. >
  58. <i className="grw-icon-container-recently-created mr-2"><RecentlyCreatedIcon /></i>
  59. <span>Recently Created</span>
  60. </button>
  61. );
  62. };
  63. return (
  64. <>
  65. {isPageExist && !isSharedUser && !isTopPagePath && <CommentLinkButton />}
  66. <div className="mt-3 d-flex justify-content-between">
  67. {pageUser && <><BookMarkLinkButton /><RecentlyCreatedLinkButton /></>}
  68. </div>
  69. </>
  70. );
  71. };
  72. ContentLinkButtons.propTypes = {
  73. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  74. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  75. };
  76. export default withUnstatedContainers(ContentLinkButtons, [AppContainer, PageContainer]);