Skip to content

Commit

Permalink
Update useConsent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rygine committed Sep 6, 2024
1 parent f4d3436 commit b58da93
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
18 changes: 8 additions & 10 deletions packages/react-sdk/src/hooks/useConsent.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ describe("useConsent", () => {

await act(async () => {
const list = await result.current.loadConsentList();
expect(list).toEqual([]);
expect(list.size).toEqual(0);
await result.current.allow([testWallet2.account.address]);
const list2 = await result.current.loadConsentList();
expect(list2.length).toEqual(1);
expect(list2[0].entryType).toEqual("address");
expect(list2[0].permissionType).toEqual("allowed");
expect(list2[0].value).toEqual(testWallet2.account.address);
expect(list2.size).toEqual(1);
const entry = list2.get(`address-${testWallet2.account.address}`);
expect(entry).toEqual("allowed");
const entries = await getCachedConsentEntries(
testWallet1.account.address,
db,
Expand All @@ -118,13 +117,12 @@ describe("useConsent", () => {

await act(async () => {
const list = await result.current.refreshConsentList();
expect(list).toEqual([]);
expect(list.size).toEqual(0);
await result.current.allow([testWallet4.account.address]);
const list2 = await result.current.refreshConsentList();
expect(list2.length).toEqual(1);
expect(list2[0].entryType).toEqual("address");
expect(list2[0].permissionType).toEqual("allowed");
expect(list2[0].value).toEqual(testWallet4.account.address);
expect(list2.size).toEqual(1);
const entry = list2.get(`address-${testWallet4.account.address}`);
expect(entry).toEqual("allowed");
const entries = await getCachedConsentEntries(
testWallet3.account.address,
db,
Expand Down
29 changes: 21 additions & 8 deletions packages/react-sdk/src/hooks/useConsent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback } from "react";
import type { ConsentState } from "@xmtp/xmtp-js";
import { useClient } from "@/hooks/useClient";
import { useDb } from "@/hooks/useDb";
import {
Expand Down Expand Up @@ -113,13 +114,19 @@ export const useConsent = () => {
}
const db = await getDbInstance();
const newEntries = await client.contacts.loadConsentList(startTime);
if (newEntries.length > 0) {
if (newEntries.size > 0) {
const addresses = Array.from(newEntries.entries())
.filter(([entry]) => entry.startsWith("address-"))
.map(
([entry, state]) =>
[entry.split("-")[1], state] as [string, ConsentState],
);
// update DB
await bulkPutConsentState(
newEntries.map((entry) => ({
value: entry.value,
addresses.map(([address, state]) => ({
value: address,
type: "address",
state: entry.permissionType,
state,
walletAddress: client.address,
})),
db,
Expand All @@ -138,13 +145,19 @@ export const useConsent = () => {
const db = await getDbInstance();
await db.table("consent").clear();
const newEntries = await client?.contacts.refreshConsentList();
if (newEntries.length > 0) {
if (newEntries.size > 0) {
const addresses = Array.from(newEntries.entries())
.filter(([entry]) => entry.startsWith("address-"))
.map(
([entry, state]) =>
[entry.split("-")[1], state] as [string, ConsentState],
);
// update DB
await bulkPutConsentState(
newEntries.map((entry) => ({
value: entry.value,
addresses.map(([address, state]) => ({
value: address,
type: "address",
state: entry.permissionType,
state,
walletAddress: client.address,
})),
db,
Expand Down

0 comments on commit b58da93

Please sign in to comment.