Skip to content

Commit 3777808

Browse files
committed
Upgrade search
1 parent 059ab95 commit 3777808

File tree

2 files changed

+116
-33
lines changed

2 files changed

+116
-33
lines changed

app/components/SearchModal.tsx

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ const Hit = ({ hit, isFocused }: { hit: any; isFocused?: boolean }) => {
158158
)
159159
}
160160

161-
function CustomRefinementList() {
161+
function LibraryRefinement() {
162162
const subpathname = useRouterState({
163163
select: (state) => state.location.pathname.split('/')[1],
164164
})
165165

166166
const { items, refine } = useRefinementList({
167-
attribute: '_tags',
167+
attribute: 'library',
168168
limit: 50,
169169
sortBy: ['isRefined:desc', 'count:desc', 'name:asc'],
170170
})
@@ -183,27 +183,99 @@ function CustomRefinementList() {
183183

184184
return (
185185
<div className="overflow-x-auto scrollbar-hide">
186-
<div className="flex gap-2 p-4 min-w-max">
187-
{items.map((item) => {
188-
const library = libraries.find((l) => l.id === item.value)
189-
190-
return (
191-
<button
192-
key={item.value}
193-
onClick={() => refine(item.value)}
194-
className={twMerge(
195-
'px-3 py-1 text-sm rounded-full transition-colors font-bold text-white',
196-
item.isRefined
197-
? library
198-
? library.bgStyle
199-
: 'bg-black dark:bg-white'
200-
: 'bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700'
201-
)}
202-
>
203-
{item.label} ({item.count.toLocaleString()})
204-
</button>
205-
)
206-
})}
186+
<div className="flex items-center gap-2 p-2 min-w-max">
187+
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 whitespace-nowrap">
188+
Libraries:
189+
</span>
190+
<div className="flex gap-1.5">
191+
{items.map((item) => {
192+
const library = libraries.find((l) => l.id === item.value)
193+
194+
return (
195+
<button
196+
key={item.value}
197+
onClick={() => refine(item.value)}
198+
className={twMerge(
199+
'px-2 py-0.5 text-xs rounded-full transition-colors font-bold text-white',
200+
item.isRefined
201+
? library
202+
? library.bgStyle
203+
: 'bg-black dark:bg-white'
204+
: 'bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700'
205+
)}
206+
>
207+
{item.label} ({item.count.toLocaleString()})
208+
</button>
209+
)
210+
})}
211+
</div>
212+
</div>
213+
</div>
214+
)
215+
}
216+
217+
function FrameworkRefinement() {
218+
const subpathname = useRouterState({
219+
select: (state) => {
220+
const path = state.location.pathname
221+
const frameworkIndex = path.indexOf('/framework/')
222+
if (frameworkIndex !== -1) {
223+
return path.split('/')[
224+
path.split('/').indexOf('framework') + 1
225+
] as string
226+
}
227+
return null
228+
},
229+
})
230+
231+
const { items, refine } = useRefinementList({
232+
attribute: 'framework',
233+
limit: 50,
234+
sortBy: ['isRefined:desc', 'count:desc', 'name:asc'],
235+
})
236+
237+
React.useEffect(() => {
238+
if (!subpathname) return
239+
240+
const isAlreadyRefined = items.some(
241+
(item) => item.value === subpathname && item.isRefined
242+
)
243+
244+
const framework = frameworkOptions.find((f) => f.value === subpathname)
245+
246+
if (!isAlreadyRefined && framework) {
247+
refine(subpathname)
248+
}
249+
}, [subpathname])
250+
251+
return (
252+
<div className="overflow-x-auto scrollbar-hide">
253+
<div className="flex items-center gap-2 p-2 min-w-max">
254+
<span className="text-xs font-medium text-gray-500 dark:text-gray-400 whitespace-nowrap">
255+
Frameworks:
256+
</span>
257+
<div className="flex gap-1.5">
258+
{items.map((item) => {
259+
const framework = frameworkOptions.find(
260+
(f) => f.value === item.value
261+
)
262+
263+
return (
264+
<button
265+
key={item.value}
266+
onClick={() => refine(item.value)}
267+
className={twMerge(
268+
'px-2 py-0.5 text-xs rounded-full transition-colors font-bold text-white',
269+
item.isRefined
270+
? framework?.color || 'bg-black dark:bg-white'
271+
: 'bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700'
272+
)}
273+
>
274+
{item.label} ({item.count.toLocaleString()})
275+
</button>
276+
)
277+
})}
278+
</div>
207279
</div>
208280
</div>
209281
)
@@ -382,7 +454,8 @@ function SearchResults({ focusedIndex }: { focusedIndex: number }) {
382454

383455
return (
384456
<>
385-
<CustomRefinementList />
457+
<LibraryRefinement />
458+
<FrameworkRefinement />
386459
<div
387460
className="max-h-[60vh] overflow-y-auto"
388461
role="listbox"

app/libraries/index.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ import { pacerProject } from './pacer'
2020
// import { optimisticProject } from './optimistic'
2121

2222
export const frameworkOptions = [
23-
{ label: 'React', value: 'react', logo: reactLogo },
24-
{ label: 'Preact', value: 'preact', logo: preactLogo },
25-
{ label: 'Vue', value: 'vue', logo: vueLogo },
26-
{ label: 'Angular', value: 'angular', logo: angularLogo },
27-
{ label: 'Solid', value: 'solid', logo: solidLogo },
28-
{ label: 'Lit', value: 'lit', logo: litLogo },
29-
{ label: 'Svelte', value: 'svelte', logo: svelteLogo },
30-
{ label: 'Qwik', value: 'qwik', logo: qwikLogo },
31-
{ label: 'Vanilla', value: 'vanilla', logo: jsLogo },
23+
{ label: 'React', value: 'react', logo: reactLogo, color: 'bg-blue-500' },
24+
{ label: 'Preact', value: 'preact', logo: preactLogo, color: 'bg-blue-400' },
25+
{ label: 'Vue', value: 'vue', logo: vueLogo, color: 'bg-green-500' },
26+
{
27+
label: 'Angular',
28+
value: 'angular',
29+
logo: angularLogo,
30+
color: 'bg-red-500',
31+
},
32+
{ label: 'Solid', value: 'solid', logo: solidLogo, color: 'bg-blue-600' },
33+
{ label: 'Lit', value: 'lit', logo: litLogo, color: 'bg-orange-500' },
34+
{
35+
label: 'Svelte',
36+
value: 'svelte',
37+
logo: svelteLogo,
38+
color: 'bg-orange-600',
39+
},
40+
{ label: 'Qwik', value: 'qwik', logo: qwikLogo, color: 'bg-purple-500' },
41+
{ label: 'Vanilla', value: 'vanilla', logo: jsLogo, color: 'bg-yellow-500' },
3242
] as const
3343

3444
export type Framework = (typeof frameworkOptions)[number]['value']

0 commit comments

Comments
 (0)