mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 08:02:21 +00:00
feat(web): model command presentation states
This commit is contained in:
1 parent
20a4b662b4
commit
de386c19bf
6 files changed
+142
-40
No files matched your search
@@ -1,32 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import type { BalanceQuery } from '../../types/commands'
|
||||
import { balanceResultText, presentBalanceState } from '../../utils/commandPresentation'
|
||||
import { Wallet24Regular } from '@vicons/fluent'
|
||||
|
||||
defineProps<{ query: BalanceQuery }>()
|
||||
|
||||
function stateLabel(query: BalanceQuery) {
|
||||
if (query.state === 'awaiting_reply') return '等待短信'
|
||||
if (query.state === 'completed') return query.parse_state === 'parsed' ? '已解析' : '已收到'
|
||||
if (query.state === 'timed_out') return '已超时'
|
||||
if (query.state === 'failed') return '失败'
|
||||
return '发送中'
|
||||
}
|
||||
|
||||
function resultText(query: BalanceQuery) {
|
||||
if (query.summary) return query.summary
|
||||
if (query.amount) return [query.amount, query.currency].filter(Boolean).join(' ')
|
||||
if (query.state === 'completed') return '已收到运营商回复'
|
||||
return '正在等待运营商返回余额'
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="balance-message" :class="query.state">
|
||||
<div class="balance-heading">
|
||||
<span><el-icon><Wallet24Regular /></el-icon>运营商余额</span>
|
||||
<span class="balance-state">{{ stateLabel(query) }}</span>
|
||||
<span class="balance-state">{{ presentBalanceState(query).label }}</span>
|
||||
</div>
|
||||
<strong>{{ resultText(query) }}</strong>
|
||||
<strong>{{ balanceResultText(query) }}</strong>
|
||||
<span class="balance-device">{{ query.device_id }}</span>
|
||||
<pre v-if="query.raw_response">{{ query.raw_response }}</pre>
|
||||
<p v-if="query.error">{{ query.error }}</p>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import type { BalanceQuery } from '../../types/commands'
|
||||
import type { DeviceMgmtListItem } from '../../types/api'
|
||||
import { formatDeviceDateTime } from '../../utils/deviceTime'
|
||||
import { balanceResultText, presentBalanceState } from '../../utils/commandPresentation'
|
||||
import { Edit24Regular, Wallet24Regular } from '@vicons/fluent'
|
||||
|
||||
defineProps<{
|
||||
@@ -18,13 +19,6 @@ const emit = defineEmits<{
|
||||
editRules: []
|
||||
}>()
|
||||
|
||||
function stateLabel(query: BalanceQuery) {
|
||||
if (query.state === 'awaiting_reply') return '等待短信'
|
||||
if (query.state === 'completed') return query.parse_state === 'parsed' ? '已解析' : '已收到'
|
||||
if (query.state === 'timed_out') return '已超时'
|
||||
if (query.state === 'failed') return '失败'
|
||||
return '发送中'
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -61,8 +55,10 @@ function stateLabel(query: BalanceQuery) {
|
||||
</div>
|
||||
<article v-for="query in queries" :key="query.id" class="balance-item">
|
||||
<div class="balance-row">
|
||||
<strong>{{ query.summary || query.amount || '等待运营商回复' }}</strong>
|
||||
<span class="query-state" :class="query.state">{{ stateLabel(query) }}</span>
|
||||
<strong>{{ balanceResultText(query) }}</strong>
|
||||
<span class="query-state" :class="presentBalanceState(query).tone">
|
||||
{{ presentBalanceState(query).label }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="balance-meta">
|
||||
<span>{{ query.device_id }}</span>
|
||||
|
||||
@@ -5,6 +5,7 @@ import CommandTimeline from './CommandTimeline.vue'
|
||||
import type { DeviceMgmtListItem } from '../../types/api'
|
||||
import type { BalanceQuery, CommandDefinition, CommandEvent } from '../../types/commands'
|
||||
import { commandTargetDevice } from '../../utils/commandInput'
|
||||
import { balanceResultText } from '../../utils/commandPresentation'
|
||||
import {
|
||||
Bot24Regular,
|
||||
Delete24Regular,
|
||||
@@ -49,11 +50,7 @@ const emit = defineEmits<{
|
||||
|
||||
function latestBalanceText(query?: BalanceQuery) {
|
||||
if (!query) return ''
|
||||
if (query.amount) return [query.amount, query.currency].filter(Boolean).join(' ')
|
||||
if (query.summary) return query.summary
|
||||
if (query.state === 'completed') return '余额已返回'
|
||||
if (query.state === 'failed' || query.state === 'timed_out') return '余额查询失败'
|
||||
return '余额查询中'
|
||||
return balanceResultText(query)
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import BalanceMessage from './BalanceMessage.vue'
|
||||
import CommandAudioPlayer from './CommandAudioPlayer.vue'
|
||||
import type { BalanceQuery, CommandEvent } from '../../types/commands'
|
||||
import { formatDeviceDateTime } from '../../utils/deviceTime'
|
||||
import { presentCommandEvent } from '../../utils/commandPresentation'
|
||||
import { Bot24Regular } from '@vicons/fluent'
|
||||
|
||||
const props = defineProps<{
|
||||
@@ -26,16 +27,8 @@ const timelineItems = computed<TimelineItem[]>(() => {
|
||||
return [...commands, ...balances].sort((left, right) => Date.parse(left.createdAt) - Date.parse(right.createdAt))
|
||||
})
|
||||
|
||||
function eventLabel(event: CommandEvent) {
|
||||
if (event.kind === 'accepted') return '你'
|
||||
if (event.kind === 'progress') return '处理中'
|
||||
if (event.kind === 'error') return '执行失败'
|
||||
return 'VoHive'
|
||||
}
|
||||
|
||||
function eventText(event: CommandEvent) {
|
||||
if (event.kind === 'accepted' && event.execution?.input) return event.execution.input
|
||||
return event.text
|
||||
return presentCommandEvent(event).detail
|
||||
}
|
||||
|
||||
function audioAttachments(event: CommandEvent) {
|
||||
@@ -61,7 +54,7 @@ function audioAttachments(event: CommandEvent) {
|
||||
}"
|
||||
>
|
||||
<div class="message-meta">
|
||||
<span>{{ item.kind === 'command' ? eventLabel(item.event) : 'VoHive' }}</span>
|
||||
<span>{{ item.kind === 'command' ? presentCommandEvent(item.event).title : 'VoHive' }}</span>
|
||||
<time>{{ formatDeviceDateTime(item.createdAt) }}</time>
|
||||
</div>
|
||||
<template v-if="item.kind === 'command'">
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { BalanceQuery, CommandEvent } from '../types/commands'
|
||||
|
||||
export type CommandEventTone = 'sent' | 'running' | 'success' | 'danger'
|
||||
export type BalanceTone = 'running' | 'waiting' | 'parsed' | 'success' | 'danger'
|
||||
|
||||
export type CommandEventPresentation = Readonly<{
|
||||
title: string
|
||||
detail: string
|
||||
tone: CommandEventTone
|
||||
}>
|
||||
|
||||
export type BalanceStatePresentation = Readonly<{
|
||||
label: string
|
||||
tone: BalanceTone
|
||||
}>
|
||||
|
||||
export function presentCommandEvent(event: CommandEvent): CommandEventPresentation {
|
||||
if (event.kind === 'error' || event.execution?.state === 'failed') {
|
||||
return {
|
||||
title: '执行失败',
|
||||
detail: event.text || event.execution?.error || '命令执行失败',
|
||||
tone: 'danger'
|
||||
}
|
||||
}
|
||||
if (event.kind === 'accepted') {
|
||||
return {
|
||||
title: '已发送',
|
||||
detail: event.execution?.input || event.text || '命令已提交',
|
||||
tone: 'sent'
|
||||
}
|
||||
}
|
||||
if (event.kind === 'progress' || event.execution?.state === 'running') {
|
||||
return { title: '执行中', detail: event.text || '命令正在执行', tone: 'running' }
|
||||
}
|
||||
return { title: '执行成功', detail: event.text || '命令执行完成', tone: 'success' }
|
||||
}
|
||||
|
||||
export function presentBalanceState(query: BalanceQuery): BalanceStatePresentation {
|
||||
if (query.state === 'failed') return { label: '查询失败', tone: 'danger' }
|
||||
if (query.state === 'timed_out') return { label: '等待超时', tone: 'danger' }
|
||||
if (query.state === 'awaiting_reply') return { label: '等待回复', tone: 'waiting' }
|
||||
if (query.state === 'sending') return { label: '正在发送', tone: 'running' }
|
||||
if (query.parse_state === 'parsed') return { label: '已解析', tone: 'parsed' }
|
||||
return { label: '已收到', tone: 'success' }
|
||||
}
|
||||
|
||||
export function balanceResultText(query: BalanceQuery): string {
|
||||
if (query.amount) return [query.amount, query.currency].filter(Boolean).join(' ')
|
||||
if (query.summary) return query.summary
|
||||
if (query.state === 'completed') return '已收到运营商回复'
|
||||
if (query.state === 'failed') return query.error || '余额查询失败'
|
||||
if (query.state === 'timed_out') return query.error || '未在有效期内收到回复'
|
||||
return '等待运营商回复'
|
||||
}
|
||||
|
||||
export function balanceTransportLabel(query: BalanceQuery): string {
|
||||
return query.transport === 'ussd' ? 'USSD' : 'SMS'
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import type { BalanceQuery, CommandEvent } from '../src/types/commands'
|
||||
import {
|
||||
balanceResultText,
|
||||
balanceTransportLabel,
|
||||
presentBalanceState,
|
||||
presentCommandEvent
|
||||
} from '../src/utils/commandPresentation'
|
||||
|
||||
function commandEvent(overrides: Partial<CommandEvent>): CommandEvent {
|
||||
return {
|
||||
id: 1,
|
||||
execution_id: 'exec-1',
|
||||
kind: 'result',
|
||||
text: '',
|
||||
created_at: '2026-08-13T12:00:00Z',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
function balanceQuery(overrides: Partial<BalanceQuery>): BalanceQuery {
|
||||
return {
|
||||
id: 'balance-1',
|
||||
device_id: 'wwan0',
|
||||
iccid: '',
|
||||
rule_id: 'rule-1',
|
||||
transport: 'sms',
|
||||
state: 'sending',
|
||||
parse_state: 'pending',
|
||||
started_at: '2026-08-13T12:00:00Z',
|
||||
expires_at: '2026-08-13T12:01:00Z',
|
||||
created_at: '2026-08-13T12:00:00Z',
|
||||
updated_at: '2026-08-13T12:00:00Z',
|
||||
...overrides
|
||||
}
|
||||
}
|
||||
|
||||
test('maps command events without turning running or failed work into success', () => {
|
||||
const accepted = commandEvent({ kind: 'accepted', execution: {
|
||||
id: 'exec-1', input: '/signal wwan0', command: 'signal', state: 'running',
|
||||
created_at: '', updated_at: ''
|
||||
} })
|
||||
assert.deepEqual(presentCommandEvent(accepted), {
|
||||
title: '已发送', detail: '/signal wwan0', tone: 'sent'
|
||||
})
|
||||
assert.equal(presentCommandEvent(commandEvent({ kind: 'progress', text: '正在读取信号' })).tone, 'running')
|
||||
assert.deepEqual(presentCommandEvent(commandEvent({ kind: 'error', text: '设备离线' })), {
|
||||
title: '执行失败', detail: '设备离线', tone: 'danger'
|
||||
})
|
||||
assert.equal(presentCommandEvent(commandEvent({ kind: 'result', text: '查询完成' })).tone, 'success')
|
||||
})
|
||||
|
||||
test('keeps all balance lifecycle states explicit', () => {
|
||||
assert.deepEqual(presentBalanceState(balanceQuery({ state: 'sending' })), {
|
||||
label: '正在发送', tone: 'running'
|
||||
})
|
||||
assert.equal(presentBalanceState(balanceQuery({ state: 'awaiting_reply' })).label, '等待回复')
|
||||
assert.equal(presentBalanceState(balanceQuery({ state: 'completed', parse_state: 'parsed' })).tone, 'parsed')
|
||||
assert.equal(presentBalanceState(balanceQuery({ state: 'completed', parse_state: 'unparsed' })).tone, 'success')
|
||||
assert.equal(presentBalanceState(balanceQuery({ state: 'timed_out' })).tone, 'danger')
|
||||
assert.equal(presentBalanceState(balanceQuery({ state: 'failed' })).label, '查询失败')
|
||||
})
|
||||
|
||||
test('renders only values and transport facts returned by the backend', () => {
|
||||
assert.equal(balanceResultText(balanceQuery({ amount: '12.89', currency: 'GBP' })), '12.89 GBP')
|
||||
assert.equal(balanceResultText(balanceQuery({ summary: '剩余 300MB' })), '剩余 300MB')
|
||||
assert.equal(balanceResultText(balanceQuery({ state: 'failed', error: '未匹配规则' })), '未匹配规则')
|
||||
assert.equal(balanceTransportLabel(balanceQuery({ transport: 'sms' })), 'SMS')
|
||||
assert.equal(balanceTransportLabel(balanceQuery({ transport: 'ussd' })), 'USSD')
|
||||
})
|
||||
Reference in new issue
Block a user