Files
Thing/src/modules/smarthome/components/HaStatusBanner.vue
T
2026-10-23 17:25:17 +08:00

132 lines
4.6 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 } from 'vue'
import { AlertTriangle, KeyRound, Loader2, Unplug } from '@lucide/vue'
import { Button } from '@/components/ui/button'
import { useSmartHomeStore } from '@/stores/smartHomeStore'
/**
* 顶部状态横幅:**只在出问题时占位**(未配置连接由设备页的空态负责,不在这里重复)。
*
* 两类问题必须区分(§3.6):
* - 令牌无效(auth):应当"重新填写令牌",**重试没有意义**
* - 地址不可达(network / config):应当"重新检测 / 去检查地址"
*
* P4 增加一类:**实时连接的自身问题**(鉴权失败 / 断开重连中)。它与链路问题是
* 两件事 —— 链路可能是好的(REST 还能用),但推送断了,设备状态会停在最后一次推送。
*/
const emit = defineEmits<{ 'open-settings': [] }>()
const store = useSmartHomeStore()
const failed = computed(() => store.link ?? null)
const visible = computed(() => !!failed.value && !failed.value.ok)
const isAuth = computed(() => failed.value?.errorKind === 'auth')
/**
* WS 自身的问题(只在用户确实想要实时连接时才提示:关掉实时推送的人
* 不该被"实时连接已断开"打扰)。
*/
const wsIssue = computed(() => {
if (!store.realtimeWanted) return null
const status = store.wsStatus
if (!status) return null
if (status.state === 'authFailed') {
return { kind: 'auth' as const, title: '实时连接鉴权失败', detail: status.detail }
}
if (status.state === 'disconnected') {
return { kind: 'net' as const, title: '实时连接已断开(正在重连)', detail: status.detail }
}
return null
})
</script>
<template>
<!-- 两条横幅都不出现时整个容器不渲染:否则会白留 16px 的间距 -->
<div v-if="visible || wsIssue" class="mb-4 flex flex-col gap-3">
<div
v-if="visible"
class="flex items-start gap-2.5 rounded-lg border px-4 py-3"
:class="isAuth ? 'border-danger bg-danger-soft' : 'border-warning bg-warning-soft'"
>
<component
:is="isAuth ? KeyRound : AlertTriangle"
class="mt-0.5 size-4 shrink-0"
:class="isAuth ? 'text-danger' : 'text-warning'"
:stroke-width="1.75"
/>
<div class="min-w-0 flex-1">
<p
class="text-[13px] font-medium"
:class="isAuth ? 'text-danger-text' : 'text-warning-text'"
>
{{ isAuth ? '访问令牌无效或已被吊销' : '无法连接 Home Assistant' }}
</p>
<p
class="mt-0.5 text-xs break-words"
:class="isAuth ? 'text-danger-text' : 'text-warning-text'"
>
{{ failed?.error }}
</p>
</div>
<Button
size="sm"
variant="outline"
class="shrink-0"
:disabled="store.linkChecking"
@click="emit('open-settings')"
>
<Loader2 v-if="store.linkChecking" class="size-4 animate-spin" />
{{ isAuth ? '重新填写令牌' : '检查连接' }}
</Button>
</div>
<!-- 实时连接自身的问题(链路可能是好的,但推送断了) -->
<div
v-if="wsIssue"
class="flex items-start gap-2.5 rounded-lg border px-4 py-3"
:class="
wsIssue.kind === 'auth' ? 'border-danger bg-danger-soft' : 'border-warning bg-warning-soft'
"
>
<Unplug
class="mt-0.5 size-4 shrink-0"
:class="wsIssue.kind === 'auth' ? 'text-danger' : 'text-warning'"
:stroke-width="1.75"
/>
<div class="min-w-0 flex-1">
<p
class="text-[13px] font-medium"
:class="wsIssue.kind === 'auth' ? 'text-danger-text' : 'text-warning-text'"
>
{{ wsIssue.title }}
</p>
<p
v-if="wsIssue.detail"
class="mt-0.5 text-xs break-words"
:class="wsIssue.kind === 'auth' ? 'text-danger-text' : 'text-warning-text'"
>
{{ wsIssue.detail }}
</p>
<p
class="mt-0.5 text-xs"
:class="wsIssue.kind === 'auth' ? 'text-danger-text' : 'text-warning-text'"
>
{{ wsIssue.kind === 'auth' ? '请到设置页重新填写访问令牌。' : '设备状态会停在最后一次推送。' }}
</p>
</div>
<Button
v-if="wsIssue.kind === 'net'"
size="sm"
variant="outline"
class="shrink-0"
:disabled="store.wsBusy"
@click="store.startRealtime()"
>
<Loader2 v-if="store.wsBusy" class="size-4 animate-spin" />重连
</Button>
<Button v-else size="sm" variant="outline" class="shrink-0" @click="emit('open-settings')">
重新填写令牌
</Button>
</div>
</div>
</template>