index.tsx 5.2 KB

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