| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 1 | import { z } from "zod"; |
| 2 | import { accessSchema, useEnv } from "./lib/state"; |
| gio | 4867946 | 2025-05-21 04:27:27 +0000 | [diff] [blame^] | 3 | import { Copy, Check } from "lucide-react"; |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 4 | import { Button } from "./components/ui/button"; |
| 5 | import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "./components/ui/tooltip"; |
| 6 | import { useCallback, useState } from "react"; |
| gio | 4867946 | 2025-05-21 04:27:27 +0000 | [diff] [blame^] | 7 | import { AccessType } from "./components/icon"; |
| 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 | |
| 33 | function Gateway({ g }: { g: z.infer<typeof accessSchema> }) { |
| 34 | const [hidden, content] = (() => { |
| 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 | ]; |
| 52 | } |
| 53 | })(); |
| 54 | const [clicked, setClicked] = useState(false); |
| 55 | const [open, setOpen] = useState(false); |
| 56 | const copy = useCallback(() => { |
| 57 | navigator.clipboard.writeText(content); |
| 58 | setClicked(true); |
| 59 | setOpen(true); |
| 60 | setTimeout(() => { |
| 61 | setClicked(false); |
| 62 | setOpen(false); |
| 63 | }, 1000); |
| 64 | }, [content, setClicked, setOpen]); |
| 65 | return ( |
| 66 | <TooltipProvider> |
| 67 | <Tooltip delayDuration={100} open={open} onOpenChange={setOpen}> |
| 68 | <TooltipTrigger asChild> |
| gio | 6d8b71c | 2025-05-19 12:57:35 +0000 | [diff] [blame] | 69 | <Button |
| 70 | variant="ghost" |
| 71 | onClick={g.type === "https" ? () => window.open(content, "_blank") : copy} |
| 72 | className="!gap-1 !p-0 !h-fit" |
| 73 | > |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 74 | <AccessType type={g.type} className="w-4 h-4" /> |
| 75 | <div className="hover:bg-gray-200 p-x-1">{hidden}</div> |
| 76 | </Button> |
| 77 | </TooltipTrigger> |
| gio | 05a993c | 2025-05-19 11:51:33 +0000 | [diff] [blame] | 78 | <TooltipContent side="right" className="!bg-transparent cursor-pointer !p-0" sideOffset={10}> |
| gio | b77cb93 | 2025-05-19 09:37:14 +0000 | [diff] [blame] | 79 | {!clicked && <Copy className="w-4 h-4 !bg-transparent" color="black" />} |
| 80 | {clicked && <Check className="w-4 h-4 !bg-transparent" color="black" />} |
| 81 | </TooltipContent> |
| 82 | </Tooltip> |
| 83 | </TooltipProvider> |
| 84 | ); |
| 85 | } |