1- import { useState , useRef , useCallback , useMemo } from "react" ;
1+ import { useState , useRef , useCallback } from "react" ;
22import {
33 ChevronDownIcon ,
44 RefreshCw ,
@@ -10,40 +10,28 @@ import { useTranslation } from "react-i18next";
1010import { isNil } from "lodash-es" ;
1111import { Popover , PopoverButton , PopoverPanel } from "@headlessui/react" ;
1212import {
13- useAsyncEffect ,
1413 useDebounce ,
1514 useKeyPress ,
1615 usePagination ,
17- useReactive ,
1816} from "ahooks" ;
1917import clsx from "clsx" ;
2018
21- import { useAppStore } from "@/stores/appStore" ;
2219import logoImg from "@/assets/icon.svg" ;
23- import platformAdapter from "@/utils/platformAdapter" ;
2420import VisibleKey from "@/components/Common/VisibleKey" ;
2521import { useConnectStore } from "@/stores/connectStore" ;
2622import FontIcon from "@/components/Common/Icons/FontIcon" ;
27- import { useChatStore } from "@/stores/chatStore" ;
2823import { useShortcutsStore } from "@/stores/shortcutsStore" ;
29- import { Post } from "@/api/axiosRequest " ;
30- import NoDataImage from "../ Common/NoDataImage " ;
31- import PopoverInput from "../Common/PopoverInput " ;
24+ import NoDataImage from "@/components/Common/NoDataImage " ;
25+ import PopoverInput from "@/components/ Common/PopoverInput " ;
26+ import { AssistantFetcher } from "./AssistantFetcher " ;
3227
3328interface AssistantListProps {
3429 assistantIDs ?: string [ ] ;
3530}
3631
37- interface State {
38- allAssistants : any [ ] ;
39- }
40-
4132export function AssistantList ( { assistantIDs = [ ] } : AssistantListProps ) {
4233 const { t } = useTranslation ( ) ;
43- const { connected } = useChatStore ( ) ;
4434
45- const isTauri = useAppStore ( ( state ) => state . isTauri ) ;
46- const setAssistantList = useConnectStore ( ( state ) => state . setAssistantList ) ;
4735 const currentService = useConnectStore ( ( state ) => state . currentService ) ;
4836 const currentAssistant = useConnectStore ( ( state ) => state . currentAssistant ) ;
4937 const setCurrentAssistant = useConnectStore ( ( state ) => {
@@ -57,130 +45,15 @@ export function AssistantList({ assistantIDs = [] }: AssistantListProps) {
5745 const searchInputRef = useRef < HTMLInputElement > ( null ) ;
5846 const [ keyword , setKeyword ] = useState ( "" ) ;
5947 const debounceKeyword = useDebounce ( keyword , { wait : 500 } ) ;
60- const state = useReactive < State > ( {
61- allAssistants : [ ] ,
62- } ) ;
63-
64- const currentServiceId = useMemo ( ( ) => {
65- return currentService ?. id ;
66- } , [ connected , currentService ?. id ] ) ;
67-
68- const fetchAssistant = async ( params : {
69- current : number ;
70- pageSize : number ;
71- } ) => {
72- try {
73- const { pageSize, current } = params ;
74-
75- const from = ( current - 1 ) * pageSize ;
76- const size = pageSize ;
77-
78- let response : any ;
79-
80- const body : Record < string , any > = {
81- serverId : currentServiceId ,
82- from,
83- size,
84- } ;
85-
86- body . query = {
87- bool : {
88- must : [ { term : { enabled : true } } ] ,
89- } ,
90- } ;
91-
92- if ( debounceKeyword ) {
93- body . query . bool . must . push ( {
94- query_string : {
95- fields : [ "combined_fulltext" ] ,
96- query : debounceKeyword ,
97- fuzziness : "AUTO" ,
98- fuzzy_prefix_length : 2 ,
99- fuzzy_max_expansions : 10 ,
100- fuzzy_transpositions : true ,
101- allow_leading_wildcard : false ,
102- } ,
103- } ) ;
104- }
105- if ( assistantIDs . length > 0 ) {
106- body . query . bool . must . push ( {
107- terms : {
108- id : assistantIDs . map ( ( id ) => id ) ,
109- } ,
110- } ) ;
111- }
112-
113- if ( isTauri ) {
114- if ( ! currentServiceId ) {
115- throw new Error ( "currentServiceId is undefined" ) ;
116- }
117-
118- response = await platformAdapter . commands ( "assistant_search" , body ) ;
119- } else {
120- const [ error , res ] = await Post ( `/assistant/_search` , body ) ;
121-
122- if ( error ) {
123- throw new Error ( error ) ;
124- }
125-
126- response = res ;
127- }
128-
129- let assistantList = response ?. hits ?. hits ?? [ ] ;
130-
131- console . log ( "assistantList" , assistantList ) ;
132-
133- for ( const item of assistantList ) {
134- const index = state . allAssistants . findIndex ( ( allItem : any ) => {
135- return item . _id === allItem . _id ;
136- } ) ;
13748
138- if ( index === - 1 ) {
139- state . allAssistants . push ( item ) ;
140- } else {
141- state . allAssistants [ index ] = item ;
142- }
143- }
144-
145- //console.log("state.allAssistants", state.allAssistants);
146-
147- const matched = state . allAssistants . find ( ( item : any ) => {
148- return item . _id === currentAssistant ?. _id ;
149- } ) ;
150-
151- //console.log("matched", matched);
152-
153- if ( matched ) {
154- setCurrentAssistant ( matched ) ;
155- } else {
156- setCurrentAssistant ( assistantList [ 0 ] ) ;
157- }
158-
159- return {
160- total : response . hits . total . value ,
161- list : assistantList ,
162- } ;
163- } catch ( error ) {
164- setCurrentAssistant ( null ) ;
165-
166- console . error ( "assistant_search" , error ) ;
167-
168- return {
169- total : 0 ,
170- list : [ ] ,
171- } ;
172- }
173- } ;
174-
175- useAsyncEffect ( async ( ) => {
176- const data = await fetchAssistant ( { current : 1 , pageSize : 1000 } ) ;
177-
178- setAssistantList ( data . list ) ;
179- } , [ currentServiceId ] ) ;
49+ const { fetchAssistant } = AssistantFetcher ( {
50+ debounceKeyword,
51+ assistantIDs,
52+ } ) ;
18053
18154 const { pagination, runAsync } = usePagination ( fetchAssistant , {
18255 defaultPageSize : 5 ,
183- refreshDeps : [ currentServiceId , debounceKeyword ] ,
56+ refreshDeps : [ currentService ?. id , debounceKeyword ] ,
18457 onSuccess ( data ) {
18558 setAssistants ( data . list ) ;
18659 } ,
0 commit comments