useOnboardingData.ts raw

   1  // src/hooks/useOnboardingData.ts
   2  
   3  import { ALBY_ACCOUNT_APP_NAME } from "src/constants";
   4  import { useAlbyMe } from "src/hooks/useAlbyMe";
   5  import { useApps } from "src/hooks/useApps";
   6  import { useBalances } from "src/hooks/useBalances";
   7  import { useChannels } from "src/hooks/useChannels";
   8  import { useInfo } from "src/hooks/useInfo";
   9  import { useNodeConnectionInfo } from "src/hooks/useNodeConnectionInfo";
  10  import { useTransactions } from "src/hooks/useTransactions";
  11  
  12  interface ChecklistItem {
  13    title: string;
  14    description: string;
  15    checked: boolean;
  16    to: string;
  17    disabled: boolean;
  18  }
  19  
  20  interface UseOnboardingDataResponse {
  21    isLoading: boolean;
  22    checklistItems: ChecklistItem[];
  23  }
  24  
  25  export const useOnboardingData = (): UseOnboardingDataResponse => {
  26    const { data: albyMe } = useAlbyMe();
  27    const { data: appsData } = useApps();
  28    const { data: balances } = useBalances();
  29    const { data: channels } = useChannels();
  30    const { data: info, hasChannelManagement, hasMnemonic } = useInfo();
  31    const { data: nodeConnectionInfo } = useNodeConnectionInfo();
  32    const { data: transactions } = useTransactions(undefined, false, 1);
  33  
  34    const isLoading =
  35      !appsData ||
  36      !balances ||
  37      !channels ||
  38      !info ||
  39      !nodeConnectionInfo ||
  40      !transactions ||
  41      (info.albyAccountConnected && !albyMe);
  42  
  43    if (isLoading) {
  44      return { isLoading: true, checklistItems: [] };
  45    }
  46  
  47    const isLinked =
  48      !!albyMe &&
  49      nodeConnectionInfo &&
  50      albyMe?.keysend_pubkey === nodeConnectionInfo?.pubkey;
  51    const hasChannel =
  52      !hasChannelManagement || (hasChannelManagement && channels.length > 0);
  53    const hasBackedUp =
  54      hasMnemonic === true &&
  55      info &&
  56      info.nextBackupReminder !== "" &&
  57      new Date(info.nextBackupReminder).getTime() > new Date().getTime();
  58    const hasCustomApp =
  59      appsData &&
  60      appsData.apps.find((x) => x.name !== ALBY_ACCOUNT_APP_NAME) !== undefined;
  61    const hasFundsOrTransaction =
  62      transactions.totalCount > 0 || balances.lightning.totalSpendableSat > 0;
  63  
  64    const checklistItems: Omit<ChecklistItem, "disabled">[] = [
  65      ...(hasChannelManagement &&
  66      !(info.jitChannelsEnabled && info.jitChannelsLiquiditySource)
  67        ? [
  68            {
  69              title: "Open your first channel",
  70              description:
  71                "Establish a new Lightning channel to enable fast and low-fee Bitcoin transactions.",
  72              checked: hasChannel,
  73              to: "/channels/first",
  74            },
  75          ]
  76        : []),
  77      ...(info.albyAccountConnected
  78        ? [
  79            {
  80              title: "Link to your Alby Account",
  81              description:
  82                "Link your lightning address & other apps to this Hub.",
  83              checked: isLinked,
  84              to: "/apps?tab=connected-apps",
  85            },
  86          ]
  87        : []),
  88      {
  89        title: "Receive your first payment",
  90        description: "Add funds to your wallet to start using it.",
  91        checked: hasFundsOrTransaction,
  92        to: "/wallet",
  93      },
  94      {
  95        title: "Connect your first app",
  96        description:
  97          "Seamlessly connect apps and integrate your wallet with other apps from your Hub.",
  98        checked: hasCustomApp,
  99        to: "/apps?tab=app-store",
 100      },
 101      ...(hasMnemonic
 102        ? [
 103            {
 104              title: "Backup your keys",
 105              description:
 106                "Secure your keys by creating a backup to ensure you don't lose access.",
 107              checked: hasBackedUp === true,
 108              to: "/settings/backup",
 109            },
 110          ]
 111        : []),
 112    ];
 113  
 114    const nextStep = checklistItems.find((x) => !x.checked);
 115  
 116    const sortedChecklistItems = checklistItems.map((item) => ({
 117      ...item,
 118      disabled: item !== nextStep,
 119    }));
 120  
 121    return { isLoading: false, checklistItems: sortedChecklistItems };
 122  };
 123