All files / kernel-ui/src/components RemoteComms.tsx

100% Statements 24/24
100% Branches 18/18
100% Functions 7/7
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                  3x     10x                                             3x 1x                       3x     11x 4x     7x                                             3x 11x 10x     1x                         2x                                                                       3x 12x 12x     12x 12x 9x         12x   12x 1x             11x   11x                                                          
import {
  Box,
  Text as TextComponent,
  TextColor,
  TextVariant,
  FontWeight,
  BadgeStatus,
  BadgeStatusStatus,
} from '@metamask/design-system-react';
import { useEffect } from 'react';
 
import { usePanelContext } from '../context/PanelContext.tsx';
import { useRegistry } from '../hooks/useRegistry.ts';
import type { ExportedOcapURL } from '../types.ts';
import { Input } from './shared/Input.tsx';
 
// RemoteCommsStatus component displays the status of remote communications.
const RemoteCommsStatus: React.FC<{ isInitialized: boolean }> = ({
  isInitialized,
}) => {
  return (
    <>
      <BadgeStatus
        status={
          isInitialized ? BadgeStatusStatus.Active : BadgeStatusStatus.Inactive
        }
      />
 
      <TextComponent
        variant={TextVariant.BodySm}
        color={
          isInitialized ? TextColor.SuccessDefault : TextColor.ErrorDefault
        }
        data-testid="initialization-status"
      >
        {isInitialized ? 'Initialized' : 'Not Initialized'}
      </TextComponent>
    </>
  );
};
 
// RemoteCommsUnavailable component displays a warning message when remote
// communications are not available.
const RemoteCommsUnavailable: React.FC = () => {
  return (
    <TextComponent
      color={TextColor.WarningDefault}
      data-testid="warning-message"
    >
      Remote communications not yet available in kernel status. Please rebuild
      the kernel to see remote comms information.
    </TextComponent>
  );
};
 
// RemoteCommsPeerId component displays the peer ID
const RemoteCommsPeerId: React.FC<{ peerId?: string | undefined }> = ({
  peerId,
}) => {
  if (!peerId) {
    return null;
  }
 
  return (
    <Box className="flex-1 min-w-0">
      <TextComponent
        variant={TextVariant.BodySm}
        fontWeight={FontWeight.Medium}
        className="mb-2"
        data-testid="peer-id-text"
      >
        Peer Identity
      </TextComponent>
      <Input
        data-testid="peer-id-display"
        value={peerId}
        style={{ width: '100%' }}
        readOnly
      />
    </Box>
  );
};
 
// RemoteCommsExportedUrls component displays exported ocap URLs.
const RemoteCommsExportedUrls: React.FC<{
  exportedUrls: ExportedOcapURL[];
}> = ({ exportedUrls }) => {
  if (exportedUrls.length === 0) {
    return null;
  }
 
  return (
    <>
      <TextComponent
        variant={TextVariant.BodySm}
        fontWeight={FontWeight.Medium}
        className="mb-4"
        data-testid="exported-urls-text"
      >
        Exported Object URLs
      </TextComponent>
 
      <Box className="space-y-3">
        {exportedUrls.map(({ vatId, promiseId, ocapUrl }) => (
          <Box key={promiseId} className="border border-muted rounded p-3">
            <Box className="flex items-center gap-2 mb-2">
              <TextComponent
                variant={TextVariant.BodyXs}
                fontWeight={FontWeight.Medium}
              >
                Vat {vatId}
              </TextComponent>
              <TextComponent
                variant={TextVariant.BodyXs}
                color={TextColor.TextMuted}
              >
                ({promiseId})
              </TextComponent>
            </Box>
 
            <Input
              data-testid={`ocap-url-${promiseId}`}
              value={ocapUrl}
              style={{ width: '100%', fontSize: '12px' }}
              readOnly
            />
          </Box>
        ))}
      </Box>
    </>
  );
};
 
/**
 * RemoteComms component displays remote communications information.
 * Shows peer ID, initialization status, and in the future could show
 * active connections, message history, etc.
 *
 * @returns JSX element for remote communications information
 */
export const RemoteComms: React.FC = () => {
  const { status, objectRegistry } = usePanelContext();
  const { fetchObjectRegistry } = useRegistry();
 
  // Fetch the object registry when component mounts
  useEffect(() => {
    if (!objectRegistry) {
      fetchObjectRegistry();
    }
  }, [fetchObjectRegistry, objectRegistry]);
 
  // Get exported ocap URLs from the object registry
  const exportedUrls = objectRegistry?.ocapUrls ?? [];
 
  if (!status) {
    return (
      <Box>
        <TextComponent>Loading remote communications status...</TextComponent>
      </Box>
    );
  }
 
  const { remoteComms } = status;
 
  return (
    <Box>
      <Box className="bg-section p-4 rounded mb-4">
        <Box className="flex align-items-start gap-12">
          {/* Status Section */}
          <Box className="flex-0 flex-none">
            <TextComponent
              variant={TextVariant.BodySm}
              fontWeight={FontWeight.Medium}
              className="mb-4"
              data-testid="status-text"
            >
              Status
            </TextComponent>
            <Box className="flex items-center gap-2">
              {remoteComms ? (
                <RemoteCommsStatus isInitialized={remoteComms.isInitialized} />
              ) : (
                <RemoteCommsUnavailable />
              )}
            </Box>
          </Box>
          <RemoteCommsPeerId peerId={remoteComms?.peerId} />
        </Box>
      </Box>
      <RemoteCommsExportedUrls exportedUrls={exportedUrls} />
    </Box>
  );
};