index.tsx 5.3 KB

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