Skip to content

Commit 5b3e39a

Browse files
committed
refactor search composable
1 parent ec42061 commit 5b3e39a

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

src/features/content-search/model/composable.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,36 @@ export function useContentSearch() {
1313
})
1414
)
1515

16-
function search(keyword: string) {
16+
const searchData = computed(() => {
1717
const shorts = shortSearch.value || []
1818
const posts = postSearch.value || []
1919

2020
const mapType = (o: Record<string, any>, type: string) => ({ ...o, type })
21-
const mixed = (
22-
[] as {
23-
id: string
24-
title: string
25-
titles: string
26-
level: number
27-
content: string
28-
type: string
29-
}[]
30-
).concat(
31-
// @ts-expect-error
32-
shorts
21+
22+
return [
23+
...shorts
3324
.filter(short => short.level === 1)
3425
.map(short => mapType(short, 'short')),
35-
posts.filter(post => post.level === 1).map(post => mapType(post, 'post'))
36-
)
37-
const fuse = new Fuse(mixed, {
26+
...posts.filter(post => post.level === 1).map(post => mapType(post, 'post'))
27+
]
28+
})
29+
30+
// Fuse 인스턴스 메모이제이션
31+
const fuse = computed(() => {
32+
return new Fuse(searchData.value, {
3833
keys: ['title', 'content'],
3934
threshold: 0.2,
4035
includeMatches: true,
4136
includeScore: true,
4237
shouldSort: true,
43-
sortFn: (a, b) => b.score - a.score
38+
sortFn: (a, b) => (b.score || 0) - (a.score || 0)
4439
})
40+
})
4541

46-
return fuse.search(keyword)
42+
// 검색 실행 함수
43+
function search(keyword: string) {
44+
if (!keyword) return []
45+
return fuse.value.search(keyword)
4746
}
4847

4948
return {

src/features/content-search/ui/ContentSearch.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ watchDebounced(
9696
variant="soft"
9797
leading-icon="i-tabler-search"
9898
trailing-icon=""
99-
placeholder="무엇을 찾고싶으신가요?"
99+
placeholder="포스트 및 메모 검색..."
100100
class="min-w-inherit"
101101
:ui="{ base: '!min-w-[inherit] !max-w-[inherit]' }"
102102
@focus.native="onFocus"

0 commit comments

Comments
 (0)