BookmarkButtons.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 (isBookmarked) {
  32. return 'tooltip.cancel_bookmark';
  33. }
  34. return 'tooltip.bookmark';
  35. }, [isBookmarked]);
  36. return (
  37. <div className={`btn-group btn-group-bookmark ${styles['btn-group-bookmark']}`} role="group" aria-label="Bookmark buttons">
  38. <button
  39. type="button"
  40. id="bookmark-button"
  41. onClick={handleClick}
  42. className={`shadow-none btn btn-bookmark border-0
  43. ${isBookmarked ? 'active' : ''} ${isGuestUser ? 'disabled' : ''}`}
  44. >
  45. <i className={`fa ${isBookmarked ? 'fa-bookmark' : 'fa-bookmark-o'}`}></i>
  46. </button>
  47. <UncontrolledTooltip data-testid="bookmark-button-tooltip" placement="top" target="bookmark-button" fade={false}>
  48. {t(getTooltipMessage())}
  49. </UncontrolledTooltip>
  50. { !hideTotalNumber && (
  51. <>
  52. <button
  53. type="button"
  54. id="po-total-bookmarks"
  55. className={`shadow-none btn btn-bookmark border-0
  56. total-bookmarks ${props.isBookmarked ? 'active' : ''}`}
  57. >
  58. {bookmarkCount ?? 0}
  59. </button>
  60. { bookmarkedUsers != null && (
  61. <Popover placement="bottom" isOpen={isPopoverOpen} target="po-total-bookmarks" toggle={togglePopover} trigger="legacy">
  62. <PopoverBody className="user-list-popover">
  63. <div className="px-2 text-right user-list-content text-truncate text-muted">
  64. {bookmarkedUsers.length ? <UserPictureList users={props.bookmarkedUsers} /> : t('No users have bookmarked yet')}
  65. </div>
  66. </PopoverBody>
  67. </Popover>
  68. ) }
  69. </>
  70. ) }
  71. </div>
  72. );
  73. };
  74. export default BookmarkButtons;