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
0 commit comments