| gio | cc5ce58 | 2025-06-25 07:45:21 +0400 | [diff] [blame] | 1 | import { useEnv } from "./lib/state"; |
| gio | 4867946 | 2025-05-21 04:27:27 +0000 | [diff] [blame] | 2 | import { Copy, Check } from "lucide-react"; |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 3 | import { Button } from "./components/ui/button"; |
| 4 | import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./components/ui/tooltip"; |
| 5 | import { useCallback, useState } from "react"; |
| gio | 4867946 | 2025-05-21 04:27:27 +0000 | [diff] [blame] | 6 | import { AccessType } from "./components/icon"; |
| gio | cc5ce58 | 2025-06-25 07:45:21 +0400 | [diff] [blame] | 7 | import { Access } from "config"; |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 8 | |
| 9 | export function Gateways() { |
| 10 | const env = useEnv(); |
| 11 | const groupedAccess = env.access.reduce((acc, curr) => { |
| 12 | if (!acc.has(curr.name)) { |
| 13 | acc.set(curr.name, []); |
| 14 | } |
| 15 | acc.get(curr.name)!.push(curr); |
| 16 | return acc; |
| 17 | }, new Map<string, typeof env.access>()); |
| 18 | return ( |
| 19 | <ul> |
| 20 | {Array.from(groupedAccess.entries()).map(([name, access]) => ( |
| gio | 4037078 | 2025-05-19 11:04:52 +0000 | [diff] [blame] | 21 | <> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 22 | {access.map((a) => ( |
| gio | 4037078 | 2025-05-19 11:04:52 +0000 | [diff] [blame] | 23 | <li key={name}> |
| 24 | <Gateway g={a} /> |
| 25 | </li> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 26 | ))} |
| gio | 4037078 | 2025-05-19 11:04:52 +0000 | [diff] [blame] | 27 | </> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 28 | ))} |
| 29 | </ul> |
| 30 | ); |
| 31 | } |
| 32 | |
| gio | 9f3d4f5 | 2025-07-04 08:42:34 +0000 | [diff] [blame^] | 33 | export function Gateway({ g }: { g: Access }) { |
| gio | cc5ce58 | 2025-06-25 07:45:21 +0400 | [diff] [blame] | 34 | const [hidden, content] = ((): [string, string] => { |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 35 | switch (g.type) { |
| 36 | case "https": |
| 37 | return [g.address, g.address]; |
| 38 | case "ssh": |
| 39 | case "tcp": |
| 40 | case "udp": |
| 41 | return [`${g.host}:${g.port}`, `${g.host}:${g.port}`]; |
| 42 | case "postgresql": |
| 43 | return [ |
| 44 | `postgresql://${g.username}:*****@${g.host}:${g.port}/${g.database}`, |
| 45 | `postgresql://${g.username}:${g.password}@${g.host}:${g.port}/${g.database}`, |
| 46 | ]; |
| 47 | case "mongodb": |
| 48 | return [ |
| 49 | `mongodb://${g.username}:*****@${g.host}:${g.port}/${g.database}`, |
| 50 | `mongodb://${g.username}:${g.password}@${g.host}:${g.port}/${g.database}`, |
| 51 | ]; |
| gio | cc5ce58 | 2025-06-25 07:45:21 +0400 | [diff] [blame] | 52 | default: |
| 53 | throw new Error(`Unknown gateway type: ${g.type}`); |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 54 | } |
| 55 | })(); |
| 56 | const [clicked, setClicked] = useState(false); |
| 57 | const [open, setOpen] = useState(false); |
| 58 | const copy = useCallback(() => { |
| 59 | navigator.clipboard.writeText(content); |
| 60 | setClicked(true); |
| 61 | setOpen(true); |
| 62 | setTimeout(() => { |
| 63 | setClicked(false); |
| 64 | setOpen(false); |
| 65 | }, 1000); |
| 66 | }, [content, setClicked, setOpen]); |
| 67 | return ( |
| 68 | <TooltipProvider> |
| 69 | <Tooltip delayDuration={100} open={open} onOpenChange={setOpen}> |
| 70 | <TooltipTrigger asChild> |
| gio | 6d8b71c | 2025-05-19 12:57:35 +0000 | [diff] [blame] | 71 | <Button |
| 72 | variant="ghost" |
| 73 | onClick={g.type === "https" ? () => window.open(content, "_blank") : copy} |
| 74 | className="!gap-1 !p-0 !h-fit" |
| 75 | > |
| gio | cc5ce58 | 2025-06-25 07:45:21 +0400 | [diff] [blame] | 76 | <AccessType access={g} className="w-4 h-4" /> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 77 | <div className="hover:bg-gray-200 p-x-1">{hidden}</div> |
| 78 | </Button> |
| 79 | </TooltipTrigger> |
| gio | 05a993c | 2025-05-19 11:51:33 +0000 | [diff] [blame] | 80 | <TooltipContent side="right" className="!bg-transparent cursor-pointer !p-0" sideOffset={10}> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 81 | {!clicked && <Copy className="w-4 h-4 !bg-transparent" color="black" />} |
| 82 | {clicked && <Check className="w-4 h-4 !bg-transparent" color="black" />} |
| 83 | </TooltipContent> |
| 84 | </Tooltip> |
| 85 | </TooltipProvider> |
| 86 | ); |
| 87 | } |