forked from Cookie-Jar-DAO/cookie-jar-v3
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseJarAllowlistStatus.ts
More file actions
60 lines (53 loc) · 1.82 KB
/
useJarAllowlistStatus.ts
File metadata and controls
60 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"use client";
import { useEffect, useState } from "react";
import { keccak256, toHex } from "viem";
import { useAccount, useChainId } from "wagmi";
import { isV2Chain } from "@/config/supported-networks";
import { useReadCookieJarHasRole } from "@/generated";
/**
* Custom hook to check user's allowlist status for a Cookie Jar
*
* Determines if the current user is allowlisted for a specific jar by
* checking the appropriate role (JAR_ALLOWLISTED for v2, JAR_WHITELISTED for v1).
* Automatically handles contract version differences.
*
* @param jarAddress - Cookie Jar contract address to check
* @returns Object with allowlist status and loading state
*
* @example
* ```tsx
* const { isAllowlisted, isLoading } = useAllowlistStatus(jarAddress);
*
* if (isLoading) return <div>Checking status...</div>;
*
* return (
* <div>
* Status: {isAllowlisted ? 'Allowlisted' : 'Not Allowlisted'}
* </div>
* );
* ```
*/
export function useAllowlistStatus(jarAddress: string) {
const [isAllowlisted, setIsAllowlisted] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState(true);
const { address: userAddress } = useAccount();
const chainId = useChainId();
// Use correct role name based on contract version
const roleName = isV2Chain(chainId) ? "JAR_ALLOWLISTED" : "JAR_WHITELISTED";
const JAR_ROLE = keccak256(toHex(roleName)) as `0x${string}`;
// Use the contract hook to check allowlist status
const { data, isLoading: isLoadingRole } = useReadCookieJarHasRole({
address: jarAddress as `0x${string}`,
args: userAddress ? [JAR_ROLE, userAddress as `0x${string}`] : undefined,
query: {
enabled: !!userAddress && !!jarAddress,
},
});
useEffect(() => {
if (!isLoadingRole) {
setIsAllowlisted(!!data);
setIsLoading(false);
}
}, [data, isLoadingRole]);
return { isAllowlisted, isLoading };
}