Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Loading indicator for connecting & receiving data channel #28

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sites/dapp-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default function ProviderApp() {
const [status, setStatus] = useState<'connected' | 'disconnected'>(
'disconnected',
);
const [loading, setLoading] = useState<boolean>(false);
useEffect(() => {
if (!client) return;
function handleDataChannel(dc: RTCDataChannel) {
Expand All @@ -91,6 +92,7 @@ export default function ProviderApp() {
}
}
}
setLoading(false);
setDataChannel(dc);
}
client.on('offer-description', (description) => {
Expand All @@ -114,6 +116,7 @@ export default function ProviderApp() {
client.on('connect', handleSocketConnect);
function handleLinkMessage(msg: LinkMessage) {
console.log('LinkMessage', msg);
setLoading(true);
setAddress(msg.wallet);
}
client.on('link-message', handleLinkMessage);
Expand All @@ -138,6 +141,8 @@ export default function ProviderApp() {
setClient,
status,
setStatus,
loading,
setLoading,
dataChannel,
setDataChannel,
}}
Expand Down
33 changes: 25 additions & 8 deletions sites/dapp-ui/src/components/ConnectModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Button from '@mui/material/Button';
import Modal from '@mui/material/Modal';
import { useState } from 'react';
import { Fade } from '@mui/material';
import CircularProgress from '@mui/material/CircularProgress';
import { useNavigate } from 'react-router-dom';
import { useSignalClient } from '@/hooks/useSignalClient.ts';
import { SignalClient } from '@algorandfoundation/liquid-client';
Expand All @@ -28,15 +29,15 @@ export function ConnectModal({
color,
}: {
color?:
| 'inherit'
| 'primary'
| 'secondary'
| 'success'
| 'error'
| 'info'
| 'warning';
| 'inherit'
| 'primary'
| 'secondary'
| 'success'
| 'error'
| 'info'
| 'warning';
}) {
const { client, dataChannel } = useSignalClient();
const { client, loading, dataChannel } = useSignalClient();
const navigate = useNavigate();
const [requestId] = useState(SignalClient.generateRequestId());

Expand Down Expand Up @@ -130,6 +131,22 @@ export function ConnectModal({
top: '50%',
}}
/>
{loading && (
<Box
sx={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
height: '80px',
width: '80px',
}}
>
<CircularProgress
size={80}
/>
</Box>
)}
</Box>
</Box>
</Fade>
Expand Down
8 changes: 6 additions & 2 deletions sites/dapp-ui/src/hooks/useSignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ export const SignalClientContext = createContext({
setStatus: (_: 'connected' | 'disconnected') => {
console.log(_);
},
loading: false,
setLoading: (_: boolean) => {
console.log(_);
},
dataChannel: null,
setDataChannel: (_: RTCDataChannel) => {
console.log(_);
},
} as SignalClientState);

export function useSignalClient() {
const { client, status, dataChannel } = useContext(SignalClientContext);
const { client, status, loading, dataChannel } = useContext(SignalClientContext);
if (!client)
throw new Error(
'SignalClient not found, make sure to wrap your component with <SignalClientProvider>',
);
return { client, status, dataChannel };
return { client, status, loading, dataChannel };
}
Loading