"use client"; import { useEffect, useState } from "react"; import Button from "./button"; import { useFlutterBridge } from "@/hooks/useFlutterBridge"; import { copyToClipboard, downloadFile, isInFlutterWebView, openExternalUrl, requestAutoLocation, pickManualLocation, } from "@/lib/webview-actions"; const EXIT_ANIMATION_MS = 220; type ReportActionsSheetProps = { onClose?: () => void; }; export function ReportActionsSheet({ onClose }: ReportActionsSheetProps) { const [isVisible, setIsVisible] = useState(true); const [isEntering, setIsEntering] = useState(true); const [isClosing, setIsClosing] = useState(false); const [showLogs, setShowLogs] = useState(false); const { sendToFlutter, logs, lastEvent, isReady } = useFlutterBridge({ enableLogging: true, }); const closeSheet = () => { if (isClosing) return; setIsClosing(true); }; // ✅ دکمه WEB_READY const handleSendWebReady = () => { sendToFlutter("WEB_READY", { timestamp: Date.now(), url: window.location.href, userAgent: navigator.userAgent, }); console.log("✅ WEB_READY ارسال شد"); }; // ✅ دکمه دریافت خودکار موقعیت مکانی GPS (Auto) const handleAutoLocation = async () => { try { const data = await requestAutoLocation(); alert(`📍 Auto Location: ${data.city || ""}, ${data.country || ""} (${data.latitude}, ${data.longitude})`); } catch (e: any) { alert(`❌ Auto Location Error: ${e.message}`); } }; // ✅ دکمه انتخاب دستی از روی نقشه فلاتر (Manual) const handleManualLocation = async () => { try { const data = await pickManualLocation({ latitude: 35.6892, longitude: 51.3890 }); if (data) { alert(`🗺️ Selected Location: ${data.city || ""}, ${data.country || ""} (${data.latitude}, ${data.longitude})`); } else { alert("⚠️ Map selection cancelled"); } } catch (e: any) { alert(`❌ Map Location Error: ${e.message}`); } }; // ✅ دکمه مشاور const handleOpenConsultant = () => { sendToFlutter("REQUEST_CONSULTANT", { consultant: "habib@gmail.com", }); console.log("👨‍⚕️ REQUEST_CONSULTANT ارسال شد"); }; // ✅ تست download_file const handleTestDownload = () => { const sent = downloadFile({ url: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", fileName: "test-document.pdf", title: "Test PDF Document", mimeType: "application/pdf", fileType: "document", }); console.log( sent ? "📥 download_file ارسال شد" : "⚠️ Flutter WebView یافت نشد", ); }; // ✅ تست copy_to_clipboard const handleTestCopy = async () => { const success = await copyToClipboard( "سلام! این یک متن تست برای کپی است. 🎉", "test_copy", ); console.log(success ? "📋 copy_to_clipboard ارسال شد" : "⚠️ کپی انجام نشد"); }; // ✅ تست open_external_url const handleTestOpenUrl = () => { const sent = openExternalUrl({ url: "https://habibapp.com", mode: "externalApplication", title: "test_open_url", }); console.log( sent ? "🔗 open_external_url ارسال شد" : "⚠️ Flutter WebView یافت نشد", ); }; useEffect(() => { const frameId = window.requestAnimationFrame(() => { setIsEntering(false); }); return () => window.cancelAnimationFrame(frameId); }, []); useEffect(() => { if (!isVisible) return; const previousBodyOverflow = document.body.style.overflow; const previousHtmlOverflow = document.documentElement.style.overflow; document.body.style.overflow = "hidden"; document.documentElement.style.overflow = "hidden"; return () => { document.body.style.overflow = previousBodyOverflow; document.documentElement.style.overflow = previousHtmlOverflow; }; }, [isVisible]); useEffect(() => { if (!isClosing) return; const timeoutId = window.setTimeout(() => { setIsVisible(false); onClose?.(); }, EXIT_ANIMATION_MS); return () => window.clearTimeout(timeoutId); }, [isClosing, onClose]); useEffect(() => { const handleFlutterResponse: NonNullable = ( event, ) => { if (event.action === "get_location" && event.success && event.data) { const message = `Location: ${event.data.latitude}, ${event.data.longitude}`; alert(message); } // Log responses for download/clipboard/external actions if (event.action === "download_file") { console.log(`📥 download_file response: ${event.status}`, event); } if (event.action === "copy_to_clipboard") { console.log(`📋 copy_to_clipboard response: ${event.success}`, event); } if (event.action === "open_external_url") { console.log(`🔗 open_external_url response: ${event.status}`, event); } }; const unsubscribe = window.addFlutterResponseListener?.( handleFlutterResponse, ); return () => { unsubscribe?.(); }; }, []); if (!isVisible) return null; return (
{ if (event.target === event.currentTarget) closeSheet(); }} >
{/* Header */}

Settings & Support

{/* Main buttons */}
{/* Test WebView Actions */}

🧪 Test WebView Utility Actions:

{!isInFlutterWebView() && (

⚠️ Outside WebView — copy_to_clipboard uses browser fallback, others require Flutter.

)}
{/* Test WEB_READY */}

🧪 Test Flutter Connection:

{/* Last Received Event */} {lastEvent && (

📥 Last Event from Flutter:

{lastEvent.type}
{JSON.stringify(lastEvent.payload, null, 2)}
)} {/* Logs */}

📋 Logs ({logs.length})

{showLogs && (
{logs.length === 0 ? (

هنوز لاگی ثبت نشده

) : ( logs.slice(-10).map((log, index) => (
{log}
)) )}
)}
{/* راهنما */}

💡 راهنما:
• WEB_READY هنگام باز شدن این شیت به‌صورت خودکار از کانال HabibApp ارسال می‌شود
• نقطهٔ سبز یعنی کانال HabibApp در دسترس است
• برای ارسال دستی، دکمه «ارسال WEB_READY» را بزنید

); } export default ReportActionsSheet;