LDKChannelMonitorSizeAlert.tsx raw

   1  import { AlertTriangleIcon } from "lucide-react";
   2  import React from "react";
   3  import { Alert, AlertDescription, AlertTitle } from "src/components/ui/alert";
   4  import { useInfo } from "src/hooks/useInfo";
   5  import { useNodeDetails } from "src/hooks/useNodeDetails";
   6  import { request } from "src/utils/request";
   7  
   8  export function LDKChannelMonitorSizeAlert() {
   9    const { data: info } = useInfo();
  10  
  11    if (info?.backendType !== "LDK") {
  12      return null;
  13    }
  14    return ChannelMonitorSizeAlert();
  15  }
  16  
  17  function ChannelMonitorSizeAlert() {
  18    const [channelMonitorSizes, setChannelMonitorSizes] =
  19      React.useState<
  20        { remotePubkey: string; sizeBytes: number; hasWarning: boolean }[]
  21      >();
  22    React.useEffect(() => {
  23      (async () => {
  24        try {
  25          const requestOptions: RequestInit = {
  26            method: "POST",
  27            headers: {
  28              "Content-Type": "application/json",
  29            },
  30            body: JSON.stringify({ command: "list_channel_monitor_sizes" }),
  31          };
  32  
  33          const data = await request("/api/command", requestOptions);
  34  
  35          setChannelMonitorSizes(data as typeof channelMonitorSizes);
  36        } catch (error) {
  37          console.error(error);
  38        }
  39      })();
  40    }, []);
  41  
  42    if (!channelMonitorSizes) {
  43      return null;
  44    }
  45  
  46    const largestChannelMonitor = channelMonitorSizes.find(
  47      (c1) => !channelMonitorSizes.find((c2) => c2.sizeBytes > c1.sizeBytes)
  48    );
  49    if (!largestChannelMonitor) {
  50      return null;
  51    }
  52  
  53    if (!largestChannelMonitor.hasWarning) {
  54      return;
  55    }
  56    return (
  57      <ChannelMonitorSizeAlertForPubkey
  58        remotePubkey={largestChannelMonitor.remotePubkey}
  59        sizeBytes={largestChannelMonitor.sizeBytes}
  60      />
  61    );
  62  }
  63  
  64  function ChannelMonitorSizeAlertForPubkey({
  65    remotePubkey,
  66    sizeBytes,
  67  }: {
  68    remotePubkey: string;
  69    sizeBytes: number;
  70  }) {
  71    const { data: peerDetails } = useNodeDetails(remotePubkey);
  72    return (
  73      <>
  74        <Alert>
  75          <AlertTriangleIcon className="h-4 w-4" />
  76          <AlertTitle>Large channel state detected</AlertTitle>
  77          <AlertDescription>
  78            The channel state for your channel with{" "}
  79            {peerDetails?.alias || remotePubkey} is over{" "}
  80            {Math.floor(sizeBytes / 1_000_000)} MB. Consider closing this channel
  81            and opening a new one to improve your node performance.
  82          </AlertDescription>
  83        </Alert>
  84      </>
  85    );
  86  }
  87