feat: search (#285)

This commit is contained in:
wheat 2022-12-17 16:35:07 -05:00 committed by GitHub
parent 5ff0900108
commit c1d1138742
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 252 additions and 6 deletions

View file

@ -0,0 +1,19 @@
<script setup lang="ts">
defineProps<{ hashtag: any }>()
</script>
<template>
<div flex flex-row items-center gap2>
<div w-12 h-12 rounded-full bg-active flex place-items-center place-content-center>
<div i-ri:hashtag text-secondary text-lg />
</div>
<div flex flex-col>
<span>
{{ hashtag.name }}
</span>
<span text-xs text-secondary>
{{ hashtag.following ? 'Following' : 'Not Following' }}
</span>
</div>
</div>
</template>

View file

@ -0,0 +1,26 @@
<script setup lang="ts">
import type { SearchResult } from './types'
const props = defineProps<{ result: SearchResult; active: boolean }>()
const el = ref<HTMLElement>()
watch(() => props.active, (active) => {
const _el = unrefElement(el)
if (active && _el)
_el.scrollIntoView({ block: 'nearest', inline: 'start' })
})
const onActivate = () => {
(document.activeElement as HTMLElement).blur()
}
</script>
<template>
<RouterLink ref="el" :to="result.to" py2 block px2 :aria-selected="active" :class="{ 'bg-active': active }" hover:bg-active @click="() => onActivate()">
<SearchHashtagInfo v-if="result.type === 'hashtag'" :hashtag="result.hashtag" />
<AccountInfo v-else-if="result.type === 'account'" :account="result.account" />
<div v-else-if="result.type === 'action'" text-center>
{{ result.action!.label }}
</div>
</RouterLink>
</template>

View file

@ -0,0 +1,13 @@
<template>
<div flex flex-col gap-2 px-4 py-3>
<div flex gap-4>
<div>
<div w-12 h-12 rounded-full class="skeleton-loading-bg" />
</div>
<div flex="~ col 1 gap-2" pb2 min-w-0>
<div flex class="skeleton-loading-bg" h-5 w-20 rounded />
<div flex class="skeleton-loading-bg" h-4 w-full rounded />
</div>
</div>
</div>
</template>

View file

@ -0,0 +1,92 @@
<script setup lang="ts">
const query = ref('')
const { accounts, hashtags, loading } = useSearch(query)
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 = [
...hashtags.value.slice(0, 3).map(hashtag => ({ type: 'hashtag', hashtag, to: `/tags/${hashtag.name}` })),
...accounts.value.map(account => ({ type: 'account', account, to: `/@${account.acct}` })),
// 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 = () => {
(document.activeElement as HTMLElement).blur()
const currentIndex = index.value
index.value = -1
if (query.value.length === 0)
return
// Disable until search page is implemented
// if (currentIndex === -1)
// router.push(`/search?q=${query.value}`)
router.push(results.value[currentIndex].to)
}
</script>
<template>
<div ref="el" relative px4 py2 group>
<div bg-base border="~ base" h10 rounded-full flex="~ row" items-center relative outline-primary outline-1 focus-within:outline transition-all transition-duration-500>
<div i-ri:search-2-line mx4 absolute pointer-events-none text-secondary mt="1px" />
<input
ref="input"
v-model="query"
h-full
pl-10
rounded-full
w-full
bg-transparent
outline="focus:none"
pr-4
:placeholder="`${t('nav_side.search')} Elk`"
pb="1px"
placeholder-text-secondary
@keydown.down.prevent="shift(1)"
@keydown.up.prevent="shift(-1)"
@keypress.enter="activate"
>
</div>
<!-- Results -->
<div p4 left-0 top-10 absolute w-full z10 group-focus-within="pointer-events-auto visible" invisible pointer-events-none>
<div w-full bg-base border="~ base" rounded max-h-100 overflow-auto py2>
<span v-if="query.length === 0" block text-center text-sm text-secondary>
{{ t('search.search_desc') }}
</span>
<template v-if="!loading">
<SearchResult v-for="(result, i) in results" :key="result.to" :active="index === parseInt(i.toString())" :result="result" :tabindex="focused ? 0 : -1" />
</template>
<div v-else>
<SearchResultSkeleton />
<SearchResultSkeleton />
<SearchResultSkeleton />
</div>
</div>
</div>
</div>
</template>

View file

@ -0,0 +1,12 @@
import type { Account } from 'masto'
export interface SearchResult {
type: 'account' | 'hashtag' | 'action'
to: string
label?: string
account?: Account
hashtag?: any
action?: {
label: string
}
}