index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import EditItem, { EditItemType } from './edit-item'
  6. import Drawer from '@/app/components/base/drawer-plus'
  7. import { MessageCheckRemove } from '@/app/components/base/icons/src/vender/line/communication'
  8. import Confirm from '@/app/components/base/confirm'
  9. import { addAnnotation, editAnnotation } from '@/service/annotation'
  10. import Toast from '@/app/components/base/toast'
  11. import { useProviderContext } from '@/context/provider-context'
  12. import AnnotationFull from '@/app/components/billing/annotation-full'
  13. import useTimestamp from '@/hooks/use-timestamp'
  14. type Props = {
  15. isShow: boolean
  16. onHide: () => void
  17. appId: string
  18. messageId?: string
  19. annotationId?: string
  20. query: string
  21. answer: string
  22. onEdited: (editedQuery: string, editedAnswer: string) => void
  23. onAdded: (annotationId: string, authorName: string, editedQuery: string, editedAnswer: string) => void
  24. createdAt?: number
  25. onRemove: () => void
  26. onlyEditResponse?: boolean
  27. }
  28. const EditAnnotationModal: FC<Props> = ({
  29. isShow,
  30. onHide,
  31. query,
  32. answer,
  33. onEdited,
  34. onAdded,
  35. appId,
  36. messageId,
  37. annotationId,
  38. createdAt,
  39. onRemove,
  40. onlyEditResponse,
  41. }) => {
  42. const { t } = useTranslation()
  43. const { formatTime } = useTimestamp()
  44. const { plan, enableBilling } = useProviderContext()
  45. const isAdd = !annotationId
  46. const isAnnotationFull = (enableBilling && plan.usage.annotatedResponse >= plan.total.annotatedResponse)
  47. const handleSave = async (type: EditItemType, editedContent: string) => {
  48. let postQuery = query
  49. let postAnswer = answer
  50. if (type === EditItemType.Query)
  51. postQuery = editedContent
  52. else
  53. postAnswer = editedContent
  54. if (!isAdd) {
  55. await editAnnotation(appId, annotationId, {
  56. message_id: messageId,
  57. question: postQuery,
  58. answer: postAnswer,
  59. })
  60. onEdited(postQuery, postAnswer)
  61. }
  62. else {
  63. const res: any = await addAnnotation(appId, {
  64. question: postQuery,
  65. answer: postAnswer,
  66. message_id: messageId,
  67. })
  68. onAdded(res.id, res.account?.name, postQuery, postAnswer)
  69. }
  70. Toast.notify({
  71. message: t('common.api.actionSuccess') as string,
  72. type: 'success',
  73. })
  74. }
  75. const [showModal, setShowModal] = useState(false)
  76. return (
  77. <div>
  78. <Drawer
  79. isShow={isShow}
  80. onHide={onHide}
  81. maxWidthClassName='!max-w-[480px]'
  82. title={t('appAnnotation.editModal.title') as string}
  83. body={(
  84. <div>
  85. <div className='p-6 pb-4 space-y-6'>
  86. <EditItem
  87. type={EditItemType.Query}
  88. content={query}
  89. readonly={(isAdd && isAnnotationFull) || onlyEditResponse}
  90. onSave={editedContent => handleSave(EditItemType.Query, editedContent)}
  91. />
  92. <EditItem
  93. type={EditItemType.Answer}
  94. content={answer}
  95. readonly={isAdd && isAnnotationFull}
  96. onSave={editedContent => handleSave(EditItemType.Answer, editedContent)}
  97. />
  98. <Confirm
  99. isShow={showModal}
  100. onCancel={() => setShowModal(false)}
  101. onConfirm={() => {
  102. onRemove()
  103. setShowModal(false)
  104. onHide()
  105. }}
  106. title={t('appDebug.feature.annotation.removeConfirm')}
  107. />
  108. </div>
  109. </div>
  110. )}
  111. foot={
  112. <div>
  113. {isAnnotationFull && (
  114. <div className='mt-6 mb-4 px-6'>
  115. <AnnotationFull />
  116. </div>
  117. )}
  118. {
  119. annotationId
  120. ? (
  121. <div className='px-4 flex h-16 items-center justify-between border-t border-divider-subtle bg-background-section-burn rounded-bl-xl rounded-br-xl system-sm-medium text-text-tertiary'>
  122. <div
  123. className='flex items-center pl-3 space-x-2 cursor-pointer'
  124. onClick={() => setShowModal(true)}
  125. >
  126. <MessageCheckRemove />
  127. <div>{t('appAnnotation.editModal.removeThisCache')}</div>
  128. </div>
  129. {createdAt && <div>{t('appAnnotation.editModal.createdAt')}&nbsp;{formatTime(createdAt, t('appLog.dateTimeFormat') as string)}</div>}
  130. </div>
  131. )
  132. : undefined
  133. }
  134. </div>
  135. }
  136. />
  137. </div>
  138. )
  139. }
  140. export default React.memo(EditAnnotationModal)