Skip to content

Commit f025721

Browse files
committed
fix: prevent skills index crash
1 parent c2cc61d commit f025721

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/__tests__/skills-index.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @vitest-environment jsdom */
2-
import { render } from '@testing-library/react'
2+
import { render, screen } from '@testing-library/react'
33
import { beforeEach, describe, expect, it, vi } from 'vitest'
44

55
import { SkillsIndex } from '../routes/skills/index'
@@ -37,4 +37,9 @@ describe('SkillsIndex', () => {
3737
limit: 50,
3838
})
3939
})
40+
41+
it('renders an empty state when no skills are returned', () => {
42+
render(<SkillsIndex />)
43+
expect(screen.getByText('No skills match that filter.')).toBeTruthy()
44+
})
4045
})

src/routes/skills/index.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export function SkillsIndex() {
5858

5959
const trimmedQuery = useMemo(() => query.trim(), [query])
6060
const hasQuery = trimmedQuery.length > 0
61+
const searchKey = trimmedQuery ? `${trimmedQuery}::${highlightedOnly ? '1' : '0'}` : ''
6162

6263
const listPage = useQuery(
6364
api.skills.listPublicPage,
@@ -88,14 +89,14 @@ export function SkillsIndex() {
8889
}, [cursor, hasQuery, listPage])
8990

9091
useEffect(() => {
91-
if (!hasQuery) {
92+
if (!searchKey) {
9293
setSearchResults([])
9394
setIsSearching(false)
9495
return
9596
}
9697
setSearchResults([])
9798
setSearchLimit(pageSize)
98-
}, [hasQuery, highlightedOnly, trimmedQuery])
99+
}, [searchKey])
99100

100101
useEffect(() => {
101102
if (!hasQuery) return
@@ -139,9 +140,7 @@ export function SkillsIndex() {
139140

140141
const filtered = useMemo(
141142
() =>
142-
baseItems.filter((entry) =>
143-
highlightedOnly ? entry.skill.batch === 'highlighted' : true,
144-
),
143+
baseItems.filter((entry) => (highlightedOnly ? entry.skill.batch === 'highlighted' : true)),
145144
[baseItems, highlightedOnly],
146145
)
147146

@@ -173,9 +172,7 @@ export function SkillsIndex() {
173172
return results
174173
}, [dir, filtered, sort])
175174

176-
const isLoadingSkills = hasQuery
177-
? isSearching && searchResults.length === 0
178-
: isLoadingList
175+
const isLoadingSkills = hasQuery ? isSearching && searchResults.length === 0 : isLoadingList
179176
const canLoadMore = hasQuery
180177
? !isSearching && searchResults.length === searchLimit && searchResults.length > 0
181178
: nextCursor !== null
@@ -318,7 +315,7 @@ export function SkillsIndex() {
318315
<div className="card">
319316
<div className="loading-indicator">Loading skills…</div>
320317
</div>
321-
) : showing === 0 ? (
318+
) : sorted.length === 0 ? (
322319
<div className="card">No skills match that filter.</div>
323320
) : view === 'cards' ? (
324321
<div className="grid">
@@ -393,7 +390,11 @@ export function SkillsIndex() {
393390
style={{ marginTop: 16, display: 'flex', justifyContent: 'center' }}
394391
>
395392
{canAutoLoad ? (
396-
isLoadingMore ? 'Loading more…' : 'Scroll to load more'
393+
isLoadingMore ? (
394+
'Loading more…'
395+
) : (
396+
'Scroll to load more'
397+
)
397398
) : (
398399
<button className="btn" type="button" onClick={loadMore} disabled={isLoadingMore}>
399400
{isLoadingMore ? 'Loading…' : 'Load more'}

0 commit comments

Comments
 (0)