import { createPortal } from 'react-dom' import { useState } from 'react' import { useQuery, useQueryClient } from '@tanstack/react-query' import { api } from '../api' /** * Startup update check against GitHub Releases. * * Compares the running version (`__APP_VERSION__`, baked from package.json at * build time) with the latest release's tag (`v-b` — only the * semver part counts, so a new build of the same version is NOT an update). * Failures are silent: an update check must never nag about itself. * "Skip this version" persists `skippedUpdateVersion` to userprefs.json via * the prefs API, keyed by the semver part, so a later version re-triggers. */ const REPO = 'MrPatben8/konduktor' interface GithubRelease { tag_name: string html_url: string body: string | null } async function fetchLatestRelease(): Promise { const res = await fetch(`https://api.github.com/repos/${REPO}/releases/latest`, { headers: { Accept: 'application/vnd.github+json' }, }) if (!res.ok) throw new Error(`GitHub responded ${res.status}`) return res.json() } /** The semver triple at the start of a version/tag string ("v0.1.2-b7" → [0,1,2]). */ function parseVersion(s: string): [number, number, number] | null { const m = /^v?(\d+)\.(\d+)\.(\d+)/.exec(s.trim()) return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null } function isNewer(a: [number, number, number], b: [number, number, number]): boolean { for (let i = 0; i < 3; i++) { if (a[i] !== b[i]) return a[i] > b[i] } return false } /** * The "What's Changed" bullet lines from an auto-generated release body * (section ends at the next heading or the Full Changelog link). If the * section is missing (hand-written notes), fall back to the whole body. * The auto-generated "by @user in " credit suffix is stripped. */ function whatsChanged(body: string | null): string[] { if (!body) return [] const lines = body.split(/\r?\n/) const start = lines.findIndex((l) => /^#{2,4}\s+what'?s changed/i.test(l.trim())) const out: string[] = [] for (const raw of lines.slice(start + 1)) { const line = raw.trim() if (start !== -1 && (/^#{1,4}\s/.test(line) || line.startsWith('**Full Changelog**'))) break if (line) { out.push( line.replace(/^[*-]\s+/, '').replace(/\s+by\s+@[\w-]+\s+in\s+https?:\/\/\S+$/i, ''), ) } } return out } /** Open a URL in the system browser (Tauri shell-open in the desktop app). */ async function openExternal(url: string) { if ('__TAURI_INTERNALS__' in window) { try { const { open } = await import('@tauri-apps/plugin-shell') await open(url) return } catch { // fall through to window.open } } window.open(url, '_blank', 'noopener') } /** Render a changelog line, turning bare URLs into links (PR URLs as #N). */ function ChangelogLine({ text }: { text: string }) { const parts = text.split(/(https?:\/\/\S+)/g) return ( <> {parts.map((part, i) => { if (!/^https?:\/\//.test(part)) return {part} const pr = /\/pull\/(\d+)/.exec(part) return ( { e.preventDefault() openExternal(part) }} className="text-accent hover:underline" > {pr ? `#${pr[1]}` : part} ) })} ) } export function UpdateCheck() { const qc = useQueryClient() const [dismissed, setDismissed] = useState(false) const [skipVersion, setSkipVersion] = useState(false) const release = useQuery({ queryKey: ['latestRelease'], queryFn: fetchLatestRelease, staleTime: Infinity, retry: false, }) // Shares App's ['prefs'] cache entry. const prefs = useQuery({ queryKey: ['prefs'], queryFn: api.getPrefs }) const latest = release.data const latestVer = latest ? parseVersion(latest.tag_name) : null const currentVer = parseVersion(__APP_VERSION__) const latestSemver = latestVer?.join('.') const updateAvailable = !!(latestVer && currentVer && isNewer(latestVer, currentVer)) const skipped = !!latestSemver && prefs.data?.skippedUpdateVersion === latestSemver // Wait for prefs before showing so a skipped version never flashes the dialog. if (dismissed || !updateAvailable || !latest || !prefs.isSuccess || skipped) return null const close = () => { if (skipVersion && latestSemver) { api .patchPrefs({ skippedUpdateVersion: latestSemver }) .then((p) => qc.setQueryData(['prefs'], p)) .catch(() => {}) } setDismissed(true) } const download = () => { openExternal(latest.html_url) setDismissed(true) } const changes = whatsChanged(latest.body) return createPortal(
e.stopPropagation()} >
Update available
Konduktor {latest.tag_name} is out — you have v {__APP_VERSION__}.
What's changed
{changes.length ? (
    {changes.map((line, i) => (
  • •
  • ))}
) : (
No release notes.
)}
, document.body, ) }