@@ -26,6 +26,7 @@ type ThreadState = {
26
26
updateCurrentThreadModel : ( model : ThreadModel ) => void
27
27
getFilteredThreads : ( searchTerm : string ) => Thread [ ]
28
28
updateCurrentThreadAssistant : ( assistant : Assistant ) => void
29
+ updateThreadTimestamp : ( threadId : string ) => void
29
30
searchIndex : Fzf < Thread [ ] > | null
30
31
}
31
32
@@ -164,7 +165,7 @@ export const useThreads = create<ThreadState>()(
164
165
id : ulid ( ) ,
165
166
title : title ?? 'New Thread' ,
166
167
model,
167
- order : 1 , // Will be set properly by setThreads
168
+ // order: 1, // Will be set properly by setThreads
168
169
updated : Date . now ( ) / 1000 ,
169
170
assistants : assistant ? [ assistant ] : [ ] ,
170
171
}
@@ -244,6 +245,62 @@ export const useThreads = create<ThreadState>()(
244
245
const { currentThreadId, threads } = get ( )
245
246
return currentThreadId ? threads [ currentThreadId ] : undefined
246
247
} ,
248
+ updateThreadTimestamp : ( threadId ) => {
249
+ set ( ( state ) => {
250
+ const thread = state . threads [ threadId ]
251
+ if ( ! thread ) return state
252
+
253
+ // If the thread is already at order 1, just update the timestamp
254
+ if ( thread . order === 1 ) {
255
+ const updatedThread = {
256
+ ...thread ,
257
+ updated : Date . now ( ) / 1000 ,
258
+ }
259
+ updateThread ( updatedThread )
260
+
261
+ return {
262
+ threads : {
263
+ ...state . threads ,
264
+ [ threadId ] : updatedThread ,
265
+ } ,
266
+ }
267
+ }
268
+
269
+ // Update the thread with new timestamp and set it to order 1 (top)
270
+ const updatedThread = {
271
+ ...thread ,
272
+ updated : Date . now ( ) / 1000 ,
273
+ order : 1 ,
274
+ }
275
+
276
+ // Update all other threads to increment their order by 1
277
+ const updatedThreads = { ...state . threads }
278
+ Object . keys ( updatedThreads ) . forEach ( id => {
279
+ if ( id !== threadId ) {
280
+ const otherThread = updatedThreads [ id ]
281
+ updatedThreads [ id ] = {
282
+ ...otherThread ,
283
+ order : ( otherThread . order || 1 ) + 1 ,
284
+ }
285
+ // Update the backend for other threads
286
+ updateThread ( updatedThreads [ id ] )
287
+ }
288
+ } )
289
+
290
+ // Set the updated thread
291
+ updatedThreads [ threadId ] = updatedThread
292
+
293
+ // Update the backend for the main thread
294
+ updateThread ( updatedThread )
295
+
296
+ return {
297
+ threads : updatedThreads ,
298
+ searchIndex : new Fzf < Thread [ ] > ( Object . values ( updatedThreads ) , {
299
+ selector : ( item : Thread ) => item . title ,
300
+ } ) ,
301
+ }
302
+ } )
303
+ } ,
247
304
} ) ,
248
305
{
249
306
name : localStorageKey . threads ,
0 commit comments