chore: fix model hub sorting (#4722)

* chore: fix model hub sorting

* chore: linter fix
This commit is contained in:
Louis 2025-02-24 15:23:06 +07:00 committed by GitHub
parent 2e9cca376c
commit 2d7dd8e8ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -121,22 +121,8 @@ const HubScreen = () => {
[sources, searchValue] [sources, searchValue]
) )
const sortedModels = useMemo(() => {
if (!sources) return []
return sources.sort((a, b) => {
if (sortSelected === 'most-downloaded') {
return b.metadata.downloads - a.metadata.downloads
} else {
return (
new Date(b.metadata.createdAt).getTime() -
new Date(a.metadata.createdAt).getTime()
)
}
})
}, [sortSelected, sources])
const filteredModels = useMemo(() => { const filteredModels = useMemo(() => {
return sortedModels.filter((model) => { return (sources ?? []).filter((model) => {
const isCompatible = const isCompatible =
!compatible || !compatible ||
model.models?.some((e) => e.size * 1.5 < totalRam * (1 << 20)) model.models?.some((e) => e.size * 1.5 < totalRam * (1 << 20))
@ -153,7 +139,7 @@ const HubScreen = () => {
return isCompatible && matchesCtxLen && matchesMinSize && matchesMaxSize return isCompatible && matchesCtxLen && matchesMinSize && matchesMaxSize
}) })
}, [ }, [
sortedModels, sources,
compatible, compatible,
ctxLenFilter, ctxLenFilter,
minModelSizeFilter, minModelSizeFilter,
@ -161,6 +147,19 @@ const HubScreen = () => {
totalRam, totalRam,
]) ])
const sortedModels = useMemo(() => {
return filteredModels.sort((a, b) => {
if (sortSelected === 'most-downloaded') {
return b.metadata.downloads - a.metadata.downloads
} else {
return (
new Date(b.metadata.createdAt).getTime() -
new Date(a.metadata.createdAt).getTime()
)
}
})
}, [sortSelected, filteredModels])
useEffect(() => { useEffect(() => {
if (modelDetail) { if (modelDetail) {
setSelectedModel(sources?.find((e) => e.id === modelDetail)) setSelectedModel(sources?.find((e) => e.id === modelDetail))
@ -516,7 +515,7 @@ const HubScreen = () => {
{(filterOption === 'on-device' || {(filterOption === 'on-device' ||
filterOption === 'all') && ( filterOption === 'all') && (
<ModelList <ModelList
models={filteredModels} models={sortedModels}
onSelectedModel={(model) => setSelectedModel(model)} onSelectedModel={(model) => setSelectedModel(model)}
/> />
)} )}