index.tsx 1.9 KB

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