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
139 changes: 139 additions & 0 deletions src/lib/components/GateUpdateCheck.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<script>
/**
* "Check for updates" for the screens that stand in front of the app (sign
* in, trial over). Those screens replace the whole shell, update dialog
* included, so a build with a sign-in bug had no way to fetch its own fix.
*
* One quiet line in the footer: the version, and a button whose label is
* the state. A found update installs and restarts from here. Every state
* change is read out through one polite live region.
*/
import { onMount } from 'svelte'
import Icon from './Icon.svelte'
import { cn } from '$lib/utils.js'
import { updateCheck, updateDownload, updateRestart, updateStatus } from '$lib/api.js'

/** @type {'idle'|'checking'|'current'|'available'|'downloading'|'ready'|'error'} */
let state = $state('idle')
let version = $state('')
let next = $state('')
let progress = $state(0)
let error = $state('')

onMount(() => {
import('@tauri-apps/api/app').then((m) => m.getVersion()).then((v) => (version = v)).catch(() => {})
// A download from an earlier run is still waiting: offer the restart.
updateStatus().then((info) => { if (info) { next = info.version; state = 'ready' } }).catch(() => {})
})

/** "2.17.3" from "2.17.3-nightly.202611170418". @param {string} v */
const short = (v) => v.replace(/-nightly\.\d+$/, '')

/** @type {ReturnType<typeof setTimeout> | undefined} */
let settle
async function check() {
if (state === 'checking' || state === 'downloading') return
clearTimeout(settle)
state = 'checking'
error = ''
try {
const info = await updateCheck()
if (info?.staged) { next = info.version; state = 'ready' }
else if (info) { next = info.version; state = 'available' }
else {
state = 'current'
settle = setTimeout(() => { if (state === 'current') state = 'idle' }, 6000)
}
} catch (e) {
error = e instanceof Error ? e.message : String(e)
state = 'error'
}
}

async function install() {
state = 'downloading'
progress = 0
let total = 0
try {
await updateDownload((event) => {
if (event.event === 'started') total = event.data.contentLength ?? 0
else if (event.event === 'progress' && total > 0) progress = Math.round((event.data.downloaded / total) * 100)
else if (event.event === 'finished') progress = 100
})
state = 'ready'
await restart()
} catch (e) {
error = e instanceof Error ? e.message : String(e)
state = 'error'
}
}

async function restart() {
try {
await updateRestart()
} catch (e) {
error = e instanceof Error ? e.message : String(e)
state = 'error'
}
}

/** What the live region reads out. */
const announce = $derived(
state === 'checking' ? 'Checking for updates'
: state === 'current' ? `Stroke is up to date${version ? `, version ${short(version)}` : ''}`
: state === 'available' ? `Stroke ${short(next)} is available`
: state === 'downloading' ? `Downloading Stroke ${short(next)}`
: state === 'ready' ? `Stroke ${short(next)} is ready to install`
: state === 'error' ? `Couldn't update: ${error}`
: '',
)

const link = 'inline-flex h-7 items-center gap-1.5 rounded-md px-1.5 transition-colors hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring disabled:pointer-events-none'
</script>

