Fix people rank sorting stability

This commit is contained in:
2026-04-17 20:35:10 +05:30
parent 318baf64b5
commit c39292ae1c
+39 -10
View File
@@ -2,7 +2,7 @@
import {onMount} from "svelte"
import {debounce} from "throttle-debounce"
import {createScroller, isMobile} from "@lib/html"
import {profileSearch} from "@welshman/app"
import {displayProfileByPubkey, profileSearch} from "@welshman/app"
import Magnifier from "@assets/icons/magnifier.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import Page from "@lib/components/Page.svelte"
@@ -17,21 +17,50 @@
let pubkeys = $state<string[]>([])
let element: Element | undefined = $state()
let requestId = 0
const rankByPubkey = new Map<string, number>()
const sortPubkeys = (items: string[]) => {
const indexed = items.map((pubkey, index) => ({
pubkey,
index,
rank: rankByPubkey.get(pubkey) ?? FALLBACK_RANK,
name: displayProfileByPubkey(pubkey).toLowerCase(),
}))
indexed.sort((a, b) => {
if (b.rank !== a.rank) {
return b.rank - a.rank
}
const byName = a.name.localeCompare(b.name)
if (byName !== 0) {
return byName
}
return a.index - b.index
})
return indexed.map(item => item.pubkey)
}
const rankPubkeys = async (items: string[]) => {
const currentRequestId = ++requestId
pubkeys = items
pubkeys = sortPubkeys(items)
const scoredPubkeys = await Promise.all(
items.map(async pubkey => ({pubkey, rank: (await getPubkeyRank(pubkey)) ?? FALLBACK_RANK})),
await Promise.all(
items.map(async pubkey => {
const rank = (await getPubkeyRank(pubkey)) ?? FALLBACK_RANK
if (currentRequestId !== requestId) {
return
}
rankByPubkey.set(pubkey, rank)
pubkeys = sortPubkeys(items)
}),
)
if (currentRequestId !== requestId) {
return
}
pubkeys = scoredPubkeys.sort((a, b) => b.rank - a.rank).map(item => item.pubkey)
}
const search = debounce(200, (term: string) => {