revite/src/components/common/UpdateIndicator.tsx

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-08-05 09:47:00 -04:00
/* eslint-disable react-hooks/rules-of-hooks */
import { Download, CloudDownload } from "@styled-icons/boxicons-regular";
2021-07-05 06:23:23 -04:00
2021-06-28 05:17:38 -04:00
import { useContext, useEffect, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { internalSubscribe } from "../../lib/eventEmitter";
import { ThemeContext } from "../../context/Theme";
import IconButton from "../ui/IconButton";
import { updateSW } from "../../main";
import Tooltip from "./Tooltip";
2021-07-05 06:23:23 -04:00
let pendingUpdate = false;
2021-07-05 06:23:23 -04:00
internalSubscribe("PWA", "update", () => (pendingUpdate = true));
2021-06-28 05:17:38 -04:00
interface Props {
style: "titlebar" | "channel";
}
export default function UpdateIndicator({ style }: Props) {
2021-07-05 06:25:20 -04:00
const [pending, setPending] = useState(pendingUpdate);
2021-06-28 05:17:38 -04:00
2021-07-05 06:25:20 -04:00
useEffect(() => {
return internalSubscribe("PWA", "update", () => setPending(true));
});
2021-06-28 05:17:38 -04:00
2021-07-05 06:25:20 -04:00
if (!pending) return null;
const theme = useContext(ThemeContext);
2021-06-28 05:17:38 -04:00
if (style === "titlebar") {
return (
<div class="actions">
<Tooltip
content="A new update is available!"
placement="bottom">
<div onClick={() => updateSW(true)}>
<CloudDownload size={22} color={theme.success} />
</div>
</Tooltip>
</div>
);
}
if (window.isNative) return null;
2021-07-05 06:25:20 -04:00
return (
<IconButton onClick={() => updateSW(true)}>
<Download size={22} color={theme.success} />
</IconButton>
);
2021-06-28 05:17:38 -04:00
}