@@ -118,27 +118,47 @@ export const importData = (
118118 const oldConversationsParsed = oldConversations
119119 ? JSON . parse ( oldConversations )
120120 : [ ] ;
121- const newHistory : Conversation [ ] = [ ...oldConversationsParsed , ...history ] ;
122- localStorage . setItem ( 'conversationHistory' , JSON . stringify ( newHistory ) ) ;
123- localStorage . setItem (
124- 'selectedConversation' ,
125- JSON . stringify ( newHistory [ newHistory . length - 1 ] ) ,
121+
122+ const newHistory : Conversation [ ] = [
123+ ...oldConversationsParsed ,
124+ ...history ,
125+ ] . filter (
126+ ( conversation , index , self ) =>
127+ index === self . findIndex ( ( c ) => c . id === conversation . id ) ,
126128 ) ;
129+ localStorage . setItem ( 'conversationHistory' , JSON . stringify ( newHistory ) ) ;
130+ if ( newHistory . length > 0 ) {
131+ localStorage . setItem (
132+ 'selectedConversation' ,
133+ JSON . stringify ( newHistory [ newHistory . length - 1 ] ) ,
134+ ) ;
135+ } else {
136+ localStorage . removeItem ( 'selectedConversation' ) ;
137+ }
127138
128139 const oldFolders = localStorage . getItem ( 'folders' ) ;
129140 const oldFoldersParsed = oldFolders ? JSON . parse ( oldFolders ) : [ ] ;
130- const newFolders : FolderInterface [ ] = [ ...oldFoldersParsed , ...folders ] ;
141+ const newFolders : FolderInterface [ ] = [
142+ ...oldFoldersParsed ,
143+ ...folders ,
144+ ] . filter (
145+ ( folder , index , self ) =>
146+ index === self . findIndex ( ( f ) => f . id === folder . id ) ,
147+ ) ;
131148 localStorage . setItem ( 'folders' , JSON . stringify ( newFolders ) ) ;
132149
133150 const oldPrompts = localStorage . getItem ( 'prompts' ) ;
134151 const oldPromptsParsed = oldPrompts ? JSON . parse ( oldPrompts ) : [ ] ;
135- const newPrompts : Prompt [ ] = [ ...oldPromptsParsed , ...prompts ] ;
152+ const newPrompts : Prompt [ ] = [ ...oldPromptsParsed , ...prompts ] . filter (
153+ ( prompt , index , self ) =>
154+ index === self . findIndex ( ( p ) => p . id === prompt . id ) ,
155+ ) ;
136156 localStorage . setItem ( 'prompts' , JSON . stringify ( newPrompts ) ) ;
137157
138158 return {
139159 version : 4 ,
140- history : newHistory . map ( ( e , idx ) => ( { ... e , id : ` ${ idx } ` } ) ) ,
141- folders : newFolders . map ( ( e , idx ) => ( { ... e , id : ` ${ idx } ` } ) ) ,
142- prompts : newPrompts . map ( ( e , idx ) => ( { ... e , id : ` ${ idx } ` } ) ) ,
160+ history : newHistory ,
161+ folders : newFolders ,
162+ prompts : newPrompts ,
143163 } ;
144164} ;
0 commit comments