2023-02-02 21:02:39 +00:00
|
|
|
<script setup lang="ts">
|
|
|
|
import type { mastodon } from 'masto'
|
2023-03-20 15:41:50 +00:00
|
|
|
import { useForm } from 'slimeform'
|
2023-02-02 21:02:39 +00:00
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(e: 'listUpdated', list: mastodon.v1.List): void
|
|
|
|
(e: 'listRemoved', id: string): void
|
|
|
|
}>()
|
2023-03-20 15:41:50 +00:00
|
|
|
const { list } = defineModel<{
|
2023-02-02 21:02:39 +00:00
|
|
|
list: mastodon.v1.List
|
|
|
|
}>()
|
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
const client = useMastoClient()
|
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
const { form, isDirty, submitter, reset } = useForm({
|
|
|
|
form: () => ({ ...list.value }),
|
|
|
|
})
|
|
|
|
|
2023-02-02 21:02:39 +00:00
|
|
|
let isEditing = $ref<boolean>(false)
|
2023-03-20 15:41:50 +00:00
|
|
|
let deleting = $ref<boolean>(false)
|
2023-02-03 12:09:08 +00:00
|
|
|
let actionError = $ref<string | undefined>(undefined)
|
2023-02-02 21:02:39 +00:00
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
const input = ref<HTMLInputElement>()
|
|
|
|
const editBtn = ref<HTMLButtonElement>()
|
|
|
|
const deleteBtn = ref<HTMLButtonElement>()
|
2023-02-02 21:02:39 +00:00
|
|
|
|
2023-03-30 20:01:24 +01:00
|
|
|
async function prepareEdit() {
|
2023-02-02 21:02:39 +00:00
|
|
|
isEditing = true
|
2023-02-03 12:09:08 +00:00
|
|
|
actionError = undefined
|
2023-03-20 15:41:50 +00:00
|
|
|
await nextTick()
|
|
|
|
input.value?.focus()
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
2023-03-30 20:01:24 +01:00
|
|
|
async function cancelEdit() {
|
2023-02-02 21:02:39 +00:00
|
|
|
isEditing = false
|
2023-02-03 12:09:08 +00:00
|
|
|
actionError = undefined
|
2023-03-20 15:41:50 +00:00
|
|
|
reset()
|
2023-02-03 12:09:08 +00:00
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
await nextTick()
|
|
|
|
editBtn.value?.focus()
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
const { submit, submitting } = submitter(async () => {
|
2023-02-02 21:02:39 +00:00
|
|
|
try {
|
2023-03-20 15:41:50 +00:00
|
|
|
list.value = await client.v1.lists.update(form.id, {
|
|
|
|
title: form.title,
|
2023-02-02 21:02:39 +00:00
|
|
|
})
|
2023-02-03 12:09:08 +00:00
|
|
|
cancelEdit()
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
2023-02-03 12:09:08 +00:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
actionError = (err as Error).message
|
2023-03-20 15:41:50 +00:00
|
|
|
await nextTick()
|
|
|
|
input.value?.focus()
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
2023-03-20 15:41:50 +00:00
|
|
|
})
|
|
|
|
|
2023-02-02 21:02:39 +00:00
|
|
|
async function removeList() {
|
2023-03-20 15:41:50 +00:00
|
|
|
if (deleting)
|
2023-02-02 21:02:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
const confirmDelete = await openConfirmDialog({
|
2023-03-20 15:41:50 +00:00
|
|
|
title: t('confirm.delete_list.title', [list.value.title]),
|
2023-02-02 21:02:39 +00:00
|
|
|
confirm: t('confirm.delete_list.confirm'),
|
|
|
|
cancel: t('confirm.delete_list.cancel'),
|
|
|
|
})
|
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
deleting = true
|
|
|
|
actionError = undefined
|
|
|
|
await nextTick()
|
|
|
|
|
2023-02-02 21:02:39 +00:00
|
|
|
if (confirmDelete === 'confirm') {
|
|
|
|
await nextTick()
|
|
|
|
try {
|
2023-03-20 15:41:50 +00:00
|
|
|
await client.v1.lists.remove(list.value.id)
|
|
|
|
emit('listRemoved', list.value.id)
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
2023-02-03 12:09:08 +00:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
actionError = (err as Error).message
|
2023-03-20 15:41:50 +00:00
|
|
|
await nextTick()
|
|
|
|
deleteBtn.value?.focus()
|
2023-02-03 12:09:08 +00:00
|
|
|
}
|
2023-02-02 21:02:39 +00:00
|
|
|
finally {
|
2023-03-20 15:41:50 +00:00
|
|
|
deleting = false
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2023-03-20 15:41:50 +00:00
|
|
|
deleting = false
|
2023-02-02 21:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-03 12:09:08 +00:00
|
|
|
|
2023-03-20 15:41:50 +00:00
|
|
|
async function clearError() {
|
2023-02-03 12:09:08 +00:00
|
|
|
actionError = undefined
|
2023-03-20 15:41:50 +00:00
|
|
|
await nextTick()
|
|
|
|
if (isEditing)
|
|
|
|
input.value?.focus()
|
|
|
|
else
|
|
|
|
deleteBtn.value?.focus()
|
2023-02-03 12:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onDeactivated(cancelEdit)
|
2023-02-02 21:02:39 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-02-03 12:09:08 +00:00
|
|
|
<form
|
|
|
|
hover:bg-active flex justify-between items-center gap-x-2
|
|
|
|
:aria-describedby="actionError ? `action-list-error-${list.id}` : undefined"
|
|
|
|
:class="actionError ? 'border border-base border-rounded rounded-be-is-0 rounded-be-ie-0 border-b-unset border-$c-danger-active' : null"
|
2023-03-20 15:41:50 +00:00
|
|
|
@submit.prevent="submit"
|
2023-02-03 12:09:08 +00:00
|
|
|
>
|
2023-02-02 21:02:39 +00:00
|
|
|
<div
|
|
|
|
v-if="isEditing"
|
|
|
|
bg-base border="~ base" h10 m2 ps-1 pe-4 rounded-3 w-full flex="~ row"
|
|
|
|
items-center relative focus-within:box-shadow-outline gap-3
|
|
|
|
>
|
2023-02-03 10:48:27 +00:00
|
|
|
<CommonTooltip v-if="isEditing" :content="$t('list.cancel_edit')" no-auto-focus>
|
2023-02-02 21:02:39 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
rounded-full text-sm p2 transition-colors
|
|
|
|
hover:text-primary
|
2023-02-03 12:09:08 +00:00
|
|
|
@click="cancelEdit()"
|
2023-02-02 21:02:39 +00:00
|
|
|
>
|
|
|
|
<span block text-current i-ri:close-fill />
|
|
|
|
</button>
|
|
|
|
</CommonTooltip>
|
|
|
|
<input
|
|
|
|
ref="input"
|
2023-03-20 15:41:50 +00:00
|
|
|
v-model="form.title"
|
|
|
|
rounded-3 w-full bg-transparent
|
|
|
|
outline="focus:none" pe-4 pb="1px"
|
|
|
|
flex-1 placeholder-text-secondary
|
2023-02-03 12:09:08 +00:00
|
|
|
@keydown.esc="cancelEdit()"
|
2023-02-02 21:02:39 +00:00
|
|
|
>
|
|
|
|
</div>
|
|
|
|
<NuxtLink v-else :to="`list/${list.id}`" block grow p4>
|
2023-03-20 15:41:50 +00:00
|
|
|
{{ form.title }}
|
2023-02-02 21:02:39 +00:00
|
|
|
</NuxtLink>
|
|
|
|
<div mr4 flex gap2>
|
2023-02-03 10:48:27 +00:00
|
|
|
<CommonTooltip v-if="isEditing" :content="$t('list.save')" no-auto-focus>
|
2023-02-02 21:02:39 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
text-sm p2 border-1 transition-colors
|
|
|
|
border-dark hover:text-primary
|
|
|
|
btn-action-icon
|
2023-03-20 15:41:50 +00:00
|
|
|
:disabled="deleting || !isDirty || submitting"
|
2023-02-02 21:02:39 +00:00
|
|
|
>
|
|
|
|
<template v-if="isEditing">
|
2023-03-20 15:41:50 +00:00
|
|
|
<span v-if="submitting" aria-hidden="true" block animate animate-spin preserve-3d class="rtl-flip">
|
2023-02-02 21:02:39 +00:00
|
|
|
<span block i-ri:loader-2-fill aria-hidden="true" />
|
|
|
|
</span>
|
|
|
|
<span v-else block text-current i-ri:save-2-fill class="rtl-flip" />
|
|
|
|
</template>
|
|
|
|
</button>
|
|
|
|
</CommonTooltip>
|
2023-02-03 10:48:27 +00:00
|
|
|
<CommonTooltip v-else :content="$t('list.edit')" no-auto-focus>
|
2023-02-02 21:02:39 +00:00
|
|
|
<button
|
2023-03-20 15:41:50 +00:00
|
|
|
ref="editBtn"
|
2023-02-02 21:02:39 +00:00
|
|
|
type="button"
|
|
|
|
text-sm p2 border-1 transition-colors
|
|
|
|
border-dark hover:text-primary
|
|
|
|
btn-action-icon
|
|
|
|
@click.prevent="prepareEdit"
|
|
|
|
>
|
|
|
|
<span block text-current i-ri:edit-2-line class="rtl-flip" />
|
|
|
|
</button>
|
|
|
|
</CommonTooltip>
|
2023-02-03 10:48:27 +00:00
|
|
|
<CommonTooltip :content="$t('list.delete')" no-auto-focus>
|
2023-02-02 21:02:39 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
text-sm p2 border-1 transition-colors
|
|
|
|
border-dark hover:text-primary
|
|
|
|
btn-action-icon
|
|
|
|
:disabled="isEditing"
|
|
|
|
@click.prevent="removeList"
|
|
|
|
>
|
2023-03-20 15:41:50 +00:00
|
|
|
<span v-if="deleting" aria-hidden="true" block animate animate-spin preserve-3d class="rtl-flip">
|
2023-02-02 21:02:39 +00:00
|
|
|
<span block i-ri:loader-2-fill aria-hidden="true" />
|
|
|
|
</span>
|
|
|
|
<span v-else block text-current i-ri:delete-bin-2-line class="rtl-flip" />
|
|
|
|
</button>
|
|
|
|
</CommonTooltip>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-02-03 12:09:08 +00:00
|
|
|
<CommonErrorMessage
|
|
|
|
v-if="actionError"
|
|
|
|
:id="`action-list-error-${list.id}`"
|
|
|
|
:described-by="`action-list-failed-${list.id}`"
|
|
|
|
class="rounded-bs-is-0 rounded-bs-ie-0 border-t-dashed m-b-2"
|
|
|
|
>
|
|
|
|
<header :id="`action-list-failed-${list.id}`" flex justify-between>
|
|
|
|
<div flex items-center gap-x-2 font-bold>
|
|
|
|
<div aria-hidden="true" i-ri:error-warning-fill />
|
|
|
|
<p>{{ $t(`list.${isEditing ? 'edit_error' : 'delete_error'}`) }}</p>
|
|
|
|
</div>
|
|
|
|
<CommonTooltip placement="bottom" :content="$t('list.clear_error')" no-auto-focus>
|
|
|
|
<button
|
|
|
|
flex rounded-4 p1 hover:bg-active cursor-pointer transition-100 :aria-label="$t('list.clear_error')"
|
|
|
|
@click="clearError"
|
|
|
|
>
|
|
|
|
<span aria-hidden="true" w="1.75em" h="1.75em" i-ri:close-line />
|
|
|
|
</button>
|
|
|
|
</CommonTooltip>
|
|
|
|
</header>
|
|
|
|
<ol ps-2 sm:ps-1>
|
|
|
|
<li flex="~ col sm:row" gap-y-1 sm:gap-x-2>
|
|
|
|
<strong sr-only>{{ $t('list.error_prefix') }}</strong>
|
|
|
|
<span>{{ actionError }}</span>
|
|
|
|
</li>
|
|
|
|
</ol>
|
|
|
|
</CommonErrorMessage>
|
2023-02-02 21:02:39 +00:00
|
|
|
</template>
|