import { ChevronDownIcon } from "lucide-react"; import React from "react"; import { useNavigate } from "react-router"; import { toast } from "sonner"; import AppHeader from "src/components/AppHeader"; import ExternalLink from "src/components/ExternalLink"; import { FormattedBitcoinAmount } from "src/components/FormattedBitcoinAmount"; import Loading from "src/components/Loading"; import { Button } from "src/components/ui/button"; import { Checkbox } from "src/components/ui/checkbox"; import { LoadingButton } from "src/components/ui/custom/loading-button"; import { Label } from "src/components/ui/label"; import { Separator } from "src/components/ui/separator"; import { useChannels } from "src/hooks/useChannels"; import { useInfo } from "src/hooks/useInfo"; import { AutoChannelRequest, AutoChannelResponse } from "src/types"; import { request } from "src/utils/request"; import { MempoolAlert } from "src/components/MempoolAlert"; import { PayLightningInvoice } from "src/components/PayLightningInvoice"; import { ChannelPublicPrivateAlert } from "src/components/channels/ChannelPublicPrivateAlert"; import LightningNetworkDarkSVG from "public/images/illustrations/lightning-network-dark.svg"; import LightningNetworkLightSVG from "public/images/illustrations/lightning-network-light.svg"; import { ExternalLinkButton } from "src/components/ui/custom/external-link-button"; import { LinkButton } from "src/components/ui/custom/link-button"; export function AutoChannel() { const { data: info } = useInfo(); const { data: channels } = useChannels(true); const [isLoading, setLoading] = React.useState(false); const [showAdvanced, setShowAdvanced] = React.useState(false); const [isPublic, setPublic] = React.useState(false); const navigate = useNavigate(); const [invoice, setInvoice] = React.useState(); const [channelSizeSat, setChannelSizeSat] = React.useState(); const [, setPrevChannelIds] = React.useState(); React.useEffect(() => { if (channels) { setPrevChannelIds((current) => { if (current) { const newChannelId = channels.find( (channel) => !current?.includes(channel.id) && channel.fundingTxId )?.id; if (newChannelId) { console.info("Found new channel", newChannelId); navigate("/channels/auto/opening", { state: { newChannelId, }, }); } return current; } return channels.map((channel) => channel.id); }); } }, [channels, navigate]); if (!info || !channels) { return ; } async function openChannel() { if (!info || !channels) { return; } setLoading(true); try { const newInstantChannelInvoiceRequest: AutoChannelRequest = { isPublic, }; const autoChannelResponse = await request( "/api/alby/auto-channel", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(newInstantChannelInvoiceRequest), } ); if (!autoChannelResponse) { throw new Error("unexpected auto channel response"); } setInvoice(autoChannelResponse.invoice); setChannelSizeSat(autoChannelResponse.channelSizeSat); } catch (error) { setLoading(false); console.error(error); toast.error("Something went wrong. Please try again"); } } return ( <> {invoice && channelSizeSat && (

Please pay the lightning invoice below which will cover the costs of opening your channel. You will receive a channel with{" "} of receiving capacity.

Other options

Open Channel with On-Chain Bitcoin Buy Bitcoin
)} {!invoice && ( <>
<>

You're now going to open a new lightning channel that you can use to send and receive payments using your Hub in the booming bitcoin economy! To make things easy, Alby has picked a channel partner for you from one of our recommended channel partners.

After paying a lightning invoice to cover on-chain fees, you'll immediately be able to receive and send bitcoin through this channel with your Hub.

Alby Hub works with selected service providers (LSPs) which provide the best network connectivity and liquidity to receive payments.{" "} Learn more

{showAdvanced && ( <>
setPublic(!isPublic)} className="mr-2" />

Not recommended for most users.{" "} Learn more

)} {!showAdvanced && (
)} {channels?.some((channel) => channel.public !== !!isPublic) && ( )} Open Channel
)} ); }