Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set autoConnet() expiry time #307

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@ const loader = ({ src }: { src: string }) => {
const ConnectModal = () => {
const modalRef = useRef<HTMLInputElement>(null);
const [isBurnerWallet, setIsBurnerWallet] = useState(false);

const { resolvedTheme } = useTheme();
const isDarkMode = resolvedTheme === "dark";

const { connectors, connect, error, status, ...props } = useConnect();

const [_, setLastConnector] = useLocalStorage<{ id: string; ix?: number }>(
"lastUsedConnector",
{ id: "" },
{
initializeWithValue: false,
},
);
const [, setLastConnectionTime] = useLocalStorage<number>(
"lastConnectionTime",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kindly extract this "lastConnectionTime" to a constant, thank you!

0,
);

const handleCloseModal = () => {
if (modalRef.current) {
Expand All @@ -44,6 +45,7 @@ const ConnectModal = () => {
}
connect({ connector });
setLastConnector({ id: connector.id });
setLastConnectionTime(Date.now()); // Store the current time
handleCloseModal();
}

Expand All @@ -70,6 +72,7 @@ const ConnectModal = () => {
>
<span>Connect</span>
</label>

<input
ref={modalRef}
type="checkbox"
Expand Down
32 changes: 21 additions & 11 deletions packages/nextjs/hooks/scaffold-stark/useAutoConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,32 @@ export const useAutoConnect = (): void => {
const savedConnector = useReadLocalStorage<{ id: string; ix?: number }>(
"lastUsedConnector",
);

const lastConnectionTime = useReadLocalStorage<number>("lastConnectionTime");

const { connect, connectors } = useConnect();

useEffect(() => {
if (scaffoldConfig.walletAutoConnect) {
const connector = connectors.find(
(conn) => conn.id == savedConnector?.id,
);
if (connector) {
if (
connector.id == "burner-wallet" &&
savedConnector?.ix !== undefined
) {
(connector as BurnerConnector).burnerAccount =
burnerAccounts[savedConnector.ix];
const currentTime = Date.now();
const ttlExpired =
currentTime - (lastConnectionTime || 0) > scaffoldConfig.autoConnectTTL;

if (!ttlExpired) {
const connector = connectors.find(
(conn) => conn.id == savedConnector?.id,
);

if (connector) {
if (
connector.id == "burner-wallet" &&
savedConnector?.ix !== undefined
) {
(connector as BurnerConnector).burnerAccount =
burnerAccounts[savedConnector.ix];
}
connect({ connector });
}
connect({ connector });
}
}
}, [connect, connectors, savedConnector]);
Expand Down
3 changes: 3 additions & 0 deletions packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type ScaffoldConfig = {
onlyLocalBurnerWallet: boolean;
rpcProviderUrl: string;
walletAutoConnect: boolean;
autoConnectTTL: number,

};

const scaffoldConfig = {
Expand All @@ -21,6 +23,7 @@ const scaffoldConfig = {
* 1. If the user was connected into a wallet before, on page reload reconnect automatically
* 2. If user is not connected to any wallet: On reload, connect to burner wallet if burnerWallet.enabled is true && burnerWallet.onlyLocal is false
*/
autoConnectTTL: 60000,
walletAutoConnect: true,
} as const satisfies ScaffoldConfig;

Expand Down
Loading