-
Notifications
You must be signed in to change notification settings - Fork 71
/
useClaimHandle.ts
81 lines (73 loc) · 2.35 KB
/
useClaimHandle.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
ClaimProfileWithHandleErrorReasonType,
Profile,
ReservedClaimable,
} from '@lens-protocol/api-bindings';
import { TransactionError, TransactionKind } from '@lens-protocol/domain/entities';
import {
ClaimHandleRequest,
FollowPolicyConfig,
ClaimHandleError as GenericClaimHandleError,
} from '@lens-protocol/domain/use-cases/profile';
import { OneOf, invariant } from '@lens-protocol/shared-kernel';
import { SessionType, useSession } from '../authentication';
import { UseDeferredTask, useDeferredTask } from '../helpers/tasks';
import { useClaimHandleController } from './adapters/useClaimHandleController';
/**
* @privateRemarks Extended class to generate proper documentation. No measurable run-time implications.
*/
export class ClaimHandleError extends GenericClaimHandleError<ClaimProfileWithHandleErrorReasonType> {}
export { ClaimProfileWithHandleErrorReasonType };
/**
* Claim a handle details.
*/
export type ClaimHandleArgs = OneOf<{
/**
* The handle local name to claim.
*/
localName: string;
/**
* The handle reservation to claim.
*/
reserved: ReservedClaimable;
}> & {
/**
* You can optionally specify a follow policy for the profile.
*/
followPolicy?: FollowPolicyConfig;
};
/**
* `useClaimHandle` is React Hook that allows you to claim a handle.
*
* You MUST be authenticated with a {@link WalletOnlySession} via {@link useLogin} to use this hook.
*
* @experimental This hook is experimental and may change in future versions.
* @category Profiles
* @group Hooks
*/
export function useClaimHandle(): UseDeferredTask<
Profile,
ClaimHandleError | TransactionError,
ClaimHandleArgs
> {
const { data: session } = useSession();
const claimHandle = useClaimHandleController();
return useDeferredTask(async (args: ClaimHandleArgs) => {
invariant(
session?.type === SessionType.JustWallet,
'You must be authenticated with just a wallet to claim an handle. Use `useLogin` hook omitting the profileId.',
);
const request: ClaimHandleRequest =
`reserved` in args && args.reserved
? {
kind: TransactionKind.CLAIM_HANDLE,
id: args.reserved?.id,
handle: args.reserved?.withHandle,
}
: {
kind: TransactionKind.CLAIM_HANDLE,
localName: args.localName,
};
return claimHandle(request);
});
}