Skip to content
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

Fix crash when search token are duplicated #22275

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [**] Re-enable the support for using Security Keys as a second factor during login [#22258]
* [*] Fix crash in editor that sometimes happens after modifying tags or categories [#22265]
* [*] Add defensive code to make sure the retain cycles in the editor don't lead to crashes [#22252]
* [*] Fix a rare crash in post search related to tags [#22275]
* [***] Block Editor: Avoid keyboard dismiss when interacting with text blocks [https://github.com/WordPress/gutenberg/pull/57070]
* [**] Block Editor: Auto-scroll upon block insertion [https://github.com/WordPress/gutenberg/pull/57273]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ actor PostSearchSuggestionsService {
let tokens = await [authors, tags]
let selectedTokenIDs = Set(selectedTokens.map(\.id))

return Array(tokens
let output = Array(tokens
.flatMap { $0 }
.filter { !selectedTokenIDs.contains($0.token.id) }
.sorted { ($0.score, $0.token.value) > ($1.score, $1.token.value) }
.map { $0.token }
.prefix(3))

// Remove duplicates
var encounteredIDs = Set<AnyHashable>()
return output.filter { encounteredIDs.insert($0.id).inserted }
kean marked this conversation as resolved.
Show resolved Hide resolved
}

private struct RankedToken {
Expand Down Expand Up @@ -79,7 +83,7 @@ actor PostSearchSuggestionsService {

private func getTagTokens(for searchTerm: String, selectedTokens: [any PostSearchToken]) async -> [RankedToken] {
guard !selectedTokens.contains(where: { $0 is PostSearchTagToken }) else {
return [] // Don't suggest authors anymore
return [] // Don't suggest tags anymore
}
let tokens = await getAllTagTokens()
let search = StringRankedSearch(searchTerm: searchTerm)
Expand Down