BookmarkButtons.tsx 2.8 KB

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