NotAvailableForGuest.jsx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import PropTypes from 'prop-types';
  4. import { UncontrolledTooltip } from 'reactstrap';
  5. import { useIsGuestUser } from '~/stores/context';
  6. const NotAvailableForGuest = (props) => {
  7. const { children } = props;
  8. const { t } = useTranslation();
  9. const { data: isGuestUser } = useIsGuestUser();
  10. if (!isGuestUser) {
  11. return props.children;
  12. }
  13. const id = children.props.id || `grw-not-available-for-guest-${Math.random().toString(32).substring(2)}`;
  14. // clone and add className
  15. const clonedChild = React.cloneElement(children, {
  16. id,
  17. className: `${children.props.className} grw-not-available-for-guest`,
  18. onClick: () => { /* do nothing */ },
  19. });
  20. return (
  21. <>
  22. { clonedChild }
  23. <UncontrolledTooltip placement="top" target={id}>{t('Not available for guest')}</UncontrolledTooltip>
  24. </>
  25. );
  26. };
  27. NotAvailableForGuest.propTypes = {
  28. children: PropTypes.node.isRequired,
  29. };
  30. export default NotAvailableForGuest;