Files
super-app/App.tsx

169 lines
4.6 KiB
TypeScript

import { init } from "@sentry/react-native";
import { Component } from "react";
import codePush from "react-native-code-push";
import {
GenericErrorScreen
} from "./App/Container/Navi-Insurance";
import {
BundleState,
CODEPUSH_METHOD,
EVENT_NAMES,
EVENT_PROPERTY_KEYS,
EVENT_PROPERTY_VALUES,
SentryConstants,
} from "./App/common/constants";
import { sendAsAnalyticsEvent } from "./App/common/hooks/useAnalyticsEvent";
import { logToSentry } from "./App/common/hooks/useSentryLogging";
import { CtaData } from "./App/common/interface";
import RnApp from "./App/common/navigator/RnAppCreator";
import {
getBuildConfigDetails,
setBuildConfigDetails,
} from "./App/common/utilities/CacheUtils";
import {
getStringPreference,
setStringPreference,
} from "./App/common/utilities/SharedPreferenceUtils";
init({
dsn: SentryConstants.SENTRY_DSN,
environment: SentryConstants.SENTRY_ENVIRONMENT_PRODUCTION,
});
interface AppState {
bundleState: BundleState;
}
const preloadBundle: string = "PreloadBundle";
export default class App extends Component<{}, AppState> {
constructor(props: {}) {
super(props);
let bundleState = BundleState.LOADING;
getBundleStatus().then(value => (bundleState = value));
this.state = {
bundleState: bundleState,
};
}
checkForUpdates = async () => {
let flavor: string | undefined;
getBuildConfigDetails().then(res => {
flavor = res?.baseUrl;
});
const cta = this.getInitialCta();
await codePush.sync(
{
installMode: (cta?.title === preloadBundle) ? codePush.InstallMode.IMMEDIATE : codePush.InstallMode.ON_NEXT_RESTART,
mandatoryInstallMode: codePush.InstallMode.IMMEDIATE,
},
status => {
this.onCodepushStatusChange(status);
},
);
};
onCodepushStatusChange = (status: codePush.SyncStatus) => {
switch (status) {
case codePush.SyncStatus.CHECKING_FOR_UPDATE:
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
case codePush.SyncStatus.INSTALLING_UPDATE:
this.setState({ bundleState: BundleState.LOADING });
storeBundleStatus(BundleState.LOADING);
break;
case codePush.SyncStatus.UP_TO_DATE:
this.setState({ bundleState: BundleState.LOADED });
storeBundleStatus(BundleState.LOADED);
break;
case codePush.SyncStatus.UPDATE_IGNORED:
this.setState({ bundleState: BundleState.LOADED });
storeBundleStatus(BundleState.LOADED);
logToSentry(
`Codepush Ignored | Status: ${status} | MethodName: ${CODEPUSH_METHOD}`,
);
break;
case codePush.SyncStatus.UNKNOWN_ERROR:
this.setState({ bundleState: BundleState.ERROR });
sendAsAnalyticsEvent({
name: EVENT_NAMES.CODEPUSH_FETCH_ERROR,
properties: {
[EVENT_PROPERTY_KEYS.STATUS]: EVENT_PROPERTY_VALUES.UNKNOWN_ERROR,
},
});
logToSentry(
`Codepush Failed | Status: ${status} | MethodName: ${CODEPUSH_METHOD}`,
);
break;
default:
this.setState({ bundleState: BundleState.LOADED });
storeBundleStatus(BundleState.LOADED);
}
};
override shouldComponentUpdate(
nextProps: Readonly<{}>,
nextState: Readonly<AppState>,
_: any,
): boolean {
return this.state.bundleState !== nextState.bundleState;
}
getInitialCta = (): CtaData | undefined => {
const { CtaData } = this.props as any;
if (!CtaData) {
logToSentry(
`CtaData is missing or invalid: ${CtaData} | MethodName: getInitialCta`,
);
return;
}
try {
const cta = JSON.parse(CtaData) as CtaData;
return cta;
} catch (error) {
logToSentry(
`Error parsing CtaData: ${CtaData} | Error: ${error} | MethodName: getInitialCta`,
);
return;
}
};
override componentDidMount(): void {
this.checkForUpdates();
setBuildConfigDetails();
}
override componentWillUnmount(): void {
invalidateStatus(BundleState.LOADING);
}
override render() {
const cta = this.getInitialCta();
if (cta?.title === preloadBundle) {
return <></>;
}
else if (!!cta){
return RnApp.create(cta)
}
else {
sendAsAnalyticsEvent({
name: EVENT_NAMES.INVALID_SCREEN_CTA
});
return <GenericErrorScreen />;
}
}
}
const storeBundleStatus = (bundleState: string) => {
return setStringPreference("reactBundleState", bundleState);
};
const invalidateStatus = (bundleState: string) => {
return setStringPreference("reactBundleState", bundleState);
};
const getBundleStatus = async () => {
return await getStringPreference("reactBundleState", "string");
};