2022-11-25 16:21:02 +03:00
|
|
|
<script setup lang="ts">
|
2023-01-08 09:21:09 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2023-08-02 13:28:18 +03:00
|
|
|
import type { CommandHandler } from '~/composables/command'
|
2022-11-26 09:55:54 +03:00
|
|
|
|
2022-11-25 16:21:02 +03:00
|
|
|
const { items, command } = defineProps<{
|
2023-01-08 09:21:09 +03:00
|
|
|
items: mastodon.v1.Account[]
|
2023-08-02 13:28:18 +03:00
|
|
|
command: CommandHandler<{ id: string }>
|
2022-11-27 09:34:38 +03:00
|
|
|
isPending?: boolean
|
2022-11-25 16:21:02 +03:00
|
|
|
}>()
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const selectedIndex = ref(0)
|
2022-11-25 16:21:02 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
watch(() => items, () => {
|
|
|
|
selectedIndex.value = 0
|
2022-11-25 16:21:02 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
function onKeyDown(event: KeyboardEvent) {
|
2023-02-03 17:21:29 +03:00
|
|
|
if (items.length === 0)
|
|
|
|
return false
|
|
|
|
|
2022-11-25 16:21:02 +03:00
|
|
|
if (event.key === 'ArrowUp') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectedIndex.value = ((selectedIndex.value + items.length) - 1) % items.length
|
2022-11-25 16:21:02 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if (event.key === 'ArrowDown') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectedIndex.value = (selectedIndex.value + 1) % items.length
|
2022-11-25 16:21:02 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if (event.key === 'Enter') {
|
2024-02-21 18:20:08 +03:00
|
|
|
selectItem(selectedIndex.value)
|
2022-11-25 16:21:02 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function selectItem(index: number) {
|
|
|
|
const item = items[index]
|
|
|
|
if (item)
|
2022-11-26 09:55:54 +03:00
|
|
|
command({ id: item.acct })
|
2022-11-25 16:21:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
onKeyDown,
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2022-11-27 09:34:38 +03:00
|
|
|
<div v-if="isPending || items.length" relative bg-base text-base shadow border="~ base rounded" text-sm py-2 overflow-x-hidden overflow-y-auto max-h-100>
|
|
|
|
<template v-if="isPending">
|
|
|
|
<div flex gap-1 items-center p2 animate-pulse>
|
2023-01-13 19:00:32 +03:00
|
|
|
<div animate-spin preserve-3d>
|
|
|
|
<div i-ri:loader-2-line />
|
|
|
|
</div>
|
2023-02-15 22:15:11 +03:00
|
|
|
<span>{{ $t('common.fetching') }}</span>
|
2022-11-27 09:34:38 +03:00
|
|
|
</div>
|
|
|
|
</template>
|
2022-11-25 16:21:02 +03:00
|
|
|
<template v-if="items.length">
|
2022-12-18 01:15:41 +03:00
|
|
|
<CommonScrollIntoView
|
|
|
|
v-for="(item, index) in items" :key="index"
|
|
|
|
:active="index === selectedIndex"
|
|
|
|
as="button"
|
2022-11-26 23:41:18 +03:00
|
|
|
:class="index === selectedIndex ? 'bg-active' : 'text-secondary'"
|
2022-11-25 16:21:02 +03:00
|
|
|
block m0 w-full text-left px2 py1
|
|
|
|
@click="selectItem(index)"
|
|
|
|
>
|
2022-11-27 06:34:55 +03:00
|
|
|
<AccountInfo :account="item" />
|
2022-12-18 01:15:41 +03:00
|
|
|
</CommonScrollIntoView>
|
2022-11-25 16:21:02 +03:00
|
|
|
</template>
|
|
|
|
</div>
|
2022-11-27 09:34:38 +03:00
|
|
|
<div v-else />
|
2022-11-25 16:21:02 +03:00
|
|
|
</template>
|