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.

annotation-ctrl-button.tsx 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import {
  6. RiEditLine,
  7. RiFileEditLine,
  8. } from '@remixicon/react'
  9. import ActionButton from '@/app/components/base/action-button'
  10. import Tooltip from '@/app/components/base/tooltip'
  11. import { addAnnotation } from '@/service/annotation'
  12. import Toast from '@/app/components/base/toast'
  13. import { useProviderContext } from '@/context/provider-context'
  14. import { useModalContext } from '@/context/modal-context'
  15. type Props = {
  16. appId: string
  17. messageId?: string
  18. cached: boolean
  19. query: string
  20. answer: string
  21. onAdded: (annotationId: string, authorName: string) => void
  22. onEdit: () => void
  23. }
  24. const AnnotationCtrlButton: FC<Props> = ({
  25. cached,
  26. query,
  27. answer,
  28. appId,
  29. messageId,
  30. onAdded,
  31. onEdit,
  32. }) => {
  33. const { t } = useTranslation()
  34. const { plan, enableBilling } = useProviderContext()
  35. const isAnnotationFull = (enableBilling && plan.usage.annotatedResponse >= plan.total.annotatedResponse)
  36. const { setShowAnnotationFullModal } = useModalContext()
  37. const handleAdd = async () => {
  38. if (isAnnotationFull) {
  39. setShowAnnotationFullModal()
  40. return
  41. }
  42. const res: any = await addAnnotation(appId, {
  43. message_id: messageId,
  44. question: query,
  45. answer,
  46. })
  47. Toast.notify({
  48. message: t('common.api.actionSuccess') as string,
  49. type: 'success',
  50. })
  51. onAdded(res.id, res.account?.name)
  52. }
  53. return (
  54. <>
  55. {cached && (
  56. <Tooltip
  57. popupContent={t('appDebug.feature.annotation.edit')}
  58. >
  59. <ActionButton onClick={onEdit}>
  60. <RiEditLine className='h-4 w-4' />
  61. </ActionButton>
  62. </Tooltip>
  63. )}
  64. {!cached && answer && (
  65. <Tooltip
  66. popupContent={t('appDebug.feature.annotation.add')}
  67. >
  68. <ActionButton onClick={handleAdd}>
  69. <RiFileEditLine className='h-4 w-4' />
  70. </ActionButton>
  71. </Tooltip>
  72. )}
  73. </>
  74. )
  75. }
  76. export default React.memo(AnnotationCtrlButton)