@@ -50,6 +50,8 @@ export const DocumentList: React.FC<DocumentListProps> = ({
5050 const taskIdRef = useRef ( nanoid ( ) ) ;
5151 const [ data , setData ] = useState < Data > ( { list : [ ] } ) ;
5252
53+ const loadingFromRef = useRef < number > ( - 1 ) ;
54+
5355 const querySourceTimeoutRef = useRef ( querySourceTimeout ) ;
5456 useEffect ( ( ) => {
5557 querySourceTimeoutRef . current = querySourceTimeout ;
@@ -107,12 +109,24 @@ export const DocumentList: React.FC<DocumentListProps> = ({
107109 }
108110 }
109111
112+
110113 console . log ( "_docs" , from , queryStrings , response ) ;
111114 const list = response ?. hits ?? [ ] ;
112- const total = response ?. total_hits ?? 0 ;
113- setTotal ( total ) ;
115+ const allTotal = response ?. total_hits ?? 0 ;
116+ // set first select hover
117+ if ( from === 0 && list . length > 0 ) {
118+ setSelectedItem ( 0 )
119+ getDocDetail ( list [ 0 ] ?. document ) ;
120+ }
114121
115122 if ( taskId === taskIdRef . current ) {
123+ // Prevent the last data from being 0
124+ setTotal ( ( prevTotal ) => {
125+ if ( list . length === 0 ) {
126+ return data ?. list ?. length === 0 ? 0 : prevTotal ;
127+ }
128+ return allTotal ;
129+ } ) ;
116130 setData ( ( prev ) => ( {
117131 ...prev ,
118132 list : prev . list . concat ( list ) ,
@@ -121,17 +135,32 @@ export const DocumentList: React.FC<DocumentListProps> = ({
121135
122136 return {
123137 list : list ,
124- hasMore : list . length === PAGE_SIZE && from + list . length < total ,
138+ hasMore : list . length === PAGE_SIZE && from + list . length < allTotal ,
125139 } ;
126140 } ;
127141
128142 const { loading } = useInfiniteScroll (
129143 ( data ) => {
130- const taskId = nanoid ( ) ;
144+ // Prevent repeated requests for the same from value
145+ const currentFrom = data ?. list ?. length || 0 ;
146+
147+ // If it starts from 0, it means it is a new search, reset the anti-duplicate flag
148+ if ( currentFrom === 0 ) {
149+ loadingFromRef . current = - 1 ;
150+ }
151+
152+ if ( loadingFromRef . current === currentFrom ) {
153+ return Promise . resolve ( { list : [ ] , hasMore : false } ) ;
154+ }
155+
156+ loadingFromRef . current = currentFrom ;
131157
158+ const taskId = nanoid ( ) ;
132159 taskIdRef . current = taskId ;
133160
134- return getData ( taskId , data ) ;
161+ return getData ( taskId , data ) . finally ( ( ) => {
162+ loadingFromRef . current = - 1 ; // reset
163+ } ) ;
135164 } ,
136165 {
137166 target : containerRef ,
@@ -163,6 +192,15 @@ export const DocumentList: React.FC<DocumentListProps> = ({
163192 setIsKeyboardMode ( false ) ;
164193 } , [ isChatMode , input ] ) ;
165194
195+ useEffect ( ( ) => {
196+ setTotal ( 0 ) ;
197+ setData ( ( prev ) => ( {
198+ ...prev ,
199+ list : [ ] ,
200+ } ) ) ;
201+ loadingFromRef . current = - 1 ;
202+ } , [ input ] ) ;
203+
166204 const { visibleContextMenu } = useSearchStore ( ) ;
167205
168206 const handleKeyDown = useCallback (
0 commit comments