Skip to content

Commit

Permalink
feat: add search by name on address inputs (#643)
Browse files Browse the repository at this point in the history
* feat: add searching by TNS on wallet address inputs

* Update packages/components/modals/SendModal.tsx

Co-authored-by: n0izn0iz <[email protected]>

---------

Co-authored-by: n0izn0iz <[email protected]>
  • Loading branch information
WaDadidou and n0izn0iz authored Jul 12, 2023
1 parent bdd2e17 commit 9391963
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
50 changes: 50 additions & 0 deletions packages/components/inputs/SearchNSInputContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { FC } from "react";
import { StyleProp, View, ViewStyle } from "react-native";

import { useNameSearch } from "../../hooks/search/useNameSearch";
import { useSelectedNetworkId } from "../../hooks/useSelectedNetwork";
import { layout } from "../../utils/style/layout";
import { AvatarWithName } from "../user/AvatarWithName";

// Used as a wrapper on a TextInput or something like. It allows to search a name in TNS and returns the address.
export const SearchNSInputContainer: FC<{
searchText: string;
onPressName: (userId: string) => void;
style?: StyleProp<ViewStyle>;
}> = ({ searchText, onPressName, style, children }) => {
const selectedNetworkId = useSelectedNetworkId();
const { names } = useNameSearch({
networkId: selectedNetworkId,
input: searchText,
limit: 12,
});
const hasNames = !!names.length;

return (
<>
{children}
{hasNames && (
<View
style={[
{
flexDirection: "row",
flexWrap: "wrap",
marginTop: layout.padding_x1_5,
},
style,
]}
>
{names.map((n) => (
<AvatarWithName
key={n}
networkId={selectedNetworkId}
name={n}
onPress={(userId) => onPressName(userId)}
style={{ marginRight: layout.padding_x1 }}
/>
))}
</View>
)}
</>
);
};
37 changes: 23 additions & 14 deletions packages/components/modals/SendModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { SVG } from "../SVG";
import { MaxButton } from "../buttons/MaxButton";
import { PrimaryButton } from "../buttons/PrimaryButton";
import { DAOSelector } from "../dao/DAOSelector";
import { SearchNSInputContainer } from "../inputs/SearchNSInputContainer";
import { TextInputCustom } from "../inputs/TextInputCustom";
import { SpacerColumn, SpacerRow } from "../spacer";

Expand Down Expand Up @@ -64,7 +65,7 @@ export const SendModal: React.FC<SendModalProps> = ({
}) => {
const { setToastError, setToastSuccess } = useFeedbacks();
const selectedWallet = useSelectedWallet();
const { control, setValue, handleSubmit } = useForm<TransactionForm>();
const { control, setValue, handleSubmit, watch } = useForm<TransactionForm>();
const [selectedDAOId, setSelectedDAOId] = useState("");
const makeProposal = useDAOMakeProposal(selectedDAOId);
const [, daoAddress] = parseUserId(selectedDAOId);
Expand Down Expand Up @@ -206,19 +207,27 @@ export const SendModal: React.FC<SendModalProps> = ({
>
<FlexRow alignItems="flex-end">
<FlexCol alignItems="flex-start" width={356}>
<TextInputCustom<TransactionForm>
height={48}
width={320}
control={control}
variant="labelOutside"
label="Receiver"
name="toAddress"
rules={{ required: true }}
placeHolder={`Enter a ${
getNetwork(networkId)?.displayName || networkId
} address`}
defaultValue=""
/>
<SearchNSInputContainer
onPressName={(userId) => {
const [, userAddress] = parseUserId(userId);
setValue("toAddress", userAddress);
}}
searchText={watch("toAddress")}
>
<TextInputCustom<TransactionForm>
height={48}
width={320}
control={control}
variant="labelOutside"
label="Receiver"
name="toAddress"
rules={{ required: true }}
placeHolder={`Enter a ${
getNetwork(networkId)?.displayName || networkId
} name or address`}
defaultValue=""
/>
</SearchNSInputContainer>
</FlexCol>
<ContactButton />
</FlexRow>
Expand Down
8 changes: 4 additions & 4 deletions packages/components/user/AvatarWithName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const AvatarWithName: React.FC<
userId: string | undefined;
}
) & {
style: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
onPress: (userId: string) => void;
}
> = (props) => {
Expand All @@ -31,7 +31,7 @@ export const AvatarWithName: React.FC<
export const AvatarWithNameFromName: React.FC<{
networkId: string | undefined;
name: string | undefined;
style: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
onPress: (userId: string) => void;
}> = ({ networkId, name, style, onPress }) => {
const { nameOwner } = useNSNameOwner(networkId, name);
Expand All @@ -47,7 +47,7 @@ export const AvatarWithNameFromName: React.FC<{

export const AvatarWithNameFromUserId: React.FC<{
userId: string | undefined;
style: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
onPress: (userId: string) => void;
}> = ({ userId, style, onPress }) => {
const { primaryAlias } = useNSPrimaryAlias(userId);
Expand All @@ -64,7 +64,7 @@ export const AvatarWithNameFromUserId: React.FC<{
export const AvatarWithNameView: React.FC<{
name: string | undefined;
userId: string | undefined;
style: StyleProp<ViewStyle>;
style?: StyleProp<ViewStyle>;
onPress: (userId: string) => void;
}> = ({ name, userId, style, onPress }) => {
const [, userAddress] = parseUserId(userId);
Expand Down

0 comments on commit 9391963

Please sign in to comment.