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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import { useEffect, useRef } from "react";
import flagsmith from "flagsmith";
import { Text } from "@common/Text";
import { showMessage } from "@common/Toast";
import { Button } from "@common/Button";
import { palette } from "@palette";
import { useLogout, useResetApp } from "../common/useHandleLogout";
import { clearToasts } from "@common/Toast/ShowToast";
import Cookies from "js-cookie";
import * as Sentry from "@sentry/react";
import { checkPortals } from "@utils/routing";
const SCHEDULER_TIME = 90000;
const storageDto = () => {
const versionKey = "v";
const isVersionUpdatedKey = "version_updated";
const currentAppVersion = localStorage.getItem(versionKey);
const isVersionUpdated = localStorage.getItem(isVersionUpdatedKey);
return {
versionKey,
currentAppVersion,
isVersionUpdated,
updateVersion: (newVersion?: string) => {
localStorage.setItem(versionKey, newVersion || "1.0");
},
updateIsVersionUpdated: (newValue: "true" | "false") => {
localStorage.setItem(isVersionUpdatedKey, newValue);
},
removeIsVersionUpdated: () => {
localStorage.removeItem(isVersionUpdatedKey);
},
};
};
export const useVersionUpdate = () => {
const { handleReset } = useResetApp();
const { mutateAsync } = useLogout();
const messageOpenStatus = useRef(false);
const { isSignup, isTOS, isPrivacyPolicy, isMobileSelfie } = checkPortals();
const { versionKey, updateVersion } = storageDto();
const handleUpdate = (newVersion: string) => async () => {
//If the user is already logged in, we need to make sure to invalidate the current session by calling logOut
messageOpenStatus.current = false;
updateVersion(newVersion);
if (Cookies.get("user")) {
await mutateAsync({} as any, {
onSettled: () => {
Sentry.setUser(null);
handleReset();
window.location.search = "?new_version=true";
},
onError: () => {
showMessage("Error", "Error when trying to log out. Try again Later");
},
});
} else {
handleReset();
}
};
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$/);
}
const pushMessage = (newVersion: string) => {
if (
checksPaymentFormPathName() ||
isSignup ||
isTOS ||
isPrivacyPolicy ||
isMobileSelfie
)
return;
messageOpenStatus.current = true;
showMessage(
"Info",
"",
true,
"A new version of the software is available",
false,
{
style: {
width: "370px",
},
},
{
action: <Action handleUpdate={handleUpdate(newVersion)} />,
onClickSnackbar: handleUpdate(newVersion),
},
);
};
function checkForUpdates() {
const newVersion = flagsmith.getValue("react_app_version");
const currentAppVersion = localStorage.getItem(versionKey);
if (newVersion && currentAppVersion !== newVersion) {
pushMessage(newVersion as string);
}
setTimeout(checkForUpdates, SCHEDULER_TIME);
}
function listenStorage(e: StorageEvent) {
if (e.key === versionKey) {
//here probabaly the user tampered with the localstorage manually
pushMessage(flagsmith.getValue("react_app_version") as string);
}
}
const onStart = async () => {
await flagsmith.getFlags();
// appendVersionToScripts(process.env.react_app_version || "1.0");
checkForUpdates();
};
useEffect(() => {
if (checksPaymentFormPathName() && messageOpenStatus.current) {
clearToasts();
messageOpenStatus.current = false;
}
}, [location.pathname]);
useEffect(() => {
onStart();
flagsmith.startListening(SCHEDULER_TIME);
window.addEventListener("storage", listenStorage);
return () => {
flagsmith.stopListening();
window.removeEventListener("storage", listenStorage);
};
}, []);
};
function Action({ handleUpdate }: any) {
return (
<Button
onClick={handleUpdate}
sx={{
marginTop: "auto",
marginBottom: "auto",
padding: "1px",
width: "240px",
"&:hover": {
backgroundColor: "transparent",
},
}}
size="medium"
background="text"
>
<Text fontSize="16px" color={palette.givebox[200]}>
Click to Refresh
</Text>
</Button>
);
}
|