2022-12-18 00:35:07 +03:00
|
|
|
<script setup lang="ts">
|
|
|
|
const query = ref('')
|
2022-12-29 17:44:26 +03:00
|
|
|
const { accounts, hashtags, loading, statuses } = useSearch(query)
|
2022-12-18 00:35:07 +03:00
|
|
|
const index = ref(0)
|
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
const el = ref<HTMLElement>()
|
|
|
|
const router = useRouter()
|
|
|
|
const { focused } = useFocusWithin(el)
|
|
|
|
|
|
|
|
const results = computed(() => {
|
|
|
|
if (query.value.length === 0)
|
|
|
|
return []
|
|
|
|
|
|
|
|
const results = [
|
2023-01-07 17:52:58 +03:00
|
|
|
...hashtags.value.slice(0, 3),
|
|
|
|
...accounts.value,
|
|
|
|
...statuses.value,
|
2022-12-18 00:35:07 +03:00
|
|
|
|
|
|
|
// Disable until search page is implemented
|
|
|
|
// {
|
|
|
|
// type: 'action',
|
|
|
|
// to: `/search?q=${query.value}`,
|
|
|
|
// action: {
|
|
|
|
// label: `Search for ${query.value}`,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
]
|
|
|
|
|
|
|
|
return results
|
|
|
|
})
|
|
|
|
|
|
|
|
// Reset index when results change
|
|
|
|
watch([results, focused], () => index.value = -1)
|
|
|
|
|
|
|
|
const shift = (delta: number) => index.value = (index.value + delta % results.value.length + results.value.length) % results.value.length
|
|
|
|
|
|
|
|
const activate = () => {
|
|
|
|
const currentIndex = index.value
|
|
|
|
index.value = -1
|
|
|
|
|
|
|
|
if (query.value.length === 0)
|
|
|
|
return
|
|
|
|
|
2023-01-08 10:57:21 +03:00
|
|
|
(document.activeElement as HTMLElement).blur()
|
|
|
|
|
2022-12-18 00:35:07 +03:00
|
|
|
// Disable until search page is implemented
|
2023-01-08 10:57:21 +03:00
|
|
|
if (currentIndex === -1)
|
|
|
|
// router.push(`/search?q=${query.value}`)
|
|
|
|
return
|
2022-12-18 00:35:07 +03:00
|
|
|
|
|
|
|
router.push(results.value[currentIndex].to)
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-01-05 14:33:00 +03:00
|
|
|
<div ref="el" relative group>
|
2023-01-06 12:44:02 +03:00
|
|
|
<div bg-base border="~ base" h10 px-4 rounded-3 flex="~ row" items-center relative focus-within:box-shadow-outline gap-3>
|
2023-01-05 14:33:00 +03:00
|
|
|
<div i-ri:search-2-line pointer-events-none text-secondary mt="1px" class="rtl-flip" />
|
2022-12-18 00:35:07 +03:00
|
|
|
<input
|
|
|
|
ref="input"
|
|
|
|
v-model="query"
|
|
|
|
h-full
|
2023-01-05 02:17:30 +03:00
|
|
|
rounded-3
|
2022-12-18 00:35:07 +03:00
|
|
|
w-full
|
|
|
|
bg-transparent
|
|
|
|
outline="focus:none"
|
2023-01-01 17:29:11 +03:00
|
|
|
pe-4
|
2022-12-28 00:41:44 +03:00
|
|
|
:placeholder="t('nav.search')"
|
2022-12-18 00:35:07 +03:00
|
|
|
pb="1px"
|
|
|
|
placeholder-text-secondary
|
|
|
|
@keydown.down.prevent="shift(1)"
|
|
|
|
@keydown.up.prevent="shift(-1)"
|
|
|
|
@keypress.enter="activate"
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
<!-- Results -->
|
2023-01-06 01:50:14 +03:00
|
|
|
<div left-0 top-12 absolute w-full z10 group-focus-within="pointer-events-auto visible" invisible pointer-events-none>
|
2023-01-05 02:17:30 +03:00
|
|
|
<div w-full bg-base border="~ base" rounded-3 max-h-100 overflow-auto py2>
|
2023-01-09 11:36:24 +03:00
|
|
|
<span v-if="query.trim().length === 0" block text-center text-sm text-secondary>
|
2022-12-18 00:35:07 +03:00
|
|
|
{{ t('search.search_desc') }}
|
|
|
|
</span>
|
2023-01-09 11:36:24 +03:00
|
|
|
<template v-else-if="!loading">
|
|
|
|
<template v-if="results.length > 0">
|
|
|
|
<SearchResult
|
|
|
|
v-for="(result, i) in results" :key="result.id"
|
|
|
|
:active="index === parseInt(i.toString())"
|
|
|
|
:result="result"
|
|
|
|
:tabindex="focused ? 0 : -1"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
<span v-else block text-center text-sm text-secondary>
|
2023-01-09 10:50:07 +03:00
|
|
|
{{ t('search.search_empty') }}
|
|
|
|
</span>
|
2022-12-18 00:35:07 +03:00
|
|
|
</template>
|
|
|
|
<div v-else>
|
|
|
|
<SearchResultSkeleton />
|
|
|
|
<SearchResultSkeleton />
|
|
|
|
<SearchResultSkeleton />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|