Skip to content

Commit

Permalink
fix: ensurepermissions without disconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
kranthicodes committed Oct 1, 2024
1 parent 75559ad commit 108552e
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/hooks/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { ConnectMsg } from "./connection/connect";
import { STRATEGY_STORE } from "../strategy";
import useActiveStrategy from "./strategy";
import useGlobalState from "./global";
import { useEffect } from "react";
import { useEffect, useRef } from "react";
import { comparePermissions } from "../utils";
import type { PermissionType } from "arconnect";

/**
Expand Down Expand Up @@ -39,8 +40,10 @@ export default function usePermissions(): PermissionType[] {

// sync permissions in global state
export function useSyncPermissions() {
const isReconnecting = useRef(false);
const { state, dispatch } = useGlobalState();
const strategy = useActiveStrategy();
const { permissions: requiredPermissions, ensurePermissions } = state.config;

useEffect(() => {
// sync permissions
Expand All @@ -55,12 +58,30 @@ export function useSyncPermissions() {

try {
const permissions = await strategy.getPermissions();
const hasPermissions = comparePermissions(
requiredPermissions,
permissions
);

dispatch({
type: "UPDATE_PERMISSIONS",
payload: permissions
});

if (requiredPermissions.length === 0 && ensurePermissions) {
fixupDisconnection();
return;
}

if (!hasPermissions && ensurePermissions && !isReconnecting.current) {
isReconnecting.current = true;
await strategy.connect(
requiredPermissions,
state.config.appInfo,
state.config.gatewayConfig
);
}

if (permissions.length === 0) {
fixupDisconnection();
}
Expand Down Expand Up @@ -130,5 +151,5 @@ export function useSyncPermissions() {
strategy.removeAddressEvent(addressChangeSync);
}
};
}, [strategy, dispatch]);
}, [strategy, requiredPermissions, dispatch]);
}

0 comments on commit 108552e

Please sign in to comment.