Spaces:
Running
Running
File size: 7,289 Bytes
9a9d18a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
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<NgrokConfigModalProps> = ({
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 (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="bg-gray-900 border-gray-800 text-white w-[95vw] max-w-[600px] max-h-[90vh] overflow-y-auto p-4 sm:p-6 md:p-8">
<DialogHeader>
<div className="flex justify-center items-center mb-4">
<Globe className="w-8 h-8 text-blue-500" />
</div>
<DialogTitle className="text-white text-center text-xl sm:text-2xl font-bold">
Ngrok Configuration
</DialogTitle>
<DialogDescription className="text-gray-400 text-center text-sm sm:text-base">
Configure ngrok tunnel for external access and phone camera
features.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 sm:space-y-6 py-4">
{/* Current Status */}
<div className="bg-gray-800 rounded-lg p-3 sm:p-4 border border-gray-700">
<div className="flex items-center gap-3 mb-2">
{isNgrokEnabled ? (
<Wifi className="w-4 h-4 sm:w-5 sm:h-5 text-green-500" />
) : (
<WifiOff className="w-4 h-4 sm:w-5 sm:h-5 text-gray-500" />
)}
<span className="font-semibold text-sm sm:text-base">
Current Mode: {isNgrokEnabled ? "Ngrok" : "Localhost"}
</span>
</div>
<p className="text-xs sm:text-sm text-gray-400 break-all">
{isNgrokEnabled
? `Using: ${ngrokUrl}`
: "Using: http://localhost:8000"}
</p>
</div>
{/* URL Input */}
<div className="space-y-2">
<Label
htmlFor="ngrokUrl"
className="text-sm font-medium text-gray-300"
>
Ngrok URL
</Label>
<Input
id="ngrokUrl"
value={inputUrl}
onChange={(e) => setInputUrl(e.target.value)}
placeholder="https://abc123.ngrok.io"
className="bg-gray-800 border-gray-700 text-white text-sm sm:text-base"
/>
<p className="text-xs text-gray-500">
Enter your ngrok tunnel URL. Leave empty to use localhost.
</p>
</div>
{/* Benefits */}
<div className="bg-green-900/20 border border-green-800 rounded-lg p-3 sm:p-4">
<h3 className="font-semibold text-green-400 mb-2 text-sm sm:text-base">
Why use Ngrok?
</h3>
<ul className="text-xs sm:text-sm text-gray-300 space-y-1">
<li>• Access your robot from anywhere on the internet</li>
<li>• Use phone cameras as secondary recording angles</li>
<li>• Share your robot session with remote collaborators</li>
<li>• Test your setup from different devices</li>
</ul>
</div>
{/* Action Buttons */}
<div className="flex flex-col gap-3 sm:gap-4 pt-4">
<Button
onClick={handleSave}
disabled={isTestingConnection}
className="w-full bg-blue-500 hover:bg-blue-600 text-white px-6 sm:px-8 py-3 text-sm sm:text-lg transition-all shadow-md shadow-blue-500/30 hover:shadow-lg hover:shadow-blue-500/40"
>
{isTestingConnection ? (
<>
<div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" />
Testing Connection...
</>
) : (
<>
<ExternalLink className="w-4 h-4 sm:w-5 sm:h-5 mr-2" />
Save Configuration
</>
)}
</Button>
<div className="flex flex-col sm:flex-row gap-3">
{isNgrokEnabled && (
<Button
onClick={handleReset}
variant="outline"
className="w-full border-orange-500 hover:border-orange-400 text-orange-400 hover:text-orange-300 px-6 sm:px-8 py-3 text-sm sm:text-lg"
>
Reset to Localhost
</Button>
)}
<Button
onClick={() => onOpenChange(false)}
variant="outline"
className="w-full border-gray-500 hover:border-gray-200 px-6 sm:px-8 py-3 text-sm sm:text-lg text-zinc-500 bg-zinc-900 hover:bg-zinc-800"
>
Cancel
</Button>
</div>
</div>
</div>
</DialogContent>
</Dialog>
);
};
export default NgrokConfigModal;
|