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
49 changes: 22 additions & 27 deletions app/components/Header/SearchBox.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 { normalizeSearchParam } from '#shared/utils/url'

withDefaults(
Expand All @@ -15,7 +14,6 @@ const emit = defineEmits(['blur', 'focus'])

const router = useRouter()
const route = useRoute()
const { isAlgolia } = useSearchProvider()

const isSearchFocused = shallowRef(false)

Expand All @@ -29,17 +27,13 @@ const searchQuery = shallowRef(normalizeSearchParam(route.query.q))
// Pages that have their own local filter using ?q
const pagesWithLocalFilter = new Set(['~username', 'org'])

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

router.push({
Expand All @@ -50,26 +44,17 @@ function updateUrlQueryImpl(value: string) {
})
}

const updateUrlQueryNpm = debounce(updateUrlQueryImpl, 250)
const updateUrlQueryAlgolia = debounce(updateUrlQueryImpl, 80)

const updateUrlQuery = Object.assign(
(value: string) => (isAlgolia.value ? updateUrlQueryAlgolia : updateUrlQueryNpm)(value),
{
flush: () => (isAlgolia.value ? updateUrlQueryAlgolia : updateUrlQueryNpm).flush(),
},
)

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 @@ -80,15 +65,25 @@ 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: query },
})
return
}

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

if (query) {
router.push({
name: 'search',
query: {
q: searchQuery.value,
},
query: { q: query },
})
} else {
updateUrlQuery.flush()
}
}

Expand Down
8 changes: 1 addition & 7 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 { isAlgolia } = useSearchProvider()
Expand All @@ -12,18 +11,14 @@
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 handleInputNpm = debounce(search, 250, { leading: true, trailing: true })

Check failure on line 18 in app/pages/index.vue

View workflow job for this annotation

GitHub Actions / 💪 Type check

Cannot find name 'debounce'.
const handleInputAlgolia = debounce(search, 80, { leading: true, trailing: true })

Check failure on line 19 in app/pages/index.vue

View workflow job for this annotation

GitHub Actions / 💪 Type check

Cannot find name 'debounce'.

function handleInput() {

Check failure on line 21 in app/pages/index.vue

View workflow job for this annotation

GitHub Actions / 💪 Type check

'handleInput' is declared but its value is never read.
if (isTouchDevice()) {
search()
} else if (isAlgolia.value) {
Expand Down Expand Up @@ -105,7 +100,6 @@
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