Skip to content

Commit

Permalink
fix: reactive chain and token properties from config (#294)
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Chybisov <imchybisov@gmail.com>
  • Loading branch information
DNR500 and chybisov authored Sep 18, 2024
1 parent ce05f7b commit 1ff7cfc
Show file tree
Hide file tree
Showing 29 changed files with 735 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import { ChainType } from '@lifi/types';
import { Button } from '@mui/material';
import { useState } from 'react';
import { useDevView } from '../../../hooks';
import { useConfigActions, useEditToolsActions } from '../../../store';
import type { FormValues } from '../../../store/widgetConfig/types';
import { useConfigFormValues } from '../../../store/widgetConfig/useConfigValues';
import { CardRowContainer, ExpandableCard } from '../../Card';
import { Switch } from '../../Switch';
import {
CapitalizeFirstLetter,
ColorControlContainer,
} from './DesignControls.style';

interface FormValuesLookUp {
[key: string]: FormValues;
}

const ChainsAndTokensLookUp: FormValuesLookUp = {
'ETH-ETH | ARB-USDC': {
fromChain: 1,
fromToken: '0x0000000000000000000000000000000000000000',
toChain: 42161,
toToken: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
},
'ARB-USDC | OPT-USDT': {
fromChain: 42161,
fromToken: '0xaf88d065e77c8cC2239327C5EDb3A432268e5831',
toChain: 10,
toToken: '0x94b008aA00579c1307B0EF2c499aD98a8ce58e58',
},
'From: OPT-USDT': {
fromChain: 10,
fromToken: '0x94b008aA00579c1307B0EF2c499aD98a8ce58e58',
},
'From: ARB-DAI': {
fromChain: 42161,
fromToken: '0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1',
},
'To: POL-WMATIC': {
toChain: 137,
toToken: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
},
'To: AVA-AVAX': {
toChain: 43114,
toToken: '0x0000000000000000000000000000000000000000',
},
RESET: {
fromChain: undefined,
fromToken: undefined,
toChain: undefined,
toToken: undefined,
},
'RESET From': {
fromChain: undefined,
fromToken: undefined,
},
'RESET To': {
toChain: undefined,
toToken: undefined,
},
};

const AddressLookUp: FormValuesLookUp = {
'0x29D...94eD7': {
toAddress: {
address: '0x29DaCdF7cCaDf4eE67c923b4C22255A4B2494eD7',
chainType: ChainType.EVM,
},
},
'0x457...22CE0': {
toAddress: {
address: '0x4577a46A3eCf44E0ed44410B7793977ffbe22CE0',
chainType: ChainType.EVM,
},
},
Lenny: {
toAddress: {
name: 'Lenny',
address: '0x552008c0f6870c2f77e5cC1d2eb9bdff03e30Ea9',
chainType: ChainType.EVM,
},
},
RESET: {
toAddress: undefined,
},
};

const fromAmountLookUp: FormValuesLookUp = {
'1': {
fromAmount: '1',
},
'0.5': {
fromAmount: 0.5,
},
RESET: {
fromAmount: undefined,
},
};

const toAmountLookUp: FormValuesLookUp = {
'1': {
toAmount: '1',
},
'0.5': {
toAmount: 0.5,
},
RESET: {
toAmount: undefined,
},
};

const forceConfigUpdate = (
nextValue: FormValues,
currentValues: FormValues,
): FormValues => ({
...nextValue,
formUpdateKey: new Date().valueOf().toString(),
});

export const FormValuesControl = () => {
const { setFormValues: setFormValuesViaConfig } = useConfigActions();
const { setFormValues: setFormValuesViaFormApiRef } = useEditToolsActions();
const formValues = useConfigFormValues();
const { isDevView } = useDevView();
const [formUpdateMethod, setFormUpdateMethod] = useState<
'formApi' | 'config'
>('config');

const setFormValues =
formUpdateMethod === 'formApi'
? setFormValuesViaFormApiRef
: setFormValuesViaConfig;

const handleChainAndTokenChange = (value: string) => {
const chainsAndTokens = ChainsAndTokensLookUp[value];

if (chainsAndTokens) {
setFormValues(forceConfigUpdate(chainsAndTokens, formValues));
}
};

const handleToAddressChange = (value: string) => {
const addressValue = AddressLookUp[value];

if (addressValue) {
setFormValues(forceConfigUpdate(addressValue, formValues));
}
};

const handleFromAmountChange = (value: string) => {
const amountValue = fromAmountLookUp[value];

if (amountValue) {
setFormValues(forceConfigUpdate(amountValue, formValues));
}
};

const handleToAmountChange = (value: string) => {
const amountValue = toAmountLookUp[value];

if (amountValue) {
setFormValues(forceConfigUpdate(amountValue, formValues));
}
};

return isDevView ? (
<ExpandableCard title={'Form values'} value=" ">
<CardRowContainer sx={{ paddingBottom: 1, paddingLeft: 1 }}>
<CapitalizeFirstLetter variant="caption">
This tool allows you to set the values in the config for fromChain,
fromToken, toChain, toToken, toAddress and fromAmount. You should see
the values update in the Widget and when the buildUrl property is not
set as false changes should be reflected in the query string
</CapitalizeFirstLetter>
</CardRowContainer>
<ColorControlContainer sx={{ marginBottom: 1, paddingRight: 2 }}>
Use config
<Switch
checked={formUpdateMethod === 'config'}
onChange={() => setFormUpdateMethod('config')}
aria-label="Set form values using reactive config"
/>
</ColorControlContainer>
<ColorControlContainer sx={{ marginBottom: 1, paddingRight: 2 }}>
Use formRef
<Switch
checked={formUpdateMethod === 'formApi'}
onChange={() => setFormUpdateMethod('formApi')}
aria-label="Set form values using formRef"
/>
</ColorControlContainer>
<CardRowContainer sx={{ paddingBottom: 1, paddingLeft: 1 }}>
Chains And Tokens
</CardRowContainer>
{Object.keys(ChainsAndTokensLookUp).map((key) => (
<Button
key={key}
variant="outlined"
onClick={() => handleChainAndTokenChange(key)}
sx={{ marginBottom: 1 }}
fullWidth
>
{key}
</Button>
))}

<CardRowContainer sx={{ paddingBottom: 1, paddingLeft: 1 }}>
From Amount
</CardRowContainer>
{Object.keys(fromAmountLookUp).map((key) => (
<Button
key={key}
variant="outlined"
onClick={() => handleFromAmountChange(key)}
sx={{ marginBottom: 1 }}
fullWidth
>
{key}
</Button>
))}

<CardRowContainer sx={{ paddingBottom: 1, paddingLeft: 1 }}>
To Amount
</CardRowContainer>
{Object.keys(toAmountLookUp).map((key) => (
<Button
key={key}
variant="outlined"
onClick={() => handleToAmountChange(key)}
sx={{ marginBottom: 1 }}
fullWidth
>
{key}
</Button>
))}

<CardRowContainer sx={{ paddingBottom: 1, paddingLeft: 1 }}>
To Address
</CardRowContainer>
{Object.keys(AddressLookUp).map((key) => (
<Button
key={key}
variant="outlined"
onClick={() => handleToAddressChange(key)}
sx={{ marginBottom: 1 }}
fullWidth
>
{key}
</Button>
))}
</ExpandableCard>
) : null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export const LayoutControls = () => {
</Select>
</ControlRowContainer>
{layoutsWithHeightControls.includes(selectedLayoutId) ? (
<CardRowContainer>
<CardRowContainer sx={{ padding: 0 }}>
<CardRowColumn>
<label htmlFor={inputId}>{inputLabel[selectedLayoutId]}</label>
{(heightValue && heightValue < defaultMaxHeight) || !heightValue ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
VariantControl,
WalletManagementControl,
} from './DesignControls';
import { FormValuesControl } from './DesignControls/FormValuesControls';
import {
Drawer,
DrawerContentContainer,
Expand Down Expand Up @@ -122,6 +123,7 @@ export const DrawerControls = () => {
<FontsControl />
<CardRadiusControl />
<ButtonRadiusControl />
<FormValuesControl />
<WalletManagementControl />
<SkeletonControl />
<LayoutControls />
Expand Down
20 changes: 18 additions & 2 deletions packages/widget-playground/src/components/Widget/WidgetView.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
import type { WidgetDrawer } from '@lifi/widget';
import type { FieldNames, FormState, WidgetDrawer } from '@lifi/widget';
import { LiFiWidget, WidgetSkeleton } from '@lifi/widget';
import { useCallback, useRef } from 'react';
import { useCallback, useEffect, useRef } from 'react';
import { useConfig, useSkeletonToolValues } from '../../store';
import { useFormValues } from '../../store/editTools/uesFormValues';
import { WidgetViewContainer } from './WidgetViewContainer';

export function WidgetView() {
const { config } = useConfig();
const drawerRef = useRef<WidgetDrawer>(null);
const formRef = useRef<FormState>(null);
const { isSkeletonShown, isSkeletonSideBySide } = useSkeletonToolValues();
const { formValues } = useFormValues();

const toggleDrawer = useCallback(() => {
drawerRef?.current?.toggleDrawer();
}, []);

useEffect(() => {
if (formRef.current && formValues) {
Object.entries(formValues).forEach(([fieldName, fieldValue]) => {
if (fieldName !== 'formUpdateKey') {
formRef.current?.setFieldValue(fieldName as FieldNames, fieldValue, {
setUrlSearchParam: true,
});
}
});
}
}, [formRef, formValues]);

return (
<WidgetViewContainer toggleDrawer={toggleDrawer}>
{!isSkeletonShown || isSkeletonSideBySide ? (
<LiFiWidget
config={config}
ref={drawerRef}
integrator="li.fi-playground"
formRef={formRef}
open
/>
) : null}
Expand Down
4 changes: 2 additions & 2 deletions packages/widget-playground/src/defaultWidgetConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const widgetBaseConfig: WidgetConfig = {
// fromAmount: '20',
// toAddress: {
// name: 'Jenny',
// address: '0xAB3Afc314e75dC1648A2E31c06861A07a048C050',
// address: '0xb9c0dE368BECE5e76B52545a8E377a4C118f597B',
// chainType: ChainType.EVM,
// },
// toAddresses: [
Expand All @@ -34,7 +34,7 @@ export const widgetBaseConfig: WidgetConfig = {
// fee: 0.01,
// useRecommendedRoute: true,
buildUrl: true,
// hiddenUI: ['poweredBy', 'language', 'appearance', 'drawerButton'],
// hiddenUI: ['poweredBy', 'language', 'appearance', 'drawerButton', 'toAddress'],
// disabledUI: ['toAddress', 'fromAmount', 'toToken', 'fromToken'],
// requiredUI: ['toAddress'],
// slippage: 0.003,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { WidgetTheme } from '@lifi/widget';
import type { StateCreator } from 'zustand';
import { persist } from 'zustand/middleware';
import { createWithEqualityFn } from 'zustand/traditional';
import type { FormValues } from '../widgetConfig/types';
import { defaultDrawerWidth } from './constants';
import type { ToolsState } from './types';

export const createEditToolsStore = (initialTheme?: WidgetTheme) =>
createWithEqualityFn<ToolsState>(
persist(
(set, get) => ({
formValues: undefined,
drawer: {
open: true,
visibleControls: 'design',
Expand Down Expand Up @@ -151,6 +153,11 @@ export const createEditToolsStore = (initialTheme?: WidgetTheme) =>
isDevView,
});
},
setFormValues: (formValues: FormValues) => {
set({
formValues,
});
},
}),
{
name: 'li.fi-playground-tools',
Expand Down
3 changes: 3 additions & 0 deletions packages/widget-playground/src/store/editTools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { WidgetTheme } from '@lifi/widget';
import type { StoreApi } from 'zustand';
import type { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
import type { Font } from '../../providers';
import type { FormValues } from '../widgetConfig/types';

type ControlType = 'design' | 'code';
type CodeControlTab = 'config' | 'examples';
Expand Down Expand Up @@ -60,6 +61,7 @@ export interface EditToolsValues {
selectedLayoutId: Layout;
};
isDevView: boolean;
formValues?: Partial<FormValues>;
}

export interface EditToolsActions {
Expand All @@ -77,6 +79,7 @@ export interface EditToolsActions {
setFixedFooter: (isFixed: boolean) => void;
setSelectedLayoutId: (layoutId: Layout) => void;
setIsDevView: (isDevView: boolean) => void;
setFormValues: (formValues: FormValues) => void;
}

export type ToolsState = EditToolsValues & EditToolsActions;
Expand Down
Loading

0 comments on commit 1ff7cfc

Please sign in to comment.