Skip to content

Commit

Permalink
update: use wagmi storage instead of internal (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochie authored Feb 6, 2024
1 parent 66fa24c commit 69d5957
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
7 changes: 0 additions & 7 deletions packages/connectkit/src/hooks/useConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { useLastConnector } from './useLastConnector';
export function useConnect({ ...props }: UseConnectParameters = {}) {
const context = useContext();

const { updateLastConnectorId } = useLastConnector();

const { connect, connectAsync, connectors, ...rest } = wagmiUseConnect({
...props,
mutation: {
Expand All @@ -30,11 +28,6 @@ export function useConnect({ ...props }: UseConnectParameters = {}) {
context.log(`Could not connect.`, err);
}
},
onSuccess(data: any) {
updateLastConnectorId(
`${data?.connector?.id}-${data?.connector?.name}` ?? ''
);
},
},
});

Expand Down
31 changes: 15 additions & 16 deletions packages/connectkit/src/hooks/useLastConnector.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { useLocalStorage } from './useLocalStorage';
import { useEffect, useState } from 'react';
import { useConfig } from 'wagmi';

export const useLastConnector = () => {
const {
data: lastConnectorId,
add,
update,
clear,
} = useLocalStorage('connectKit.lastConnectorId');
const { storage } = useConfig();
const [lastConnectorId, setLastConnectorId] = useState<string | null>(null);

const updateLastConnectorId = (id: string) => {
if (lastConnectorId) {
if (lastConnectorId === id) return;
clear();
update(id);
} else {
add(id);
}
useEffect(() => {
const init = async () => {
const id = await storage?.getItem('recentConnectorId');
setLastConnectorId(id ?? '');
};
init();
}, []);

const update = (id: string) => {
storage?.setItem('recentConnectorId', id);
};

return {
lastConnectorId,
updateLastConnectorId,
updateLastConnectorId: update,
};
};

0 comments on commit 69d5957

Please sign in to comment.