feat: handle auto update for macos

This commit is contained in:
Zamitto 2024-05-31 13:17:12 -03:00
parent 2138aa9711
commit 45f30a9208
9 changed files with 92 additions and 56 deletions

View file

@ -0,0 +1,67 @@
import { useTranslation } from "react-i18next";
import { useEffect, useState } from "react";
import { SyncIcon } from "@primer/octicons-react";
import * as styles from "./header.css";
import { AppUpdaterEvents } from "@types";
export function AutoUpdateSubHeader() {
const [showUpdateSubheader, setShowUpdateSubheader] = useState(false);
const [newVersion, setNewVersion] = useState("");
const [newVersionText, setNewVersionText] = useState("");
const { t } = useTranslation("header");
const handleClickNewUpdate = () => {
window.electron.restartAndInstallUpdate();
};
useEffect(() => {
if (window.electron.platform == "darwin") {
setNewVersionText(
t("version_available_download", { version: newVersion })
);
} else {
setNewVersionText(
t("version_available_install", { version: newVersion })
);
}
}, [t, newVersion]);
useEffect(() => {
const unsubscribe = window.electron.onAutoUpdaterEvent(
(event: AppUpdaterEvents) => {
if (event.type == "update-available") {
setNewVersion(event.info.version || "");
}
if (event.type == "update-downloaded") {
setShowUpdateSubheader(true);
}
}
);
window.electron.checkForUpdates();
return () => {
unsubscribe();
};
}, []);
return (
<>
{showUpdateSubheader && (
<header className={styles.subheader}>
<button
type="button"
className={styles.newVersionButton}
onClick={handleClickNewUpdate}
>
<SyncIcon size={12} />
<small>{newVersionText}</small>
</button>
</header>
)}
</>
);
}

View file

@ -1,18 +1,13 @@
import { useTranslation } from "react-i18next";
import { useEffect, useMemo, useRef, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import {
ArrowLeftIcon,
SearchIcon,
SyncIcon,
XIcon,
} from "@primer/octicons-react";
import { ArrowLeftIcon, SearchIcon, XIcon } from "@primer/octicons-react";
import { useAppDispatch, useAppSelector } from "@renderer/hooks";
import * as styles from "./header.css";
import { clearSearch } from "@renderer/features";
import { AppUpdaterEvents } from "@types";
import { AutoUpdateSubHeader } from "./auto-update-sub-header";
export interface HeaderProps {
onSearch: (query: string) => void;
@ -40,9 +35,6 @@ export function Header({ onSearch, onClear, search }: HeaderProps) {
const [isFocused, setIsFocused] = useState(false);
const [showUpdateSubheader, setShowUpdateSubheader] = useState(false);
const [newVersion, setNewVersion] = useState("");
const { t } = useTranslation("header");
const title = useMemo(() => {
@ -58,30 +50,6 @@ export function Header({ onSearch, onClear, search }: HeaderProps) {
}
}, [location.pathname, search, dispatch]);
const handleClickRestartAndUpdate = () => {
window.electron.restartAndInstallUpdate();
};
useEffect(() => {
const unsubscribe = window.electron.onAutoUpdaterEvent(
(event: AppUpdaterEvents) => {
if (event.type == "update-available") {
setNewVersion(event.info.version || "");
}
if (event.type == "update-downloaded") {
setShowUpdateSubheader(true);
}
}
);
window.electron.checkForUpdates();
return () => {
unsubscribe();
};
}, []);
const focusInput = () => {
setIsFocused(true);
inputRef.current?.focus();
@ -158,18 +126,7 @@ export function Header({ onSearch, onClear, search }: HeaderProps) {
</div>
</section>
</header>
{showUpdateSubheader && (
<header className={styles.subheader}>
<button
type="button"
className={styles.newVersionButton}
onClick={handleClickRestartAndUpdate}
>
<SyncIcon size={12} />
<small>{t("version_available", { version: newVersion })}</small>
</button>
</header>
)}
<AutoUpdateSubHeader />
</>
);
}