|
| 1 | +import clsx from "clsx"; |
| 2 | +import { Input } from "./Input"; |
| 3 | +import { useSettingsContext } from "../context/useRDTContext"; |
| 4 | +import { ExtendedRoute, constructRoutePath } from "../utils/routing"; |
| 5 | +import type { MouseEvent } from "react"; |
| 6 | +import { Tag } from "./Tag"; |
| 7 | +import { X } from "lucide-react"; |
| 8 | + |
| 9 | +interface RouteInfoProps { |
| 10 | + route: ExtendedRoute; |
| 11 | + className?: string; |
| 12 | + openNewRoute: (path: string) => (e?: MouseEvent<HTMLDivElement | HTMLButtonElement>) => void; |
| 13 | + onClose?: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +export const RouteInfo = ({ route, className, openNewRoute, onClose }: RouteInfoProps) => { |
| 17 | + const { settings, setSettings } = useSettingsContext(); |
| 18 | + const { routeWildcards, routeViewMode } = settings; |
| 19 | + const { hasWildcard, path, pathToOpen } = constructRoutePath(route, routeWildcards); |
| 20 | + const isTreeView = routeViewMode === "tree"; |
| 21 | + return ( |
| 22 | + <div className={clsx(className, "rdt-relative")}> |
| 23 | + {isTreeView && ( |
| 24 | + <> |
| 25 | + <X onClick={onClose} className="rdt-absolute rdt-right-2 rdt-top-2 rdt-cursor-pointer rdt-text-red-600" /> |
| 26 | + <h1 className="rdt-text-xl rdt-font-semibold">{route.url}</h1> |
| 27 | + <hr className="rdt-mb-4 rdt-mt-1" /> |
| 28 | + <h3> |
| 29 | + <span className="rdt-text-gray-500">Path:</span> {path} |
| 30 | + </h3> |
| 31 | + <h3> |
| 32 | + <span className="rdt-text-gray-500">Url:</span> {pathToOpen} |
| 33 | + </h3> |
| 34 | + </> |
| 35 | + )} |
| 36 | + <div className="rdt-flex rdt-gap-2"> |
| 37 | + <span className="rdt-text-gray-500">Key:</span> |
| 38 | + {route.id} |
| 39 | + </div> |
| 40 | + <div className="rdt-mb-4 rdt-mt-4 rdt-flex rdt-flex-col rdt-gap-2"> |
| 41 | + <span className="rdt-text-gray-500">Components contained in the route:</span> |
| 42 | + <div className="rdt-flex rdt-gap-2"> |
| 43 | + <Tag color={route.hasLoader ? "GREEN" : "RED"}>Loader</Tag> |
| 44 | + <Tag color={route.hasAction ? "GREEN" : "RED"}>Action</Tag> |
| 45 | + <Tag color={route.hasErrorBoundary ? "GREEN" : "RED"}>ErrorBoundary</Tag> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + {hasWildcard && ( |
| 49 | + <> |
| 50 | + <p className="rdt-mb-2 rdt-text-gray-500">Wildcard parameters:</p> |
| 51 | + <div |
| 52 | + className={clsx("rdt-mb-4 rdt-grid rdt-w-full rdt-grid-cols-2 rdt-gap-2", isTreeView && "rdt-grid-cols-1")} |
| 53 | + > |
| 54 | + {route.url |
| 55 | + .split("/") |
| 56 | + .filter((p) => p.startsWith(":")) |
| 57 | + .map((param) => ( |
| 58 | + <div key={param} className="rdt-flex rdt-w-full rdt-gap-2"> |
| 59 | + <Tag key={param} color="BLUE"> |
| 60 | + {param} |
| 61 | + </Tag> |
| 62 | + <Input |
| 63 | + value={routeWildcards[route.id]?.[param] || ""} |
| 64 | + onChange={(e) => |
| 65 | + setSettings({ |
| 66 | + routeWildcards: { |
| 67 | + ...routeWildcards, |
| 68 | + [route.id]: { |
| 69 | + ...routeWildcards[route.id], |
| 70 | + [param]: e.target.value, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }) |
| 74 | + } |
| 75 | + placeholder={param} |
| 76 | + /> |
| 77 | + </div> |
| 78 | + ))} |
| 79 | + </div> |
| 80 | + </> |
| 81 | + )} |
| 82 | + {isTreeView && ( |
| 83 | + <button |
| 84 | + className="rdt-mr-2 rdt-whitespace-nowrap rdt-rounded rdt-border rdt-border-gray-400 rdt-px-2 rdt-py-1 rdt-text-sm" |
| 85 | + onClick={openNewRoute(path)} |
| 86 | + > |
| 87 | + Open in browser |
| 88 | + </button> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + ); |
| 92 | +}; |
0 commit comments