|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { match } from "ts-pattern" |
| 4 | +import { Methods } from ".." |
| 5 | +import { useWorkerValue } from "../providers/useMswDevtoolsContext" |
| 6 | + |
| 7 | +import { cn } from "@/shared/lib/utils" |
| 8 | +import { Checkbox } from "@/shared/ui/checkbox" |
| 9 | +import { InlineCode } from "@/shared/ui/typography" |
| 10 | +import { generatorSerializedRouteHandlers } from "@/shared/utils/generatorSerializedRouteHandlers" |
| 11 | + |
| 12 | +export const DevtoolsHandlerList = () => { |
| 13 | + const worker = useWorkerValue() |
| 14 | + const handlers = worker?.listHandlers() ?? [] |
| 15 | + const serializedHandlers = generatorSerializedRouteHandlers(handlers) |
| 16 | + |
| 17 | + return ( |
| 18 | + <ul className="[&>li]:border-b-[1px] [&>li]:border-solid [&>li]:border-white list-none overflow-y-auto h-[250px] scrollbar-hide"> |
| 19 | + {serializedHandlers.map((handler) => ( |
| 20 | + <li key={handler.id} className="px-[6px] py-[12px] flex items-center"> |
| 21 | + <Checkbox id={handler.id} /> |
| 22 | + <label htmlFor={handler.id} className="pl-[12px]"> |
| 23 | + <MethodTag method={handler.method} /> |
| 24 | + <span className="ml-2 font-semibold">{handler.url}</span> |
| 25 | + </label> |
| 26 | + </li> |
| 27 | + ))} |
| 28 | + </ul> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +export const MethodTag = ({ method }: { method: Methods }) => { |
| 33 | + const className = match(method) |
| 34 | + .with("GET", () => "text-red-500") |
| 35 | + .with("POST", () => "text-blue-500") |
| 36 | + .with("PUT", () => "text-green-500") |
| 37 | + .with("DELETE", () => "text-yellow-500") |
| 38 | + .with("PATCH", () => "text-purple-500") |
| 39 | + .otherwise(() => "text-white") |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="inline-flex w-[100px] justify-center"> |
| 43 | + <InlineCode className={cn("bg-slate-950 w-[80px]", className)}> |
| 44 | + {method} |
| 45 | + </InlineCode> |
| 46 | + </div> |
| 47 | + ) |
| 48 | +} |
0 commit comments