Skip to content

Commit 2740d1a

Browse files
author
sheepzh
committed
Display the time last backup (#200)
1 parent 5c18f93 commit 2740d1a

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (c) 2023 Hengyang Zhang
3+
*
4+
* This software is released under the MIT License.
5+
* https://opensource.org/licenses/MIT
6+
*/
7+
import { PropType, Ref, watch } from "vue"
8+
9+
import { t } from "@app/locale"
10+
import { UploadFilled } from "@element-plus/icons-vue"
11+
import { ElButton, ElLoading, ElMessage, ElText } from "element-plus"
12+
import { defineComponent, h, ref } from "vue"
13+
import metaService from "@service/meta-service"
14+
import processor from "@src/common/backup/processor"
15+
import { formatTime } from "@util/time"
16+
17+
async function handleBackup(lastTime: Ref<number>) {
18+
const loading = ElLoading.service({
19+
text: "Doing backup...."
20+
})
21+
const result = await processor.syncData()
22+
loading.close()
23+
if (result.success) {
24+
ElMessage.success('Successfully!')
25+
lastTime.value = result.data || Date.now()
26+
} else {
27+
ElMessage.error(result.errorMsg || 'Unknown error')
28+
}
29+
}
30+
31+
const TIME_FORMAT = t(msg => msg.calendar.timeFormat)
32+
33+
const _default = defineComponent({
34+
props: {
35+
type: {
36+
type: String as PropType<timer.backup.Type>,
37+
required: false,
38+
}
39+
},
40+
setup(props) {
41+
const lastTime: Ref<number> = ref(undefined)
42+
43+
const queryLastTime = async () => {
44+
const backInfo = await metaService.getLastBackUp(props.type)
45+
lastTime.value = backInfo?.ts
46+
}
47+
48+
queryLastTime()
49+
watch(() => props.type, queryLastTime)
50+
51+
return () => {
52+
const children = [
53+
h(ElButton, {
54+
type: 'primary',
55+
icon: UploadFilled,
56+
onClick: () => handleBackup(lastTime)
57+
}, () => t(msg => msg.option.backup.operation))
58+
]
59+
const lastTimeVal = lastTime.value
60+
if (lastTimeVal) {
61+
const tips = t(msg => msg.option.backup.lastTimeTip, {
62+
lastTime: formatTime(lastTimeVal, TIME_FORMAT)
63+
})
64+
const tipText = h(ElText, { style: { marginLeft: '20px' } }, () => tips)
65+
children.push(tipText)
66+
}
67+
lastTime.value && children.push()
68+
return h('div', {}, children)
69+
}
70+
}
71+
})
72+
73+
export default _default

src/app/components/option/components/backup/index.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { defaultBackup } from "@util/constant/option"
1414
import { ElInput, ElOption, ElSelect, ElDivider, ElAlert, ElButton, ElMessage, ElLoading } from "element-plus"
1515
import { defineComponent, ref, h } from "vue"
1616
import { renderOptionItem, tooltip } from "../../common"
17-
import { UploadFilled } from "@element-plus/icons-vue"
1817
import BackUpAutoInput from "./auto-input"
18+
import Footer from "./footer"
1919

2020
const ALL_TYPES: timer.backup.Type[] = [
2121
'none',
@@ -114,19 +114,6 @@ const _default = defineComponent({
114114
}
115115
}
116116

117-
async function handleBackup() {
118-
const loading = ElLoading.service({
119-
text: "Doing backup...."
120-
})
121-
const result = await processor.syncData()
122-
loading.close()
123-
if (result.success) {
124-
ElMessage.success('Successfully!')
125-
} else {
126-
ElMessage.error(result.errorMsg || 'Unknown error')
127-
}
128-
}
129-
130117
ctx.expose({
131118
async reset() {
132119
// Only reset type and auto flag
@@ -151,6 +138,7 @@ const _default = defineComponent({
151138
t(msg => msg.option.backup.meta[DEFAULT.backupType].label)
152139
)
153140
]
141+
console.log(type.value)
154142
type.value !== 'none' && nodes.push(
155143
h(ElDivider),
156144
renderOptionItem({
@@ -178,11 +166,7 @@ const _default = defineComponent({
178166
msg => msg.backup.client
179167
),
180168
h(ElDivider),
181-
h(ElButton, {
182-
type: 'primary',
183-
icon: UploadFilled,
184-
onClick: handleBackup
185-
}, () => t(msg => msg.option.backup.operation)),
169+
h(Footer, { type: type.value }),
186170
)
187171
return h('div', nodes)
188172
}

src/common/backup/processor.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Processor {
145145
}
146146
}
147147

148-
async syncData(): Promise<Result<void>> {
148+
async syncData(): Promise<Result<number>> {
149149
const option = (await optionService.getAllOption()) as timer.option.BackupOption
150150
const auth = option?.backupAuths?.[option.backupType || 'none']
151151

@@ -173,8 +173,9 @@ class Processor {
173173
clients.push(client)
174174
await coordinator.updateClients(context, clients)
175175
// Update time
176-
metaService.updateBackUpTime(type, Date.now())
177-
return success()
176+
const now = Date.now()
177+
metaService.updateBackUpTime(type, now)
178+
return success(now)
178179
}
179180

180181
async query(type: timer.backup.Type, auth: string, start: Date, end: Date): Promise<timer.stat.Row[]> {

src/i18n/message/app/option.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export type OptionMessage = {
7373
alert: string
7474
test: string
7575
operation: string
76+
lastTimeTip: string
7677
auto: {
7778
label: string
7879
interval: string
@@ -167,6 +168,7 @@ const _default: Messages<OptionMessage> = {
167168
},
168169
alert: '这是一项实验性功能,如果有任何问题请联系作者~ (returnzhy1996@outlook.com)',
169170
test: '测试',
171+
lastTimeTip: '上次备份时间: {lastTime}',
170172
operation: '备份数据',
171173
auto: {
172174
label: '是否开启自动备份',
@@ -253,6 +255,7 @@ const _default: Messages<OptionMessage> = {
253255
alert: '這是一項實驗性功能,如果有任何問題請聯繫作者 (returnzhy1996@outlook.com) ~',
254256
test: '測試',
255257
operation: '備份數據',
258+
lastTimeTip: '上次備份時間: {lastTime}',
256259
auto: {
257260
label: '是否開啟自動備份',
258261
interval: '每 {input} 分鐘備份一次',
@@ -338,6 +341,7 @@ const _default: Messages<OptionMessage> = {
338341
alert: 'This is an experimental feature, if you have any questions please contact the author via returnzhy1996@outlook.com~',
339342
test: 'Test',
340343
operation: 'Backup',
344+
lastTimeTip: 'Last backup time: {lastTime}',
341345
auto: {
342346
label: 'Whether to enable automatic backup',
343347
interval: 'and run every {input} minutes',
@@ -423,6 +427,7 @@ const _default: Messages<OptionMessage> = {
423427
alert: 'これは実験的な機能です。質問がある場合は、作成者に連絡してください (returnzhy1996@outlook.com)',
424428
test: 'テスト',
425429
operation: 'バックアップ',
430+
lastTimeTip: '前回のバックアップ時間: {lastTime}',
426431
auto: {
427432
label: '自動バックアップを有効にするかどうか',
428433
interval: ' {input} 分ごとに実行',

src/i18n/message/common/calendar.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,33 @@ export type CalendarMessage = {
99
weekDays: string
1010
months: string
1111
dateFormat: string
12+
timeFormat: string
1213
}
1314

1415
const _default: Messages<CalendarMessage> = {
1516
zh_CN: {
1617
weekDays: '星期一|星期二|星期三|星期四|星期五|星期六|星期天',
1718
months: '一月|二月|三月|四月|五月|六月|七月|八月|九月|十月|十一月|十二月',
1819
dateFormat: '{y}/{m}/{d}',
20+
timeFormat: '{y}/{m}/{d} {h}:{i}:{s}',
1921
},
2022
zh_TW: {
2123
weekDays: '禮拜一|禮拜二|禮拜三|禮拜四|禮拜五|禮拜六|禮拜天',
2224
months: '一月|二月|三月|四月|五月|六月|七月|八月|九月|十月|十一月|十二月',
2325
dateFormat: '{y}/{m}/{d}',
26+
timeFormat: '{y}/{m}/{d} {h}:{i}:{s}',
2427
},
2528
en: {
2629
weekDays: 'Mon|Tue|Wed|Thu|Fri|Sat|Sun',
2730
months: 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Set|Oct|Nov|Dec',
2831
dateFormat: '{m}/{d}/{y}',
32+
timeFormat: '{m}/{d}/{y} {h}:{i}:{s}',
2933
},
3034
ja: {
3135
weekDays: 'Mon|Tue|Wed|Thu|Fri|Sat|Sun',
3236
months: 'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Set|Oct|Nov|Dec',
3337
dateFormat: '{y}/{m}/{d}',
38+
timeFormat: '{y}/{m}/{d} {h}:{i}:{s}',
3439
},
3540
}
3641

0 commit comments

Comments
 (0)