index.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { Dispatch, FC, ReactNode, RefObject, SetStateAction } from 'react'
  2. import { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { uniqueId } from 'lodash-es'
  5. import { File02 } from '@/app/components/base/icons/src/vender/line/files'
  6. import PromptLogModal from '@/app/components/base/prompt-log-modal'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. export type LogData = {
  9. role: string
  10. text: string
  11. }
  12. type LogProps = {
  13. containerRef: RefObject<HTMLElement>
  14. log: LogData[]
  15. children?: (v: Dispatch<SetStateAction<boolean>>) => ReactNode
  16. }
  17. const Log: FC<LogProps> = ({
  18. containerRef,
  19. children,
  20. log,
  21. }) => {
  22. const { t } = useTranslation()
  23. const [showModal, setShowModal] = useState(false)
  24. const [width, setWidth] = useState(0)
  25. const adjustModalWidth = () => {
  26. if (containerRef.current)
  27. setWidth(document.body.clientWidth - (containerRef.current?.clientWidth + 56 + 16))
  28. }
  29. useEffect(() => {
  30. adjustModalWidth()
  31. }, [])
  32. return (
  33. <>
  34. {
  35. children
  36. ? children(setShowModal)
  37. : (
  38. <Tooltip selector={`prompt-log-modal-trigger-${uniqueId()}`} content={t('common.operation.log') || ''}>
  39. <div className={`
  40. hidden absolute -left-[14px] -top-[14px] group-hover:block w-7 h-7
  41. p-0.5 rounded-lg border-[0.5px] border-gray-100 bg-white shadow-md cursor-pointer
  42. `}>
  43. <div
  44. className='flex items-center justify-center rounded-md w-full h-full hover:bg-gray-100'
  45. onClick={(e) => {
  46. e.stopPropagation()
  47. setShowModal(true)
  48. }
  49. }
  50. >
  51. <File02 className='w-4 h-4 text-gray-500' />
  52. </div>
  53. </div>
  54. </Tooltip>
  55. )
  56. }
  57. {
  58. showModal && (
  59. <PromptLogModal
  60. width={width}
  61. log={log}
  62. onCancel={() => setShowModal(false)}
  63. />
  64. )
  65. }
  66. </>
  67. )
  68. }
  69. export default Log