GuideQuestions.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { useEffect, useMemo, useState } from "react"
  2. import { useMessageStore } from "./messageStore"
  3. import { useTranslation } from "react-i18next"
  4. // 引导词推荐
  5. export default function GuideQuestions({ locked, chatId, questions, onClick }) {
  6. const [showGuideQuestion, setShowGuideQuestion] = useMessageStore(state => [state.showGuideQuestion, state.setShowGuideQuestion])
  7. const { t } = useTranslation()
  8. useEffect(() => {
  9. questions.length && setShowGuideQuestion(true)
  10. }, [chatId])
  11. const words = useMemo(() => {
  12. if (questions.length < 4) return questions
  13. // 随机按序取三个
  14. const res = []
  15. const randomIndex = Math.floor(Math.random() * questions.length)
  16. for (let i = 0; i < 3; i++) {
  17. const item = questions[(randomIndex + i) % (questions.length - 1)]
  18. res.push(item)
  19. }
  20. return res
  21. }, [questions])
  22. if (locked || !words.length) return null
  23. if (showGuideQuestion) return <div className="relative">
  24. <div className="absolute left-[14px] bottom-0">
  25. <p className="text-[#fff] mb-[10px] bg-[]">推荐问题</p>
  26. {
  27. words.map((question, index) => (
  28. <div
  29. key={index}
  30. className="w-fit bg-[#52430c] shadow-md text-[#fff] rounded-md mb-1 px-4 py-1 text-sm cursor-pointer"
  31. onClick={() => {
  32. setShowGuideQuestion(false)
  33. onClick(question)
  34. }}
  35. >{question}</div>
  36. ))
  37. }
  38. </div>
  39. </div>
  40. return null
  41. };