@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
104 lines (76 loc) • 3 kB
text/typescript
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint max-len: off */
import { useContext } from "react";
import { usePickFromState } from "@applicaster/zapp-react-native-redux/hooks";
import { legacyScreenData } from "@applicaster/quick-brick-core/App/NavigationProvider/utils";
import { getTargetScreenData } from "../screen/useTargetScreenData";
import { useHookModalScreenData } from "../hookModal/hooks/useHookModalScreenData";
import { useNavigation } from "./useNavigation";
import { useModalStoreState } from "../../modalState";
import { ScreenDataContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ScreenDataContext";
import { usePathname } from "./usePathname";
// starts with modal/
const isModalPathname = (pathname: string) => /^modal\//.test(pathname);
const isVideoModalPathname = (pathname: string) =>
/^video-modal\//.test(pathname);
const isHookModalPathname = (pathname: string) =>
/^hooks-modal\//.test(pathname);
const isHookPathname = (pathname: string) => /^\/hooks\//.test(pathname);
type VariousScreenData = LegacyNavigationScreenData | ZappRiver | ZappEntry;
export const useRoute = (
useLegacy = true
): {
screenData: VariousScreenData;
pathname: string;
} => {
const pathname = usePathname() || "";
const navigator = useNavigation();
const screenContext = useContext(ScreenDataContext);
const screenDataContext = useLegacy
? legacyScreenData(screenContext)
: screenContext;
const { plugins, contentTypes, rivers } = usePickFromState([
"plugins",
"rivers",
"contentTypes",
]);
const modalState = useModalStoreState();
const modalScreenData = modalState.screen;
const hookModalScreenData = useHookModalScreenData();
const videoModalScreenData =
navigator?.videoModalState?.item &&
legacyScreenData(
{
entry: navigator.videoModalState?.item as ZappEntry,
screen: getTargetScreenData(
navigator.videoModalState?.item as ZappEntry,
rivers,
contentTypes
),
},
plugins
);
// There are 4 route scenarios
// For regular screens take date from navigator stack.
// is path is model grab screenData from modal state
// if path is video modal grab screenData from video state
// if path is hook modal grab screenData from hook state
// if path is hook grab screenData from screenData
if (isModalPathname(pathname)) {
const screenData = modalScreenData ?? ({} as ZappEntry);
return { screenData, pathname };
}
if (isVideoModalPathname(pathname)) {
const screenData = videoModalScreenData as LegacyNavigationScreenData;
return { screenData, pathname };
}
if (isHookModalPathname(pathname)) {
const screenData = hookModalScreenData;
return { screenData, pathname };
}
if (isHookPathname(pathname)) {
return { screenData: screenDataContext, pathname };
}
const screenData = screenDataContext;
return { screenData, pathname };
};