Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
chore: simplify import node conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jun 8, 2024
1 parent bdedbeb commit 3c40311
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
10 changes: 8 additions & 2 deletions frontend/src/screens/setup/ImportMnemonic.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as bip39 from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { LifeBuoy, ShieldCheck } from "lucide-react";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

import MnemonicInputs from "src/components/MnemonicInputs";
Expand All @@ -16,6 +16,12 @@ export function ImportMnemonic() {
const navigate = useNavigate();
const setupStore = useSetupStore();

useEffect(() => {
// in case the user presses back, remove their last-saved mnemonic
useSetupStore.getState().updateNodeInfo({
mnemonic: undefined,
});
}, []);
const [mnemonic, setMnemonic] = useState<string>("");

async function onSubmit(e: React.FormEvent) {
Expand All @@ -41,7 +47,7 @@ export function ImportMnemonic() {
nextBackupReminder: sixMonthsLater.toISOString(),
});

navigate(`/setup/node?wallet=import`);
navigate(`/setup/node`);
}

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/setup/SetupAdvanced.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function SetupAdvanced() {
Import Existing Mnemonic
</Button>
</Link>
<Link to="/setup/password?wallet=new" className="w-full">
<Link to="/setup/password" className="w-full">
<Button className="w-full" variant="secondary">
Create Wallet with Custom Node
</Button>
Expand Down
13 changes: 6 additions & 7 deletions frontend/src/screens/setup/SetupNode.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactElement } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { useNavigate } from "react-router-dom";
import Container from "src/components/Container";
import TwoColumnLayoutHeader from "src/components/TwoColumnLayoutHeader";
import { BreezIcon } from "src/components/icons/Breez";
Expand All @@ -12,6 +12,7 @@ import { BackendType } from "src/types";

import cashu from "src/assets/images/node/cashu.png";
import lnd from "src/assets/images/node/lnd.png";
import useSetupStore from "src/state/SetupStore";

type BackendTypeDefinition = {
id: BackendType;
Expand Down Expand Up @@ -54,17 +55,15 @@ const backendTypes: BackendTypeDefinition[] = [

export function SetupNode() {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const setupStore = useSetupStore();
const [selectedBackendType, setSelectedBackupType] =
React.useState<BackendTypeDefinition>();

function next() {
navigate(
`/setup/node/${selectedBackendType?.id.toLowerCase()}?wallet=${searchParams.get("wallet")}`
);
navigate(`/setup/node/${selectedBackendType?.id.toLowerCase()}`);
}

const importSelected = searchParams.get("wallet") === "import";
const hasImportedMnemonic = !!setupStore.nodeInfo.mnemonic;

return (
<>
Expand All @@ -77,7 +76,7 @@ export function SetupNode() {
<div className="w-full grid grid-cols-2 gap-4">
{backendTypes
.filter((item) =>
importSelected ? backendTypeHasMnemonic(item.id) : true
hasImportedMnemonic ? backendTypeHasMnemonic(item.id) : true
)
.map((item) => (
<div
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/screens/setup/SetupPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function SetupPassword() {
} else if (node) {
navigate(`/setup/node/${node}`);
} else {
navigate(`/setup/node?wallet=new`);
navigate(`/setup/node`);
}
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/src/screens/setup/node/LDKForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function LDKForm() {

// No configuration needed, automatically proceed with the next step
useEffect(() => {
if (searchParams.get("wallet") !== "import") {
// only generate a mnemonic if one is not already imported
if (!useSetupStore.getState().nodeInfo.mnemonic) {
useSetupStore.getState().updateNodeInfo({
mnemonic: bip39.generateMnemonic(wordlist, 128),
});
Expand Down

0 comments on commit 3c40311

Please sign in to comment.