Spaces:
Running
Running
feat: highlight fuzzy search matches on app cards
Browse filesShow orange highlights on matching text in card name, description, and
author. Only highlight spans of 2+ characters to avoid noisy single
letter matches. Match data flows from the search worker.
Co-authored-by: Cursor <cursoragent@cursor.com>
Files changed (2)
28 unmodified lines293031323334353689 unmodified lines126127128129130131132114 unmodified lines24724824925025125225323 unmodified lines277278279280281282283251 unmodified lines5355365375385395405414 unmodified lines546547548549550551403 unmodified lines95595695795895996028 unmodified linesimport { useAuth } from '../context/AuthContext';import InstallModal from '../components/InstallModal';// App Card Component (memoized to avoid re-renders when only search changes)const AppCard = memo(function AppCard({ app, onInstallClick, isLiked, onToggleLike, isLoggedIn }) {const isOfficial = app.isOfficial;const isPythonApp = app.extra?.isPythonApp !== false; // Default to true for backwards compatibilityconst cardData = app.extra?.cardData || {};89 unmodified lineswhiteSpace: 'nowrap',}}>{author}</Typography></Box>)}114 unmodified linesflex: 1,}}>{app.name || app.id?.split('/').pop()}</Typography><Typography23 unmodified linesflex: 1,}}>{cardData.short_description || app.description || 'No description'}</Typography>{/* Date + Install Button */}251 unmodified linesconst scoreMap = new Map(searchResults.map((r) => [r.id, r.score]));const matchedIds = new Set(searchResults.map((r) => r.id));result = result.filter((app) => matchedIds.has(app.id));// Sort by relevance (best score first)result.sort((a, b) => (scoreMap.get(a.id) || 1) - (scoreMap.get(b.id) || 1));return result;}4 unmodified linesreturn result;}, [apps, officialOnly, selectedCategory, searchResults]);const isFiltered = searchResults !== null || officialOnly || selectedCategory;return (403 unmodified linesisLiked={isSpaceLiked(app.id)}onToggleLike={handleToggleLike}isLoggedIn={isLoggedIn}/>))}</Box>28 unmodified lines293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868789 unmodified lines177178179180181182183114 unmodified lines29829930030130230330430530630723 unmodified lines331332333334335336337338339340251 unmodified lines5925935945955965974 unmodified lines602603604605606607608609610611612613614615616617403 unmodified lines102110221023102410251026102728 unmodified linesimport { useAuth } from '../context/AuthContext';import InstallModal from '../components/InstallModal';/*** Render text with highlighted match ranges from Fuse.js.* indices is an array of [start, end] pairs.*/function HighlightText({ text, indices }) {if (!text) return null;if (!indices || indices.length === 0) return text;// Only keep matches that span at least 2 charactersconst significant = indices.filter(([start, end]) => end - start >= 1);if (significant.length === 0) return text;// Merge overlapping / adjacent ranges and sortconst sorted = [...significant].sort((a, b) => a[0] - b[0]);const merged = [sorted[0]];for (let i = 1; i < sorted.length; i++) {const prev = merged[merged.length - 1];if (sorted[i][0] <= prev[1] + 1) {prev[1] = Math.max(prev[1], sorted[i][1]);} else {merged.push(sorted[i]);}}const parts = [];let cursor = 0;for (const [start, end] of merged) {if (cursor < start) {parts.push(<span key={`t${cursor}`}>{text.slice(cursor, start)}</span>);}parts.push(<spankey={`h${start}`}style={{backgroundColor: 'rgba(255, 149, 0, 0.18)',color: '#b36b00',borderRadius: 2,padding: '0 1px',}}>{text.slice(start, end + 1)}</span>);cursor = end + 1;}if (cursor < text.length) {parts.push(<span key={`t${cursor}`}>{text.slice(cursor)}</span>);}return <>{parts}</>;}// App Card Component (memoized to avoid re-renders when only search changes)const AppCard = memo(function AppCard({ app, onInstallClick, isLiked, onToggleLike, isLoggedIn, matchData }) {const isOfficial = app.isOfficial;const isPythonApp = app.extra?.isPythonApp !== false; // Default to true for backwards compatibilityconst cardData = app.extra?.cardData || {};89 unmodified lineswhiteSpace: 'nowrap',}}><HighlightText text={author} indices={matchData?._searchAuthor} /></Typography></Box>)}114 unmodified linesflex: 1,}}><HighlightTexttext={app.name || app.id?.split('/').pop()}indices={matchData?.name}/></Typography><Typography23 unmodified linesflex: 1,}}><HighlightTexttext={cardData.short_description || app.description || 'No description'}indices={matchData?._searchDescription}/></Typography>{/* Date + Install Button */}251 unmodified linesconst scoreMap = new Map(searchResults.map((r) => [r.id, r.score]));const matchedIds = new Set(searchResults.map((r) => r.id));result = result.filter((app) => matchedIds.has(app.id));result.sort((a, b) => (scoreMap.get(a.id) || 1) - (scoreMap.get(b.id) || 1));return result;}4 unmodified linesreturn result;}, [apps, officialOnly, selectedCategory, searchResults]);// Build a map of app ID → match highlight dataconst matchDataMap = useMemo(() => {if (!searchResults) return null;const map = new Map();for (const r of searchResults) {map.set(r.id, r.matches);}return map;}, [searchResults]);const isFiltered = searchResults !== null || officialOnly || selectedCategory;return (403 unmodified linesisLiked={isSpaceLiked(app.id)}onToggleLike={handleToggleLike}isLoggedIn={isLoggedIn}matchData={matchDataMap?.get(app.id) || null}/>))}</Box>
12 unmodified lines13141516171818 unmodified lines3738394041424344454612 unmodified linesthreshold: 0.35,ignoreLocation: true,includeScore: true,};/**18 unmodified lines/*** Search and return ordered list of matching app IDs with scores.*/function search(query) {if (!fuse || !query.trim()) return [];const results = fuse.search(query.trim());return results.map((r) => ({ id: r.item.id, score: r.score }));}// Handle messages from the main thread12 unmodified lines1314151617181918 unmodified lines383940414243444546474849505152535455565758596012 unmodified linesthreshold: 0.35,ignoreLocation: true,includeScore: true,includeMatches: true,};/**18 unmodified lines/*** Search and return ordered list of matching app IDs with scores.*//*** Search and return ordered list of matching app IDs with scores and match indices.* Matches are keyed by field name for easy highlight rendering.*/function search(query) {if (!fuse || !query.trim()) return [];const results = fuse.search(query.trim());return results.map((r) => {// Convert Fuse matches array into a map: fieldName → [[start, end], ...]const matches = {};if (r.matches) {for (const m of r.matches) {matches[m.key] = m.indices;}}return { id: r.item.id, score: r.score, matches };});}// Handle messages from the main thread
src/pages/Apps.jsx+72-5
src/workers/searchWorker.js+15-1