Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | import { useEffect, useRef } from "react";
import { showMessage } from "@common/Toast";
import GiveButton from "shared/Button/GiveButton";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { checkPortals } from "@utils/routing";
import { clearToasts } from "@common/Toast/ShowToast";
import { safeParse } from "@utils/index";
import { useAppSelector } from "@redux/hooks";
import { selectAuth } from "@redux/slices/auth/auth";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
const SCHEDULER_TIME = 90000;
const versionEl = document.head.querySelector('meta[name="script-version"]');
export const useEasyVersionUpdate = () => {
const version = useRef(versionEl?.getAttribute("content") ?? "");
const poll = useRef<NodeJS.Timeout | null>(null);
const { isDesktopView } = useCustomThemeV2();
const { isAppUpdateSnackbarEnabled } = useGetFeatureFlagValues();
const {
isSignup,
isTOS,
isSLA,
isPrivacyPolicy,
isMobileSelfie,
isChangePassword,
isSetPassword,
isMobileBankPhoto,
} = checkPortals();
const shouldHideSnackbar =
checksPaymentFormPathName() ||
isSignup ||
isTOS ||
isSLA ||
isPrivacyPolicy ||
isMobileSelfie ||
isChangePassword ||
isSetPassword;
isMobileBankPhoto;
const handleUpdate = () => {
window.location.reload();
};
const pushMessage = (cdnVersion: string) => {
showMessage(
"Info",
"A new version of the software is available. Refresh your app",
isDesktopView,
"New Version",
false,
undefined,
{
rightContent: <Action handleUpdate={handleUpdate} />,
onClickSnackbar: () => {
version.current = cdnVersion;
poll.current = null;
document.head
.querySelector('meta[name="script-version"]')
?.setAttribute("content", cdnVersion);
},
},
);
};
const checkForUpdates = () => {
fetch(`${process.env.VITE_ASSETS_CDN_HOST}/ui/version.json`, {
headers: {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache", //For older browsers
},
})
.then((res) => res.text())
.then((text) => {
const data = safeParse<{ version: string }>(text);
const cdnVersion = data?.version;
if (version.current !== cdnVersion && cdnVersion) {
pushMessage(cdnVersion);
} else {
onStart();
}
})
.catch((err) => {
console.log("Failed to fetch version.json:", err);
});
};
const onStart = () => {
poll.current = setTimeout(checkForUpdates, SCHEDULER_TIME);
};
const isAuthenticated = useAppSelector(selectAuth);
useEffect(() => {
const localEnvironments = ["local.development", "local.staging"];
if (
!isAuthenticated ||
localEnvironments.includes(process.env.VITE_ENVIRONMENT || "")
) {
// stop polling if user logged out
if (poll.current) {
clearTimeout(poll.current);
poll.current = null;
}
return;
}
if (isAppUpdateSnackbarEnabled && !shouldHideSnackbar) {
onStart();
return () => {
if (poll.current) {
clearTimeout(poll.current);
poll.current = null;
}
};
}
if (shouldHideSnackbar && poll.current) {
clearTimeout(poll.current);
poll.current = null;
clearToasts();
}
}, [isAppUpdateSnackbarEnabled, shouldHideSnackbar, isAuthenticated]);
};
function Action({ handleUpdate }: any) {
return (
<GiveButton
label="Refresh"
variant="filled"
color="light"
onClick={handleUpdate}
forceDarkMode
sx={{
whiteSpace: "nowrap",
}}
/>
);
}
function checksPaymentFormPathName() {
const pathname = location.pathname.replace("/", "");
// Public form name is a string that contains an integer
return (
!Number.isNaN(Number(pathname)) ||
pathname.match(/^\d+\/(checkout|checkout_success)$/)
);
}
|