2021-05-12 20:51:37 -05:00
import { useQuery } from '@apollo/client'
2023-07-23 10:08:43 -05:00
import { CenterLayout } from '../../components/layout'
2021-05-13 08:28:38 -05:00
import { CopyInput , Input , InputSkeleton } from '../../components/form'
import InputGroup from 'react-bootstrap/InputGroup'
import InvoiceStatus from '../../components/invoice-status'
2021-10-26 15:49:37 -05:00
import { useRouter } from 'next/router'
import { WITHDRAWL } from '../../fragments/wallet'
2022-01-07 15:22:44 -06:00
import Link from 'next/link'
2023-07-31 14:54:30 -05:00
import { SSR } from '../../lib/constants'
2023-08-08 17:04:06 -04:00
import { numWithUnits } from '../../lib/format'
2021-05-12 20:51:37 -05:00
2021-10-26 15:49:37 -05:00
export default function Withdrawl ( ) {
2021-05-12 20:51:37 -05:00
return (
2023-07-23 10:08:43 -05:00
< CenterLayout >
2021-10-26 15:49:37 -05:00
< LoadWithdrawl / >
2023-07-23 10:08:43 -05:00
< / C e n t e r L a y o u t >
2021-05-12 20:51:37 -05:00
)
}
2021-05-13 16:19:51 -05:00
export function WithdrawlSkeleton ( { status } ) {
return (
< >
< div className = 'w-100' >
< InputSkeleton label = 'invoice' / >
< / d i v >
< div className = 'w-100' >
< InputSkeleton label = 'max fee' / >
< / d i v >
< InvoiceStatus status = { status } / >
< / >
)
}
2021-10-26 15:49:37 -05:00
function LoadWithdrawl ( ) {
const router = useRouter ( )
2023-07-31 14:54:30 -05:00
const { loading , error , data } = useQuery ( WITHDRAWL , SSR
? { }
: {
variables : { id : router . query . id } ,
pollInterval : 1000 ,
nextFetchPolicy : 'cache-and-network'
} )
2021-05-12 20:51:37 -05:00
if ( error ) return < div > error < / d i v >
if ( ! data || loading ) {
2021-05-13 16:19:51 -05:00
return < WithdrawlSkeleton status = 'loading' / >
}
2022-01-07 15:22:44 -06:00
const TryMaxFee = ( ) =>
2023-07-23 10:08:43 -05:00
< Link href = '/wallet?type=withdraw' className = 'text-reset text-underline' >
2023-07-24 13:35:05 -05:00
< small className = 'ms-3' > try increasing max fee < / s m a l l >
2022-01-07 15:22:44 -06:00
< / L i n k >
2021-05-13 16:19:51 -05:00
let status = 'pending'
let variant = 'default'
switch ( data . withdrawl . status ) {
case 'CONFIRMED' :
2023-08-08 17:04:06 -04:00
status = ` sent ${ numWithUnits ( data . withdrawl . satsPaid , { abbreviate : false } )} with ${ numWithUnits ( data . withdrawl . satsFeePaid , { abbreviate : false } )} in routing fees `
2021-05-13 16:19:51 -05:00
variant = 'confirmed'
break
case 'INSUFFICIENT_BALANCE' :
2023-07-24 13:35:05 -05:00
status = < > insufficient balance < small className = 'ms-3' > contact keyan ! < /small></ >
2021-05-13 16:19:51 -05:00
variant = 'failed'
break
case 'INVALID_PAYMENT' :
status = 'invalid invoice'
variant = 'failed'
break
case 'PATHFINDING_TIMEOUT' :
2022-01-07 15:22:44 -06:00
status = < > timed out finding route < TryMaxFee / > < / >
2021-05-13 16:19:51 -05:00
variant = 'failed'
break
case 'ROUTE_NOT_FOUND' :
2022-01-07 15:22:44 -06:00
status = < > no route < TryMaxFee / > < / >
2021-05-13 16:19:51 -05:00
variant = 'failed'
break
2022-01-05 14:37:34 -06:00
case 'UNKNOWN_FAILURE' :
status = < > unknown error < / >
variant = 'failed'
break
2021-05-13 16:19:51 -05:00
default :
break
2021-05-12 20:51:37 -05:00
}
2021-05-13 08:28:38 -05:00
return (
< >
< div className = 'w-100' >
< CopyInput
label = 'invoice' type = 'text'
2022-08-18 13:15:24 -05:00
placeholder = { data . withdrawl . bolt11 } readOnly noForm
2021-05-13 08:28:38 -05:00
/ >
< / d i v >
< div className = 'w-100' >
< Input
label = 'max fee' type = 'text'
2022-08-18 13:15:24 -05:00
placeholder = { data . withdrawl . satsFeePaying } readOnly noForm
2021-05-13 16:19:51 -05:00
append = { < InputGroup . Text className = 'text-monospace' > sats < / I n p u t G r o u p . T e x t > }
2021-05-13 08:28:38 -05:00
/ >
< / d i v >
2021-05-13 16:19:51 -05:00
< InvoiceStatus variant = { variant } status = { status } / >
2021-05-13 08:28:38 -05:00
< / >
)
2021-05-12 20:51:37 -05:00
}