Skip to content

Commit

Permalink
fix: debounce search request and make search page query reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
Cl0v1s committed Jan 24, 2025
1 parent 79a97c9 commit b1a8929
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
7 changes: 5 additions & 2 deletions elk/components/search/Search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ const tabs = computed(() => [
const currentTab = ref(tabs.value[0].name)
const { q } = useUrlSearchParams<{ q: string }>()
const route = useRoute()
const query = ref(route.query.q as string || '')
watch(route, () => query.value = route.query.q as string)
const query = ref(q || '')
const queryType = computed(() => currentTab.value === 'all' ? undefined : currentTab.value as ('accounts' | 'hashtags' | 'statuses'))
const offset = ref(0)
Expand Down
3 changes: 2 additions & 1 deletion elk/components/search/SearchWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ function shift(delta: number) {
return index.value = (index.value + delta % results.value.length + results.value.length) % results.value.length
}
function activate() {
function activate(e: KeyboardEvent) {
if (query.value.length === 0)
return
(e.currentTarget as HTMLInputElement).blur()
router.push(`/search?q=${query.value}`)
}
</script>
Expand Down
11 changes: 10 additions & 1 deletion elk/composables/akko/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function useSearch(query: MaybeRefOrGetter<string>, mayBeOptions: UseSear
const accounts = ref<AccountSearchResult[]>([])
const hashtags = ref<HashTagSearchResult[]>([])
const statuses = ref<StatusSearchResult[]>([])
let requestIndex = 0

const q = computed(() => resolveUnref(query).trim())
const options = computed(() => resolveUnref(mayBeOptions))
Expand Down Expand Up @@ -67,6 +68,8 @@ export function useSearch(query: MaybeRefOrGetter<string>, mayBeOptions: UseSear
if (!q.value || !isHydrated.value)
return

requestIndex += 1
const currentRequestIndex = requestIndex
loading.value = true
/**
* Based on the source it seems like modifying the params when calling next would result in a new search,
Expand All @@ -78,6 +81,8 @@ export function useSearch(query: MaybeRefOrGetter<string>, mayBeOptions: UseSear
resolve: !!currentUser.value,
})
const nextResults = await paginator.next()
if (requestIndex !== currentRequestIndex)
return

done.value = !!nextResults.done
if (!nextResults.done)
Expand All @@ -88,13 +93,15 @@ export function useSearch(query: MaybeRefOrGetter<string>, mayBeOptions: UseSear

onMounted(search)

debouncedWatch(() => resolveUnref(query), search, { debounce: 300 })
debouncedWatch(() => resolveUnref(query), search, { debounce: 800 })

const next = async () => {
if (!q.value || !isHydrated.value || !paginator)
return

loading.value = true
requestIndex += 1
const currentRequestIndex = requestIndex
/**
* Based on the source it seems like modifying the params when calling next would result in a new search,
* but that doesn't seem to be the case. So instead we just create a new paginator with the new params.
Expand All @@ -105,6 +112,8 @@ export function useSearch(query: MaybeRefOrGetter<string>, mayBeOptions: UseSear
resolve: !!currentUser.value,
})
const nextResults = await paginator.next()
if (requestIndex !== currentRequestIndex)
return
loading.value = false

done.value = !!nextResults.done
Expand Down

0 comments on commit b1a8929

Please sign in to comment.