Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions frontend/src/components/ProgressModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useI18n } from 'vue-i18n'

const isOpen = ref(false)

const mode = ref<'export' | 'import' | 'download'>('export')
const mode = ref<'export' | 'import' | 'download' | 'validate'>('export')

const snapshot = ref<porter.JobSnapshot | null>(null)

Expand All @@ -17,7 +17,9 @@ const steps = computed(() => {
case 'export':
return ['initialisation', 'compression', 'complete']
case 'download':
return ['initialisation', 'download', 'complete']
return ['initialisation', 'download', 'compare', 'complete']
case 'validate':
return ['initialisation', 'compare', 'complete']
case 'import':
return ['initialisation', 'backup', 'extract', 'cleanup', 'complete']
default:
Expand Down Expand Up @@ -53,6 +55,37 @@ function startPolling() {
interval = setInterval(updateProgress, 300)
}

// runPreviewJob drives the modal for promise-returning preview flows (download,
// validate): opens the modal, polls, resolves with the preview or toasts+rejects.
function runPreviewJob(
modeName: 'download' | 'validate',
titleText: string,
op: Promise<porter.ImportPreview>
): Promise<porter.ImportPreview> {
return new Promise((resolve, reject) => {
isOpen.value = true
mode.value = modeName
title.value = titleText
snapshot.value = null
messages.value = []

op.then((preview: porter.ImportPreview) => {
resolve(preview)
isOpen.value = false
})
.catch(err => {
toast.add({ title: decodeError(err, t), color: 'error' })
reject(err)
})
.finally(() => {
resetInterval()
updateProgress()
})

startPolling()
})
}

defineExpose({
export: (destination: string) => {
isOpen.value = true
Expand All @@ -71,32 +104,10 @@ defineExpose({

startPolling()
},
download: (url: string): Promise<porter.ImportPreview> => {
return new Promise((resolve, reject) => {
isOpen.value = true
mode.value = 'download'
title.value = t('labelUpdate')
snapshot.value = null
messages.value = []

programPorter
.DownloadAndValidate(url)
.then((preview: porter.ImportPreview) => {
resolve(preview)
isOpen.value = false
})
.catch(err => {
toast.add({ title: decodeError(err, t), color: 'error' })
reject(err)
})
.finally(() => {
resetInterval()
updateProgress()
})

startPolling()
})
},
download: (url: string): Promise<porter.ImportPreview> =>
runPreviewJob('download', t('labelUpdate'), programPorter.DownloadAndValidate(url)),
validate: (path: string): Promise<porter.ImportPreview> =>
runPreviewJob('validate', t('labelValidate'), programPorter.ValidateZip(path)),
import: (from: 'url' | 'file', source: string, opts: porter.ImportOptions) => {
isOpen.value = true
mode.value = 'import'
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/ProgressStepper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function stepKey(name: string): string {
compression: 'stepCompression',
complete: 'complete',
download: 'stepDownload',
compare: 'stepCompare',
backup: 'stepBackup',
extract: 'stepExtract',
cleanup: 'stepCleanup'
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
"labelBuildType": "Build Type",
"labelCaseSensitive": "Case Sensitive",
"labelCreated": "Created",
"labelDiffChanged": "Changed",
"labelDiffFileCount": "{count} file | {count} files",
"labelDiffIdentical": "Identical",
"labelDiffIncoming": "Backup",
"labelDiffLocal": "This PC",
"labelDiffNew": "New on this machine",
"labelCurrentVersion": "Latest Version",
"labelData": "Data",
"labelDatabase": "Database",
Expand Down Expand Up @@ -203,6 +209,7 @@
"stepBackupLabel": "Backup",
"stepCleanup": "Cleaning up",
"stepCleanupLabel": "Cleanup",
"stepCompare": "Compare",
"stepCompression": "Compressing",
"stepCompressionLabel": "Compression",
"stepDecompressionLabel": "Decompression",
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/i18n/zh_Hant_HK.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
"labelBuildType": "組建類型",
"labelCaseSensitive": "區分大小寫",
"labelCreated": "建立日期",
"labelDiffChanged": "有變更",
"labelDiffFileCount": "{count} 個檔案",
"labelDiffIdentical": "相同",
"labelDiffIncoming": "備份",
"labelDiffLocal": "本機",
"labelDiffNew": "本機未有",
"labelCurrentVersion": "現有版本",
"labelData": "資料",
"labelDatabase": "資料庫",
Expand Down Expand Up @@ -203,6 +209,7 @@
"stepBackupLabel": "備份",
"stepCleanup": "清理中",
"stepCleanupLabel": "清理檔案",
"stepCompare": "比對",
"stepCompression": "壓縮中",
"stepCompressionLabel": "壓縮檔案",
"stepDecompressionLabel": "解壓縮檔案",
Expand Down
143 changes: 125 additions & 18 deletions frontend/src/pages/porter.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<script setup lang="ts">
import { Cwd, SelectFile, SelectFolder } from '@/wailsjs/go/main/App'
import { porter } from '@/wailsjs/go/models'
import { ValidateZip } from '@/wailsjs/go/porter/Porter'
import * as appStorage from '@/wailsjs/go/storage/AppSettingStorage'
import { decodeError } from '@/utils/index'
import { onBeforeMount, ref, useTemplateRef } from 'vue'
import { computed, onBeforeMount, ref, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'

const toast = useToast()
Expand All @@ -26,8 +25,17 @@ const importInput = ref<{

const previewOpen = ref(false)
const preview = ref<porter.ImportPreview | null>(null)

type DiffState = 'identical' | 'changed' | 'new'

const diffStateMeta: Record<DiffState, { labelKey: string; badgeClass: string }> = {
identical: { labelKey: 'labelDiffIdentical', badgeClass: 'bg-green-50 text-green-700' },
changed: { labelKey: 'labelDiffChanged', badgeClass: 'bg-amber-50 text-amber-700' },
new: { labelKey: 'labelDiffNew', badgeClass: 'bg-blue-50 text-blue-700' }
}
const previewSource = ref({ from: 'file' as 'file' | 'url', source: '' })
const importOpts = ref({ data: true, settings: true })
const validating = ref(false)

onBeforeMount(() => {
appStorage
Expand Down Expand Up @@ -57,6 +65,53 @@ function formatBytes(bytes: number): string {
return `${value.toFixed(value >= 10 ? 0 : 1)} ${units[unitIndex]}`
}

function targetDiff(key: 'data' | 'settings'): porter.TargetDiff | null {
const d = preview.value?.[key]
return d && d.available ? d : null
}

function diffState(d: porter.TargetDiff): DiffState {
if (d.localPresent) return d.identical ? 'identical' : 'changed'
return 'new'
}

function sideFacts(key: 'data' | 'settings', d: porter.TargetDiff, local: boolean): string {
const prefix = t(local ? 'labelDiffLocal' : 'labelDiffIncoming')
const count = local ? d.localCount : d.incomingCount
const size = local ? d.localSize : d.incomingSize
return key === 'settings'
? `${prefix} ${formatBytes(size)}`
: `${prefix} ${t('labelDiffFileCount', { count })}, ${formatBytes(size)}`
}

function diffFacts(key: 'data' | 'settings', d: porter.TargetDiff, state: DiffState): string {
if (state === 'changed') {
return `${sideFacts(key, d, false)} · ${sideFacts(key, d, true)}`
}

// identical / new: single side is enough (identical sides are equal)
if (state === 'identical') {
return key === 'settings'
? formatBytes(d.incomingSize)
: `${t('labelDiffFileCount', { count: d.incomingCount })}, ${formatBytes(d.incomingSize)}`
}

return sideFacts(key, d, false)
}

function rowDiff(key: 'data' | 'settings'): { state: DiffState; facts: string } | null {
const d = targetDiff(key)
if (!d) return null

const state = diffState(d)
return { state, facts: diffFacts(key, d, state) }
}

const diffRows = computed(() => ({
data: rowDiff('data'),
settings: rowDiff('settings')
}))

function openPreview(p: porter.ImportPreview, from: 'file' | 'url', source: string) {
preview.value = p
previewSource.value = { from, source }
Expand All @@ -67,30 +122,45 @@ function openPreview(p: porter.ImportPreview, from: 'file' | 'url', source: stri
previewOpen.value = true
}

function handleValidateFile() {
async function handleValidateFile() {
if (validating.value) return

if (!importInput.value.filePath) {
toast.add({ title: t('errPathNotFound'), color: 'warning' })
return
}

ValidateZip(importInput.value.filePath)
.then(p => openPreview(p, 'file', importInput.value.filePath))
.catch(err => toast.add({ title: decodeError(err, t), color: 'error' }))
validating.value = true
try {
const p = await progressModal.value?.validate(importInput.value.filePath)
if (p) {
openPreview(p, 'file', importInput.value.filePath)
}
} catch {
// error already handled in modal
} finally {
validating.value = false
}
}

async function handleDownloadUrl() {
if (validating.value) return

if (!importInput.value.url) {
toast.add({ title: t('errUnsupportedUrlProtocol'), color: 'warning' })
return
}

validating.value = true
try {
const p = await progressModal.value?.download(importInput.value.url)
if (p) {
openPreview(p, 'url', '')
}
} catch {
// error already handled in modal
} finally {
validating.value = false
}
}

Expand Down Expand Up @@ -262,6 +332,7 @@ function handleImport() {
type="button"
color="secondary"
class="mt-3 w-28 justify-center"
:disabled="validating"
@click="importInput.from == 'file' ? handleValidateFile() : handleDownloadUrl()"
>
{{ importInput.from == 'file' ? $t('labelValidate') : $t('labelUpdate') }}
Expand Down Expand Up @@ -296,20 +367,38 @@ function handleImport() {
:class="preview.hasData ? 'text-green-500' : 'text-gray-400'"
/>

<span>
{{ $t('labelData') }}
<template v-if="preview.hasData && (preview.hasDatabase || preview.hasDrivers)">
—
<template v-if="preview.hasDatabase">{{ $t('labelDatabase') }}</template>
<div class="min-w-0 grow">
<span>
{{ $t('labelData') }}
<template v-if="preview.hasData && (preview.hasDatabase || preview.hasDrivers)">
—
<template v-if="preview.hasDatabase">{{ $t('labelDatabase') }}</template>

<template v-if="preview.hasDatabase && preview.hasDrivers">, </template>
<template v-if="preview.hasDatabase && preview.hasDrivers">, </template>

<template v-if="preview.hasDrivers">
{{ $t('labelDrivers') }} ({{ preview.driverCount }} files,
{{ formatBytes(preview.driverSize) }})
<template v-if="preview.hasDrivers">
{{ $t('labelDrivers') }} ({{ preview.driverCount }} files,
{{ formatBytes(preview.driverSize) }})
</template>
</template>
</template>
</span>
</span>

<div
v-if="diffRows.data"
class="mt-0.5 flex flex-wrap items-center gap-x-1.5 text-xs"
>
<span
class="rounded px-1.5 py-0.5 whitespace-nowrap"
:class="diffStateMeta[diffRows.data.state].badgeClass"
>
{{ $t(diffStateMeta[diffRows.data.state].labelKey) }}
</span>

<span v-if="diffRows.data.facts" class="text-gray-500">
{{ diffRows.data.facts }}
</span>
</div>
</div>
</li>

<li class="flex items-center gap-x-2">
Expand All @@ -318,7 +407,25 @@ function handleImport() {
:class="preview.hasSettings ? 'text-green-500' : 'text-gray-400'"
/>

<span>{{ $t('labelSettings') }}</span>
<div class="min-w-0 grow">
<span>{{ $t('labelSettings') }}</span>

<div
v-if="diffRows.settings"
class="mt-0.5 flex flex-wrap items-center gap-x-1.5 text-xs"
>
<span
class="rounded px-1.5 py-0.5 whitespace-nowrap"
:class="diffStateMeta[diffRows.settings.state].badgeClass"
>
{{ $t(diffStateMeta[diffRows.settings.state].labelKey) }}
</span>

<span v-if="diffRows.settings.facts" class="text-gray-500">
{{ diffRows.settings.facts }}
</span>
</div>
</div>
</li>
</ul>
</div>
Expand Down
Loading
Loading