|
| 1 | +import React, { useEffect, useState, useRef } from "react"; |
| 2 | +import Image from "next/image"; |
| 3 | +import GurubuAITooltip from "./gurubu-ai-tooltip"; |
| 4 | +import { motion } from "framer-motion"; |
| 5 | +import { useGroomingRoom } from "@/contexts/GroomingRoomContext"; |
| 6 | + |
| 7 | +interface GurubuAIParticipantProps { |
| 8 | + sortedParticipants: string[]; |
| 9 | +} |
| 10 | + |
| 11 | +const GurubuAIParticipant = ({ |
| 12 | + sortedParticipants, |
| 13 | +}: GurubuAIParticipantProps) => { |
| 14 | + const { groomingInfo } = useGroomingRoom(); |
| 15 | + const [aiMessage, setAiMessage] = useState<string>(""); |
| 16 | + const [showTooltip, setShowTooltip] = useState(false); |
| 17 | + const participantRef = useRef<HTMLLIElement>(null); |
| 18 | + |
| 19 | + const isSFWCBoard = groomingInfo?.selectedBoard?.includes("SFWC"); |
| 20 | + const isGroomingInfoLoaded = Boolean(Object.keys(groomingInfo).length); |
| 21 | + |
| 22 | + const getAllParticipantsVotes = () => { |
| 23 | + const allParticipantsVotes: { storyPoint: string; nickname: string }[] = []; |
| 24 | + sortedParticipants?.forEach((participantKey) => { |
| 25 | + const participant = groomingInfo?.participants[participantKey]; |
| 26 | + allParticipantsVotes.push({ |
| 27 | + storyPoint: participant?.votes?.["storyPoint"], |
| 28 | + nickname: participant?.nickname, |
| 29 | + }); |
| 30 | + }); |
| 31 | + return allParticipantsVotes; |
| 32 | + }; |
| 33 | + |
| 34 | + const generateAIAnalysis = ( |
| 35 | + votes: { storyPoint: string; nickname: string }[] |
| 36 | + ) => { |
| 37 | + const validVotes = votes.filter( |
| 38 | + (vote) => |
| 39 | + vote.storyPoint && |
| 40 | + vote.storyPoint !== "?" && |
| 41 | + vote.storyPoint !== "break" |
| 42 | + ); |
| 43 | + if (validVotes.length === 0) return ""; |
| 44 | + |
| 45 | + return `Based on the analysis of the tasks we previously scored, I believe the score for this task should be ${Number( |
| 46 | + groomingInfo?.score |
| 47 | + )?.toFixed( |
| 48 | + 0 |
| 49 | + )}. The acceptance criteria are as follows. Our team is experienced in this area, and my suggestion is as follows.`; |
| 50 | + }; |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + let timeoutId: NodeJS.Timeout; |
| 54 | + |
| 55 | + if (isGroomingInfoLoaded && groomingInfo?.isResultShown && isSFWCBoard) { |
| 56 | + // example result of allParticipantsVotes: { storyPoint: "1", nickname: "John Doe" } |
| 57 | + const allParticipantsVotes = getAllParticipantsVotes(); |
| 58 | + const analysis = generateAIAnalysis(allParticipantsVotes); |
| 59 | + setAiMessage(analysis); |
| 60 | + |
| 61 | + // Delay showing the tooltip to ensure proper positioning |
| 62 | + timeoutId = setTimeout(() => { |
| 63 | + setShowTooltip(true); |
| 64 | + }, 300); |
| 65 | + } else { |
| 66 | + setShowTooltip(false); |
| 67 | + } |
| 68 | + |
| 69 | + return () => { |
| 70 | + if (timeoutId) { |
| 71 | + clearTimeout(timeoutId); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // add sortedParticipants dependency if you want to trigger the tooltip when the votes change |
| 76 | + }, [groomingInfo.isResultShown, isGroomingInfoLoaded]); |
| 77 | + |
| 78 | + const handleCloseTooltip = () => { |
| 79 | + setShowTooltip(false); |
| 80 | + }; |
| 81 | + |
| 82 | + if (!groomingInfo.selectedBoard || !isSFWCBoard) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <motion.li |
| 88 | + ref={participantRef} |
| 89 | + layout |
| 90 | + initial={{ opacity: 0, scale: 0.9 }} |
| 91 | + animate={{ opacity: 1, scale: 1 }} |
| 92 | + exit={{ opacity: 0, scale: 0.9 }} |
| 93 | + transition={{ duration: 0.3, ease: "easeInOut" }} |
| 94 | + key="gurubu-ai-participant" |
| 95 | + className="gurubu-ai-participant" |
| 96 | + > |
| 97 | + <GurubuAITooltip |
| 98 | + message={aiMessage} |
| 99 | + isVisible={showTooltip} |
| 100 | + anchorRef={participantRef} |
| 101 | + onClose={handleCloseTooltip} |
| 102 | + /> |
| 103 | + <div className="profile-container"> |
| 104 | + <div className="avatar"> |
| 105 | + <Image src="https://cdn.dsmcdn.com/web/develop/gurubu-ai.svg" alt="GuruBu AI" width={32} height={32} /> |
| 106 | + </div> |
| 107 | + <div className="name">GuruBu AI</div> |
| 108 | + </div> |
| 109 | + <div className="score"> |
| 110 | + {groomingInfo.isResultShown |
| 111 | + ? Number(groomingInfo?.score)?.toFixed(0) |
| 112 | + : "Thinking..."} |
| 113 | + </div> |
| 114 | + </motion.li> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export default GurubuAIParticipant; |
0 commit comments