Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
14 changes: 3 additions & 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 @@ -89,12 +80,13 @@ defineOgImageComponent('Default', {
name="q"
autofocus
:placeholder="$t('search.placeholder')"
v-bind="noCorrect"
class="w-full bg-bg-subtle border border-border rounded-xl ps-8 pe-24 h-14 py-4 font-mono text-base text-fg placeholder:text-fg-subtle transition-[border-color,outline-color] duration-300 motion-reduce:transition-none hover:border-fg-subtle outline-2 outline-transparent focus:border-accent focus-visible:(outline-2 outline-accent/70)"
no-correct
size="large"
class="w-full ps-8 pe-24"
@focus="isSearchFocused = true"
@blur="isSearchFocused = false"
@input="handleInput"
/>

<ButtonBase
Expand Down
Loading