@@ -11,6 +11,8 @@ import { Get } from "@/api/axiosRequest";
1111import { useAppStore } from "@/stores/appStore" ;
1212import { useConnectStore } from "@/stores/connectStore" ;
1313import SearchEmpty from "../Common/SearchEmpty" ;
14+ import { Data } from "ahooks/lib/useInfiniteScroll/types" ;
15+ import { nanoid } from "nanoid" ;
1416
1517interface DocumentListProps {
1618 onSelectDocument : ( id : string ) => void ;
@@ -45,72 +47,88 @@ export const DocumentList: React.FC<DocumentListProps> = ({
4547 const containerRef = useRef < HTMLDivElement > ( null ) ;
4648 const itemRefs = useRef < ( HTMLDivElement | null ) [ ] > ( [ ] ) ;
4749 const [ isKeyboardMode , setIsKeyboardMode ] = useState ( false ) ;
50+ const taskIdRef = useRef ( nanoid ( ) ) ;
51+ const [ data , setData ] = useState < Data > ( ) ;
4852
4953 const querySourceTimeoutRef = useRef ( querySourceTimeout ) ;
5054 useEffect ( ( ) => {
5155 querySourceTimeoutRef . current = querySourceTimeout ;
5256 } , [ querySourceTimeout ] ) ;
5357
54- const { data, loading } = useInfiniteScroll (
55- async ( d ) => {
56- const from = d ?. list ?. length || 0 ;
57- let queryStrings : any = {
58+ const getData = async ( taskId : string , data ?: Data ) => {
59+ const from = data ?. list ?. length || 0 ;
60+
61+ let queryStrings : any = {
62+ query : input ,
63+ datasource : sourceData ?. source ?. id ,
64+ querysource : sourceData ?. querySource ?. id ,
65+ } ;
66+
67+ if ( sourceData ?. rich_categories ) {
68+ queryStrings = {
5869 query : input ,
59- datasource : sourceData ?. source ?. id ,
60- querysource : sourceData ?. querySource ?. id ,
70+ rich_category : sourceData ?. rich_categories [ 0 ] ?. key ,
6171 } ;
72+ }
6273
63- if ( sourceData ?. rich_categories ) {
64- queryStrings = {
65- query : input ,
66- rich_category : sourceData ?. rich_categories [ 0 ] ?. key ,
67- } ;
74+ let response : any ;
75+ if ( isTauri ) {
76+ response = await platformAdapter . commands ( "query_coco_fusion" , {
77+ from : from ,
78+ size : PAGE_SIZE ,
79+ queryStrings : queryStrings ,
80+ queryTimeout : querySourceTimeoutRef . current ,
81+ } ) ;
82+ } else {
83+ let url = `/query/_search?query=${ queryStrings . query } &datasource=${ queryStrings . datasource } &from=${ from } &size=${ PAGE_SIZE } ` ;
84+ if ( queryStrings ?. rich_categories ) {
85+ url = `/query/_search?query=${ queryStrings . query } &rich_category=${ queryStrings . rich_category } &from=${ from } &size=${ PAGE_SIZE } ` ;
6886 }
87+ const [ error , res ] : any = await Get ( url ) ;
6988
70- let response : any ;
71- if ( isTauri ) {
72- response = await platformAdapter . commands ( "query_coco_fusion" , {
73- from : from ,
74- size : PAGE_SIZE ,
75- queryStrings : queryStrings ,
76- queryTimeout : querySourceTimeoutRef . current ,
77- } ) ;
89+ if ( error ) {
90+ console . error ( "_search" , error ) ;
91+ response = { hits : [ ] , total : 0 } ;
7892 } else {
79- let url = `/query/_search?query=${ queryStrings . query } &datasource=${ queryStrings . datasource } &from=${ from } &size=${ PAGE_SIZE } ` ;
80- if ( queryStrings ?. rich_categories ) {
81- url = `/query/_search?query=${ queryStrings . query } &rich_category=${ queryStrings . rich_category } &from=${ from } &size=${ PAGE_SIZE } ` ;
82- }
83- const [ error , res ] : any = await Get ( url ) ;
84-
85- if ( error ) {
86- console . error ( "_search" , error ) ;
87- response = { hits : [ ] , total : 0 } ;
88- } else {
89- const hits =
90- res ?. hits ?. hits ?. map ( ( hit : any ) => ( {
91- document : {
92- ...hit . _source ,
93- } ,
94- score : hit . _score || 0 ,
95- source : hit . _source . source || null ,
96- } ) ) || [ ] ;
97- const total = res ?. hits ?. total ?. value || 0 ;
98-
99- response = {
100- hits : hits ,
101- total_hits : total ,
102- } ;
103- }
93+ const hits =
94+ res ?. hits ?. hits ?. map ( ( hit : any ) => ( {
95+ document : {
96+ ...hit . _source ,
97+ } ,
98+ score : hit . _score || 0 ,
99+ source : hit . _source . source || null ,
100+ } ) ) || [ ] ;
101+ const total = res ?. hits ?. total ?. value || 0 ;
102+
103+ response = {
104+ hits : hits ,
105+ total_hits : total ,
106+ } ;
104107 }
105- console . log ( "_docs" , from , queryStrings , response ) ;
106- const list = response ?. hits || [ ] ;
107- const total = response ?. total_hits || 0 ;
108- setTotal ( total ) ;
109-
110- return {
111- list : list ,
112- hasMore : list . length === PAGE_SIZE && from + list . length < total ,
113- } ;
108+ }
109+
110+ console . log ( "_docs" , from , queryStrings , response ) ;
111+ const list = response ?. hits || [ ] ;
112+ const total = response ?. total_hits || 0 ;
113+ setTotal ( total ) ;
114+
115+ if ( taskId === taskIdRef . current ) {
116+ setData ( { list } ) ;
117+ }
118+
119+ return {
120+ list : list ,
121+ hasMore : list . length === PAGE_SIZE && from + list . length < total ,
122+ } ;
123+ } ;
124+
125+ const { loading } = useInfiniteScroll (
126+ ( data ) => {
127+ const taskId = nanoid ( ) ;
128+
129+ taskIdRef . current = taskId ;
130+
131+ return getData ( taskId , data ) ;
114132 } ,
115133 {
116134 target : containerRef ,
0 commit comments