useUpdater

Hook for auto-update functionality

Overview

useUpdater provides complete control over the auto-update process.

Import

import { useUpdater } from '@linch-tech/desktop-core';

Basic Usage

import { useUpdater } from '@linch-tech/desktop-core';

function UpdateChecker() {
  const { status, updateInfo, progress, error, check, download, install } = useUpdater();

  return (
    <div>
      {status === 'idle' && (
        <button onClick={check}>Check for Updates</button>
      )}

      {status === 'checking' && <p>Checking...</p>}

      {status === 'available' && (
        <div>
          <p>New version available: {updateInfo?.version}</p>
          <button onClick={download}>Download Update</button>
        </div>
      )}

      {status === 'downloading' && (
        <div>
          <p>Downloading: {progress?.percent}%</p>
          <progress value={progress?.percent} max={100} />
        </div>
      )}

      {status === 'ready' && (
        <button onClick={install}>Restart Now</button>
      )}

      {status === 'up-to-date' && <p>Up to date</p>}

      {error && <p className="text-red-500">{error.message}</p>}
    </div>
  );
}

Return Value

interface UseUpdaterReturn {
  enabled: boolean;             // Whether update is enabled
  status: UpdateStatus;         // Current status
  updateInfo: UpdateInfo | null; // Update info
  progress: UpdateProgress | null; // Download progress
  error: Error | null;          // Error info
  check: () => Promise<UpdateInfo>; // Check for updates
  download: () => Promise<void>;    // Download update
  install: () => Promise<void>;     // Install and restart
}

Parameters

useUpdater(options?: { enabled?: boolean })

If enabled is false, all operations will return a disabled error.

Status Types

type UpdateStatus =
  | 'idle'           // Initial state
  | 'checking'       // Checking for updates
  | 'available'      // Update available
  | 'up-to-date'     // Already up to date
  | 'downloading'    // Downloading
  | 'ready'          // Ready to install
  | 'check-error'    // Check failed
  | 'download-error' // Download failed

Update Info

interface UpdateInfo {
  available: boolean;  // Whether update is available
  version?: string;    // New version number
  notes?: string;      // Release notes
  date?: string;       // Release date
}

Download Progress

interface UpdateProgress {
  downloaded: number;  // Downloaded bytes
  total: number;       // Total bytes
  percent: number;     // Progress percentage (0-100)
}

Auto-check on Startup

import { useEffect } from 'react';
import { useUpdater } from '@linch-tech/desktop-core';

function App() {
  const { check } = useUpdater();

  useEffect(() => {
    // Check for updates 2 seconds after startup
    const timer = setTimeout(() => {
      check().catch(console.error);
    }, 2000);

    return () => clearTimeout(timer);
  }, [check]);

  return <MainApp />;
}

Using with SettingsPage

SettingsPage component has built-in update UI, no extra code needed:

import { SettingsPage } from '@linch-tech/desktop-core';

function Settings() {
  return <SettingsPage />;
}

Configuration Requirements

For auto-update to work, you need:

  1. features.updater to be true (default)
  2. Configure updater in tauri.conf.json
{
  "plugins": {
    "updater": {
      "endpoints": [
        "https://your-server.com/updates/{{target}}/{{arch}}/{{current_version}}"
      ],
      "pubkey": "your-public-key"
    }
  }
}

Notes

Update functionality only works in production builds. In development mode, check() will return "up-to-date".

install() will restart the app. Make sure to save all user data before calling.