Skip to content

Commit

Permalink
feat: dynamic actions on floating button
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s committed Mar 15, 2024
1 parent 3476c77 commit 5717734
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 53 deletions.
8 changes: 7 additions & 1 deletion packages/devnext/src/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ export const Layout = ({ children }: LayoutProps) => {
router.push('/');
}}
/>
<FloatingMetaMaskButton distance={{ bottom: 40 }} />
<FloatingMetaMaskButton
distance={{ bottom: 40 }}
buy={true}
gasprice={true}
network={true}
swap={true}
/>
{children}
<SDKDebugPanel bottom={40} />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StyleSheet,
} from 'react-native';
import { FAB } from 'react-native-paper';
// eslint-disable-next-line @typescript-eslint/no-unused-vars

import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import FABGroupFix from '../fab-group-fix/FabGroupFix';
import { IconOriginal } from '../icons/IconOriginal';
Expand All @@ -16,11 +16,21 @@ import {
MetaMaskModalProps,
} from '../metamask-modal/metamask-modal';

export interface ActionConfig {
label: string;
icon: string;
onPress: () => void;
}
export interface FloatingMetaMaskButtonProps {
distance?: {
bottom?: number;
right?: number;
};
network?: boolean;
swap?: boolean;
buy?: boolean;
gasprice?: boolean;
customActions?: ActionConfig[];
}

const getStyles = ({
Expand All @@ -41,8 +51,18 @@ const getStyles = ({
});
};

// Icon component defined outside
const InfuraGasPriceIcon = ({ color }: { color: string }) => {
return <MaterialIcons name="price-change" color={color} size={24} />;
};

export const FloatingMetaMaskButton = ({
distance,
network,
swap,
buy,
gasprice,
customActions,
}: FloatingMetaMaskButtonProps) => {
const { sdk, connected, connecting, provider } = useSDK();
const [modalOpen, setModalOpen] = useState(false);
Expand Down Expand Up @@ -83,6 +103,73 @@ export const FloatingMetaMaskButton = ({
Platform.OS === 'web' && /Mobi|Android/i.test(navigator.userAgent);
const DynamicFabGroup = isMobileBrowser ? FABGroupFix : FAB.Group;

const generateActions = () => {
const actions = [];

if (network) {
actions.push({
label: 'Network',
icon: 'swap-horizontal',
onPress: () => {
setTarget('network');
setModalOpen(true);
},
});
}

if (swap) {
actions.push({
label: 'SWAP',
icon: 'swap-horizontal',
onPress: () => {
setTarget('swap');
setModalOpen(true);
},
});
}

if (buy) {
actions.push({
label: 'Buy ETH',
icon: 'swap-horizontal',
onPress: () => {
provider?.request({
method: 'metamask_open',
params: [{ target: 'buy' }],
});
},
});
}

if (gasprice) {
actions.push({
icon: InfuraGasPriceIcon,
label: 'Infura GAS Api',
onPress: () => {
setTarget('gasprice');
setModalOpen(true);
},
});
}

// Add customActions
if (customActions) {
actions.push(...customActions);
}

// Additional action for disconnection
actions.push({
label: 'Disconnect',
icon: 'logout',
onPress: () => {
sdk?.terminate();
setActive(false);
},
});

return actions;
};

return (
<>
<DynamicFabGroup
Expand All @@ -92,57 +179,7 @@ export const FloatingMetaMaskButton = ({
onPress={handlePress}
fabStyle={styles.fabStyle}
style={styles.container}
actions={[
// {
// icon: () => <IconOriginal />,
// label: 'Open MetaMask',
// onPress: () => console.log('Pressed notifications'),
// },
{
label: 'Network',
icon: 'swap-horizontal',
onPress: () => {
setTarget('network');
setModalOpen(true);
},
},
// {
// label: 'Buy ETH',
// icon: 'swap-horizontal',
// onPress: () => {
// provider?.request({
// method: 'metamask_open',
// params: [{ target: 'buy' }],
// });
// },
// },
// {
// label: 'SWAP',
// icon: 'swap-horizontal',
// onPress: () => {
// setTarget('swap');
// setModalOpen(true);
// },
// },
// {
// icon: ({ color }) => (
// <MaterialIcons name="price-change" color={color} size={24} />
// ),
// label: 'Infura GAS Api',
// onPress: () => {
// setTarget('gasprice');
// setModalOpen(true);
// },
// },
{
label: 'Disconnect',
icon: 'logout',
onPress: () => {
sdk?.terminate();
setActive(false);
},
},
]}
actions={generateActions()}
onStateChange={handleStateChange}
/>
<MetaMaskModal
Expand Down

0 comments on commit 5717734

Please sign in to comment.