BookmarkButtons.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React, { FC, useState, useCallback } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { UncontrolledTooltip, Popover, PopoverBody } from 'reactstrap';
  4. import { useIsGuestUser } from '~/stores/context';
  5. import { IUser } from '../interfaces/user';
  6. import UserPictureList from './User/UserPictureList';
  7. interface Props {
  8. bookmarkCount?: number
  9. isBookmarked?: boolean
  10. bookmarkedUsers?: IUser[]
  11. hideTotalNumber?: boolean
  12. onBookMarkClicked: ()=>void;
  13. }
  14. const BookmarkButtons: FC<Props> = (props: Props) => {
  15. const { t } = useTranslation();
  16. const {
  17. bookmarkCount, isBookmarked, bookmarkedUsers, hideTotalNumber,
  18. } = props;
  19. const [isPopoverOpen, setIsPopoverOpen] = useState(false);
  20. const { data: isGuestUser } = useIsGuestUser();
  21. const togglePopover = () => {
  22. setIsPopoverOpen(!isPopoverOpen);
  23. };
  24. const handleClick = async() => {
  25. if (props.onBookMarkClicked != null) {
  26. props.onBookMarkClicked();
  27. }
  28. };
  29. const getTooltipMessage = useCallback(() => {
  30. if (isGuestUser) {
  31. return 'Not available for guest';
  32. }
  33. if (isBookmarked) {
  34. return 'tooltip.cancel_bookmark';
  35. }
  36. return 'tooltip.bookmark';
  37. }, [isGuestUser, isBookmarked]);
  38. return (
  39. <div className="btn-group" role="group" aria-label="Bookmark buttons">
  40. <button
  41. type="button"
  42. id="bookmark-button"
  43. onClick={handleClick}
  44. className={`shadow-none btn btn-bookmark border-0
  45. ${isBookmarked ? 'active' : ''} ${isGuestUser ? 'disabled' : ''}`}
  46. >
  47. <i className={`fa ${isBookmarked ? 'fa-bookmark' : 'fa-bookmark-o'}`}></i>
  48. </button>
  49. <UncontrolledTooltip placement="top" target="bookmark-button" fade={false}>
  50. {t(getTooltipMessage())}
  51. </UncontrolledTooltip>
  52. { !hideTotalNumber && (
  53. <>
  54. <button
  55. type="button"
  56. id="po-total-bookmarks"
  57. className={`shadow-none btn btn-bookmark border-0
  58. total-bookmarks ${props.isBookmarked ? 'active' : ''}`}
  59. >
  60. {bookmarkCount ?? 0}
  61. </button>
  62. { bookmarkedUsers != null && (
  63. <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
  64. <PopoverBody className="user-list-popover">
  65. <div className="px-2 text-right user-list-content text-truncate text-muted">
  66. {bookmarkedUsers.length ? <UserPictureList users={props.bookmarkedUsers} /> : t('No users have bookmarked yet')}
  67. </div>
  68. </PopoverBody>
  69. </Popover>
  70. ) }
  71. </>
  72. ) }
  73. </div>
  74. );
  75. };
  76. export default BookmarkButtons;