|
| 1 | +<!-- apps/pythinker-web/src/components/UpdateToast.vue --> |
| 2 | +<!-- Desktop-only prompt: a new version is ready (or waiting to download). |
| 3 | + Unlike WarningToasts this never auto-dismisses — it asks a question. --> |
| 4 | +<script setup lang="ts"> |
| 5 | +import { computed, onMounted, onUnmounted, ref } from 'vue'; |
| 6 | +import { useI18n } from 'vue-i18n'; |
| 7 | +
|
| 8 | +const { t } = useI18n(); |
| 9 | +
|
| 10 | +const bridge = typeof window !== 'undefined' ? window.pythinkerDesktop : undefined; |
| 11 | +const state = ref<DesktopUpdateState>(); |
| 12 | +const busy = ref(false); |
| 13 | +/** Versions the user skipped. Persisted per version so the prompt does not |
| 14 | + return for the same build; an unnamed version is skipped for the session. */ |
| 15 | +const skipped = ref<string[]>(readSkipped()); |
| 16 | +const SKIP_KEY = 'pythinker.update.skipped'; |
| 17 | +
|
| 18 | +let removeListener: (() => void) | undefined; |
| 19 | +
|
| 20 | +function readSkipped(): string[] { |
| 21 | + try { |
| 22 | + const raw: unknown = JSON.parse(localStorage.getItem(SKIP_KEY) ?? '[]'); |
| 23 | + return Array.isArray(raw) ? raw.filter((v): v is string => typeof v === 'string') : []; |
| 24 | + } catch { |
| 25 | + return []; |
| 26 | + } |
| 27 | +} |
| 28 | +
|
| 29 | +const visible = computed(() => { |
| 30 | + const current = state.value; |
| 31 | + if (current === undefined) return false; |
| 32 | + if (current.status !== 'downloaded' && !(current.status === 'available' && !current.autoUpdate)) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + return !skipped.value.includes(current.version ?? ''); |
| 36 | +}); |
| 37 | +
|
| 38 | +const title = computed(() => |
| 39 | + state.value?.version |
| 40 | + ? t('update.availableVersion', { version: state.value.version }) |
| 41 | + : t('update.available'), |
| 42 | +); |
| 43 | +
|
| 44 | +const primaryLabel = computed(() => |
| 45 | + state.value?.status === 'downloaded' ? t('settings.desktop.restartToUpdate') : t('update.download'), |
| 46 | +); |
| 47 | +
|
| 48 | +async function primary(): Promise<void> { |
| 49 | + if (bridge === undefined || busy.value) return; |
| 50 | + busy.value = true; |
| 51 | + try { |
| 52 | + state.value = |
| 53 | + state.value?.status === 'downloaded' ? await bridge.quitAndInstall() : await bridge.checkForUpdates(); |
| 54 | + } finally { |
| 55 | + busy.value = false; |
| 56 | + } |
| 57 | +} |
| 58 | +
|
| 59 | +function skip(): void { |
| 60 | + const next = [...skipped.value, state.value?.version ?? '']; |
| 61 | + skipped.value = next; |
| 62 | + try { |
| 63 | + localStorage.setItem(SKIP_KEY, JSON.stringify(next.filter((v) => v !== ''))); |
| 64 | + } catch { |
| 65 | + // A blocked localStorage still skips for this session. |
| 66 | + } |
| 67 | +} |
| 68 | +
|
| 69 | +onMounted(() => { |
| 70 | + // Dev-only preview: the real updater stays disabled outside packaged builds, |
| 71 | + // so `?updateDemo=1` on the Vite dev server is the only way to see this. |
| 72 | + if (import.meta.env.DEV && new URLSearchParams(window.location.search).has('updateDemo')) { |
| 73 | + state.value = { status: 'downloaded', version: '0.0.0-dev', autoUpdate: true }; |
| 74 | + return; |
| 75 | + } |
| 76 | + if (bridge === undefined) return; |
| 77 | + removeListener = bridge.onUpdateState((next) => { |
| 78 | + state.value = next; |
| 79 | + }); |
| 80 | + void bridge.getUpdateState().then((next) => { |
| 81 | + state.value = next; |
| 82 | + }, () => { |
| 83 | + // A failed initial read leaves the prompt hidden until the next event. |
| 84 | + }); |
| 85 | +}); |
| 86 | +
|
| 87 | +onUnmounted(() => { |
| 88 | + removeListener?.(); |
| 89 | +}); |
| 90 | +</script> |
| 91 | + |
| 92 | +<template> |
| 93 | + <div v-if="visible" class="update-toast" role="status" aria-live="polite"> |
| 94 | + <div class="body"> |
| 95 | + <div class="title">{{ title }}</div> |
| 96 | + <div class="msg">{{ t('update.prompt') }}</div> |
| 97 | + </div> |
| 98 | + <div class="acts"> |
| 99 | + <button type="button" class="skip" @click="skip">{{ t('update.skip') }}</button> |
| 100 | + <button type="button" class="go" :disabled="busy" @click="void primary()">{{ primaryLabel }}</button> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | +</template> |
| 104 | + |
| 105 | +<style scoped> |
| 106 | +.update-toast { |
| 107 | + position: fixed; |
| 108 | + right: 16px; |
| 109 | + /* Sits above the WarningToasts stack (bottom: 84px) so the two never overlap. */ |
| 110 | + bottom: 152px; |
| 111 | + z-index: 61; |
| 112 | + width: min(360px, calc(100vw - 32px)); |
| 113 | + display: flex; |
| 114 | + flex-direction: column; |
| 115 | + gap: 10px; |
| 116 | + padding: 12px 13px; |
| 117 | + border: 1px solid var(--line); |
| 118 | + border-radius: 8px; |
| 119 | + background: var(--panel); |
| 120 | + box-shadow: 0 6px 22px rgba(0, 0, 0, 0.12); |
| 121 | + font-size: var(--ui-font-size); |
| 122 | + line-height: 1.45; |
| 123 | +} |
| 124 | +.title { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; } |
| 125 | +.msg { margin-top: 2px; color: var(--muted); } |
| 126 | +.acts { display: flex; justify-content: flex-end; gap: 8px; } |
| 127 | +.skip, |
| 128 | +.go { |
| 129 | + padding: 5px 12px; |
| 130 | + border: 1px solid var(--line); |
| 131 | + border-radius: 8px; |
| 132 | + background: var(--bg); |
| 133 | + color: var(--muted); |
| 134 | + font: inherit; |
| 135 | + font-size: var(--ui-font-size-xs); |
| 136 | + cursor: pointer; |
| 137 | +} |
| 138 | +.skip:hover { color: var(--ink); } |
| 139 | +.go { border-color: transparent; background: var(--blue); color: #fff; font-weight: 600; } |
| 140 | +.go:disabled { opacity: 0.6; cursor: default; } |
| 141 | +
|
| 142 | +@media (max-width: 640px) { |
| 143 | + .update-toast { |
| 144 | + left: 12px; |
| 145 | + right: 12px; |
| 146 | + bottom: calc(150px + env(safe-area-inset-bottom)); |
| 147 | + width: auto; |
| 148 | + } |
| 149 | +} |
| 150 | +</style> |
0 commit comments