From 18e75aa4d3b1211792aee1bf1b03911901484c15 Mon Sep 17 00:00:00 2001 From: Tom Robiquet Date: Wed, 4 Oct 2023 15:50:52 +0200 Subject: [PATCH 1/6] setup components for fee paying asset selection --- components/settings/FeePayingAssetSelect.tsx | 67 ++++++++++++++++++++ components/settings/SettingsModal.tsx | 30 +++++++-- lib/state/fee-paying-asset.ts | 19 ++++++ 3 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 components/settings/FeePayingAssetSelect.tsx create mode 100644 lib/state/fee-paying-asset.ts diff --git a/components/settings/FeePayingAssetSelect.tsx b/components/settings/FeePayingAssetSelect.tsx new file mode 100644 index 000000000..36eb15a76 --- /dev/null +++ b/components/settings/FeePayingAssetSelect.tsx @@ -0,0 +1,67 @@ +import AssetSelect, { AssetOption } from "components/ui/AssetSelect"; +import { + FOREIGN_ASSET_METADATA, + findAssetImageForSymbol, +} from "lib/constants/foreign-asset"; +import { useAllAssetMetadata } from "lib/hooks/queries/useAssetMetadata"; +import { useMemo } from "react"; + +const isSupportedAsset = (id: number) => { + return Object.keys(FOREIGN_ASSET_METADATA).includes(`${id}`); +}; + +const FeePayingAssetSelect = () => { + const { data: assetMetadata, isSuccess } = useAllAssetMetadata(); + + const options = useMemo(() => { + if (!isSuccess) { + return []; + } + let options: AssetOption[] = []; + for (const [id, meta] of assetMetadata) { + if (id === "Ztg") { + options = [ + ...options, + { + label: meta.symbol, + value: { Ztg: null }, + image: findAssetImageForSymbol(), + }, + ]; + } else { + if (!isSupportedAsset(id)) { + continue; + } + options = [ + ...options, + { + label: meta.symbol, + value: { ForeignAsset: id }, + image: findAssetImageForSymbol(meta.symbol), + }, + ]; + } + } + return options; + }, [assetMetadata, isSuccess]); + + return ( +
+ +
+ { + // onAssetChange?.(option); + }} + /> +
+
+ ); +}; +export default FeePayingAssetSelect; diff --git a/components/settings/SettingsModal.tsx b/components/settings/SettingsModal.tsx index 1acf202f5..cc268d0c2 100644 --- a/components/settings/SettingsModal.tsx +++ b/components/settings/SettingsModal.tsx @@ -1,11 +1,11 @@ -import React, { Fragment, useState } from "react"; import { Dialog, Tab } from "@headlessui/react"; import Modal from "components/ui/Modal"; -import AcccountSettingsForm from "./AccountSettingsForm"; -import OtherSettingsForm from "./OtherSettingsForm"; import { useIdentity } from "lib/hooks/queries/useIdentity"; import { useWallet } from "lib/state/wallet"; -import { AddressOption } from "components/ui/AddressInput"; +import React, { Fragment } from "react"; +import AcccountSettingsForm from "./AccountSettingsForm"; +import FeePayingAssetSelect from "./FeePayingAssetSelect"; +import OtherSettingsForm from "./OtherSettingsForm"; export type SettingsModalProps = { open: boolean; @@ -14,7 +14,8 @@ export type SettingsModalProps = { enum TabSelection { Account, - Other, + Proxy, + Fees, } const SettingsModal: React.FC = ({ open, onClose }) => { @@ -58,7 +59,21 @@ const SettingsModal: React.FC = ({ open, onClose }) => { (selected ? "font-semibold text-black" : "") } > - Other Settings + Proxy + + )} + + +
+ + {({ selected }) => ( + + Fee Paying Asset )} @@ -73,7 +88,8 @@ const SettingsModal: React.FC = ({ open, onClose }) => { ) : ( <> ), - [TabSelection.Other]: , + [TabSelection.Proxy]: , + [TabSelection.Fees]: , }[tabSelection] } diff --git a/lib/state/fee-paying-asset.ts b/lib/state/fee-paying-asset.ts new file mode 100644 index 000000000..4504bee3e --- /dev/null +++ b/lib/state/fee-paying-asset.ts @@ -0,0 +1,19 @@ +import { useAtom } from "jotai"; +import { persistentAtom } from "./util/persistent-atom"; + +const feePayingAssetStateAtom = persistentAtom({ + key: "fee-paying-asset", + defaultValue: "", +}); + +const useFeePayingAssetSelection = () => { + const [state, setState] = useAtom(feePayingAssetStateAtom); + + const setAsset = (selection: any) => { + setState(selection); + }; + + return { assetSelection: state, setAsset }; +}; + +export default useFeePayingAssetSelection; From b394cc28e5e51449aa4f622b23739f304171dce3 Mon Sep 17 00:00:00 2001 From: Tom Robiquet Date: Fri, 6 Oct 2023 12:42:39 +0200 Subject: [PATCH 2/6] saved selected asset --- components/settings/FeePayingAssetSelect.tsx | 35 ++++++++++--- components/ui/AssetSelect.tsx | 52 ++++++++++++-------- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/components/settings/FeePayingAssetSelect.tsx b/components/settings/FeePayingAssetSelect.tsx index 36eb15a76..edadf8a80 100644 --- a/components/settings/FeePayingAssetSelect.tsx +++ b/components/settings/FeePayingAssetSelect.tsx @@ -4,14 +4,23 @@ import { findAssetImageForSymbol, } from "lib/constants/foreign-asset"; import { useAllAssetMetadata } from "lib/hooks/queries/useAssetMetadata"; -import { useMemo } from "react"; +import useFeePayingAssetSelection from "lib/state/fee-paying-asset"; +import { useMemo, useState } from "react"; +import { Check } from "react-feather"; const isSupportedAsset = (id: number) => { return Object.keys(FOREIGN_ASSET_METADATA).includes(`${id}`); }; +const defaultSelection = { + label: "Default", + additionalText: "Uses first available asset", +}; + const FeePayingAssetSelect = () => { const { data: assetMetadata, isSuccess } = useAllAssetMetadata(); + const { assetSelection, setAsset } = useFeePayingAssetSelection(); + const [showSaved, setShowSaved] = useState(false); const options = useMemo(() => { if (!isSuccess) { @@ -42,22 +51,36 @@ const FeePayingAssetSelect = () => { ]; } } + + options.push(defaultSelection); return options; }, [assetMetadata, isSuccess]); return ( -
- +
+
+ + {showSaved && ( +
+ +
Saved
+
+ )} +
{ - // onAssetChange?.(option); + setAsset(option); + setShowSaved(true); + setTimeout(() => { + setShowSaved(false); + }, 3000); }} />
diff --git a/components/ui/AssetSelect.tsx b/components/ui/AssetSelect.tsx index 7f6fa45b9..f8fb68e3f 100644 --- a/components/ui/AssetSelect.tsx +++ b/components/ui/AssetSelect.tsx @@ -17,8 +17,9 @@ import Select, { export type AssetOption = { label: string; - value: AssetId; - image: string; + value?: AssetId; + image?: string; + additionalText?: string; }; const Control = ({ children, ...rest }: ControlProps) => { @@ -52,21 +53,25 @@ const SingleValue = (props: SingleValueProps) => { const { label, image } = props.data; return (
- {label} + {image ? ( + {label} + ) : ( +
+ )} {label}
); }; const Option = (props: OptionProps) => { - const { label, value, image } = props.data; + const { label, value, image, additionalText } = props.data; const wallet = useWallet(); const address = wallet.activeAccount?.address; @@ -77,20 +82,27 @@ const Option = (props: OptionProps) => { {...props} className="!flex items-center w-full bg-anti-flash-white rounded-md h-14 mb-2 last:mb-0 font-semibold px-4 !cursor-pointer" > - {label} + {image ? ( + {label} + ) : ( +
+ )} {label} {balance && (
Balance: {formatNumberLocalized(balance.div(ZTG).toNumber())}
)} + {additionalText && ( +
{additionalText}
+ )} ); }; @@ -119,7 +131,7 @@ const AssetSelect: React.FC = ({ }) => { return ( Date: Fri, 6 Oct 2023 13:26:15 +0200 Subject: [PATCH 6/6] keep current behaviour --- components/settings/FeePayingAssetSelect.tsx | 1 + components/ui/AssetSelect.tsx | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/components/settings/FeePayingAssetSelect.tsx b/components/settings/FeePayingAssetSelect.tsx index 2e05510b2..2ac5ff3e1 100644 --- a/components/settings/FeePayingAssetSelect.tsx +++ b/components/settings/FeePayingAssetSelect.tsx @@ -75,6 +75,7 @@ const FeePayingAssetSelect = () => { { setAsset(option); setShowSaved(true); diff --git a/components/ui/AssetSelect.tsx b/components/ui/AssetSelect.tsx index f8fb68e3f..c73f84df0 100644 --- a/components/ui/AssetSelect.tsx +++ b/components/ui/AssetSelect.tsx @@ -122,16 +122,18 @@ export type AssetSelectProps = { options: AssetOption[]; selectedOption?: AssetOption; onChange: (value: AssetOption) => void; + showArrowRight?: boolean; }; const AssetSelect: React.FC = ({ options, selectedOption, onChange, + showArrowRight = false, }) => { return (