Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5747 | Rev 5772 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import React, { useEffect, useRef, useState } from 'react'
import RecommendIcon from '@mui/icons-material/Recommend'
import FavoriteIcon from '@mui/icons-material/FavoriteTwoTone'
import VolunteerActivismIcon from '@mui/icons-material/VolunteerActivism'
import EmojiEmotionsIcon from '@mui/icons-material/EmojiEmotions'
import TungstenIcon from '@mui/icons-material/Tungsten'
import { debounce } from '../../../utils'
import useOutsideClick from '../../../hooks/useOutsideClick'

export const ReactionButton = () => {
  const [settedReaction, setSettedReaction] = useState(null)
  const [showReactions, setShowReactions] = useState(false)
  const rectionBtn = useRef(null)
  const outsideClick = useOutsideClick(rectionBtn)

  const reactions = [
    {
      type: 'r',
      icon: <RecommendIcon style={{ color: '#7405f9' }} />,
    },
    {
      type: 's',
      icon: <VolunteerActivismIcon style={{ color: '#6495ED' }} />,
    },
    {
      type: 'l',
      icon: <FavoriteIcon style={{ color: '#DF704D' }} />,
    },
    {
      type: 'i',
      icon: (
        <TungstenIcon
          style={{ color: '#F5BB5C', transform: 'rotate(180deg)' }}
        />
      ),
    },
    {
      type: 'f',
      icon: <EmojiEmotionsIcon style={{ color: '#FF7F50' }} />,
    },
  ]

  const onHover = debounce(() => setShowReactions(true), 500)

  const onUnhover = debounce((e) => setShowReactions(false), 500)

  const handleClick = (reaction) => {
    setSettedReaction(reaction)
    setShowReactions(false)
  }

  useEffect(() => {
    if (outsideClick) {
      setShowReactions(false)
    }
  }, [outsideClick])

  return (
    <button
      type="button"
      className="reaction-btn"
      onMouseOver={onHover}
      onMouseOut={onUnhover}
      ref={rectionBtn}
    >
      {settedReaction ? (
        reactions.find((reaction) => reaction.type === settedReaction).icon
      ) : (
        <RecommendIcon style={{ color: '#626d7a' }} />
      )}
      <div className={`reactions ${showReactions ? 'active' : ''}`}>
        {reactions.map((reaction) => (
          <button
            key={reaction.type}
            onClick={() => handleClick(reaction.type)}
          >
            {reaction.icon}
          </button>
        ))}
      </div>
    </button>
  )
}