173 lines
6.8 KiB
Vue
173 lines
6.8 KiB
Vue
<script setup lang="ts">
|
|
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import { useSession } from '../session'
|
|
|
|
const session = useSession()
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const error = ref('')
|
|
const busy = ref(false)
|
|
const browser = window.xhchat?.openBrowser
|
|
type NoticeKind = 'group' | 'private'
|
|
const notice = ref<{ href: string; label: string }>()
|
|
let noticeTimer: ReturnType<typeof setTimeout> | undefined
|
|
const systemNotices = ref(false)
|
|
const noticeSettingStatus = ref('')
|
|
let webNotice: Notification | undefined
|
|
let lastSystemNotice = 0
|
|
|
|
function noticeSettingKey() {
|
|
return 'xhchat-group-notifications:' + (window.xhchat?.apiBase ?? location.origin) + '/' + session.identity?.device.id
|
|
}
|
|
|
|
function closeSystemNotice() {
|
|
webNotice?.close()
|
|
webNotice = undefined
|
|
void window.xhchat?.closeMessageNotification().catch(() => {})
|
|
}
|
|
|
|
function readNoticeSetting() {
|
|
try { systemNotices.value = localStorage.getItem(noticeSettingKey()) === 'enabled' }
|
|
catch { systemNotices.value = false }
|
|
if (!systemNotices.value) closeSystemNotice()
|
|
}
|
|
|
|
function storageChanged(event: StorageEvent) {
|
|
if (event.key === noticeSettingKey()) readNoticeSetting()
|
|
}
|
|
|
|
async function toggleSystemNotices() {
|
|
noticeSettingStatus.value = ''
|
|
const enabled = !systemNotices.value
|
|
if (enabled) {
|
|
if (window.xhchat) {
|
|
if (!await window.xhchat.messageNotificationsSupported()) throw new Error('当前系统不支持桌面通知')
|
|
} else {
|
|
if (!('Notification' in window)) throw new Error('当前浏览器不支持系统通知')
|
|
if (await Notification.requestPermission() !== 'granted') throw new Error('请在浏览器站点设置中允许通知')
|
|
}
|
|
}
|
|
if (!session.identity) return
|
|
localStorage.setItem(noticeSettingKey(), enabled ? 'enabled' : 'disabled')
|
|
systemNotices.value = enabled
|
|
if (!enabled) closeSystemNotice()
|
|
noticeSettingStatus.value = enabled ? '已在本设备启用消息系统通知' : '已在本设备关闭消息系统通知'
|
|
}
|
|
|
|
function showSystemNotice(kind: NoticeKind, href: string, label: string) {
|
|
if (!systemNotices.value) return
|
|
if (window.xhchat) {
|
|
void window.xhchat.showMessageNotification(kind).catch(() => {})
|
|
return
|
|
}
|
|
if (!('Notification' in window) || Notification.permission !== 'granted' ||
|
|
(document.visibilityState === 'visible' && document.hasFocus()) || Date.now() - lastSystemNotice < 2500) return
|
|
lastSystemNotice = Date.now()
|
|
try {
|
|
webNotice?.close()
|
|
webNotice = new Notification('XHChat', { body: label, icon: '/brand/xhchat.png', tag: 'xhchat-' + kind })
|
|
webNotice.onclick = () => {
|
|
if (!session.identity) return
|
|
window.focus()
|
|
void router.push(href)
|
|
closeSystemNotice()
|
|
}
|
|
} catch {
|
|
// The application notification remains available in browsers without desktop delivery.
|
|
}
|
|
}
|
|
|
|
function dismissNotice() {
|
|
clearTimeout(noticeTimer)
|
|
notice.value = undefined
|
|
}
|
|
|
|
function showNotice(kind: NoticeKind, id: string) {
|
|
if (!/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(id)) return
|
|
const href = (kind === 'group' ? '/groups/' : '/chat/') + id
|
|
const label = kind === 'group' ? '有新的群消息' : '有新的私聊消息'
|
|
showSystemNotice(kind, href, label)
|
|
if (route.path === href) return
|
|
dismissNotice()
|
|
notice.value = { href, label }
|
|
noticeTimer = setTimeout(dismissNotice, 8000)
|
|
}
|
|
|
|
watch(() => session.groupChange, (event) => {
|
|
if (event?.notify) showNotice('group', event.groupId)
|
|
})
|
|
watch(() => session.privateNotice, (event) => {
|
|
if (event) showNotice('private', event.contactId)
|
|
})
|
|
watch(() => route.path, (path) => { if (path === notice.value?.href) dismissNotice() })
|
|
onMounted(() => {
|
|
readNoticeSetting()
|
|
window.addEventListener('storage', storageChanged)
|
|
})
|
|
onBeforeUnmount(() => {
|
|
dismissNotice()
|
|
closeSystemNotice()
|
|
window.removeEventListener('storage', storageChanged)
|
|
})
|
|
|
|
async function act(action: () => Promise<void>) {
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
await action()
|
|
} catch (cause) {
|
|
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<main v-if="session.identity" class="workspace">
|
|
<nav class="rail" aria-label="主导航">
|
|
<RouterLink class="mark" to="/devices" aria-label="XHChat"><img src="/brand/xhchat.png" alt=""></RouterLink>
|
|
<RouterLink class="rail-link" to="/chat" title="消息" aria-label="消息">◇</RouterLink>
|
|
<RouterLink class="rail-link" to="/contacts" title="联系人" aria-label="联系人">人</RouterLink>
|
|
<RouterLink class="rail-link" to="/favorites" title="收藏" aria-label="收藏">☆</RouterLink>
|
|
<RouterLink class="rail-link" to="/devices" title="设备管理" aria-label="设备管理">▣</RouterLink>
|
|
<button v-if="browser" type="button" class="rail-link browser-button" title="浏览器" aria-label="浏览器" @click="act(browser)">◎</button>
|
|
</nav>
|
|
<aside class="account-panel">
|
|
<div class="account-summary">
|
|
<span class="avatar" aria-hidden="true">{{ session.identity.user.username[0]?.toUpperCase() }}</span>
|
|
<div><strong>{{ session.identity.user.username }}</strong><small>我的账号</small></div>
|
|
</div>
|
|
<nav class="panel-links" aria-label="工作区">
|
|
<RouterLink to="/chat">消息</RouterLink>
|
|
<RouterLink to="/contacts">联系人</RouterLink>
|
|
<RouterLink to="/groups">群组</RouterLink>
|
|
<RouterLink to="/favorites">收藏</RouterLink>
|
|
<RouterLink to="/devices">设备管理</RouterLink>
|
|
<button v-if="browser" class="plain mobile-browser" @click="act(browser)">浏览器</button>
|
|
</nav>
|
|
<div class="account-footer">
|
|
<p aria-live="polite"><span class="dot" :class="{ online: session.connection === '实时连接已建立' }"></span>{{ session.connection }}</p>
|
|
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
|
<button class="plain" type="button" :aria-pressed="systemNotices" :disabled="busy" @click="act(toggleSystemNotices)">消息系统通知</button>
|
|
<small v-if="noticeSettingStatus" role="status">{{ noticeSettingStatus }}</small>
|
|
<button class="plain" :disabled="busy" @click="act(session.logout)">退出登录</button>
|
|
</div>
|
|
</aside>
|
|
<RouterView />
|
|
<div v-if="notice" class="message-notice" role="status">
|
|
<RouterLink :to="notice.href" @click="dismissNotice">{{ notice.label }}</RouterLink>
|
|
<button type="button" class="plain" aria-label="关闭消息提醒" @click="dismissNotice">关闭</button>
|
|
</div>
|
|
</main>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.message-notice {
|
|
position: fixed; inset-inline-end: 1rem; inset-block-end: 1rem; z-index: 6;
|
|
display: flex; align-items: center; gap: .7rem; padding: .6rem .85rem;
|
|
background: var(--surface); border: 1px solid var(--line); border-radius: .5rem;
|
|
}
|
|
</style>
|