HomeRedirect.tsx raw
1 import React from "react";
2 import { useLocation, useNavigate } from "react-router";
3 import Loading from "src/components/Loading";
4 import { localStorageKeys } from "src/constants";
5 import { useInfo } from "src/hooks/useInfo";
6
7 export function HomeRedirect() {
8 const { data: info } = useInfo();
9 const location = useLocation();
10 const navigate = useNavigate();
11
12 React.useEffect(() => {
13 if (!info) {
14 return;
15 }
16
17 if (info.nodeMigrationFileCreated) {
18 // the hub is halted after creating a migration file and should not be
19 // used anymore, so keep showing the migration success instructions
20 navigate("/create-node-migration-file-success", { replace: true });
21 return;
22 }
23
24 const setupReturnTo = window.localStorage.getItem(
25 localStorageKeys.setupReturnTo
26 );
27
28 let to: string | undefined;
29 if (!setupReturnTo) {
30 if (info.setupCompleted && info.running) {
31 if (info.unlocked) {
32 if (info.albyAccountConnected || !info.albyUserIdentifier) {
33 const returnTo = window.localStorage.getItem(
34 localStorageKeys.returnTo
35 );
36 // setTimeout hack needed for React strict mode (in development)
37 // because the effect runs twice before the navigation occurs
38 setTimeout(() => {
39 window.localStorage.removeItem(localStorageKeys.returnTo);
40 }, 100);
41 to = returnTo || "/home";
42 } else {
43 to = "/alby/auth";
44 }
45 } else {
46 to = "/unlock";
47 }
48 } else if (info.setupCompleted && !info.running) {
49 to = "/start";
50 } else if (info.albyAccountConnected) {
51 // in case user goes back after authenticating in setup
52 // we don't want to show the intro twice
53 to = "/welcome";
54 } else {
55 to = "/intro";
56 }
57 } else {
58 // setTimeout hack needed for React strict mode (in development)
59 // because the effect runs twice before the navigation occurs
60 setTimeout(() => {
61 window.localStorage.removeItem(localStorageKeys.setupReturnTo);
62 }, 100);
63 to = setupReturnTo;
64 }
65 navigate(to, {
66 replace: true,
67 });
68 }, [info, location, navigate]);
69
70 if (!info) {
71 return <Loading />;
72 }
73 }
74