-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPaymentSourceBrandIcon.tsx
More file actions
56 lines (53 loc) · 1.65 KB
/
PaymentSourceBrandIcon.tsx
File metadata and controls
56 lines (53 loc) · 1.65 KB
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
import PaymentSourceContext, {
type IconBrand
} from '#context/PaymentSourceContext'
import { useContext, useRef, type JSX } from 'react';
import Parent from '#components/utils/Parent'
import type { ChildrenFunction } from '#typings'
import CustomerPaymentSourceContext from '#context/CustomerPaymentSourceContext'
interface ChildrenProps extends Omit<Props, 'children'> {
brand: IconBrand
defaultSrc: string
url: string
}
interface Props extends Omit<JSX.IntrinsicElements['img'], 'children'> {
children?: ChildrenFunction<ChildrenProps>
width?: number
height?: number
}
export function PaymentSourceBrandIcon({
src,
width = 32,
children,
...p
}: Props): JSX.Element {
const { brand, issuer_type: issuerType } = useContext(PaymentSourceContext)
const { brand: customerCardBrand, issuer_type: customerIssuerType } =
useContext(CustomerPaymentSourceContext)
const cardBrand =
brand ?? customerCardBrand ?? issuerType ?? customerIssuerType
const ref = useRef<HTMLImageElement>(null)
const defaultSrc =
'//data.commercelayer.app/assets/images/icons/credit-cards/color/credit-card.svg'
const url =
src ||
`//data.commercelayer.app/assets/images/icons/credit-cards/color/${
cardBrand ?? 'credit-card'
}.svg`
const handleError = (): void => {
if (ref.current) ref.current.src = defaultSrc
}
const parentProps = {
brand: cardBrand,
defaultSrc,
url,
width,
...p
}
return children ? (
<Parent {...parentProps}>{children}</Parent>
) : (
<img alt={cardBrand ?? "credit card"} ref={ref} src={url} onError={handleError} width={width} {...p} />
)
}
export default PaymentSourceBrandIcon