8000 Fix update check by VPKSoft · Pull Request #443 · VPKSoft/PasswordKeeper · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix update check #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"preview": "vite preview",
"tauri": "tauri",
"test-release": "tauri build -b none",
"release": "tauri build",
"lint": "eslint --max-warnings=0 --config ./.eslintrc.cjs --ext .tsx,.ts ./src/"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ arboard = "3.3.0"
default = ["custom-protocol"]
# this feature is used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = ["tauri/custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
4 changes: 2 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "PasswordKeeper",
"version": "0.4.0"
"version": "0.5.0"
},
"tauri": {
"allowlist": {
Expand Down Expand Up @@ -65,7 +65,7 @@
},
"resources": [],
"shortDescription": "",
"targets": "all",
"targets": ["app", "appimage", "nsis", "updater"],
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
Expand Down
20 changes: 19 additions & 1 deletion src/components/software/popups/AboutPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { useLocalize } from "../../../i18n";
import { CommonProps } from "../../Types";
import { GithubLogo, LogoImage } from "../../../utilities/app/Images";
import { useTauriUpdater } from "../../../hooks/UseTauriUpdater";
import { useNotify } from "../../reusable/Notify";
/**
* The props for the {@link AboutPopup} component.
*/
Expand All @@ -52,11 +53,12 @@ const AboutPopup = ({
}: AboutPopupProps) => {
const lc = useLocalize("common");
const ud = useLocalize("updates");
const [contextHolder, notification] = useNotify();

const [appVersion, setAppVersion] = React.useState("");
const [appName, setAppName] = React.useState("");

const [shouldUpdate, manifest, reCheck, update] = useTauriUpdater(false);
const [shouldUpdate, manifest, reCheck, update, error, errorMessage] = useTauriUpdater(!visible);

// Get the application name and current version.
React.useEffect(() => {
Expand All @@ -83,6 +85,12 @@ const AboutPopup = ({
void open("https://github.com/VPKSoft/PasswordKeeper/releases/latest");
}, []);

React.useEffect(() => {
if (error) {
notification("error", errorMessage, 5);
}
}, [error, errorMessage, notification]);

return (
<Modal //
title={lc("about")}
Expand All @@ -93,6 +101,7 @@ const AboutPopup = ({
>
centered
>
{contextHolder}
<div className={classNames(AboutPopup.name, className)}>
<div className="Popup-versionText">{`${appName}, ${lc("copyright")} © 2023 VPKSoft, v.${appVersion}`}</div>
<div className="Popup-licenseText">{lc("license")}</div>
Expand Down Expand Up @@ -147,6 +156,7 @@ SOFTWARE."
</Button>
</div>
</div>
{error && <div className="VersionCheckError-text">{errorMessage}</div>}
<div className="Popup-ButtonRow">
<Button //
>
Expand Down Expand Up @@ -191,6 +201,14 @@ const StyledAboutPopup = styled(AboutPopup)`
flex-wrap: wrap;
margin-right: 10px;
}
.VersionCheckError-text {
align-content: center;
display: flex;
flex-wrap: wrap;
margin-right: 10px;
font-weight: bold;
color: red;
}
.Popup-UpdateNotify-button {
margin-right: 4px;
}
Expand Down
37 changes: 21 additions & 16 deletions src/hooks/UseTauriUpdater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOFTWARE.
import { relaunch } from "@tauri-apps/api/process";
import { UpdateManifest, UpdateResult, checkUpdate, installUpdate } from "@tauri-apps/api/updater";
import * as React from "react";
import { useLocalize } from "../i18n";

/** A value indicating whether the application should update it self. `null` value indicates an unresolved status, `undefined` indicates an error status. */
type ShouldUpdate = boolean | null | undefined;
Expand All @@ -39,7 +40,7 @@ type Update = () => Promise<void>;
/**
* A result data for the {@link useTauriUpdater} hook.
*/
type TauriUpdaterResult = [shouldUpdate: ShouldUpdate, manifest: Manifest, reCheck: ReCheck, update: Update, checkError: boolean];
type TauriUpdaterResult = [shouldUpdate: ShouldUpdate, manifest: Manifest, reCheck: ReCheck, update: Update, checkError: boolean, errorMessage: string];

/**
* A custom hook for Tauri updater check. See: https://tauri.app/v1/api/js/updater.
Expand All @@ -50,9 +51,12 @@ const useTauriUpdater = (passive: boolean, retryCount: number = 5): TauriUpdater
const [shouldUpdate, setShouldUpdate] = React.useState<boolean | null | undefined>(null);
const [manifest, setManifest] = React.useState<UpdateManifest | undefined | null>(null);
const [checkError, setCheckError] = React.useState(false);
const [retries, setRetries] = React.useState(0);
const [errorMessage, setErrorMessage] = React.useState("");

const lm = useLocalize("messages");

const shouldRetry = React.useRef<boolean>(false);
const retries = React.useRef(0);

// An internal call back to check for updates.
const checkUpdateInternal = React.useCallback(() => {
Expand All @@ -61,49 +65,50 @@ const useTauriUpdater = (passive: boolean, retryCount: number = 5): TauriUpdater
setShouldUpdate(updateResult.shouldUpdate);
setManifest(updateResult.manifest ?? null);
setCheckError(false);
setRetries(0);
retries.current = 0;
setErrorMessage("");
shouldRetry.current = false;
})
.catch(() => {
.catch(error => {
// eslint-disable-next-line unicorn/no-useless-undefined
setShouldUpdate(undefined);
// eslint-disable-next-line unicorn/no-useless-undefined
setManifest(undefined);
setCheckError(true);
const newRetries = retries + 1;
setRetries(newRetries);
shouldRetry.current = newRetries < retryCount;
retries.current++;
shouldRetry.current = retries.current < retryCount;
setErrorMessage(lm("updateCheckFailed", "File open failed with message '{{error}}'.", { error: error }));
});
}, [retries, retryCount]);
}, [lm, retries, retryCount]);

const checkUpdateExternal = React.useCallback(() => {
setRetries(0);
retries.current = 0;
shouldRetry.current = true;
checkUpdateInternal();
}, [checkUpdateInternal]);

// Check for updates.
React.useEffect(() => {
if (!passive || (shouldRetry.current && retries < retryCount)) {
if (!passive || (shouldRetry.current && retries.current < retryCount)) {
checkUpdateInternal();
}
}, [checkUpdateInternal, passive, retries, retryCount]);
}, [checkUpdateInternal, passive, retryCount]);

React.useEffect(() => {
const timeout = setTimeout(() => {
if (!passive || (shouldRetry.current && retries < retryCount)) {
const timeout = setInterval(() => {
if (!passive || (shouldRetry.current && retries.current < retryCount)) {
checkUpdateInternal();
}
}, 5_000);

return () => clearTimeout(timeout);
}, [checkUpdateInternal, passive, retries, retryCount]);
return () => clearInterval(timeout);
}, [checkUpdateInternal, passive, retryCount]);

const updateCallback = React.useCallback(() => {
return installUpdate().then(relaunch);
}, []);

return [shouldUpdate, manifest, checkUpdateExternal, updateCallback, checkError];
return [shouldUpdate, manifest, checkUpdateExternal, updateCallback, checkError, errorMessage];
};

export { useTauriUpdater };
Expand Down
0