BookmarkButtons.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import React, { FC, useState } from 'react';
  2. import { UncontrolledTooltip, Popover, PopoverBody } from 'reactstrap';
  3. import { useTranslation } from 'react-i18next';
  4. import { IUser } from '../interfaces/user';
  5. import UserPictureList from './User/UserPictureList';
  6. import { useIsGuestUser } from '~/stores/context';
  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. return (
  30. <div className="btn-group" role="group" aria-label="Bookmark buttons">
  31. <button
  32. type="button"
  33. id="bookmark-button"
  34. onClick={handleClick}
  35. className={`btn btn-bookmark border-0
  36. ${isBookmarked ? 'active' : ''} ${isGuestUser ? 'disabled' : ''}`}
  37. >
  38. <i className={`fa ${isBookmarked ? 'fa-bookmark' : 'fa-bookmark-o'}`}></i>
  39. </button>
  40. {isGuestUser && (
  41. <UncontrolledTooltip placement="top" target="bookmark-button" fade={false}>
  42. {t('Not available for guest')}
  43. </UncontrolledTooltip>
  44. )}
  45. { !hideTotalNumber && (
  46. <>
  47. <button type="button" id="po-total-bookmarks" className={`btn btn-bookmark border-0 total-bookmarks ${props.isBookmarked ? 'active' : ''}`}>
  48. {bookmarkCount ?? 0}
  49. </button>
  50. { bookmarkedUsers != null && (
  51. <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
  52. <PopoverBody className="user-list-popover">
  53. <div className="px-2 text-right user-list-content text-truncate text-muted">
  54. {bookmarkedUsers.length ? <UserPictureList users={props.bookmarkedUsers} /> : t('No users have bookmarked yet')}
  55. </div>
  56. </PopoverBody>
  57. </Popover>
  58. ) }
  59. </>
  60. ) }
  61. </div>
  62. );
  63. };
  64. export default BookmarkButtons;