Skip to content

Commit

Permalink
feat(dapp-ui): added loading status and indicator for conveying progr…
Browse files Browse the repository at this point in the history
…ess between link-message and receiving a data channel
  • Loading branch information
kylebeee committed Jun 18, 2024
1 parent 4101822 commit 44ed666
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
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
10 changes: 10 additions & 0 deletions sites/dapp-ui/src/components/user/StatusCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type ProfileCardProps = {
export function StatusCard({ session, user, socket }: ProfileCardProps) {
const { refetch } = useUserState();
const { client, setDataChannel } = useContext(SignalClientContext);

const isLoading = socket.isConnected && !socket.hasDataChannel;

return (
<Card sx={{ maxWidth: 300, zIndex: 1000 }} raised>
<CardContent>
Expand Down Expand Up @@ -81,6 +84,13 @@ export function StatusCard({ session, user, socket }: ProfileCardProps) {
<IconButton aria-label="share">
<ShareIcon />
</IconButton>
{
isLoading && (
<Typography variant="h6" color="text.secondary">
Loading...
</Typography>
)
}
</CardActions>
)}
</Card>
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 };
}

0 comments on commit 44ed666

Please sign in to comment.