-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: thread list state order after dragable #5141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,10 +46,14 @@ export const useThreads = create<ThreadState>()( | |
(acc: Record<string, Thread>, thread) => { | ||
acc[thread.id] = thread | ||
return acc | ||
}, {} as Record<string, Thread>) | ||
}, | ||
{} as Record<string, Thread> | ||
) | ||
set({ | ||
threads: threadMap, | ||
searchIndex: new Fzf<Thread[]>(Object.values(threadMap), { selector: (item: Thread) => item.title }) | ||
searchIndex: new Fzf<Thread[]>(Object.values(threadMap), { | ||
selector: (item: Thread) => item.title, | ||
}), | ||
}) | ||
}, | ||
getFilteredThreads: (searchTerm: string) => { | ||
|
@@ -63,25 +67,26 @@ export const useThreads = create<ThreadState>()( | |
|
||
let currentIndex = searchIndex | ||
if (!currentIndex) { | ||
currentIndex = new Fzf<Thread[]>( | ||
Object.values(threads), | ||
{ selector: (item: Thread) => item.title } | ||
) | ||
currentIndex = new Fzf<Thread[]>(Object.values(threads), { | ||
selector: (item: Thread) => item.title, | ||
}) | ||
set({ searchIndex: currentIndex }) | ||
} | ||
|
||
// Use the index to search and return matching threads | ||
const fzfResults = currentIndex.find(searchTerm) | ||
return fzfResults.map((result: { item: Thread; positions: Set<number> }) => { | ||
const thread = result.item; // Fzf stores the original item here | ||
// Ensure result.positions is an array, default to empty if undefined | ||
const positions = Array.from(result.positions) || []; | ||
const highlightedTitle = highlightFzfMatch(thread.title, positions); | ||
return { | ||
...thread, | ||
title: highlightedTitle, // Override title with highlighted version | ||
}; | ||
}); | ||
return fzfResults.map( | ||
(result: { item: Thread; positions: Set<number> }) => { | ||
const thread = result.item // Fzf stores the original item here | ||
// Ensure result.positions is an array, default to empty if undefined | ||
const positions = Array.from(result.positions) || [] | ||
const highlightedTitle = highlightFzfMatch(thread.title, positions) | ||
return { | ||
...thread, | ||
title: highlightedTitle, // Override title with highlighted version | ||
} | ||
} | ||
) | ||
}, | ||
toggleFavorite: (threadId) => { | ||
set((state) => { | ||
|
@@ -107,7 +112,9 @@ export const useThreads = create<ThreadState>()( | |
deleteThread(threadId) | ||
return { | ||
threads: remainingThreads, | ||
searchIndex: new Fzf<Thread[]>(Object.values(remainingThreads), { selector: (item: Thread) => item.title }) | ||
searchIndex: new Fzf<Thread[]>(Object.values(remainingThreads), { | ||
selector: (item: Thread) => item.title, | ||
}), | ||
} | ||
}) | ||
}, | ||
|
@@ -119,7 +126,7 @@ export const useThreads = create<ThreadState>()( | |
}) | ||
return { | ||
threads: {}, | ||
searchIndex: null // Or new Fzf([], {selector...}) | ||
searchIndex: null, // Or new Fzf([], {selector...}) | ||
} | ||
}) | ||
}, | ||
|
@@ -157,21 +164,24 @@ export const useThreads = create<ThreadState>()( | |
id: ulid(), | ||
title: title ?? 'New Thread', | ||
model, | ||
// order: 1, | ||
order: 1, // Will be set properly by setThreads | ||
updated: Date.now() / 1000, | ||
assistants: assistant ? [assistant] : [], | ||
} | ||
return await createThread(newThread).then((createdThread) => { | ||
set((state) => { | ||
const newThreads = { | ||
...state.threads, | ||
[createdThread.id]: createdThread, | ||
}; | ||
// Get all existing threads as an array | ||
const existingThreads = Object.values(state.threads) | ||
|
||
// Create new array with the new thread at the beginning | ||
const reorderedThreads = [createdThread, ...existingThreads] | ||
|
||
// Use setThreads to handle proper ordering (this will assign order 1, 2, 3...) | ||
get().setThreads(reorderedThreads) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
|
||
return { | ||
threads: newThreads, | ||
currentThreadId: createdThread.id, | ||
searchIndex: new Fzf<Thread[]>(Object.values(newThreads), { selector: (item: Thread) => item.title }), | ||
}; | ||
} | ||
}) | ||
return createdThread | ||
}) | ||
|
@@ -221,10 +231,12 @@ export const useThreads = create<ThreadState>()( | |
title: newTitle, | ||
} | ||
updateThread(updatedThread) // External call, order is fine | ||
const newThreads = { ...state.threads, [threadId]: updatedThread }; | ||
const newThreads = { ...state.threads, [threadId]: updatedThread } | ||
return { | ||
threads: newThreads, | ||
searchIndex: new Fzf<Thread[]>(Object.values(newThreads), { selector: (item: Thread) => item.title }), | ||
searchIndex: new Fzf<Thread[]>(Object.values(newThreads), { | ||
selector: (item: Thread) => item.title, | ||
}), | ||
} | ||
}) | ||
}, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sorting comparator logic is duplicated in both
ThreadList
andDataProvider
. Consider extracting it into a shared utility to reduce maintenance overhead.