Files
XHChat/web/src/views/ContactsView.vue
T

137 lines
6.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useSession } from '../session'
import type { Contact, ContactAction } from '../contacts'
const session = useSession()
const list = ref<Contact[]>([])
const search = ref('')
const result = ref<Contact>()
const selected = ref('')
const error = ref('')
const busy = ref(false)
const alias = ref('')
const category = ref('')
const detail = computed(() => list.value.find((contact) => contact.id === selected.value) ??
(result.value?.id === selected.value ? result.value : undefined))
const labels = { none: '可添加', incoming: '收到申请', outgoing: '等待接受', friend: '联系人', blocked: '已拉黑' }
const sections = [
{ state: 'incoming', label: '好友申请' },
{ state: 'friend', label: '好友' },
{ state: 'outgoing', label: '发出的申请' },
{ state: 'blocked', label: '黑名单' },
] as const
const pendingAction = ref<'delete' | 'block'>()
let refreshVersion = 0
async function refresh() {
const version = ++refreshVersion
const next = await session.contacts()
if (version === refreshVersion) list.value = next
}
async function operation(callback: () => Promise<void>) {
busy.value = true
error.value = ''
try {
await callback()
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
} finally {
busy.value = false
}
}
function refreshQuietly() {
void refresh().catch((cause: unknown) => {
error.value = cause instanceof Error ? cause.message : '无法加载联系人'
})
}
onMounted(refreshQuietly)
watch(() => [session.contactChanges, session.connection], ([, connection]) => {
if (connection === '实时连接已建立') refreshQuietly()
})
watch(selected, () => {
alias.value = detail.value?.alias ?? ''
category.value = detail.value?.category ?? ''
pendingAction.value = undefined
})
async function find() {
await operation(async () => {
const contact = await session.searchContact(search.value.trim())
result.value = contact
selected.value = contact.id
})
}
async function change(action: ContactAction) {
const contact = detail.value
if (!contact) return
await operation(async () => {
await session.changeContact(contact.id, action, alias.value, category.value)
pendingAction.value = undefined
if (['cancel', 'reject', 'delete', 'unblock'].includes(action)) {
result.value = undefined
selected.value = ''
}
await refresh()
})
}
</script>
<template>
<section class="contacts-layout">
<div class="contact-list">
<form class="contact-search" @submit.prevent="find">
<label>按用户名查找<input v-model="search" required maxlength="32" placeholder="输入完整用户名"></label>
<button class="plain" :disabled="busy">查找</button>
</form>
<section v-for="section in sections" :key="section.state" class="contact-group">
<template v-if="list.some((contact) => contact.state === section.state)">
<h2>{{ section.label }}</h2>
<button v-for="contact in list.filter((item) => item.state === section.state)" :key="contact.id"
type="button" class="contact-item" :aria-pressed="selected === contact.id" @click="selected = contact.id">
<span class="avatar" aria-hidden="true">{{ contact.username[0]?.toUpperCase() }}</span>
<span><strong>{{ contact.alias || contact.username }}</strong><small>{{ contact.category || '@' + contact.username }}</small></span>
</button>
</template>
</section>
<p v-if="!list.length" class="muted">还没有联系人</p>
</div>
<section class="contact-detail">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<template v-if="detail">
<div class="contact-heading">
<span class="avatar" aria-hidden="true">{{ detail.username[0]?.toUpperCase() }}</span>
<div><h1>{{ detail.alias || detail.username }}</h1><small>@{{ detail.username }} · {{ labels[detail.state] }}</small></div>
</div>
<div v-if="detail.state === 'incoming'" class="contact-actions">
<button class="plain" :disabled="busy" @click="change('accept')">接受申请</button>
<button class="plain" :disabled="busy" @click="change('reject')">拒绝申请</button>
</div>
<button v-if="detail.state === 'none'" class="primary" :disabled="busy" @click="change('request')">添加好友</button>
<button v-if="detail.state === 'outgoing'" class="plain" :disabled="busy" @click="change('cancel')">取消申请</button>
<button v-if="detail.state === 'blocked'" class="plain" :disabled="busy" @click="change('unblock')">解除拉黑</button>
<form v-if="detail.state === 'friend'" class="contact-preferences" @submit.prevent="change('preferences')">
<RouterLink class="plain" :to="'/chat/' + detail.id">打开加密聊天</RouterLink>
<label>备注<input v-model="alias" maxlength="64"></label>
<label>分组<input v-model="category" maxlength="32"></label>
<button class="plain" :disabled="busy">保存</button>
</form>
<div v-if="detail.state !== 'blocked'" class="contact-actions">
<button v-if="detail.state === 'friend'" class="plain danger" :disabled="busy" @click="pendingAction = 'delete'">删除好友</button>
<button class="plain danger" :disabled="busy" @click="pendingAction = 'block'">拉黑</button>
</div>
<section v-if="pendingAction" class="confirmation" aria-label="确认联系人操作">
<p>{{ pendingAction === 'block' ? '拉黑将移除好友关系并阻止新的好友申请。' : '删除后,双方的好友关系将解除。' }}</p>
<button class="plain" @click="pendingAction = undefined">取消</button>
<button class="plain danger" :disabled="busy" @click="change(pendingAction)">确认</button>
</section>
</template>
<p v-else-if="!error" class="muted">选择联系人,或按用户名查找。</p>
</section>
</section>
</template>