Skip to content

Commit

Permalink
feat(llm): validate stellar memo on recipient step
Browse files Browse the repository at this point in the history
  • Loading branch information
thesan committed Oct 23, 2024
1 parent 5b4c88a commit 0364227
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
22 changes: 15 additions & 7 deletions apps/ledger-live-mobile/src/families/stellar/MemoTagInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { useState } from "react";
import { useTranslation } from "react-i18next";

import type { Transaction as StellarTransaction } from "@ledgerhq/live-common/families/stellar/types";
import type { MemoTagInputProps } from "LLM/features/MemoTag/types";
import { isMemoValid } from "@ledgerhq/live-common/families/stellar/bridge/logic";
import {
StellarWrongMemoFormat,
type Transaction as StellarTransaction,
} from "@ledgerhq/live-common/families/stellar/types";
import { AnimatedInputSelect } from "@ledgerhq/native-ui";
import type { MemoTagInputProps } from "LLM/features/MemoTag/types";
import { MemoTypeDrawer, MEMO_TYPES } from "./MemoTypeDrawer";

export default ({ onChange }: MemoTagInputProps<StellarTransaction>) => {
Expand All @@ -13,23 +17,27 @@ export default ({ onChange }: MemoTagInputProps<StellarTransaction>) => {
const [memoValue, setMemoValue] = React.useState("");
const [isOpen, setIsOpen] = useState(false);

const handleChange = (type: MemoType, value: string) => {
const error = isMemoValid(type, value) ? undefined : new StellarWrongMemoFormat();
const patch = { memoType: type, memoValue: value };
onChange({ value, patch, error });
};

const handleChangeType = (type: MemoType) => {
const value = type === "NO_MEMO" ? "" : memoValue;
handleChange(type, value);

setIsOpen(false);
setMemoType(type);
if (value !== memoValue) setMemoValue(value);

onChange({ value, patch: { memoType: type, memoValue: value } });
setIsOpen(false);
};

const handleChangeValue = (value: string) => {
const type = memoType === "NO_MEMO" && value ? "MEMO_TEXT" : memoType;
handleChange(type, value);

setMemoValue(value);
if (type !== memoType) setMemoType(type);

onChange({ value, patch: { memoType: type, memoValue: value } });
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export const useMemoTagInput = (
null;

const [isEmpty, setIsEmpty] = useState(true);
const [error, setError] = useState<Error | undefined>();
const handleChange = useCallback<MemoTagInputProps["onChange"]>(
({ patch, value }) => {
({ patch, value, error }) => {
setIsEmpty(!value);
setError(error);
updateTransaction(patch);
},
[updateTransaction],
);

return Input && { Input, isEmpty, handleChange };
return Input && { Input, isEmpty, error, handleChange };
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import type { AnimatedInputProps } from "@ledgerhq/native-ui/components/Form/Inp
export type MemoTagInputProps<T extends Transaction = Transaction> = Omit<
AnimatedInputProps,
"value" | "onChangeText" | "onChange"
> & { onChange: (update: { patch: Partial<T>; value: string }) => void };
> & { onChange: (update: { patch: Partial<T>; value: string; error?: Error }) => void };
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import NavigationScrollView from "~/components/NavigationScrollView";
import RetryButton from "~/components/RetryButton";
import { SendFundsNavigatorStackParamList } from "~/components/RootNavigator/types/SendFundsNavigator";
import { BaseComposite, StackNavigatorProps } from "~/components/RootNavigator/types/helpers";
import TranslatedError from "~/components/TranslatedError";
import { ScreenName } from "~/const";
import { accountScreenSelector } from "~/reducers/accounts";
import { currencySettingsForAccountSelector } from "~/reducers/settings";
Expand Down Expand Up @@ -316,6 +317,9 @@ export default function SendSelectRecipient({ navigation, route }: Props) {
placeholder={t("send.summary.memo.title")}
onChange={memoTag.handleChange}
/>
<LText mt={4} pl={2} color="alert">
<TranslatedError error={memoTag.error} />
</LText>
</View>
)}

Expand All @@ -337,7 +341,7 @@ export default function SendSelectRecipient({ navigation, route }: Props) {
testID="recipient-continue-button"
type="primary"
title={<Trans i18nKey="common.continue" />}
disabled={debouncedBridgePending || !!status.errors.recipient}
disabled={debouncedBridgePending || !!status.errors.recipient || memoTag?.error}
pending={debouncedBridgePending}
onPress={onPressContinue}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "@ledgerhq/coin-stellar/bridge/logic";

0 comments on commit 0364227

Please sign in to comment.