Skip to content

Commit 4fd44d9

Browse files
committed
Hub: improve On Device search relevance
Search results in the On Device tab now rank by match quality instead of source order: - name prefix match: highest weight - name substring match: medium weight - haystack (tags / metadata) match: lowest weight A row must match every search token somewhere, otherwise it scores zero and is filtered out — same semantics as the existing matchTokens filter.
1 parent 1e2a0e8 commit 4fd44d9

4 files changed

Lines changed: 103 additions & 7 deletions

File tree

‎studio/frontend/src/features/hub/catalog/models-catalog-lists.tsx‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "@hugeicons/core-free-icons";
1111
import type { RefObject } from "react";
1212
import { useMemo } from "react";
13-
import { inventoryRowMatches } from "../lib/inventory-search";
13+
import { inventoryRowMatches, scoreInventoryRow } from "../lib/inventory-search";
1414
import type {
1515
CachedInventoryRow,
1616
DiscoverRow,
@@ -214,13 +214,21 @@ export function DownloadedList({
214214
deviceType: string | null;
215215
onInventoryChange?: (hint?: InventoryHint) => void;
216216
}) {
217-
const inventoryItems = useMemo<InventoryItem[]>(
218-
() => [
217+
const inventoryItems = useMemo<InventoryItem[]>(() => {
218+
const merged: InventoryItem[] = [
219219
...cachedRows.map((row) => ({ variant: "cached" as const, row })),
220220
...localRows.map((row) => ({ variant: "local" as const, row })),
221-
],
222-
[cachedRows, localRows],
223-
);
221+
];
222+
if (inventoryTokens.length === 0) return merged;
223+
return merged
224+
.map((item, index) => ({
225+
item,
226+
index,
227+
score: scoreInventoryRow(item.row, inventoryTokens),
228+
}))
229+
.sort((a, b) => b.score - a.score || a.index - b.index)
230+
.map((entry) => entry.item);
231+
}, [cachedRows, localRows, inventoryTokens]);
224232
const hasInventoryRows = cachedRows.length > 0 || localRows.length > 0;
225233

226234
if (!downloadedReady && !hasInventoryRows) {

‎studio/frontend/src/features/hub/catalog/models-catalog-rows.tsx‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
memo,
3636
useCallback,
3737
useContext,
38+
useEffect,
39+
useLayoutEffect,
3840
useMemo,
3941
useRef,
4042
useState,
@@ -717,6 +719,20 @@ export const InventoryRow = memo(function InventoryRow({
717719

718720
const ROW_HEIGHT_PX = 57;
719721

722+
const ORDER_SIGNATURE_SEP = "";
723+
const REORDER_ANIMATION_MS = 240;
724+
725+
function isReorder(previous: string, next: string): boolean {
726+
if (previous === next) return false;
727+
const a = previous.split(ORDER_SIGNATURE_SEP);
728+
const b = next.split(ORDER_SIGNATURE_SEP);
729+
if (a.length !== b.length) return false;
730+
return (
731+
[...a].sort().join(ORDER_SIGNATURE_SEP) ===
732+
[...b].sort().join(ORDER_SIGNATURE_SEP)
733+
);
734+
}
735+
720736
export function VirtualRows<T>({
721737
items,
722738
scrollElement,
@@ -737,8 +753,45 @@ export function VirtualRows<T>({
737753
getItemKey: (index) => getKey(items[index], index),
738754
});
739755

756+
const getKeyRef = useRef(getKey);
757+
getKeyRef.current = getKey;
758+
const orderSignature = useMemo(
759+
() =>
760+
items
761+
.map((item, index) => getKeyRef.current(item, index))
762+
.join(ORDER_SIGNATURE_SEP),
763+
[items],
764+
);
765+
const previousSignatureRef = useRef(orderSignature);
766+
const animateTimerRef = useRef<number | null>(null);
767+
const [animating, setAnimating] = useState(false);
768+
769+
useLayoutEffect(() => {
770+
const previous = previousSignatureRef.current;
771+
previousSignatureRef.current = orderSignature;
772+
if (!isReorder(previous, orderSignature)) return;
773+
setAnimating(true);
774+
if (animateTimerRef.current !== null) {
775+
window.clearTimeout(animateTimerRef.current);
776+
}
777+
animateTimerRef.current = window.setTimeout(() => {
778+
setAnimating(false);
779+
animateTimerRef.current = null;
780+
}, REORDER_ANIMATION_MS);
781+
}, [orderSignature]);
782+
783+
useEffect(
784+
() => () => {
785+
if (animateTimerRef.current !== null) {
786+
window.clearTimeout(animateTimerRef.current);
787+
}
788+
},
789+
[],
790+
);
791+
740792
return (
741793
<ul
794+
data-animating={animating || undefined}
742795
style={{
743796
height: virtualizer.getTotalSize(),
744797
position: "relative",

‎studio/frontend/src/features/hub/hub.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@
301301
background-color: color-mix(in srgb, var(--status-success) 12%, transparent);
302302
box-shadow: inset 2px 0 0 0 var(--primary);
303303
}
304+
305+
.hub-page [data-hub-scroll="true"] ul[data-animating="true"] > li {
306+
transition: transform 220ms cubic-bezier(0.22, 0.72, 0.18, 1);
307+
}
308+
309+
@media (prefers-reduced-motion: reduce) {
310+
.hub-page [data-hub-scroll="true"] ul[data-animating="true"] > li {
311+
transition: none;
312+
}
313+
}
304314
}
305315

306316
@layer utilities {

‎studio/frontend/src/features/hub/lib/inventory-search.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// SPDX-License-Identifier: AGPL-3.0-only
22
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
33

4-
import { matchTokens, tokenizeQuery } from "@/features/hub/lib/search-text";
4+
import {
5+
matchTokens,
6+
normalizeForSearch,
7+
tokenizeQuery,
8+
} from "@/features/hub/lib/search-text";
59
import type { CachedInventoryRow, LocalInventoryRow } from "../types";
610

711
export { tokenizeQuery };
812

13+
function inventoryRowName(row: CachedInventoryRow | LocalInventoryRow): string {
14+
return row.kind === "cache" ? row.repoId : row.title;
15+
}
16+
917
function inventoryRowHaystack(
1018
row: CachedInventoryRow | LocalInventoryRow,
1119
): string {
@@ -35,3 +43,20 @@ export function inventoryRowMatches(
3543
if (tokens.length === 0) return true;
3644
return matchTokens(inventoryRowHaystack(row), tokens);
3745
}
46+
47+
export function scoreInventoryRow(
48+
row: CachedInventoryRow | LocalInventoryRow,
49+
tokens: readonly string[],
50+
): number {
51+
if (tokens.length === 0) return 0;
52+
const name = normalizeForSearch(inventoryRowName(row));
53+
const haystack = normalizeForSearch(inventoryRowHaystack(row));
54+
let score = 0;
55+
for (const token of tokens) {
56+
if (name.startsWith(token)) score += 3;
57+
else if (name.includes(token)) score += 2;
58+
else if (haystack.includes(token)) score += 1;
59+
else return 0;
60+
}
61+
return score;
62+
}

0 commit comments

Comments
 (0)