- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
import { Check,Copy,Eye,EyeOff } from 'lucide-react';
|
|
import React,{ useState } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
|
|
interface MaskableFieldProps {
|
|
value?: string | number | null;
|
|
maskedValue?: string;
|
|
isMaskedByDefault?: boolean;
|
|
label?: string;
|
|
copyable?: boolean;
|
|
className?: string;
|
|
valueClassName?: string;
|
|
dir?: 'ltr' | 'rtl';
|
|
}
|
|
|
|
export default function MaskableField({
|
|
value,
|
|
maskedValue,
|
|
isMaskedByDefault = true,
|
|
label,
|
|
copyable = true,
|
|
className = '',
|
|
valueClassName = '',
|
|
dir = 'ltr',
|
|
}: MaskableFieldProps) {
|
|
const [isRevealed, setIsRevealed] = useState(!isMaskedByDefault);
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
const rawVal = value !== undefined && value !== null ? String(value) : '';
|
|
|
|
// Create mask if not provided
|
|
const generateMask = (str: string) => {
|
|
if (!str) return '-';
|
|
if (str.length <= 4) return '****';
|
|
if (str.length === 16) {
|
|
// 16-digit Card: 6037-99**-****-1234
|
|
return `${str.slice(0, 4)}-${str.slice(4, 6)}**-****-${str.slice(12)}`;
|
|
}
|
|
const visibleStart = Math.min(4, Math.floor(str.length / 3));
|
|
const visibleEnd = Math.min(4, Math.floor(str.length / 3));
|
|
return `${str.slice(0, visibleStart)}${'*'.repeat(Math.max(4, str.length - visibleStart - visibleEnd))}${str.slice(-visibleEnd)}`;
|
|
};
|
|
|
|
const displayMasked = maskedValue || generateMask(rawVal);
|
|
const currentText = isRevealed ? (rawVal || '-') : displayMasked;
|
|
|
|
const handleCopy = (e: React.MouseEvent) => {
|
|
e.stopPropagation();
|
|
if (!rawVal) return;
|
|
navigator.clipboard.writeText(rawVal);
|
|
setCopied(true);
|
|
toast.success(`${label || 'مقدار'} در کلیپبورد کپی شد`);
|
|
setTimeout(() => setCopied(false), 2000);
|
|
};
|
|
|
|
return (
|
|
<div className={`inline-flex items-center gap-1.5 ${className}`}>
|
|
<span
|
|
dir={dir}
|
|
className={`font-mono text-xs select-all ${valueClassName}`}
|
|
title={isRevealed ? 'مقدار واقعی' : 'مقدار پنهانشده'}
|
|
>
|
|
{currentText}
|
|
</span>
|
|
|
|
{rawVal && (
|
|
<div className="inline-flex items-center gap-1">
|
|
{/* Toggle Eye Button */}
|
|
<button
|
|
type="button"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
setIsRevealed(!isRevealed);
|
|
}}
|
|
className="p-1 text-gray-400 hover:text-indigo-600 hover:bg-indigo-50 rounded-md transition-colors cursor-pointer"
|
|
title={isRevealed ? 'پنهانسازی' : 'نمایش مقدار اصلی'}
|
|
>
|
|
{isRevealed ? <EyeOff className="w-3.5 h-3.5" /> : <Eye className="w-3.5 h-3.5" />}
|
|
</button>
|
|
|
|
{/* Copy to Clipboard Button */}
|
|
{copyable && (
|
|
<button
|
|
type="button"
|
|
onClick={handleCopy}
|
|
className="p-1 text-gray-400 hover:text-emerald-600 hover:bg-emerald-50 rounded-md transition-colors cursor-pointer"
|
|
title="کپی در کلیپبورد"
|
|
>
|
|
{copied ? <Check className="w-3.5 h-3.5 text-emerald-600" /> : <Copy className="w-3.5 h-3.5" />}
|
|
</button>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|