index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. 'use client'
  2. import List from './list'
  3. import React, { useEffect, useState } from 'react'
  4. import Input from '@/app/components/base/input'
  5. import cn from '@/utils/classnames'
  6. import { RiAddLine, RiPriceTagLine, RiRefreshLine, RiSearchLine } from '@remixicon/react'
  7. import Button from '@/app/components/base/button'
  8. import useSWR from 'swr'
  9. import { fetchCorpus, fetchIntentName, fetchIntentType } from '@/service/common'
  10. import { SimpleSelect } from '@/app/components/base/select'
  11. import DetailModal from './detail-modal'
  12. import TypeModal from './type-modal'
  13. const CorpusIndex = () => {
  14. const [page, setPage] = React.useState<number>(0)
  15. const [limit, setLimit] = useState<number>(10)
  16. const [question, setQuestion] = useState<string>('') // the input value
  17. const [intentType, setIntentType] = useState<string>('') // the input value
  18. const { data: dataOptionsIntentType }: any = useSWR(
  19. {
  20. url: '/xxx',
  21. params: {
  22. page: 1,
  23. limit: 1000,
  24. },
  25. },
  26. fetchIntentType,
  27. )
  28. const optionsIntentType: any = dataOptionsIntentType?.data.map((v: any) => ({ name: v.name, value: v.id })) || []
  29. const [intentName, setIntentName] = useState<string>('') // the input value
  30. const { data: dataOptionsIntentName }: any = useSWR(
  31. {
  32. url: '/xxx',
  33. params: {
  34. page: 1,
  35. limit: 1000,
  36. intentType,
  37. },
  38. },
  39. fetchIntentName,
  40. )
  41. const optionsIntentName: any = dataOptionsIntentName?.data.map((v: any) => ({ name: v.name, value: v.id })) || []
  42. const [query, setQuery] = useState<any>({})
  43. const { data, mutate }: any = useSWR(
  44. {
  45. url: '/xxx',
  46. params: {
  47. page: page + 1,
  48. limit,
  49. ...query,
  50. },
  51. },
  52. fetchCorpus,
  53. )
  54. const list: any = data?.data || []
  55. const total = data?.total || 0
  56. const handleSearch = (reset = false) => {
  57. if (reset) {
  58. setQuestion('')
  59. setIntentName('')
  60. setIntentType('')
  61. }
  62. const params: any = {}
  63. if (question)
  64. params.question = question
  65. if (intentType)
  66. params.intentType = intentType
  67. if (intentName)
  68. params.intentName = intentName
  69. setQuery(params)
  70. setPage(0)
  71. }
  72. useEffect(() => {
  73. mutate()
  74. }, [page, limit])
  75. const [detailModalVisible, setDetailModalVisible] = useState(false)
  76. const [transfer, setTransfer] = useState<any>({
  77. mode: 'add',
  78. row: {},
  79. })
  80. const [showIntentTypeModalVisible, setShowIntentTypeModalVisible] = useState(true)
  81. return (
  82. <>
  83. <div className='flex h-full w-full flex-col bg-background-default-subtle p-6'>
  84. <div className="flex items-center gap-2">
  85. <div className="flex shrink-0 items-center text-gray-500">
  86. 意图类型
  87. <div className="ml-2 flex h-[32px]">
  88. <SimpleSelect
  89. className="h-[32px] w-[200px]"
  90. defaultValue={intentType}
  91. onSelect={(i: any) => {
  92. setIntentType(i.value)
  93. }}
  94. items={optionsIntentType}
  95. allowSearch={false}
  96. placeholder="请选择意图类型"
  97. />
  98. </div>
  99. </div>
  100. <div className="flex shrink-0 items-center text-gray-500">
  101. 意图名称
  102. <Input
  103. className="ml-2"
  104. showClearIcon
  105. wrapperClassName='!w-[200px]'
  106. value={question}
  107. onChange={e => setQuestion(e.target.value)}
  108. onClear={() => setQuestion('')}
  109. />
  110. </div>
  111. <Button variant='primary' className={cn('ml-auto shrink-0')} onClick={() => {
  112. handleSearch(false)
  113. }}>
  114. <RiSearchLine className='mr-1 h-4 w-4' />
  115. 搜索
  116. </Button>
  117. <Button variant='primary' className={cn('shrink-0')} onClick={() => {
  118. handleSearch(true)
  119. }}>
  120. <RiRefreshLine className='mr-1 h-4 w-4' />
  121. 重置
  122. </Button>
  123. </div>
  124. <div className="mt-2">
  125. <Button variant='primary' className={cn('shrink-0')}
  126. onClick={() => {
  127. setTransfer({ mode: 'add', row: null })
  128. setDetailModalVisible(true)
  129. }}>
  130. <RiAddLine className='mr-1 h-4 w-4' />
  131. 新增
  132. </Button>
  133. <Button variant='primary' className={cn('ml-2 shrink-0')}
  134. onClick={() => {
  135. setTransfer({ mode: 'add', row: null })
  136. setDetailModalVisible(true)
  137. }}>
  138. <RiPriceTagLine className='mr-1 h-4 w-4' />
  139. 意图类型管理
  140. </Button>
  141. </div>
  142. <div className="flex-1">
  143. <List
  144. list={list || []}
  145. onUpdate={mutate}
  146. pagination={{
  147. total,
  148. limit,
  149. onLimitChange: setLimit,
  150. current: page,
  151. onChange: setPage,
  152. }}
  153. />
  154. </div>
  155. </div>
  156. {
  157. detailModalVisible && (
  158. <DetailModal
  159. transfer={transfer}
  160. onCancel={() => setDetailModalVisible(false)}
  161. onSend={() => {
  162. setDetailModalVisible(false)
  163. mutate()
  164. }}
  165. />
  166. )
  167. }
  168. {
  169. showIntentTypeModalVisible && (
  170. <TypeModal
  171. onCancel={() => setShowIntentTypeModalVisible(false)}
  172. onSend={() => {
  173. setShowIntentTypeModalVisible(false)
  174. // mutate()
  175. }}
  176. />
  177. )
  178. }
  179. </>
  180. )
  181. }
  182. export default CorpusIndex