mirror of
https://wget.la/https://github.com/leookun/cursor-byok
synced 2026-10-06 13:44:21 +08:00
feat: add update indicators and stabilize Cursor streams
This commit is contained in:
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "cursor-byok-desktop",
|
||||
"version": "0.1.0-beta.2",
|
||||
"version": "0.1.0-beta.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "cursor-byok-desktop",
|
||||
"version": "0.1.0-beta.2",
|
||||
"version": "0.1.0-beta.3",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@floating-ui/dom": "^1.8.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cursor-byok-desktop",
|
||||
"version": "0.1.0-beta.2",
|
||||
"version": "0.1.0-beta.3",
|
||||
"description": "Cursor BYOK desktop management application",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "cursor-byok-desktop"
|
||||
version = "0.1.0-beta.2"
|
||||
version = "0.1.0-beta.3"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "Cursor BYOK",
|
||||
"version": "0.1.0-beta.2",
|
||||
"version": "0.1.0-beta.3",
|
||||
"identifier": "dev.cursorbyok.desktop",
|
||||
"build": {
|
||||
"beforeDevCommand": "npm run dev",
|
||||
|
||||
@@ -10,8 +10,8 @@ import { CursorSettingsPage } from "./pages/CursorSettingsPage";
|
||||
import { HomePage } from "./pages/HomePage";
|
||||
import { ProvidersPage } from "./pages/ProvidersPage";
|
||||
import { SettingsPage } from "./pages/SettingsPage";
|
||||
import { checkForUpdate, hasNativeAppLifecycle } from "./native/appLifecycle";
|
||||
import { useAppStore } from "./store/appStore";
|
||||
import { updateStore } from "./store/updateStore";
|
||||
|
||||
export function App() {
|
||||
return (
|
||||
@@ -47,11 +47,9 @@ function AppMessages() {
|
||||
}, [error, showMessage]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!hasNativeAppLifecycle()) return;
|
||||
void checkForUpdate().then(async (update) => {
|
||||
if (!update) return;
|
||||
showMessage(t("发现新版本 {version},可在设置中安装", { version: update.version }), { duration: 6_000 });
|
||||
await update.close();
|
||||
void updateStore.check().then((version) => {
|
||||
if (!version) return;
|
||||
showMessage(t("发现新版本 {version},可在设置中安装", { version }), { duration: 6_000 });
|
||||
}).catch(() => {
|
||||
// Startup checks are best-effort; manual checks in Settings report errors.
|
||||
});
|
||||
|
||||
@@ -21,3 +21,11 @@
|
||||
font-size: type.$font-size-xs;
|
||||
}
|
||||
}
|
||||
|
||||
.updateDot {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
flex: 0 0 7px;
|
||||
background: #ef4444;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import type { Update } from "@tauri-apps/plugin-updater";
|
||||
import { useEffect, useState } from "react";
|
||||
import {
|
||||
checkForUpdate,
|
||||
currentAppVersion,
|
||||
hasNativeAppLifecycle,
|
||||
installUpdate,
|
||||
readAutostart,
|
||||
writeAutostart,
|
||||
} from "../../native/appLifecycle";
|
||||
import { updateStore, useUpdateStore } from "../../store/updateStore";
|
||||
import { Button } from "../ui/Button";
|
||||
import { Switch } from "../ui/Switch";
|
||||
import { TitledCard } from "../ui/TitledCard";
|
||||
@@ -17,13 +15,10 @@ import styles from "./AppLifecycleSettingsCard.module.scss";
|
||||
export function AppLifecycleSettingsCard() {
|
||||
const message = useMessage();
|
||||
const native = hasNativeAppLifecycle();
|
||||
const updateRef = useRef<Update | null>(null);
|
||||
const { availableVersion, checking, installing } = useUpdateStore();
|
||||
const [version, setVersion] = useState("…");
|
||||
const [autostart, setAutostart] = useState(false);
|
||||
const [loadingAutostart, setLoadingAutostart] = useState(native);
|
||||
const [checking, setChecking] = useState(false);
|
||||
const [installing, setInstalling] = useState(false);
|
||||
const [availableVersion, setAvailableVersion] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let disposed = false;
|
||||
@@ -34,12 +29,7 @@ export function AppLifecycleSettingsCard() {
|
||||
.catch((cause) => message(cause instanceof Error ? cause.message : String(cause)))
|
||||
.finally(() => { if (!disposed) setLoadingAutostart(false); });
|
||||
}
|
||||
return () => {
|
||||
disposed = true;
|
||||
const update = updateRef.current;
|
||||
updateRef.current = null;
|
||||
if (update) void update.close();
|
||||
};
|
||||
return () => { disposed = true; };
|
||||
}, [message, native]);
|
||||
|
||||
const toggleAutostart = async (enabled: boolean) => {
|
||||
@@ -57,29 +47,17 @@ export function AppLifecycleSettingsCard() {
|
||||
|
||||
const checkUpdate = async () => {
|
||||
try {
|
||||
setChecking(true);
|
||||
const previous = updateRef.current;
|
||||
updateRef.current = null;
|
||||
if (previous) await previous.close();
|
||||
const update = await checkForUpdate();
|
||||
updateRef.current = update;
|
||||
setAvailableVersion(update?.version ?? null);
|
||||
message(update ? t("发现新版本 {version}", { version: update.version }) : t("当前已是最新版本"));
|
||||
const nextVersion = await updateStore.check();
|
||||
message(nextVersion ? t("发现新版本 {version}", { version: nextVersion }) : t("当前已是最新版本"));
|
||||
} catch (cause) {
|
||||
message(cause instanceof Error ? cause.message : String(cause));
|
||||
} finally {
|
||||
setChecking(false);
|
||||
}
|
||||
};
|
||||
|
||||
const updateNow = async () => {
|
||||
const update = updateRef.current;
|
||||
if (!update) return;
|
||||
try {
|
||||
setInstalling(true);
|
||||
await installUpdate(update);
|
||||
await updateStore.install();
|
||||
} catch (cause) {
|
||||
setInstalling(false);
|
||||
message(cause instanceof Error ? cause.message : String(cause));
|
||||
}
|
||||
};
|
||||
@@ -107,6 +85,7 @@ export function AppLifecycleSettingsCard() {
|
||||
{availableVersion
|
||||
? <Button size="small" variant="primary" disabled={installing} onClick={() => void updateNow()}>
|
||||
{installing ? t("安装中…") : t("下载并安装")}
|
||||
<span className={styles.updateDot} aria-hidden="true" />
|
||||
</Button>
|
||||
: <Button size="small" disabled={!native || checking} onClick={() => void checkUpdate()}>
|
||||
{checking ? t("检查中…") : t("检查更新")}
|
||||
|
||||
@@ -527,7 +527,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 109,
|
||||
"line": 87,
|
||||
"column": 27
|
||||
}
|
||||
]
|
||||
@@ -740,8 +740,8 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 67,
|
||||
"column": 24
|
||||
"line": 51,
|
||||
"column": 29
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -754,7 +754,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 105,
|
||||
"line": 83,
|
||||
"column": 13
|
||||
}
|
||||
]
|
||||
@@ -766,7 +766,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 220,
|
||||
"line": 223,
|
||||
"column": 14
|
||||
}
|
||||
]
|
||||
@@ -805,7 +805,7 @@
|
||||
},
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 221,
|
||||
"line": 224,
|
||||
"column": 19
|
||||
},
|
||||
{
|
||||
@@ -956,7 +956,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 222,
|
||||
"line": 225,
|
||||
"column": 20
|
||||
}
|
||||
]
|
||||
@@ -1243,18 +1243,6 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"4510b63fdcf7451f": {
|
||||
"source": "Cursor 助手 v0.1.0",
|
||||
"kind": "text",
|
||||
"placeholders": [],
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppHeader.tsx",
|
||||
"line": 20,
|
||||
"column": 18
|
||||
}
|
||||
]
|
||||
},
|
||||
"470049252e54de6a": {
|
||||
"source": "成功占比:{rate}",
|
||||
"kind": "template",
|
||||
@@ -1304,7 +1292,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 104,
|
||||
"line": 82,
|
||||
"column": 13
|
||||
}
|
||||
]
|
||||
@@ -1328,7 +1316,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 71,
|
||||
"line": 73,
|
||||
"column": 48
|
||||
}
|
||||
]
|
||||
@@ -1362,7 +1350,7 @@
|
||||
},
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 194,
|
||||
"line": 196,
|
||||
"column": 67
|
||||
}
|
||||
]
|
||||
@@ -1553,6 +1541,20 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"5d59857bf039cac9": {
|
||||
"source": "Cursor 助手 v{version}",
|
||||
"kind": "template",
|
||||
"placeholders": [
|
||||
"version"
|
||||
],
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppHeader.tsx",
|
||||
"line": 31,
|
||||
"column": 18
|
||||
}
|
||||
]
|
||||
},
|
||||
"5f8d556a9c47da3c": {
|
||||
"source": "已关闭开机启动",
|
||||
"kind": "text",
|
||||
@@ -1560,7 +1562,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 50,
|
||||
"line": 40,
|
||||
"column": 40
|
||||
}
|
||||
]
|
||||
@@ -1572,7 +1574,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 226,
|
||||
"line": 229,
|
||||
"column": 11
|
||||
}
|
||||
]
|
||||
@@ -1696,7 +1698,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 102,
|
||||
"line": 80,
|
||||
"column": 18
|
||||
}
|
||||
]
|
||||
@@ -1775,7 +1777,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 74,
|
||||
"line": 76,
|
||||
"column": 47
|
||||
}
|
||||
]
|
||||
@@ -1838,8 +1840,8 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 67,
|
||||
"column": 76
|
||||
"line": 51,
|
||||
"column": 78
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -1984,7 +1986,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "App.tsx",
|
||||
"line": 53,
|
||||
"line": 52,
|
||||
"column": 19
|
||||
}
|
||||
]
|
||||
@@ -2061,7 +2063,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 112,
|
||||
"line": 91,
|
||||
"column": 37
|
||||
}
|
||||
]
|
||||
@@ -2114,12 +2116,12 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 90,
|
||||
"line": 68,
|
||||
"column": 18
|
||||
},
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 96,
|
||||
"line": 74,
|
||||
"column": 16
|
||||
}
|
||||
]
|
||||
@@ -2240,7 +2242,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 228,
|
||||
"line": 231,
|
||||
"column": 16
|
||||
}
|
||||
]
|
||||
@@ -2295,7 +2297,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 69,
|
||||
"line": 71,
|
||||
"column": 39
|
||||
}
|
||||
]
|
||||
@@ -2523,7 +2525,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 87,
|
||||
"line": 65,
|
||||
"column": 29
|
||||
}
|
||||
]
|
||||
@@ -2734,7 +2736,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 50,
|
||||
"line": 40,
|
||||
"column": 25
|
||||
}
|
||||
]
|
||||
@@ -2746,7 +2748,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 109,
|
||||
"line": 87,
|
||||
"column": 39
|
||||
}
|
||||
]
|
||||
@@ -2796,12 +2798,12 @@
|
||||
},
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 241,
|
||||
"line": 244,
|
||||
"column": 64
|
||||
},
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 241,
|
||||
"line": 244,
|
||||
"column": 125
|
||||
}
|
||||
]
|
||||
@@ -3020,7 +3022,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 233,
|
||||
"line": 236,
|
||||
"column": 24
|
||||
}
|
||||
]
|
||||
@@ -3338,7 +3340,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 91,
|
||||
"line": 69,
|
||||
"column": 17
|
||||
}
|
||||
]
|
||||
@@ -3408,7 +3410,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 180,
|
||||
"line": 182,
|
||||
"column": 54
|
||||
}
|
||||
]
|
||||
@@ -3605,7 +3607,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 70,
|
||||
"line": 72,
|
||||
"column": 44
|
||||
}
|
||||
]
|
||||
@@ -3788,7 +3790,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 75,
|
||||
"line": 77,
|
||||
"column": 53
|
||||
},
|
||||
{
|
||||
@@ -4021,7 +4023,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "components/settings/AppLifecycleSettingsCard.tsx",
|
||||
"line": 112,
|
||||
"line": 91,
|
||||
"column": 25
|
||||
}
|
||||
]
|
||||
@@ -4113,7 +4115,7 @@
|
||||
"refs": [
|
||||
{
|
||||
"file": "layouts/AppLayout.tsx",
|
||||
"line": 72,
|
||||
"line": 74,
|
||||
"column": 48
|
||||
}
|
||||
]
|
||||
|
||||
@@ -83,7 +83,6 @@
|
||||
"42a1d9e5b037c210": "Bytes",
|
||||
"42aa8e01e98c0d8c": "Total duration",
|
||||
"43cb41d62de2d179": "Proxy requires authentication",
|
||||
"4510b63fdcf7451f": "Cursor Assistant v0.1.0",
|
||||
"470049252e54de6a": "Success rate: {rate}",
|
||||
"48b970b568a7f8f9": "Proxy settings",
|
||||
"48d8db17bae06246": "{count} total",
|
||||
@@ -105,6 +104,7 @@
|
||||
"5a3bd99fa69a40c1": "Use public service",
|
||||
"5ae715656ffbc35d": "Merge into the request body for every model from this provider.",
|
||||
"5c55a67935af8f45": "All",
|
||||
"5d59857bf039cac9": "Cursor Assistant v{version}",
|
||||
"5f8d556a9c47da3c": "Launch at login disabled",
|
||||
"5f9acfb945229062": "Are you sure you no longer want to see this ad?",
|
||||
"5fd2ec5a6e9b654c": "Total: {cost}",
|
||||
|
||||
@@ -83,7 +83,6 @@
|
||||
"42a1d9e5b037c210": "字节数",
|
||||
"42aa8e01e98c0d8c": "总耗时",
|
||||
"43cb41d62de2d179": "代理需要认证",
|
||||
"4510b63fdcf7451f": "Cursor 助手 v0.1.0",
|
||||
"470049252e54de6a": "成功占比:{rate}",
|
||||
"48b970b568a7f8f9": "代理设置",
|
||||
"48d8db17bae06246": "共 {count} 条",
|
||||
@@ -105,6 +104,7 @@
|
||||
"5a3bd99fa69a40c1": "使用公益服务",
|
||||
"5ae715656ffbc35d": "合并到该上游所有模型的请求体。",
|
||||
"5c55a67935af8f45": "全部",
|
||||
"5d59857bf039cac9": "Cursor 助手 v{version}",
|
||||
"5f8d556a9c47da3c": "已关闭开机启动",
|
||||
"5f9acfb945229062": "你确认不想再看到此广告吗?",
|
||||
"5fd2ec5a6e9b654c": "合计:{cost}",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import appIcon from "../../src-tauri/icons/32x32.png";
|
||||
import { currentAppVersion } from "../native/appLifecycle";
|
||||
import type { DesktopPlatform } from "./AppFrame";
|
||||
import { WindowControls } from "./WindowControls";
|
||||
import styles from "./AppHeader.module.scss";
|
||||
@@ -10,6 +12,15 @@ type AppHeaderProps = {
|
||||
|
||||
export function AppHeader({ platform, nativeDesktop }: AppHeaderProps) {
|
||||
const showNativeUi = nativeDesktop && platform !== "macos";
|
||||
const [version, setVersion] = useState("…");
|
||||
|
||||
useEffect(() => {
|
||||
let disposed = false;
|
||||
void currentAppVersion().then((next) => {
|
||||
if (!disposed) setVersion(next);
|
||||
});
|
||||
return () => { disposed = true; };
|
||||
}, []);
|
||||
|
||||
return <header className={styles.root}>
|
||||
<div className={styles.dragLayer} data-tauri-drag-region aria-hidden="true" />
|
||||
@@ -17,7 +28,7 @@ export function AppHeader({ platform, nativeDesktop }: AppHeaderProps) {
|
||||
{showNativeUi && <>
|
||||
<div className={styles.identity} aria-label="Cursor BYOK">
|
||||
<img src={appIcon} alt="" />
|
||||
<span>{t("Cursor 助手 v0.1.0")}</span>
|
||||
<span>{t("Cursor 助手 v{version}", { version })}</span>
|
||||
</div>
|
||||
<WindowControls />
|
||||
</>}
|
||||
|
||||
@@ -83,7 +83,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
.menuUnreadDot {
|
||||
.menuIndicatorDot {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
flex: 0 0 7px;
|
||||
|
||||
@@ -18,6 +18,7 @@ import { useMessage } from "../components/ui/message";
|
||||
import { VirtualList } from "../components/virtual/VirtualList";
|
||||
import { useI18n } from "../i18n/store";
|
||||
import { appStore, useAppStore } from "../store/appStore";
|
||||
import { useUpdateStore } from "../store/updateStore";
|
||||
import styles from "./AppLayout.module.scss";
|
||||
import { PageActionsTarget } from "./PageActions";
|
||||
|
||||
@@ -43,6 +44,7 @@ function loadStoredAdIds(key: string): Set<string> {
|
||||
|
||||
export function AppLayout() {
|
||||
const { busy } = useAppStore();
|
||||
const { availableVersion } = useUpdateStore();
|
||||
const { locale } = useI18n();
|
||||
const message = useMessage();
|
||||
const location = useLocation();
|
||||
@@ -198,7 +200,7 @@ export function AppLayout() {
|
||||
? <Icon src={item.icon} size="1.3em" />
|
||||
: <Icon icon={item.icon} size="1.3em" />}
|
||||
<span>{item.label}</span>
|
||||
{!tutorialRead && <span className={styles.menuUnreadDot} aria-hidden="true" />}
|
||||
{!tutorialRead && <span className={styles.menuIndicatorDot} aria-hidden="true" />}
|
||||
</button>
|
||||
</div>
|
||||
: <div className={styles.navigationRow} key={item.path}>
|
||||
@@ -207,6 +209,7 @@ export function AppLayout() {
|
||||
? <Icon src={item.icon} size="1.3em" />
|
||||
: <Icon icon={item.icon} size="1.3em" />}
|
||||
<span>{item.label}</span>
|
||||
{item.path === "/settings" && availableVersion && <span className={styles.menuIndicatorDot} aria-hidden="true" />}
|
||||
</NavLink>
|
||||
</div>}
|
||||
</VirtualList>
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { useSyncExternalStore } from "react";
|
||||
import type { Update } from "@tauri-apps/plugin-updater";
|
||||
import {
|
||||
checkForUpdate,
|
||||
hasNativeAppLifecycle,
|
||||
installUpdate,
|
||||
} from "../native/appLifecycle";
|
||||
|
||||
export type UpdateSnapshot = {
|
||||
availableVersion: string | null;
|
||||
checking: boolean;
|
||||
installing: boolean;
|
||||
};
|
||||
|
||||
let snapshot: UpdateSnapshot = {
|
||||
availableVersion: null,
|
||||
checking: false,
|
||||
installing: false,
|
||||
};
|
||||
let availableUpdate: Update | null = null;
|
||||
let pendingCheck: Promise<string | null> | null = null;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function update(patch: Partial<UpdateSnapshot>) {
|
||||
snapshot = { ...snapshot, ...patch };
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
|
||||
async function replaceAvailableUpdate(next: Update | null) {
|
||||
const previous = availableUpdate;
|
||||
availableUpdate = next;
|
||||
update({ availableVersion: next?.version ?? null });
|
||||
if (previous && previous !== next) await previous.close();
|
||||
}
|
||||
|
||||
export const updateStore = {
|
||||
subscribe(listener: () => void) {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
},
|
||||
getSnapshot: () => snapshot,
|
||||
|
||||
async check(): Promise<string | null> {
|
||||
if (!hasNativeAppLifecycle()) return null;
|
||||
if (pendingCheck) return pendingCheck;
|
||||
update({ checking: true });
|
||||
pendingCheck = (async () => {
|
||||
const next = await checkForUpdate();
|
||||
await replaceAvailableUpdate(next);
|
||||
return next?.version ?? null;
|
||||
})();
|
||||
try {
|
||||
return await pendingCheck;
|
||||
} finally {
|
||||
pendingCheck = null;
|
||||
update({ checking: false });
|
||||
}
|
||||
},
|
||||
|
||||
async install(): Promise<void> {
|
||||
const current = availableUpdate;
|
||||
if (!current) return;
|
||||
update({ installing: true });
|
||||
try {
|
||||
await installUpdate(current);
|
||||
} finally {
|
||||
update({ installing: false });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export function useUpdateStore(): UpdateSnapshot {
|
||||
return useSyncExternalStore(updateStore.subscribe, updateStore.getSnapshot);
|
||||
}
|
||||
Reference in New Issue
Block a user