index.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useRef } from 'react'
  4. import { useContext } from 'use-context-selector'
  5. import s from '../style.module.css'
  6. import type { IChatItem } from '../type'
  7. import Log from '../log'
  8. import MoreInfo from '../more-info'
  9. import AppContext from '@/context/app-context'
  10. import { Markdown } from '@/app/components/base/markdown'
  11. import ImageGallery from '@/app/components/base/image-gallery'
  12. type IQuestionProps = Pick<IChatItem, 'id' | 'content' | 'more' | 'useCurrentUserAvatar'> & {
  13. isShowPromptLog?: boolean
  14. item: IChatItem
  15. isResponding?: boolean
  16. }
  17. const Question: FC<IQuestionProps> = ({ id, content, more, useCurrentUserAvatar, isShowPromptLog, item, isResponding }) => {
  18. const { userProfile } = useContext(AppContext)
  19. const userName = userProfile?.name
  20. const ref = useRef(null)
  21. const imgSrcs = item.message_files?.map(item => item.url)
  22. return (
  23. <div className={`flex items-start justify-end ${isShowPromptLog && 'first-of-type:pt-[14px]'}`} key={id} ref={ref}>
  24. <div className={s.questionWrapWrap}>
  25. <div className={`${s.question} group relative text-sm text-gray-900`}>
  26. {
  27. isShowPromptLog && !isResponding && (
  28. <Log log={item.log!} containerRef={ref} />
  29. )
  30. }
  31. <div
  32. className={'mr-2 py-3 px-4 bg-blue-500 rounded-tl-2xl rounded-b-2xl'}
  33. >
  34. {imgSrcs && imgSrcs.length > 0 && (
  35. <ImageGallery srcs={imgSrcs} />
  36. )}
  37. <Markdown content={content} />
  38. </div>
  39. </div>
  40. {more && <MoreInfo more={more} isQuestion={true} />}
  41. </div>
  42. {useCurrentUserAvatar
  43. ? (
  44. <div className='w-10 h-10 shrink-0 leading-10 text-center mr-2 rounded-full bg-primary-600 text-white'>
  45. {userName?.[0].toLocaleUpperCase()}
  46. </div>
  47. )
  48. : (
  49. <div className={`${s.questionIcon} w-10 h-10 shrink-0 `}></div>
  50. )}
  51. </div>
  52. )
  53. }
  54. export default React.memo(Question)