Skip to content

Commit

Permalink
feat: disconnect pairings
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Aug 6, 2024
1 parent dcb6ac5 commit 35896d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
19 changes: 19 additions & 0 deletions advanced/dapps/react-dapp-v2/src/contexts/ClientContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface IContext {
client: Client | undefined;
session: SessionTypes.Struct | undefined;
connect: (pairing?: { topic: string }) => Promise<void>;
disconnectPairing: (pairing?: { topic: string }) => Promise<void>;
disconnect: () => Promise<void>;
isInitializing: boolean;
chains: string[];
Expand Down Expand Up @@ -192,6 +193,21 @@ export function ClientContextProvider({
[chains, client, onSessionConnected]
);

const disconnectPairing = useCallback(
async (pairing: any) => {
if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized");
}
if (typeof pairing === "undefined") {
throw new Error("Pairing is not defined");
}

await client.core.pairing.disconnect({ topic: pairing?.topic });
setPairings(client.pairing.getAll({ active: true }));
},
[client]
);

const disconnect = useCallback(async () => {
if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized");
Expand Down Expand Up @@ -260,6 +276,7 @@ export function ClientContextProvider({
);
console.log("RESTORED SESSION:", _session);
await onSessionConnected(_session);

return _session;
}
},
Expand Down Expand Up @@ -375,6 +392,7 @@ export function ClientContextProvider({
setChains,
setRelayerRegion,
origin,
disconnectPairing,
}),
[
pairings,
Expand All @@ -392,6 +410,7 @@ export function ClientContextProvider({
setChains,
setRelayerRegion,
origin,
disconnectPairing,
]
);

Expand Down
17 changes: 11 additions & 6 deletions advanced/dapps/react-dapp-v2/src/modals/PairingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import toast from "react-hot-toast";
interface PairingModalProps {
pairings: PairingTypes.Struct[];
connect: (pairing?: { topic: string }) => Promise<void>;
disconnectPairing: (pairing?: { topic: string }) => Promise<void>;
}

const PairingModal = (props: PairingModalProps) => {
const { pairings, connect } = props;
const { pairings, connect, disconnectPairing } = props;
const [pairing, setPairing] = React.useState<PairingTypes.Struct>();

const onConnect = React.useCallback(
Expand All @@ -43,11 +44,15 @@ const PairingModal = (props: PairingModalProps) => {
<SModalTitle>{"Select available pairing or create new one"}</SModalTitle>
<STable>
{pairings.map((pairing) => (
<Pairing
key={pairing.topic}
pairing={pairing}
onClick={() => onConnect(pairing)}
/>
<div key={pairing.topic} style={{ display: "flex" }}>
<Pairing pairing={pairing} onClick={() => onConnect(pairing)} />
<span
style={{ margin: "auto 20px", cursor: "pointer" }}
onClick={() => disconnectPairing(pairing)}
>
X
</span>
</div>
))}
</STable>
<Button onClick={() => connect()}>{`New Pairing`}</Button>
Expand Down
9 changes: 8 additions & 1 deletion advanced/dapps/react-dapp-v2/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const Home: NextPage = () => {
setChains,
setRelayerRegion,
origin,
disconnectPairing,
} = useWalletConnectClient();

// Use `JsonRpcContext` to provide us with relevant RPC methods and states.
Expand Down Expand Up @@ -524,7 +525,13 @@ const Home: NextPage = () => {
if (typeof client === "undefined") {
throw new Error("WalletConnect is not initialized");
}
return <PairingModal pairings={pairings} connect={connect} />;
return (
<PairingModal
pairings={pairings}
connect={connect}
disconnectPairing={disconnectPairing}
/>
);
case "request":
return (
<RequestModal pending={isRpcRequestPending} result={rpcResult} />
Expand Down

0 comments on commit 35896d8

Please sign in to comment.