|
| 1 | +import { createSignal, onMount, Show } from "solid-js"; |
| 2 | +import * as Tauri from "../tauri/constants"; |
| 3 | +import Tooltip from "./tooltip"; |
| 4 | + |
| 5 | +const module = Tauri.DESKTOP ? await import("../tauri/update") : undefined; |
| 6 | + |
| 7 | +type Status = |
| 8 | + | { |
| 9 | + type: "available"; |
| 10 | + version: string; |
| 11 | + } |
| 12 | + | { |
| 13 | + type: "downloading"; |
| 14 | + version: string; |
| 15 | + size?: number; |
| 16 | + progress: number; |
| 17 | + } |
| 18 | + | { |
| 19 | + type: "downloaded"; |
| 20 | + version: string; |
| 21 | + size: number; |
| 22 | + } |
| 23 | + | { |
| 24 | + type: "installing"; |
| 25 | + version: string; |
| 26 | + } |
| 27 | + | { |
| 28 | + type: "error"; |
| 29 | + error: string; |
| 30 | + }; |
| 31 | + |
| 32 | +export default function UpdaterIcon() { |
| 33 | + const [status, setStatus] = createSignal<Status | undefined>(); |
| 34 | + |
| 35 | + onMount(async () => { |
| 36 | + // Import the update module |
| 37 | + const updater = (await module)?.update; |
| 38 | + if (!updater) return; |
| 39 | + |
| 40 | + // Subscribe to status changes |
| 41 | + const dispose = updater.status.subscribe((newStatus) => { |
| 42 | + setStatus(newStatus); |
| 43 | + }); |
| 44 | + |
| 45 | + return () => dispose(); |
| 46 | + }); |
| 47 | + |
| 48 | + const handleClick = async () => { |
| 49 | + const s = status(); |
| 50 | + if (!s) return; |
| 51 | + |
| 52 | + const module = await import("../tauri/update"); |
| 53 | + |
| 54 | + if (s.type === "available") { |
| 55 | + module.update.download(); |
| 56 | + } else if (s.type === "downloaded") { |
| 57 | + module.update.install(); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const tooltipContent = () => { |
| 62 | + const s = status(); |
| 63 | + if (!s) return ""; |
| 64 | + |
| 65 | + if (s.type === "error") return `Error: ${s.error}`; |
| 66 | + if (s.type === "available") return `Update ${s.version} available`; |
| 67 | + if (s.type === "downloading") return `Downloading ${s.version}...`; |
| 68 | + if (s.type === "downloaded") return `Install ${s.version} now`; |
| 69 | + if (s.type === "installing") return `Installing ${s.version}...`; |
| 70 | + return ""; |
| 71 | + }; |
| 72 | + |
| 73 | + const progressPercent = () => { |
| 74 | + const s = status(); |
| 75 | + if (!s || s.type !== "downloading" || !s.size) return 0; |
| 76 | + return (s.progress / s.size) * 100; |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <Show when={status()}> |
| 81 | + <Tooltip content={tooltipContent()} position="bottom"> |
| 82 | + <button |
| 83 | + type="button" |
| 84 | + onClick={handleClick} |
| 85 | + class="p-2 text-white hover:text-gray-300 hover:bg-gray-700 rounded-lg transition-all cursor-pointer relative" |
| 86 | + classList={{ |
| 87 | + "text-red-500": status()?.type === "error", |
| 88 | + "text-blue-500": status()?.type === "available" || status()?.type === "downloading", |
| 89 | + "text-green-500": status()?.type === "downloaded", |
| 90 | + "text-yellow-500": status()?.type === "installing", |
| 91 | + }} |
| 92 | + > |
| 93 | + <Show when={status()?.type === "error"}> |
| 94 | + <span class="icon-[mdi--alert-circle]" /> |
| 95 | + </Show> |
| 96 | + <Show when={status()?.type === "available"}> |
| 97 | + <span class="icon-[mdi--download]" /> |
| 98 | + </Show> |
| 99 | + <Show when={status()?.type === "downloading"}> |
| 100 | + <svg class="w-6 h-6" viewBox="0 0 24 24" aria-label="Downloading update"> |
| 101 | + <title>Downloading update</title> |
| 102 | + <circle |
| 103 | + cx="12" |
| 104 | + cy="12" |
| 105 | + r="10" |
| 106 | + stroke="currentColor" |
| 107 | + stroke-width="2" |
| 108 | + fill="none" |
| 109 | + opacity="0.2" |
| 110 | + /> |
| 111 | + <circle |
| 112 | + cx="12" |
| 113 | + cy="12" |
| 114 | + r="10" |
| 115 | + stroke="currentColor" |
| 116 | + stroke-width="2" |
| 117 | + fill="none" |
| 118 | + stroke-dasharray={`${2 * Math.PI * 10}`} |
| 119 | + stroke-dashoffset={`${2 * Math.PI * 10 * (1 - progressPercent() / 100)}`} |
| 120 | + stroke-linecap="round" |
| 121 | + transform="rotate(-90 12 12)" |
| 122 | + /> |
| 123 | + <path |
| 124 | + fill="currentColor" |
| 125 | + d="M12 6v8l4 2" |
| 126 | + stroke="currentColor" |
| 127 | + stroke-width="1.5" |
| 128 | + stroke-linecap="round" |
| 129 | + /> |
| 130 | + </svg> |
| 131 | + </Show> |
| 132 | + <Show when={status()?.type === "downloaded"}> |
| 133 | + <span class="icon-[mdi--download-circle]" /> |
| 134 | + </Show> |
| 135 | + <Show when={status()?.type === "installing"}> |
| 136 | + <span class="icon-[mdi--loading] animate-spin" /> |
| 137 | + </Show> |
| 138 | + </button> |
| 139 | + </Tooltip> |
| 140 | + </Show> |
| 141 | + ); |
| 142 | +} |
0 commit comments