Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
dgca committed Nov 13, 2024
1 parent 11c6daf commit cbca050
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 10 additions & 7 deletions renderer/ui/Forms/MnemonicPhrase/MnemonicPhrase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { useCopyToClipboard, useToggle } from "usehooks-ts";
import { COLORS } from "@/ui/colors";
import { useIFToast } from "@/ui/Toast/Toast";
import { useHasGroupBlur } from "@/utils/formUtils";
import { formatMnemonic } from "@/utils/mnemonic";
import {
formatMnemonic,
formatMnemonicArray,
formatMnemonicWord,
} from "@/utils/mnemonic";
import { MergeProps } from "@/utils/react";

import { FormField, FormFieldProps } from "../FormField/FormField";
Expand Down Expand Up @@ -80,7 +84,7 @@ export function MnemonicPhrase({
}
const index = parseInt(number, 10) - 1;
const nextValues = phrase
.toSpliced(index, 1, e.target.value.toLowerCase().replace(/\s+/g, ""))
.toSpliced(index, 1, formatMnemonicWord(e.target.value))
.slice(0, PHRASE_ITEM_COUNT);
const formatted = formatMnemonic(nextValues);
onChange(formatted.split(/\s+/));
Expand All @@ -100,11 +104,10 @@ export function MnemonicPhrase({
throw new Error("data-number not found in mnemonic phrase input");
}

const words = e.clipboardData
.getData("text")
.toLowerCase()
.trim()
.split(/\s+/g);
const clipboardText = e.clipboardData.getData("text");

const words = formatMnemonicArray(clipboardText.split(/\s+/g));

const index = parseInt(number, 10) - 1;

if (words.length === PHRASE_ITEM_COUNT) {
Expand Down
8 changes: 5 additions & 3 deletions renderer/utils/mnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ function lowerCaseAZ(str: string) {
});
}

export function formatMnemonicWord(word: string) {
return lowerCaseAZ(word).trim();
}

export function formatMnemonicArray(phrase: Array<string>) {
return phrase.map((word) => {
return lowerCaseAZ(word).trim();
});
return phrase.map(formatMnemonicWord);
}

export function formatMnemonic(phrase: string | Array<string>) {
Expand Down

0 comments on commit cbca050

Please sign in to comment.