You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import dayjs from 'dayjs'
  6. import EditItem, { EditItemType } from './edit-item'
  7. import Drawer from '@/app/components/base/drawer-plus'
  8. import { MessageCheckRemove } from '@/app/components/base/icons/src/vender/line/communication'
  9. import DeleteConfirmModal from '@/app/components/base/modal/delete-confirm-modal'
  10. import { addAnnotation, editAnnotation } from '@/service/annotation'
  11. import Toast from '@/app/components/base/toast'
  12. import { useProviderContext } from '@/context/provider-context'
  13. import AnnotationFull from '@/app/components/billing/annotation-full'
  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 { plan, enableBilling } = useProviderContext()
  44. const isAdd = !annotationId
  45. const isAnnotationFull = (enableBilling && plan.usage.annotatedResponse >= plan.total.annotatedResponse)
  46. const handleSave = async (type: EditItemType, editedContent: string) => {
  47. let postQuery = query
  48. let postAnswer = answer
  49. if (type === EditItemType.Query)
  50. postQuery = editedContent
  51. else
  52. postAnswer = editedContent
  53. if (!isAdd) {
  54. await editAnnotation(appId, annotationId, {
  55. message_id: messageId,
  56. question: postQuery,
  57. answer: postAnswer,
  58. })
  59. onEdited(postQuery, postAnswer)
  60. }
  61. else {
  62. const res: any = await addAnnotation(appId, {
  63. question: postQuery,
  64. answer: postAnswer,
  65. message_id: messageId,
  66. })
  67. onAdded(res.id, res.account?.name, postQuery, postAnswer)
  68. }
  69. Toast.notify({
  70. message: t('common.api.actionSuccess') as string,
  71. type: 'success',
  72. })
  73. }
  74. const [showModal, setShowModal] = useState(false)
  75. return (
  76. <div>
  77. <Drawer
  78. isShow={isShow}
  79. onHide={onHide}
  80. maxWidthClassName='!max-w-[480px]'
  81. title={t('appAnnotation.editModal.title') as string}
  82. body={(
  83. <div className='p-6 pb-4 space-y-6'>
  84. <EditItem
  85. type={EditItemType.Query}
  86. content={query}
  87. readonly={(isAdd && isAnnotationFull) || onlyEditResponse}
  88. onSave={editedContent => handleSave(EditItemType.Query, editedContent)}
  89. />
  90. <EditItem
  91. type={EditItemType.Answer}
  92. content={answer}
  93. readonly={isAdd && isAnnotationFull}
  94. onSave={editedContent => handleSave(EditItemType.Answer, editedContent)}
  95. />
  96. </div>
  97. )}
  98. foot={
  99. <div>
  100. {isAnnotationFull && (
  101. <div className='mt-6 mb-4 px-6'>
  102. <AnnotationFull />
  103. </div>
  104. )}
  105. {
  106. annotationId
  107. ? (
  108. <div className='px-4 flex h-16 items-center justify-between border-t border-black/5 bg-gray-50 rounded-bl-xl rounded-br-xl leading-[18px] text-[13px] font-medium text-gray-500'>
  109. <div
  110. className='flex items-center pl-3 space-x-2 cursor-pointer'
  111. onClick={() => setShowModal(true)}
  112. >
  113. <MessageCheckRemove />
  114. <div>{t('appAnnotation.editModal.removeThisCache')}</div>
  115. </div>
  116. {createdAt && <div>{t('appAnnotation.editModal.createdAt')}&nbsp;{dayjs(createdAt * 1000).format('YYYY-MM-DD HH:mm')}</div>}
  117. </div>
  118. )
  119. : undefined
  120. }
  121. </div>
  122. }
  123. >
  124. </Drawer>
  125. <DeleteConfirmModal
  126. isShow={showModal}
  127. onHide={() => setShowModal(false)}
  128. onRemove={() => {
  129. onRemove()
  130. setShowModal(false)
  131. onHide()
  132. }}
  133. text={t('appDebug.feature.annotation.removeConfirm') as string}
  134. />
  135. </div>
  136. )
  137. }
  138. export default React.memo(EditAnnotationModal)