| 1 | import { useEffect, useLayoutEffect, useRef } from "react"; |
| 2 | import { useDesktopUpdateState } from "~/state/desktopUpdate"; |
| 3 | import { prepareUiRefresh, startAutomaticUiRefresh } from "~/uiRefresh"; |
| 4 | import { APP_VERSION } from "~/branding"; |
| 5 | import { installLocalUpdate } from "./desktopUpdate.actions"; |
| 6 | |
| 7 | export function AutomaticUiRefresh() { |
| 8 | const state = useDesktopUpdateState(); |
| 9 | const latest = useRef(state); |
| 10 | const controller = useRef<ReturnType<typeof startAutomaticUiRefresh> | null>(null); |
| 11 | |
| 12 | useEffect(() => { |
| 13 | const bridge = window.desktopBridge; |
| 14 | if (!bridge) return; |
| 15 | const refresh = startAutomaticUiRefresh({ |
| 16 | pending: () => |
| 17 | (latest.current?.status === "downloaded" && latest.current.installKind === "ui") || |
| 18 | Boolean(latest.current?.uiVersion && latest.current.uiVersion !== APP_VERSION), |
| 19 | apply: async () => { |
| 20 | if (latest.current?.uiVersion && latest.current.uiVersion !== APP_VERSION) { |
| 21 | if (prepareUiRefresh()) window.location.reload(); |
| 22 | return; |
| 23 | } |
| 24 | await installLocalUpdate(bridge, undefined, { automatic: true }); |
| 25 | }, |
| 26 | }); |
| 27 | controller.current = refresh; |
| 28 | return () => { |
| 29 | controller.current = null; |
| 30 | refresh.dispose(); |
| 31 | }; |
| 32 | }, []); |
| 33 | |
| 34 | useLayoutEffect(() => { |
| 35 | latest.current = state; |
| 36 | controller.current?.check(); |
| 37 | }, [state]); |
| 38 | return null; |
| 39 | } |