Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions app/components/Header/SearchBox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,23 @@ const searchQuery = shallowRef(normalizeSearchParam(route.query.q))
// Pages that have their own local filter using ?q
const pagesWithLocalFilter = new Set(['~username', 'org'])

// Debounced URL update for search query
const updateUrlQuery = debounce((value: string) => {
// Don't navigate away from pages that use ?q for local filtering
if (pagesWithLocalFilter.has(route.name as string)) {
return
}
if (route.name === 'search') {
router.replace({ query: { q: value || undefined } })
return
}
if (!value) {
return
}

router.push({
name: 'search',
query: {
q: value,
},
})
}, 250)

// Watch input and debounce URL updates
watch(searchQuery, value => {
updateUrlQuery(value)
if (route.name === 'search') {
updateUrlQuery(value)
}
})

// Sync input with URL when navigating (e.g., back button)
watch(
() => route.query.q,
urlQuery => {
// Don't sync from pages that use ?q for local filtering
if (pagesWithLocalFilter.has(route.name as string)) {
if (pagesWithLocalFilter.has(route.name)) {
return
}
const value = normalizeSearchParam(urlQuery)
Expand All @@ -71,15 +55,26 @@ watch(
)

function handleSubmit() {
if (pagesWithLocalFilter.has(route.name as string)) {
const query = searchQuery.value.trim()
if (pagesWithLocalFilter.has(route.name)) {
if (!query) return
router.push({
name: 'search',
query: {
q: searchQuery.value,
},
query: { q: query },
})
} else {
return
}

if (route.name === 'search') {
updateUrlQuery.flush()
return
}

if (query) {
router.push({
name: 'search',
query: { q: query },
})
}
}

Expand Down
12 changes: 1 addition & 11 deletions app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
import { debounce } from 'perfect-debounce'
import { SHOWCASED_FRAMEWORKS } from '~/utils/frameworks'

const searchQuery = shallowRef('')
Expand All @@ -10,18 +9,10 @@ async function search() {
if (!query) return
await navigateTo({
path: '/search',
query: query ? { q: query } : undefined,
query: { q: query },
})
const newQuery = searchQuery.value.trim()
if (newQuery !== query) {
await search()
}
}

const handleInput = isTouchDevice()
? search
: debounce(search, 250, { leading: true, trailing: true })

useSeoMeta({
title: () => $t('seo.home.title'),
ogTitle: () => $t('seo.home.title'),
Expand Down Expand Up @@ -94,7 +85,6 @@ defineOgImageComponent('Default', {
class="w-full ps-8 pe-24"
@focus="isSearchFocused = true"
@blur="isSearchFocused = false"
@input="handleInput"
/>

<ButtonBase
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/interactions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ test.describe('Search Pages', () => {
await homeSearchInput.click()
await page.keyboard.type('vue')

await page.keyboard.press('Enter')

// Wait for navigation to /search (debounce is 250ms)
await expect(page).toHaveURL(/\/search/, { timeout: 10000 })

Expand All @@ -117,6 +119,8 @@ test.describe('Search Pages', () => {
await searchInput.click()
await searchInput.fill('vue')

await page.keyboard.press('Enter')

await expect(page).toHaveURL(/\/search/, { timeout: 10000 })

await expect(page.locator('[data-result-index="0"]').first()).toBeVisible({
Expand Down
Loading