<div class="flex items-center gap-1">
{#if version && (state === 'idle' || state === 'checking' || state === 'current')}
<span class="tabular-nums">v{short(version)}</span>
<span aria-hidden="true" class="px-1">·</span>
{/if}

{#if state === 'idle'}
<button type="button" class={link} onclick={check}>Check for updates</button>
{:else if state === 'checking'}
<button type="button" class={link} disabled aria-disabled="true">
<Icon name="loader-2" class="size-3 shrink-0 animate-spin" />Checking
</button>
{:else if state === 'current'}
<button type="button" class={link} onclick={check}>
<Icon name="check" class="size-3 shrink-0 text-success" />Up to date
</button>
{:else if state === 'available'}
<span class="text-foreground">Stroke {short(next)} is available</span>
<button type="button" class={cn(link, 'font-medium text-foreground underline-offset-4 hover:underline')} onclick={install}>Update and restart</button>
{:else if state === 'downloading'}
<span class="inline-flex h-7 items-center gap-2">
Downloading {short(next)}
<span
role="progressbar"
aria-label="Download progress"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow={progress}
class="relative h-1 w-20 overflow-hidden rounded-full bg-muted"
>
<span class="absolute inset-y-0 left-0 rounded-full bg-foreground/70 transition-[width] duration-200" style:width="{progress}%"></span>
</span>
<span class="tabular-nums">{progress}%</span>
</span>
{:else if state === 'ready'}
<span class="text-foreground">Stroke {short(next)} is ready</span>
<button type="button" class={cn(link, 'font-medium text-foreground underline-offset-4 hover:underline')} onclick={restart}>Restart to install</button>
{:else if state === 'error'}
<span class="inline-flex items-center gap-1.5" title={error}>
<Icon name="alert-circle" class="size-3 shrink-0 text-destructive" />Couldn't update
</span>
<button type="button" class={link} onclick={check}>Retry</button>
{/if}

<span class="sr-only" role="status" aria-live="polite">{announce}</span>
</div>
5 changes: 4 additions & 1 deletion src/lib/components/Onboarding.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@
<svelte:window onkeydown={onWindowKeydown} />

{#if open}
<!-- z-[45]: over the app, under the menus, dialogs and popovers (z-50 and
up). At z-[200] the title bar's menus (workspace, account, File...)
opened behind the tour and looked like they did nothing. -->
<div
class="fixed inset-x-0 bottom-0 z-[200] flex flex-col bg-background"
class="fixed inset-x-0 bottom-0 z-[45] flex flex-col bg-background"
style={isMac ? 'top: 0;' : 'top: var(--app-titlebar-h, 38px);'}
transition:fade={{ duration: 160 }}
>
Expand Down
5 changes: 5 additions & 0 deletions src/lib/components/SignInScreen.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { detectOs } from '$lib/platform.js'
import { cn } from '$lib/utils.js'
import ArrowLeft from '@lucide/svelte/icons/arrow-left'
import GateUpdateCheck from './GateUpdateCheck.svelte'

/** Called once signed in (or a key was activated); the gate hands over to the app. */
let { ondone = () => {} } = $props()
Expand Down Expand Up @@ -103,5 +104,9 @@
<a href="https://stroke.click/terms" target="_blank" rel="noopener noreferrer" class="transition-colors hover:text-foreground">Terms</a>
<span aria-hidden="true">·</span>
<a href="https://stroke.click/privacy" target="_blank" rel="noopener noreferrer" class="transition-colors hover:text-foreground">Privacy</a>
<span aria-hidden="true">·</span>
<!-- This screen replaces the app, update dialog included: without this a
build that can't get past sign-in could never fetch its own fix. -->
<GateUpdateCheck />
</div>
</div>
6 changes: 6 additions & 0 deletions src/lib/components/TrialExpiredScreen.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import LicenseActivation from './LicenseActivation.svelte'
import AccountSignInButtons from './AccountSignInButtons.svelte'
import WindowControls from './WindowControls.svelte'
import GateUpdateCheck from './GateUpdateCheck.svelte'
import { Button } from '$lib/components/ui/button/index.js'
import { licenseStatus, isBlocked } from '$lib/stores/license.js'
import { account, isSignedIn, signInState, refreshAccount, signOut } from '$lib/stores/account.js'
Expand Down Expand Up @@ -205,4 +206,9 @@
{/if}
</div>
</div>
<!-- The app (and its update dialog) is not rendered behind this screen, so
updates are checked from here. -->
<div class="flex h-10 shrink-0 items-center justify-center text-ui-2xs text-muted-foreground/80">
<GateUpdateCheck />
</div>
</div>
Loading