Skip to content

Commit b48e294

Browse files
committed
refactor: Remove unused TonConnectUI import and commented code
- Removed the unused import of `useTonConnectUI` from `lib/components/Header/Wallet.tsx` and `lib/components/Swap/Swap.tsx`. - Also removed the commented code related to `useTonConnectUI` in `lib/components/Header/Wallet.tsx` and `lib/components/Swap/Swap.tsx`. fix: Fix error handling and reporting in multiple files - Updated error handling and reporting in `lib/components/Header/Wallet.tsx`, `lib/components/Swap/Swap.tsx`, `lib/components/SwapButton/ConfirmationModal.tsx`. - Removed the unused import of `useTonConnectUI` from `lib/components/SwapButton/ConfirmationModal.tsx`. refactor: Update Swap component rendering optimization - Optimized the rendering of the Swap component in `lib/components/Swap/Swap.tsx` by removing unnecessary imports and code. update: Update readme file - Updated the readme file. feat: Add createSwap function to index.tsx - Added a new function `createSwap` to `lib/components/index.tsx` that allows creating a chore: Update options store to include TonConnectUI instance - Updated the options store in `lib/store/options.store.ts` to include a TonConnectUI instance. - Added a new action `setTonConnectInstance` to set the TonConnectUI instance in the options store.
1 parent 3d2a221 commit b48e294

File tree

7 files changed

+53
-51
lines changed

7 files changed

+53
-51
lines changed

lib/components/Header/Wallet.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import shortAddress from "../../utils/shortAddress";
88
import { FaCheck, FaCopy } from "react-icons/fa6";
99
import { useState } from "react";
1010
import { MdArrowOutward } from "react-icons/md";
11-
import { useTonConnectUI } from "@tonconnect/ui-react";
11+
// import { useTonConnectUI } from "@tonconnect/ui-react";
1212

