|
| 1 | +<template> |
| 2 | + <InteractionDialog |
| 3 | + v-model="showUpdateDialog" |
| 4 | + :title="dialogTitle" |
| 5 | + :message="dialogMessage" |
| 6 | + :variant="dialogVariant" |
| 7 | + :actions="dialogActions" |
| 8 | + max-width="560" |
| 9 | + > |
| 10 | + <template #content> |
| 11 | + <div v-if="updateInfo" class="mt-2"> |
| 12 | + <strong>Update Details:</strong> |
| 13 | + <p>Current Version: {{ app_version.version }}</p> |
| 14 | + <p>New Version: {{ updateInfo.version }}</p> |
| 15 | + <p>Release Date: {{ formatDate(updateInfo.releaseDate) }}</p> |
| 16 | + </div> |
| 17 | + <v-progress-linear |
| 18 | + v-if="showProgress" |
| 19 | + :model-value="downloadProgress" |
| 20 | + color="primary" |
| 21 | + height="25" |
| 22 | + rounded |
| 23 | + class="my-4" |
| 24 | + > |
| 25 | + <template #default> |
| 26 | + <strong>{{ Math.round(downloadProgress) }}%</strong> |
| 27 | + </template> |
| 28 | + </v-progress-linear> |
| 29 | + </template> |
| 30 | + </InteractionDialog> |
| 31 | +</template> |
| 32 | + |
| 33 | +<script setup lang="ts"> |
| 34 | +import { useStorage } from '@vueuse/core' |
| 35 | +import { onBeforeMount, ref } from 'vue' |
| 36 | +
|
| 37 | +import InteractionDialog, { type Action } from '@/components/InteractionDialog.vue' |
| 38 | +import { app_version } from '@/libs/cosmos' |
| 39 | +import { isElectron } from '@/libs/utils' |
| 40 | +
|
| 41 | +const showUpdateDialog = ref(false) |
| 42 | +const dialogTitle = ref('') |
| 43 | +const dialogMessage = ref('') |
| 44 | +const dialogVariant = ref<'error' | 'info' | 'success' | 'warning' | 'text-only'>('info') |
| 45 | +const showProgress = ref(false) |
| 46 | +const downloadProgress = ref(0) |
| 47 | +const dialogActions = ref<Action[]>([]) |
| 48 | +const updateInfo = ref({ |
| 49 | + version: '', |
| 50 | + releaseDate: '', |
| 51 | + releaseNotes: '', |
| 52 | +}) |
| 53 | +const ignoredUpdateVersions = useStorage<string[]>('cockpit-ignored-update-versions', []) |
| 54 | +
|
| 55 | +const formatDate = (date: string): string => { |
| 56 | + return new Date(date).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }) |
| 57 | +} |
| 58 | +
|
| 59 | +onBeforeMount(() => { |
| 60 | + if (!isElectron()) { |
| 61 | + console.info('Not in Electron environment. UpdateNotification will not be initialized.') |
| 62 | + return |
| 63 | + } |
| 64 | +
|
| 65 | + if (!window.electronAPI) { |
| 66 | + console.error('window.electronAPI is not defined. UpdateNotification will not be initialized.') |
| 67 | + return |
| 68 | + } |
| 69 | +
|
| 70 | + // Listen for update events |
| 71 | + window.electronAPI.onCheckingForUpdate(() => { |
| 72 | + console.log('Checking if there are updates for the Electron app...') |
| 73 | + dialogTitle.value = 'Checking for Updates' |
| 74 | + dialogMessage.value = 'Looking for new versions of the application...' |
| 75 | + dialogVariant.value = 'info' |
| 76 | + dialogActions.value = [] |
| 77 | + showProgress.value = false |
| 78 | + showUpdateDialog.value = true |
| 79 | + }) |
| 80 | +
|
| 81 | + window.electronAPI.onUpdateNotAvailable(() => { |
| 82 | + console.log('No updates available for the Electron app.') |
| 83 | + dialogTitle.value = 'No Updates Available' |
| 84 | + dialogMessage.value = 'You are running the latest version of the application.' |
| 85 | + dialogVariant.value = 'success' |
| 86 | + dialogActions.value = [ |
| 87 | + { |
| 88 | + text: 'OK', |
| 89 | + action: () => { |
| 90 | + showUpdateDialog.value = false |
| 91 | + }, |
| 92 | + }, |
| 93 | + ] |
| 94 | + showProgress.value = false |
| 95 | + }) |
| 96 | +
|
| 97 | + window.electronAPI.onUpdateAvailable((info) => { |
| 98 | + console.log('Update available for the Electron app.', info) |
| 99 | + dialogTitle.value = 'Update Available' |
| 100 | + dialogMessage.value = 'A new version of the application is available. Would you like to download it now?' |
| 101 | + dialogVariant.value = 'info' |
| 102 | + updateInfo.value = { ...info } |
| 103 | + dialogActions.value = [ |
| 104 | + { |
| 105 | + text: 'Ignore This Version', |
| 106 | + action: () => { |
| 107 | + console.log(`User chose to ignore version ${updateInfo.value.version}`) |
| 108 | + ignoredUpdateVersions.value.push(updateInfo.value.version) |
| 109 | + window.electronAPI!.cancelUpdate() |
| 110 | + showUpdateDialog.value = false |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + text: 'Download', |
| 115 | + action: () => { |
| 116 | + window.electronAPI!.downloadUpdate() |
| 117 | + showProgress.value = true |
| 118 | + dialogActions.value = [ |
| 119 | + { |
| 120 | + text: 'Cancel', |
| 121 | + action: () => { |
| 122 | + console.log('User chose to cancel the update for the Electron app.') |
| 123 | + window.electronAPI!.cancelUpdate() |
| 124 | + showUpdateDialog.value = false |
| 125 | + dialogMessage.value = 'Downloading update...' |
| 126 | + }, |
| 127 | + }, |
| 128 | + ] |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + text: 'Not Now', |
| 133 | + action: () => { |
| 134 | + window.electronAPI!.cancelUpdate() |
| 135 | + showUpdateDialog.value = false |
| 136 | + }, |
| 137 | + }, |
| 138 | + ] |
| 139 | +
|
| 140 | + // Check if this version is in the ignored list |
| 141 | + if (ignoredUpdateVersions.value.includes(info.version)) { |
| 142 | + console.log(`Skipping ignored version ${info.version}.`) |
| 143 | + showUpdateDialog.value = false |
| 144 | + return |
| 145 | + } |
| 146 | +
|
| 147 | + showUpdateDialog.value = true |
| 148 | + }) |
| 149 | +
|
| 150 | + window.electronAPI.onDownloadProgress((progressInfo) => { |
| 151 | + downloadProgress.value = progressInfo.percent |
| 152 | + }) |
| 153 | +
|
| 154 | + window.electronAPI.onUpdateDownloaded(() => { |
| 155 | + console.log('Finished downloading the update for the Electron app.') |
| 156 | + dialogTitle.value = 'Update Ready to Install' |
| 157 | + dialogMessage.value = |
| 158 | + 'The update has been downloaded. Would you like to install it now? The application will restart during installation.' |
| 159 | + dialogVariant.value = 'info' |
| 160 | + showProgress.value = false |
| 161 | + dialogActions.value = [ |
| 162 | + { |
| 163 | + text: 'Install Now', |
| 164 | + action: () => { |
| 165 | + console.log('User chose to install the update for the Electron app now.') |
| 166 | + window.electronAPI!.installUpdate() |
| 167 | + showUpdateDialog.value = false |
| 168 | + }, |
| 169 | + }, |
| 170 | + { |
| 171 | + text: 'Later', |
| 172 | + action: () => { |
| 173 | + console.log('User chose to install the update for the Electron app later.') |
| 174 | + showUpdateDialog.value = false |
| 175 | + }, |
| 176 | + }, |
| 177 | + ] |
| 178 | + showUpdateDialog.value = true |
| 179 | + }) |
| 180 | +}) |
| 181 | +</script> |
0 commit comments