-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathSelfRelayButton.tsx
More file actions
54 lines (46 loc) · 1.63 KB
/
Copy pathSelfRelayButton.tsx
File metadata and controls
54 lines (46 loc) · 1.63 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
'use client';
import { SpinnerIcon } from '@hyperlane-xyz/widgets';
import { useConnectModal } from '@rainbow-me/rainbowkit';
import { useCallback, useEffect, useRef } from 'react';
import { useAccount } from 'wagmi';
import { Message, MessageStatus } from '../../types';
import { useSelfRelay } from './useSelfRelay';
interface SelfRelayButtonProps {
message: Message;
disabled?: boolean;
}
export function SelfRelayButton({ message, disabled }: SelfRelayButtonProps) {
const { isConnected } = useAccount();
const { openConnectModal } = useConnectModal();
const { relay, isRelaying } = useSelfRelay();
const isDelivered = message.status === MessageStatus.Delivered;
const pendingRelayRef = useRef(false);
// Trigger relay after wallet connects
useEffect(() => {
if (isConnected && pendingRelayRef.current) {
pendingRelayRef.current = false;
relay({ message });
}
}, [isConnected, relay, message]);
const handleClick = useCallback(() => {
if (!isConnected) {
pendingRelayRef.current = true;
openConnectModal?.();
return;
}
relay({ message });
}, [isConnected, openConnectModal, relay, message]);
if (isDelivered) {
return null;
}
return (
<button
onClick={handleClick}
disabled={isRelaying || disabled}
className="mt-3 flex items-center justify-center gap-2 rounded-md bg-pink-500 px-3 py-1.5 text-sm font-medium text-white transition-all hover:bg-pink-600 active:bg-pink-700 disabled:bg-gray-300 disabled:text-gray-500"
>
{isRelaying && <SpinnerIcon width={14} height={14} />}
{isRelaying ? 'Relaying...' : 'Self Relay'}
</button>
);
}