1313
const Wallet = () => {
1414
// make function and state for copy to clipboard address button
@@ -28,9 +28,9 @@ const Wallet = () => {
2828
};
2929

3030
const { wallet, balance, disconnect } = useWalletStore();
31-
const [tc] = useTonConnectUI();
31+
// const [tc] = useTonConnectUI();
3232
const handleDisconnect = async () => {
33-
await tc.disconnect();
33+
// await tc.disconnect();
3434
disconnect();
3535
};
3636
const { colors } = useThemeStore();

lib/components/Swap/Swap.tsx

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,18 @@ import SwapCard from "../SwapCard/SwapCard";
44
import SwapDetails from "../SwapDetails/SwapDetails";
55
import clsx from "clsx";
66
import { ColorTheme, useThemeStore } from "../../store/theme.store";
7-
import {
8-
ITonConnect,
9-
TonConnectUIProvider,
10-
useTonWallet,
11-
} from "@tonconnect/ui-react";
7+
import { TonConnectUI, useTonWallet } from "@tonconnect/ui-react";
128
import { useOptionsStore, SwapOptions } from "../../store/options.store";
139
import { ModalState, useSwapStore } from "../../store/swap.store";
1410
import { useWalletStore } from "../../store/wallet.store";
1511
import SwapButton from "../SwapButton/SwapButton";
1612
import "./Swap.scss";
1713
import { ErrorBoundary } from "react-error-boundary";
1814
import { Toaster } from "react-hot-toast";
19-
type SwapProps = {
20-
theme?: ColorTheme;
21-
options?: SwapOptions;
22-
} & (
23-
| { connector: ITonConnect; manifestUrl?: never }
24-
| { connector?: never; manifestUrl: string }
25-
);
26-
27-
type SwapComponentProps = {
15+
export type SwapProps = {
2816
theme?: ColorTheme;
2917
options?: SwapOptions;
18+
tonConnectInstance: TonConnectUI;
3019
};
3120

3221
// declare telegram in window
@@ -40,12 +29,19 @@ declare global {
4029
}
4130
}
4231

43-
export const SwapComponent: FC<SwapComponentProps> = ({ theme, options }) => {
32+
export const SwapComponent: FC<SwapProps> = ({
33+
theme,
34+
options,
35+
tonConnectInstance,
36+
}) => {
4437
const { colors, setTheme } = useThemeStore();
45-
const { setOptions } = useOptionsStore();
38+
const { setOptions, setTonConnectInstance } = useOptionsStore();
4639
if (theme) {
4740
setTheme(theme);
4841
}
42+
if (tonConnectInstance) {
43+
setTonConnectInstance(tonConnectInstance);
44+
}
4945
if (options) {
5046
setOptions(options);
5147
}
@@ -114,7 +110,11 @@ export const SwapComponent: FC<SwapComponentProps> = ({ theme, options }) => {
114110
style={{ color: colors.text_black }}
115111
>
116112
Service provided by{" "}
117-
<a href="https://mytonswap.com" target="_blank">
113+
<a
114+
href="https://mytonswap.com"
115+
target="_blank"
116+
rel="noreferrer"
117+
>
118118
MyTonSwap
119119
</a>
120120
</div>
@@ -127,18 +127,6 @@ export const SwapComponent: FC<SwapComponentProps> = ({ theme, options }) => {
127127
);
128128
};
129129

130-
export const Swap: FC<SwapProps> = ({
131-
connector,
132-
manifestUrl,
133-
...restProps
134-
}) => {
135-
return (
136-
<TonConnectUIProvider
137-
{...(connector
138-
? { connector: connector }
139-
: { manifestUrl: manifestUrl })}
140-
>
141-
<SwapComponent {...restProps} />
142-
</TonConnectUIProvider>
143-
);
130+
export const Swap: FC<SwapProps> = ({ ...restProps }) => {
131+
return <SwapComponent {...restProps} />;
144132
};

lib/components/SwapButton/ConfirmationModal.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { FaArrowRightArrowLeft } from "react-icons/fa6";
66
import SwapKeyValue from "../SwapDetails/SwapKeyValue";
77
import formatNumber from "../../utils/formatNum";
88
import swap from "../../utils/swap";
9-
import { useTonConnectUI } from "@tonconnect/ui-react";
109
import { FC } from "react";
1110
import "./ConfirmationModal.scss";
11+
import { useOptionsStore } from "../../store/options.store";
1212
type ConfirmationModalProps = {
1313
setConfirmModal: (state: boolean) => void;
1414
};
@@ -17,10 +17,10 @@ const ConfirmationModal: FC<ConfirmationModalProps> = ({ setConfirmModal }) => {
1717
const handleConfirmClose = () => {
1818
setConfirmModal(false);
1919
};
20-
const [tonConnect, setOptions] = useTonConnectUI();
21-
setOptions({
22-
actionsConfiguration: { modals: [] },
23-
});
20+
const { tonConnectInstance } = useOptionsStore();
21+
// tonConnectInstance?.uiOptions = {
22+
// actionsConfiguration: { modals: [] },
23+
// }
2424

2525
const { colors } = useThemeStore();
2626
const {
@@ -33,9 +33,11 @@ const ConfirmationModal: FC<ConfirmationModalProps> = ({ setConfirmModal }) => {
3333
setModalState,
3434
} = useSwapStore();
3535
const handleConfirmSwap = () => {
36-
swap(tonConnect, bestRoute!);
37-
setConfirmModal(false);
38-
setModalState(ModalState.WAITING);
36+
if (tonConnectInstance?.wallet) {
37+
swap(tonConnectInstance, bestRoute!);
38+
setConfirmModal(false);
39+
setModalState(ModalState.WAITING);
40+
}
3941
};
4042
return (
4143
<div className="confirm-modal-container">

lib/components/SwapButton/SwapButton.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useThemeStore } from "../../store/theme.store";
2-
import { useTonConnectModal, useTonWallet } from "@tonconnect/ui-react";
32
import { useWalletStore } from "../../store/wallet.store";
43
import { ModalState, useSwapStore } from "../../store/swap.store";
54
import { AnimatePresence, motion } from "framer-motion";
@@ -18,19 +17,20 @@ import {
1817
} from "../../constants";
1918
import { useLongPress } from "@uidotdev/usehooks";
2019
import toast from "react-hot-toast";
20+
import { useOptionsStore } from "../../store/options.store";
2121

2222
const SwapButton = () => {
23-
const { open } = useTonConnectModal();
23+
const { tonConnectInstance } = useOptionsStore();
2424
const { colors } = useThemeStore();
25-
const wallet = useTonWallet();
25+
2626
const isDesktop = useMediaQuery("(min-width: 768px)");
2727
const { balance } = useWalletStore();
2828
const { pay_amount, pay_token, bestRoute, swapModal, receive_token } =
2929
useSwapStore();
3030

3131
const [confirmModal, setConfirmModal] = useState(false);
3232
const getSwapText = () => {
33-
if (!wallet) return "Connect Wallet";
33+
if (!tonConnectInstance?.wallet) return "Connect Wallet";
3434
if (!receive_token || !pay_token) return "Choose a token";
3535
if (pay_amount === 0n) return "Enter an amount";
3636
if (bestRoute && !bestRoute.pool_data.status)
@@ -42,7 +42,7 @@ const SwapButton = () => {
4242
};
4343

4444
const isButtonDisabled = () => {
45-
if (!wallet) return false;
45+
if (!tonConnectInstance?.wallet) return false;
4646
if (!pay_amount || !pay_token) return true;
4747
if (bestRoute && !bestRoute.pool_data.status) return true;
4848
if (pay_amount > Number(balance.get(pay_token!.address)?.balance ?? 0))
@@ -55,8 +55,8 @@ const SwapButton = () => {
5555
const buttonDisabled = isButtonDisabled();
5656

5757
const handleSwapClick = () => {
58-
if (!wallet) {
59-
open();
58+
if (!tonConnectInstance?.wallet) {
59+
tonConnectInstance?.openModal();
6060
} else {
6161
setConfirmModal(true);
6262
}

lib/components/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

lib/components/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Swap, SwapProps } from "./Swap/Swap";
2+
import { createRoot } from "react-dom/client";
3+
export const createSwap = (elementId: string, options: SwapProps) => {
4+
const root = document.getElementById(elementId);
5+
if (!root) throw new Error("Element does not exist");
6+
createRoot(root).render(<Swap {...options} />);
7+
};

lib/store/options.store.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { create } from "zustand";
22
import { defaultsDeep } from "lodash";
3+
import { TonConnectUI } from "@tonconnect/ui-react";
34

45
export type SwapOptions = {
56
default_pay_token?: string;
@@ -14,14 +15,17 @@ export type SwapOptions = {
1415
type SwapOptionsStates = {
1516
options: SwapOptions;
1617
userOptions: SwapOptions;
18+
tonConnectInstance: TonConnectUI | null;
1719
};
1820

1921
type SwapOptionsActions = {
2022
setOptions: (options: SwapOptions) => void;
23+
setTonConnectInstance: (instance: TonConnectUI) => void;
2124
};
2225

2326
export const useOptionsStore = create<SwapOptionsActions & SwapOptionsStates>(
2427
(set, get) => ({
28+
tonConnectInstance: null,
2529
options: {
2630
ui_preferences: {
2731
disable_provided_text: false,
@@ -37,5 +41,8 @@ export const useOptionsStore = create<SwapOptionsActions & SwapOptionsStates>(
3741
) satisfies SwapOptions;
3842
set({ options: newSchema, userOptions: option });
3943
},
44+
setTonConnectInstance: (instance) => {
45+
set({ tonConnectInstance: instance });
46+
},
4047
})
4148
);

0 commit comments

Comments
 (0)