Skip to content

Commit

Permalink
Merge pull request #23 from labscommunity/kranthi/ensurePermissions
Browse files Browse the repository at this point in the history
fix: ensurepermissions without disconnecting
  • Loading branch information
7i7o authored Oct 10, 2024
2 parents 75559ad + 469e87f commit 12019c3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "arweave-wallet-kit",
"private": false,
"version": "1.0.6",
"version": "1.0.7",
"type": "module",
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -58,7 +58,7 @@
"@types/react-dom": "^18.0.10",
"@types/react-helmet": "^6.1.6",
"@vitejs/plugin-react": "^3.0.0",
"arconnect": "^1.0.3",
"arconnect": "^1.0.4",
"babel-loader": "^8.3.0",
"prettier": "^2.8.2",
"react": "^18.2.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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]);
}
5 changes: 5 additions & 0 deletions src/strategy/strategies/ArConnect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BrowserWalletStrategy from "./BrowserWallet";
import { callWindowApi } from "../../utils";
import type Strategy from "../Strategy";
import type { DataItem } from "arconnect";

export default class ArConnectStrategy
extends BrowserWalletStrategy
Expand Down Expand Up @@ -31,4 +32,8 @@ export default class ArConnectStrategy
public async addToken(id: string): Promise<void> {
return await callWindowApi("addToken", [id]);
}

public async batchSignDataItem(dataItems: DataItem[]): Promise<ArrayBufferLike[]> {
return await callWindowApi("batchSignDataItem", [dataItems]);
}
}

0 comments on commit 12019c3

Please sign in to comment.