miaopanel
1<script setup lang="ts">
2import { computed, ref } from 'vue'
3import { api } from '../api'
4import type { UpdateInfo, UpdateResult } from '../types'
5import { bytes, time } from '../format'
6
7const props = defineProps<{ info: UpdateInfo }>()
8const emit = defineEmits<{ (e: 'close'): void }>()
9
10type Phase = 'idle' | 'installing' | 'restarting' | 'manual' | 'error'
11
12const phase = ref<Phase>('idle')
13const message = ref('')
14const result = ref<UpdateResult | null>(null)
15
16const busy = computed(() => phase.value === 'installing' || phase.value === 'restarting')
17
18function sleep(ms: number) {
19 return new Promise((resolve) => setTimeout(resolve, ms))
20}
21
22async function install() {
23 phase.value = 'installing'
24 message.value = ''
25 try {
26 // The server always re-checks the control plane itself; the client never
27 // sends a download URL.
28 const res = await api.post<UpdateResult>('api/v1/system/update', { confirm: 'UPDATE' })
29 result.value = res
30 if (res.restart === 'manual') {
31 phase.value = 'manual'
32 return
33 }
34 phase.value = 'restarting'
35 await waitRestart(res.version)
36 } catch (e) {
37 phase.value = 'error'
38 message.value = (e as Error).message
39 }
40}
41
42// Poll the public health endpoint until the restarted panel reports the new
43// version, then reload so the UI picks up the new embedded assets.
44async function waitRestart(target: string) {
45 for (let i = 0; i < 90; i++) {
46 await sleep(2000)
47 try {
48 const health = await api.get<{ version: string }>('api/v1/health')
49 if (health.version === target) {
50 location.reload()
51 return
52 }
53 } catch {
54 // The panel is still restarting; keep waiting.
55 }
56 }
57 phase.value = 'error'
58 message.value = '面板重启时间超出预期,请稍后手动刷新页面'
59}
60
61function close() {
62 if (!busy.value) emit('close')
63}
64</script>
65
66<template>
67 <div class="overlay" @click.self="close">
68 <div class="modal" style="width: min(560px, 100%)">
69 <div class="modal-head">
70 <h3>发现新版本 v{{ props.info.version }}</h3>
71 <button class="icon-btn" :disabled="busy" title="关闭" @click="close">×</button>
72 </div>
73
74 <dl class="kv" style="margin: 0 0 14px">
75 <dt>当前版本</dt>
76 <dd class="mono">v{{ props.info.current }}</dd>
77 <dt>最新版本</dt>
78 <dd class="mono">v{{ props.info.version }}</dd>
79 <dt>更新包</dt>
80 <dd class="mono">
81 {{ props.info.size ? bytes(props.info.size) : '未知大小' }} · {{ props.info.os }}/{{
82 props.info.arch
83 }}
84 </dd>
85 <dt>发布时间</dt>
86 <dd class="mono">{{ props.info.published_at ? time(props.info.published_at) : '—' }}</dd>
87 </dl>
88
89 <div v-if="props.info.notes" class="update-notes">{{ props.info.notes }}</div>
90
91 <div v-if="phase === 'idle'" class="alert ok" style="margin-top: 12px">
92 更新会下载新版本并通过 SHA256 校验后替换面板二进制;面板随后自动重启(数秒),网站与数据库配置不受影响。
93 </div>
94
95 <div v-else-if="phase === 'installing'" class="row" style="margin-top: 14px">
96 <span class="spin"></span>
97 <span class="muted">正在下载、校验并安装,请勿关闭页面…</span>
98 </div>
99
100 <div v-else-if="phase === 'restarting'" class="row" style="margin-top: 14px">
101 <span class="spin"></span>
102 <span class="muted">面板正在重启,恢复后页面会自动刷新…</span>
103 </div>
104
105 <div v-else-if="phase === 'manual'" class="alert danger" style="margin-top: 14px">
106 {{ result?.message }}<br />
107 请在服务器执行 <span class="mono">systemctl restart miaopanel</span>,旧版二进制已备份到
108 <span class="mono">{{ result?.backup }}</span>。
109 </div>
110
111 <div v-else class="alert danger" style="margin-top: 14px">{{ message }}</div>
112
113 <div class="modal-foot">
114 <div class="spacer"></div>
115 <template v-if="phase === 'idle'">
116 <button class="btn btn-ghost" @click="close">稍后</button>
117 <button class="btn btn-primary" @click="install">立即更新</button>
118 </template>
119 <template v-else-if="phase === 'error'">
120 <button class="btn btn-ghost" @click="close">关闭</button>
121 <button class="btn btn-primary" @click="install">重试</button>
122 </template>
123 <template v-else-if="phase === 'manual'">
124 <button class="btn btn-primary" @click="close">我知道了</button>
125 </template>
126 </div>
127 </div>
128 </div>
129</template>
130
131<style scoped>
132.update-notes {
133 max-height: 220px;
134 overflow: auto;
135 margin: 0;
136 padding: 12px;
137 border: 1px solid var(--line);
138 border-radius: 12px;
139 background: #f7fcfb;
140 color: #2c5058;
141 font-size: 12.5px;
142 line-height: 1.7;
143 white-space: pre-wrap;
144 word-break: break-word;
145}
146</style>