index.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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={(e) => {
  45. e.stopPropagation()
  46. setShowModal(true)
  47. }
  48. }
  49. >
  50. <File02 className='w-4 h-4 text-gray-500' />
  51. </div>
  52. </div>
  53. </Tooltip>
  54. )
  55. }
  56. {
  57. showModal && (
  58. <PromptLogModal
  59. width={width}
  60. log={log}
  61. onCancel={() => setShowModal(false)}
  62. />
  63. )
  64. }
  65. </>
  66. )
  67. }
  68. export default Log