elk/components/user/UserSignIn.vue

156 lines
4.3 KiB
Vue
Raw Normal View History

2022-11-23 03:06:56 +00:00
<script setup lang="ts">
2022-12-24 00:40:51 +00:00
import Fuse from 'fuse.js'
2022-12-24 00:46:43 +00:00
import { $fetch } from 'ofetch'
2022-11-23 03:06:56 +00:00
import { DEFAULT_SERVER } from '~/constants'
2022-11-29 12:04:16 +00:00
const input = $ref<HTMLInputElement>()
2022-11-23 09:06:32 +00:00
let server = $ref<string>('')
let busy = $ref<boolean>(false)
let error = $ref<boolean>(false)
let displayError = $ref<boolean>(false)
2022-12-24 00:40:51 +00:00
let knownServers = $ref<string[]>([])
2022-12-24 11:42:54 +00:00
let autocompleteIndex = $ref(0)
let autocompleteShow = $ref(false)
2022-11-23 03:06:56 +00:00
async function oauth() {
if (busy)
return
busy = true
error = false
displayError = false
await nextTick()
2022-11-27 09:29:25 +00:00
if (server)
server = server.split('/')[0]
try {
location.href = await $fetch<string>(`/api/${server || DEFAULT_SERVER}/login`)
}
catch {
displayError = true
error = true
await nextTick()
input?.focus()
await nextTick()
setTimeout(() => {
busy = false
error = false
}, 512)
}
2022-11-23 09:06:32 +00:00
}
async function handleInput() {
if (server.startsWith('https://'))
server = server.replace('https://', '')
if (server?.length)
displayError = false
2022-11-23 03:06:56 +00:00
}
2022-11-29 12:04:16 +00:00
2022-12-24 00:40:51 +00:00
let fuse = $shallowRef(new Fuse([] as string[]))
const filteredServers = $computed(() => {
if (!server)
return []
const results = fuse.search(server, { limit: 6 }).map(result => result.item)
2022-12-24 11:42:54 +00:00
if (results[0] === server)
2022-12-24 00:40:51 +00:00
return []
return results
})
function move(delta: number) {
2022-12-24 11:42:54 +00:00
autocompleteIndex = ((autocompleteIndex + delta) + filteredServers.length) % filteredServers.length
2022-12-24 00:40:51 +00:00
}
2022-12-24 11:42:54 +00:00
function onEnter(e: KeyboardEvent) {
if (autocompleteShow === true && filteredServers[autocompleteIndex]) {
server = filteredServers[autocompleteIndex]
e.preventDefault()
autocompleteShow = false
}
2022-12-24 00:40:51 +00:00
}
onMounted(async () => {
2022-11-29 12:04:16 +00:00
input?.focus()
2022-12-24 00:40:51 +00:00
knownServers = await $fetch('/api/list-servers')
fuse = new Fuse(knownServers, { shouldSort: true })
2022-11-29 12:04:16 +00:00
})
2022-11-23 03:06:56 +00:00
</script>
2022-11-23 02:53:22 +00:00
<template>
2022-12-02 07:02:44 +00:00
<form text-center justify-center items-center max-w-150 py6 flex="~ col gap-3" @submit.prevent="oauth">
2022-11-30 01:45:52 +00:00
<div flex="~ center" mb2>
<img src="/logo.svg" w-12 h-12 mxa height="48" width="48" alt="logo">
2022-11-30 01:45:52 +00:00
<div text-3xl>
{{ $t('action.sign_in') }}
</div>
</div>
<div>
{{ $t('user.server_address_label') }}
</div>
<div :class="error ? 'animate animate-shake-x animate-delay-100' : null">
<div
flex bg-gray:10 px4 py2 mxa rounded
border="~ base" items-center font-mono
focus:outline-none focus:ring="2 primary inset"
2022-12-24 00:40:51 +00:00
relative
:class="displayError ? 'border-red-600 dark:border-red-400' : null"
>
<span text-secondary-light mr1>https://</span>
2022-12-24 00:40:51 +00:00
<input
ref="input"
v-model="server"
outline-none bg-transparent w-full max-w-50
@input="handleInput"
2022-12-24 01:06:46 +00:00
@keydown.down="move(1)"
@keydown.up="move(-1)"
2022-12-24 00:40:51 +00:00
@keydown.enter="onEnter"
2022-12-24 11:42:54 +00:00
@keydown.esc.prevent="autocompleteShow = false"
@blur="autocompleteShow = false"
@focus="autocompleteShow = true"
2022-12-24 00:40:51 +00:00
>
<div
2022-12-24 11:42:54 +00:00
v-if="autocompleteShow && filteredServers.length"
2022-12-24 00:40:51 +00:00
absolute left-6em right-0 top="100%"
bg-base rounded border="~ base"
2022-12-24 01:06:46 +00:00
text-left z-10 shadow of-auto
>
2022-12-24 00:40:51 +00:00
<div
v-for="server, idx in filteredServers"
:key="server"
:value="server"
px-2 py1 font-mono
2022-12-24 11:42:54 +00:00
:class="autocompleteIndex === idx ? 'text-primary font-bold' : null"
2022-12-24 00:40:51 +00:00
>
{{ server }}
</div>
</div>
</div>
<div min-h-4>
<Transition css enter-active-class="animate animate-fade-in">
<p v-if="displayError" role="alert" p-0 m-0 text-xs text-red-600 dark:text-red-400>
{{ $t('error.sign_in_error') }}
</p>
</Transition>
</div>
2022-11-23 02:53:22 +00:00
</div>
2022-11-30 01:45:52 +00:00
<div text-secondary text-sm flex>
<div i-ri:lightbulb-line mr-1 />
<span>
<i18n-t keypath="user.tip_no_account">
<a href="https://joinmastodon.org/servers" target="_blank" hover="underline text-primary">{{ $t('user.tip_register_account') }}</a>
</i18n-t>
</span>
2022-11-23 03:06:56 +00:00
</div>
<button flex="~ row" gap-x-2 items-center btn-solid mt2 :disabled="!server || busy">
<span aria-hidden="true" inline-block :class="busy ? 'i-ri:loader-2-fill animate animate-spin' : 'i-ri:login-circle-line'" />
{{ $t('action.sign_in') }}
2022-11-23 03:06:56 +00:00
</button>
</form>
2022-11-23 02:53:22 +00:00
</template>