BookmarkButtons.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { FC, useState, useCallback } from 'react';
  2. import { useTranslation } from 'react-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={`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 type="button" id="po-total-bookmarks" className={`btn btn-bookmark border-0 total-bookmarks ${props.isBookmarked ? 'active' : ''}`}>
  55. {bookmarkCount ?? 0}
  56. </button>
  57. { bookmarkedUsers != null && (
  58. <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
  59. <PopoverBody className="user-list-popover">
  60. <div className="px-2 text-right user-list-content text-truncate text-muted">
  61. {bookmarkedUsers.length ? <UserPictureList users={props.bookmarkedUsers} /> : t('No users have bookmarked yet')}
  62. </div>
  63. </PopoverBody>
  64. </Popover>
  65. ) }
  66. </>
  67. ) }
  68. </div>
  69. );
  70. };
  71. export default BookmarkButtons;