@@ -188,9 +188,14 @@ function App() {
188188
189189 const filteredSessions = useMemo ( ( ) => {
190190 const query = normalizeForSearch ( debouncedQuery ) . trim ( )
191- if ( ! query ) return sessions
191+ // Always exclude items without a valid sessionId first
192+ const validSessions = Array . isArray ( sessions )
193+ ? sessions . filter ( ( s ) => Boolean ( s ?. sessionId ) )
194+ : [ ]
192195
193- return sessions . filter ( ( session ) => {
196+ if ( ! query ) return validSessions
197+
198+ return validSessions . filter ( ( session ) => {
194199 // Search in session name
195200 const sessionName = normalizeForSearch ( session . sessionName )
196201 if ( sessionName . includes ( query ) ) {
@@ -254,69 +259,67 @@ function App() {
254259 { t ( 'No conversations found' ) }
255260 </ div >
256261 ) }
257- { filteredSessions
258- . filter ( ( session ) => Boolean ( session . sessionId ) )
259- . map ( ( session ) => (
260- < div role = "listitem" key = { session . sessionId } >
261- < div
262- role = "button"
263- tabIndex = { 0 }
264- aria-pressed = { sessionId === session . sessionId }
265- className = { `normal-button chat-list-item ${
266- sessionId === session . sessionId ? 'active' : ''
267- } `}
268- onClick = { ( ) => {
262+ { filteredSessions . map ( ( session ) => (
263+ < div role = "listitem" key = { session . sessionId } >
264+ < div
265+ role = "button"
266+ tabIndex = { 0 }
267+ aria-pressed = { sessionId === session . sessionId }
268+ className = { `normal-button chat-list-item ${
269+ sessionId === session . sessionId ? 'active' : ''
270+ } `}
271+ onClick = { ( ) => {
272+ setSessionIdSafe ( session . sessionId )
273+ } }
274+ onKeyDown = { ( e ) => {
275+ if ( ( e . key === 'Enter' || e . key === ' ' ) && e . currentTarget === e . target ) {
276+ e . preventDefault ( )
269277 setSessionIdSafe ( session . sessionId )
278+ }
279+ } }
280+ >
281+ { session . sessionName }
282+ < span
283+ className = "gpt-util-group"
284+ onClick = { ( e ) => {
285+ e . stopPropagation ( )
270286 } }
271287 onKeyDown = { ( e ) => {
272- if ( ( e . key === 'Enter' || e . key === ' ' ) && e . currentTarget === e . target ) {
288+ if ( e . key === 'Enter' || e . key === ' ' ) {
273289 e . preventDefault ( )
274- setSessionIdSafe ( session . sessionId )
290+ e . stopPropagation ( )
275291 }
276292 } }
277293 >
278- { session . sessionName }
279- < span
280- className = "gpt-util-group"
281- onClick = { ( e ) => {
282- e . stopPropagation ( )
283- } }
284- onKeyDown = { ( e ) => {
285- if ( e . key === 'Enter' || e . key === ' ' ) {
286- e . preventDefault ( )
287- e . stopPropagation ( )
294+ < DeleteButton
295+ size = { 14 }
296+ text = { t ( 'Delete Conversation' ) }
297+ onConfirm = { async ( ) => {
298+ const deletedId = session . sessionId
299+ const updatedSessions = await deleteSession ( deletedId )
300+ setSessions ( updatedSessions )
301+ if ( ! updatedSessions || updatedSessions . length === 0 ) {
302+ stopCurrentPort ( )
303+ setSessionId ( null )
304+ setCurrentSession ( null )
305+ return
288306 }
289- } }
290- >
291- < DeleteButton
292- size = { 14 }
293- text = { t ( 'Delete Conversation' ) }
294- onConfirm = { async ( ) => {
295- const deletedId = session . sessionId
296- const updatedSessions = await deleteSession ( deletedId )
297- setSessions ( updatedSessions )
298- if ( ! updatedSessions || updatedSessions . length === 0 ) {
299- stopCurrentPort ( )
307+ // Only change active session if the deleted one was active
308+ if ( sessionId === deletedId ) {
309+ const next = updatedSessions . find ( ( s ) => s && s . sessionId )
310+ if ( next ) {
311+ await setSessionIdSafe ( next . sessionId )
312+ } else {
300313 setSessionId ( null )
301314 setCurrentSession ( null )
302- return
303315 }
304- // Only change active session if the deleted one was active
305- if ( sessionId === deletedId ) {
306- const next = updatedSessions . find ( ( s ) => s && s . sessionId )
307- if ( next ) {
308- await setSessionIdSafe ( next . sessionId )
309- } else {
310- setSessionId ( null )
311- setCurrentSession ( null )
312- }
313- }
314- } }
315- />
316- </ span >
317- </ div >
316+ }
317+ } }
318+ />
319+ </ span >
318320 </ div >
319- ) ) }
321+ </ div >
322+ ) ) }
320323 </ div >
321324 < hr />
322325 < div className = "chat-sidebar-button-group" >
0 commit comments