import React, { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog"; import { Globe, Wifi, WifiOff, ExternalLink } from "lucide-react"; import { useApi } from "@/contexts/ApiContext"; import { useToast } from "@/hooks/use-toast"; interface NgrokConfigModalProps { open: boolean; onOpenChange: (open: boolean) => void; } const NgrokConfigModal: React.FC = ({ open, onOpenChange, }) => { const { ngrokUrl, isNgrokEnabled, setNgrokUrl, resetToLocalhost, fetchWithHeaders, } = useApi(); const [inputUrl, setInputUrl] = useState(ngrokUrl); const [isTestingConnection, setIsTestingConnection] = useState(false); const { toast } = useToast(); const handleSave = async () => { if (!inputUrl.trim()) { resetToLocalhost(); toast({ title: "Ngrok Disabled", description: "Switched back to localhost mode.", }); onOpenChange(false); return; } setIsTestingConnection(true); try { // Clean the URL let cleanUrl = inputUrl.trim(); if (!cleanUrl.startsWith("http")) { cleanUrl = `https://${cleanUrl}`; } cleanUrl = cleanUrl.replace(/\/$/, ""); // Test the connection const testResponse = await fetchWithHeaders(`${cleanUrl}/health`, { method: "GET", headers: { Accept: "application/json", }, }); if (testResponse.ok) { setNgrokUrl(cleanUrl); toast({ title: "Ngrok Configured Successfully", description: `Connected to ${cleanUrl}. All API calls will now use this URL.`, }); onOpenChange(false); } else { throw new Error(`Server responded with status ${testResponse.status}`); } } catch (error) { console.error("Failed to connect to ngrok URL:", error); toast({ title: "Connection Failed", description: `Could not connect to ${inputUrl}. Please check the URL and ensure your ngrok tunnel is running.`, variant: "destructive", }); } finally { setIsTestingConnection(false); } }; const handleReset = () => { resetToLocalhost(); setInputUrl(""); toast({ title: "Reset to Localhost", description: "All API calls will now use localhost:8000.", }); onOpenChange(false); }; return (
Ngrok Configuration Configure ngrok tunnel for external access and phone camera features.
{/* Current Status */}
{isNgrokEnabled ? ( ) : ( )} Current Mode: {isNgrokEnabled ? "Ngrok" : "Localhost"}

{isNgrokEnabled ? `Using: ${ngrokUrl}` : "Using: http://localhost:8000"}

{/* URL Input */}
setInputUrl(e.target.value)} placeholder="https://abc123.ngrok.io" className="bg-gray-800 border-gray-700 text-white text-sm sm:text-base" />

Enter your ngrok tunnel URL. Leave empty to use localhost.

{/* Benefits */}

Why use Ngrok?

  • • Access your robot from anywhere on the internet
  • • Use phone cameras as secondary recording angles
  • • Share your robot session with remote collaborators
  • • Test your setup from different devices
{/* Action Buttons */}
{isNgrokEnabled && ( )}
); }; export default NgrokConfigModal;