index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useRef, useState } from 'react'
  4. import { useHover } from 'ahooks'
  5. import { useTranslation } from 'react-i18next'
  6. import cn from '@/utils/classnames'
  7. import { MessageCheckRemove, MessageFastPlus } from '@/app/components/base/icons/src/vender/line/communication'
  8. import { MessageFast } from '@/app/components/base/icons/src/vender/solid/communication'
  9. import { Edit04 } from '@/app/components/base/icons/src/vender/line/general'
  10. import RemoveAnnotationConfirmModal from '@/app/components/app/annotation/remove-annotation-confirm-modal'
  11. import Tooltip from '@/app/components/base/tooltip'
  12. import { addAnnotation, delAnnotation } from '@/service/annotation'
  13. import Toast from '@/app/components/base/toast'
  14. import { useProviderContext } from '@/context/provider-context'
  15. import { useModalContext } from '@/context/modal-context'
  16. type Props = {
  17. appId: string
  18. messageId?: string
  19. annotationId?: string
  20. className?: string
  21. cached: boolean
  22. query: string
  23. answer: string
  24. onAdded: (annotationId: string, authorName: string) => void
  25. onEdit: () => void
  26. onRemoved: () => void
  27. }
  28. const CacheCtrlBtn: FC<Props> = ({
  29. className,
  30. cached,
  31. query,
  32. answer,
  33. appId,
  34. messageId,
  35. annotationId,
  36. onAdded,
  37. onEdit,
  38. onRemoved,
  39. }) => {
  40. const { t } = useTranslation()
  41. const { plan, enableBilling } = useProviderContext()
  42. const isAnnotationFull = (enableBilling && plan.usage.annotatedResponse >= plan.total.annotatedResponse)
  43. const { setShowAnnotationFullModal } = useModalContext()
  44. const [showModal, setShowModal] = useState(false)
  45. const cachedBtnRef = useRef<HTMLDivElement>(null)
  46. const isCachedBtnHovering = useHover(cachedBtnRef)
  47. const handleAdd = async () => {
  48. if (isAnnotationFull) {
  49. setShowAnnotationFullModal()
  50. return
  51. }
  52. const res: any = await addAnnotation(appId, {
  53. message_id: messageId,
  54. question: query,
  55. answer,
  56. })
  57. Toast.notify({
  58. message: t('common.api.actionSuccess') as string,
  59. type: 'success',
  60. })
  61. onAdded(res.id, res.account?.name)
  62. }
  63. const handleRemove = async () => {
  64. await delAnnotation(appId, annotationId!)
  65. Toast.notify({
  66. message: t('common.api.actionSuccess') as string,
  67. type: 'success',
  68. })
  69. onRemoved()
  70. setShowModal(false)
  71. }
  72. return (
  73. <div className={cn('inline-block', className)}>
  74. <div className='inline-flex p-0.5 space-x-0.5 rounded-lg bg-white border border-gray-100 shadow-md text-gray-500 cursor-pointer'>
  75. {cached
  76. ? (
  77. <div>
  78. <div
  79. ref={cachedBtnRef}
  80. className={cn(isCachedBtnHovering ? 'bg-[#FEF3F2] text-[#D92D20]' : 'bg-[#EEF4FF] text-[#444CE7]', 'flex p-1 space-x-1 items-center rounded-md leading-4 text-xs font-medium')}
  81. onClick={() => setShowModal(true)}
  82. >
  83. {!isCachedBtnHovering
  84. ? (
  85. <>
  86. <MessageFast className='w-4 h-4' />
  87. <div>{t('appDebug.feature.annotation.cached')}</div>
  88. </>
  89. )
  90. : <>
  91. <MessageCheckRemove className='w-4 h-4' />
  92. <div>{t('appDebug.feature.annotation.remove')}</div>
  93. </>}
  94. </div>
  95. </div>
  96. )
  97. : answer
  98. ? (
  99. <Tooltip
  100. popupContent={t('appDebug.feature.annotation.add')}
  101. >
  102. <div
  103. className='p-1 rounded-md hover:bg-[#EEF4FF] hover:text-[#444CE7] cursor-pointer'
  104. onClick={handleAdd}
  105. >
  106. <MessageFastPlus className='w-4 h-4' />
  107. </div>
  108. </Tooltip>
  109. )
  110. : null
  111. }
  112. <Tooltip
  113. popupContent={t('appDebug.feature.annotation.edit')}
  114. >
  115. <div
  116. className='p-1 cursor-pointer rounded-md hover:bg-black/5'
  117. onClick={onEdit}
  118. >
  119. <Edit04 className='w-4 h-4' />
  120. </div>
  121. </Tooltip>
  122. </div>
  123. <RemoveAnnotationConfirmModal
  124. isShow={showModal}
  125. onHide={() => setShowModal(false)}
  126. onRemove={handleRemove}
  127. />
  128. </div>
  129. )
  130. }
  131. export default React.memo(CacheCtrlBtn)