| gio | a5fde99 | 2025-07-07 08:18:19 +0000 | [diff] [blame] | 1 | import { useMemo, useState } from "react"; |
| 2 | import { useEnv, useLeadAgent } from "@/lib/state"; |
| 3 | import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; |
| 4 | import { Button } from "./ui/button"; |
| 5 | import { RefreshCw } from "lucide-react"; |
| 6 | import { Agent } from "@/Agent"; |
| 7 | import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable"; |
| 8 | |
| 9 | export function Preview() { |
| 10 | const leadAgent = useLeadAgent(); |
| 11 | if (leadAgent == null) { |
| 12 | return <PreviewImpl />; |
| 13 | } |
| 14 | return ( |
| 15 | <ResizablePanelGroup direction="horizontal" className="h-full"> |
| 16 | <ResizablePanel defaultSize={25}> |
| 17 | <div className="p-4 h-full"> |
| gio | 007c857 | 2025-07-08 04:27:35 +0000 | [diff] [blame] | 18 | {leadAgent ? <Agent agent={leadAgent} compact={true} /> : <div>No agent available</div>} |
| gio | a5fde99 | 2025-07-07 08:18:19 +0000 | [diff] [blame] | 19 | </div> |
| 20 | </ResizablePanel> |
| 21 | <ResizableHandle withHandle /> |
| 22 | <ResizablePanel> |
| 23 | <PreviewImpl /> |
| 24 | </ResizablePanel> |
| 25 | </ResizablePanelGroup> |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | function PreviewImpl() { |
| 30 | const env = useEnv(); |
| 31 | const httpsAccess = useMemo(() => env.access.filter((a) => a.type === "https"), [env]); |
| 32 | const [selectedUrl, setSelectedUrl] = useState<string | null>(null); |
| 33 | const [refreshKey, setRefreshKey] = useState(0); |
| 34 | return ( |
| 35 | <div className="w-full h-full flex flex-col"> |
| 36 | <div className="p-4 border-b flex items-center gap-2"> |
| 37 | <Select onValueChange={setSelectedUrl}> |
| 38 | <SelectTrigger className="w-[300px]"> |
| 39 | <SelectValue placeholder="Select a gateway" /> |
| 40 | </SelectTrigger> |
| 41 | <SelectContent> |
| 42 | {httpsAccess.map((a) => ( |
| 43 | <SelectItem key={a.address} value={a.address}> |
| 44 | {a.address} |
| 45 | </SelectItem> |
| 46 | ))} |
| 47 | </SelectContent> |
| 48 | </Select> |
| 49 | <Button variant="outline" size="icon" onClick={() => setRefreshKey((prev) => prev + 1)}> |
| 50 | <RefreshCw className="h-4 w-4" /> |
| 51 | </Button> |
| 52 | </div> |
| 53 | <div className="flex-1"> |
| 54 | {selectedUrl ? ( |
| 55 | <iframe key={refreshKey} src={selectedUrl} className="w-full h-full border-0" /> |
| 56 | ) : ( |
| 57 | <div className="flex items-center justify-center h-full"> |
| 58 | <p>Select a gateway to preview</p> |
| 59 | </div> |
| 60 | )} |
| 61 | </div> |
| 62 | </div> |
| 63 | ); |
| 64 | } |