'use client' import { useCallback, useEffect, useRef, useState } from 'react' import { Loader2 } from 'lucide-react' import { cn } from '@/lib/utils' import Script from 'next/script' export interface Bank { name: string country: string logo?: string bic?: string } interface BankSelectorProps { onSelect: (bank: Bank) => void selectedBank?: Bank | null isLoading?: boolean className?: string country?: string sandbox?: boolean } export function BankSelector({ onSelect, isLoading = false, className, country = 'SE', sandbox = true, // Default to sandbox for development }: BankSelectorProps) { const widgetRef = useRef(null) const [scriptReady, setScriptReady] = useState(false) // Stable ref for onSelect so the event listener never goes stale const onSelectRef = useRef(onSelect) useEffect(() => { onSelectRef.current = onSelect }, [onSelect]) const handleSelected = useCallback((e: Event) => { const customEvent = e as CustomEvent const detail = customEvent.detail as { name: string country: string psuType: string sandbox: boolean } onSelectRef.current({ name: detail.name, country: detail.country, }) }, []) // Attach the event listener once the script is ready and the widget is in the DOM. // Using onReady instead of onLoad ensures the script has fully executed and // the custom element has been defined/upgraded before we interact with it. useEffect(() => { if (!scriptReady) return const widget = widgetRef.current if (!widget) return widget.addEventListener('selected', handleSelected) return () => { widget.removeEventListener('selected', handleSelected) } }, [scriptReady, handleSelected]) if (isLoading) { return (
Ansluter...
) } return (