63 lines
2.6 KiB
Vue
63 lines
2.6 KiB
Vue
<script setup lang="ts">
|
||
import { onMounted, ref } from 'vue'
|
||
import { useSession } from '../session'
|
||
import type { Device } from '../session'
|
||
|
||
const session = useSession()
|
||
const devices = ref<Device[]>([])
|
||
const error = ref('')
|
||
const busy = ref(false)
|
||
const target = ref<Device>()
|
||
const dates = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'short' })
|
||
|
||
async function perform(operation: () => Promise<void>) {
|
||
busy.value = true
|
||
error.value = ''
|
||
try {
|
||
await operation()
|
||
} catch (cause) {
|
||
error.value = cause instanceof Error ? cause.message : '操作失败,请稍后重试'
|
||
} finally {
|
||
busy.value = false
|
||
}
|
||
}
|
||
|
||
const refresh = () => perform(async () => { devices.value = await session.devices() })
|
||
onMounted(refresh)
|
||
|
||
async function revoke() {
|
||
if (!target.value) return
|
||
const id = target.value.id
|
||
target.value = undefined
|
||
await perform(async () => {
|
||
await session.revoke(id)
|
||
devices.value = await session.devices()
|
||
})
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<section v-if="session.identity" class="device-page">
|
||
<header class="page-header"><h1>设备管理</h1><button class="plain" :disabled="busy" @click="refresh">刷新</button></header>
|
||
<div class="device-content">
|
||
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||
<p v-if="busy && !devices.length" role="status">正在载入设备…</p>
|
||
<article v-for="device in devices" :key="device.id" class="device-row">
|
||
<span class="device-icon" aria-hidden="true">▣</span>
|
||
<div class="device-meta">
|
||
<div><strong>{{ device.name }}</strong><span v-if="device.id === session.identity.device.id" class="tag">当前设备</span></div>
|
||
<small>{{ device.platform === 'windows' ? 'Windows · 桌面端' : 'Web · 浏览器' }} · {{ device.signedIn ? '已登录' : '已退出' }}</small>
|
||
<small>首次登录 {{ dates.format(new Date(device.createdAt)) }} · 最近活动 {{ dates.format(new Date(device.lastActiveAt)) }}</small>
|
||
</div>
|
||
<span v-if="device.revokedAt" class="muted">已撤销</span>
|
||
<button v-else-if="device.id !== session.identity.device.id" class="plain danger" :disabled="busy" @click="target = device">撤销</button>
|
||
</article>
|
||
<section v-if="target" class="confirmation" aria-label="确认撤销">
|
||
<p>撤销「{{ target.name }}」后,该设备的登录会话将结束。</p>
|
||
<button class="plain" @click="target = undefined">取消</button>
|
||
<button class="plain danger" @click="revoke">确认撤销</button>
|
||
</section>
|
||
</div>
|
||
</section>
|
||
</template>
|