Skip to content

Commit

Permalink
fix: use hook
Browse files Browse the repository at this point in the history
  • Loading branch information
reneaaron committed Sep 3, 2024
1 parent 80bef49 commit fb1da85
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 47 deletions.
42 changes: 29 additions & 13 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ SplashScreen.preventAutoHideAsync();
export default function RootLayout() {
const { isDarkColorScheme } = useColorScheme();
const [fontsLoaded, setFontsLoaded] = React.useState(false);
const [checkedOnboarding, setCheckedOnboarding] = React.useState(false);
useConnectionChecker();

const rootNavigationState = useRootNavigationState();
const hasNavigationState = !!rootNavigationState?.key;

React.useEffect(() => {
const checkOnboardingStatus = async () => {
const hasOnboarded = await secureStorage.getItem(hasOnboardedKey);
if (!hasOnboarded && hasNavigationState) {
router.replace("/onboarding");
}
};
async function checkOnboardingStatus() {
const hasOnboarded = await secureStorage.getItem(hasOnboardedKey);
if (!hasOnboarded && hasNavigationState) {
router.replace("/onboarding");
}

checkOnboardingStatus();
setCheckedOnboarding(true);
};

async function loadFonts() {

(async () => {
await Font.loadAsync({
OpenRunde: require("./../assets/fonts/OpenRunde-Regular.otf"),
"OpenRunde-Medium": require("./../assets/fonts/OpenRunde-Medium.otf"),
Expand All @@ -71,12 +72,27 @@ export default function RootLayout() {
});

setFontsLoaded(true);
})().finally(() => {
SplashScreen.hideAsync();
});
}

React.useEffect(() => {
const init = async () => {
try {
await Promise.all([
checkOnboardingStatus(),
loadFonts(),
]);
}
finally {

SplashScreen.hideAsync();
}
};

init();

}, [hasNavigationState]);

if (!fontsLoaded) {
if (!fontsLoaded || !checkedOnboarding) {
return null;
}

Expand Down
5 changes: 0 additions & 5 deletions app/home.js

This file was deleted.

4 changes: 2 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Index } from "../pages/Index";
import { Home } from "../pages/Home";

export default function Page() {
return <Index />;
return <Home />;
}
19 changes: 19 additions & 0 deletions hooks/useOnboarding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { hasOnboardedKey } from "lib/state/appStore";
import React from "react";
import { useEffect } from "react";
import { secureStorage } from "~/lib/secureStorage";

export function useOnboarding() {
const [onboarded, setOnboarded] = React.useState(false);
useEffect(() => {
async function checkOnboardingStatus() {
await secureStorage.removeItem(hasOnboardedKey);
const hasOnboarded = await secureStorage.getItem(hasOnboardedKey);
setOnboarded(!!hasOnboarded);
};

checkOnboardingStatus();
});

return onboarded;
}
7 changes: 5 additions & 2 deletions pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import LargeArrowDown from "~/components/icons/LargeArrowDown";
import { SvgProps } from "react-native-svg";
import { Button } from "~/components/ui/button";
import Screen from "~/components/Screen";
import { useOnboarding } from "~/hooks/useOnboarding";

dayjs.extend(relativeTime);

Expand All @@ -40,6 +41,7 @@ export function Home() {
);
const [pressed, setPressed] = React.useState(false);
const rootNavigationState = useRootNavigationState();
const isOnboarded = useOnboarding();

useFocusEffect(() => {
reloadBalance();
Expand All @@ -48,10 +50,11 @@ export function Home() {
let hasNavigationState = !!rootNavigationState?.key;
const hasNwcClient = !!nwcClient;
React.useEffect(() => {
if (hasNavigationState && !hasNwcClient) {
if (hasNavigationState && !hasNwcClient && isOnboarded) {
router.replace(`/settings/wallets/${selectedWalletId}/wallet-connection`);
}
}, [hasNwcClient, hasNavigationState]);
}, [hasNwcClient, hasNavigationState, isOnboarded]);

if (!nwcClient) {
return <WalletConnection />;
}
Expand Down
20 changes: 0 additions & 20 deletions pages/Index.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion pages/Onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { hasOnboardedKey } from "~/lib/state/appStore";
export function Onboarding() {
async function finish() {
secureStorage.setItem(hasOnboardedKey, "true");
router.replace("/home");
router.replace("/");
}

return (
Expand Down
2 changes: 1 addition & 1 deletion pages/receive/ReceiveSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function ReceiveSuccess() {
<Button
size="lg"
onPress={() => {
router.replace("/home");
router.replace("/");
}}
>
<Text>Close</Text>
Expand Down
2 changes: 1 addition & 1 deletion pages/send/PaymentSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function PaymentSuccess() {
size="lg"
className="w-full"
onPress={() => {
router.replace("/home");
router.replace("/");
}}
>
<Text>Close</Text>
Expand Down
1 change: 1 addition & 0 deletions pages/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function Settings() {
onPress: () => {
router.dismissAll();
useAppStore.getState().reset();
router.replace("/");
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions pages/settings/wallets/WalletConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function WalletConnection() {
if (router.canDismiss()) {
router.dismissAll();
}
router.replace("/home");
router.replace("/");
Toast.show({
type: "success",
text1: "Wallet Connected",
Expand All @@ -97,7 +97,7 @@ export function WalletConnection() {
useAppStore
.getState()
.setSelectedWalletId(walletIdWithConnection);
router.replace("/home");
router.replace("/");
}}
>
<X className="text-foreground" />
Expand Down

0 comments on commit fb1da85

Please sign in to comment.