revite/src/components/common/UpdateIndicator.tsx

57 lines
1.6 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
import { useEffect, useState } from "preact/hooks";
2021-06-28 05:17:38 -04:00
2022-05-30 07:01:47 -04:00
import { IconButton } from "@revoltchat/ui";
2021-07-05 06:23:23 -04:00
import { internalSubscribe } from "../../lib/eventEmitter";
import { useApplicationState } from "../../mobx/State";
2021-07-05 06:23:23 -04:00
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 = useApplicationState().settings.theme;
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.getVariable("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.getVariable("success")} />
2021-07-05 06:25:20 -04:00
</IconButton>
);
2021-06-28 05:17:38 -04:00
}