"use client"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { createPortal } from "react-dom"; import { CURRENCIES, type CurrencyItem } from "@/data/currencies"; import { useI18n } from "@/translations/provider"; import { useSheetScrollLock } from "./use-sheet-scroll-lock"; const EXIT_ANIMATION_MS = 200; export type CurrencySheetProps = { isOpen: boolean; onClose: () => void; selectedCurrency: string; onSelectCurrency: (code: string) => void; title?: string; }; export function CurrencySheet({ isOpen, onClose, selectedCurrency, onSelectCurrency, title, }: CurrencySheetProps) { const { dictionary: t, locale } = useI18n(); const [isClosing, setIsClosing] = useState(false); const [searchQuery, setSearchQuery] = useState(""); const listRef = useRef(null); const sheetRef = useRef(null); const isRtl = locale === "fa" || locale === "ar" || locale === "ur"; const defaultTitle = isRtl ? "انتخاب ارز" : "Select Currency"; const sheetTitle = title || defaultTitle; const closeSheet = useCallback(() => { setIsClosing(true); window.setTimeout(() => { onClose(); setIsClosing(false); setSearchQuery(""); }, EXIT_ANIMATION_MS); }, [onClose]); useSheetScrollLock(isOpen && !isClosing, { onBack: closeSheet }); // Escape key handler useEffect(() => { if (!isOpen) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") { closeSheet(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [isOpen, closeSheet]); const filteredCurrencies = useMemo(() => { const q = searchQuery.toLowerCase().trim(); if (!q) return CURRENCIES; return CURRENCIES.filter( (c) => c.code.toLowerCase().includes(q) || c.nameEn.toLowerCase().includes(q) || c.nameFa.toLowerCase().includes(q) || (c.symbol && c.symbol.toLowerCase().includes(q)), ); }, [searchQuery]); const handleSelect = (code: string) => { onSelectCurrency(code); closeSheet(); }; const searchPlaceholder = locale === "fa" ? "جستجوی ارز یا کشور..." : locale === "ar" ? "بحث عن العملة..." : "Search currency or country..."; const noResultsText = locale === "fa" ? "ارزی یافت نشد" : locale === "ar" ? "لم يتم العثور على عملات" : "No currencies found"; if (!isOpen && !isClosing) return null; return createPortal(
{ if (e.key === "Escape") closeSheet(); }} onClick={(event) => { if (event.target === event.currentTarget) closeSheet(); }} >
{/* Header */}

{sheetTitle}

{/* Search Bar */}
setSearchQuery(e.target.value)} placeholder={searchPlaceholder} className="flex-1 bg-transparent text-[14px] font-medium text-[#181818] outline-none placeholder:text-[#98A2B3]" /> {searchQuery ? ( ) : null}
{/* Currencies List */}
event.stopPropagation()} onTouchMove={(event) => event.stopPropagation()} onTouchEnd={(event) => event.stopPropagation()} className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto overscroll-contain px-5 py-3" > {filteredCurrencies.length > 0 ? ( filteredCurrencies.map((item: CurrencyItem) => { const isSelected = item.code === selectedCurrency; const displayName = locale === "fa" || locale === "fa-ir" ? item.nameFa : item.nameEn; return ( ); }) ) : (
{noResultsText}
)}
, document.body, ); } export default CurrencySheet;