|
| 1 | +import { useMemo } from "react"; |
| 2 | +import type { CastActionDefinitionResponse } from "../frames/route"; |
| 3 | +import type { ParsingReport } from "frames.js"; |
| 4 | +import { Table, TableBody, TableCell, TableRow } from "@/components/table"; |
| 5 | +import { AlertTriangleIcon, CheckCircle2Icon, XCircleIcon } from "lucide-react"; |
| 6 | +import { cn } from "@/lib/utils"; |
| 7 | +import { ShortenedText } from "./shortened-text"; |
| 8 | + |
| 9 | +function isPropertyExperimental([key, value]: [string, string]) { |
| 10 | + return false; |
| 11 | +} |
| 12 | + |
| 13 | +type ActionDebuggerPropertiesTableProps = { |
| 14 | + actionMetadataItem: CastActionDefinitionResponse; |
| 15 | +}; |
| 16 | + |
| 17 | +export function ActionDebuggerPropertiesTable({ |
| 18 | + actionMetadataItem, |
| 19 | +}: ActionDebuggerPropertiesTableProps) { |
| 20 | + const properties = useMemo(() => { |
| 21 | + /** tuple of key and value */ |
| 22 | + const validProperties: [string, string][] = []; |
| 23 | + /** tuple of key and error message */ |
| 24 | + const invalidProperties: [string, ParsingReport[]][] = []; |
| 25 | + const visitedInvalidProperties: string[] = []; |
| 26 | + const result = actionMetadataItem; |
| 27 | + |
| 28 | + // we need to check validation errors first because getFrame incorrectly return a value for a key even if it's invalid |
| 29 | + for (const [key, reports] of Object.entries(result.reports)) { |
| 30 | + invalidProperties.push([key, reports]); |
| 31 | + visitedInvalidProperties.push(key); |
| 32 | + } |
| 33 | + |
| 34 | + for (const [key, value] of Object.entries(result.action)) { |
| 35 | + if (visitedInvalidProperties.includes(key) || value == null) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + if (typeof value === "object") { |
| 40 | + validProperties.push([key, JSON.stringify(value)]); |
| 41 | + } else { |
| 42 | + validProperties.push([key, value]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return { |
| 47 | + validProperties, |
| 48 | + invalidProperties, |
| 49 | + isValid: invalidProperties.length === 0, |
| 50 | + hasExperimentalProperties: false, |
| 51 | + }; |
| 52 | + }, [actionMetadataItem]); |
| 53 | + |
| 54 | + return ( |
| 55 | + <Table> |
| 56 | + <TableBody> |
| 57 | + {properties.validProperties.map(([propertyKey, value]) => { |
| 58 | + return ( |
| 59 | + <TableRow key={`${propertyKey}-valid`}> |
| 60 | + <TableCell> |
| 61 | + {isPropertyExperimental([propertyKey, value]) ? ( |
| 62 | + <span className="whitespace-nowrap flex"> |
| 63 | + <div className="inline"> |
| 64 | + <CheckCircle2Icon size={20} color="orange" /> |
| 65 | + </div> |
| 66 | + <div className="inline text-slate-500">*</div> |
| 67 | + </span> |
| 68 | + ) : ( |
| 69 | + <CheckCircle2Icon size={20} color="green" /> |
| 70 | + )} |
| 71 | + </TableCell> |
| 72 | + <TableCell>{propertyKey}</TableCell> |
| 73 | + <TableCell className="text-slate-500"> |
| 74 | + <ShortenedText text={value} maxLength={30} /> |
| 75 | + </TableCell> |
| 76 | + </TableRow> |
| 77 | + ); |
| 78 | + })} |
| 79 | + {properties.invalidProperties.flatMap( |
| 80 | + ([propertyKey, errorMessages]) => { |
| 81 | + return errorMessages.map((errorMessage, i) => { |
| 82 | + return ( |
| 83 | + <TableRow key={`${propertyKey}-${i}-invalid`}> |
| 84 | + <TableCell> |
| 85 | + {errorMessage.level === "error" ? ( |
| 86 | + <XCircleIcon size={20} color="red" /> |
| 87 | + ) : ( |
| 88 | + <AlertTriangleIcon size={20} color="orange" /> |
| 89 | + )} |
| 90 | + </TableCell> |
| 91 | + <TableCell>{propertyKey}</TableCell> |
| 92 | + <TableCell className="text-slate-500"> |
| 93 | + <p |
| 94 | + className={cn( |
| 95 | + "font-bold", |
| 96 | + errorMessage.level === "error" |
| 97 | + ? "text-red-500" |
| 98 | + : "text-orange-500" |
| 99 | + )} |
| 100 | + > |
| 101 | + {errorMessage.message} |
| 102 | + </p> |
| 103 | + </TableCell> |
| 104 | + </TableRow> |
| 105 | + ); |
| 106 | + }); |
| 107 | + } |
| 108 | + )} |
| 109 | + {properties.hasExperimentalProperties && ( |
| 110 | + <TableRow> |
| 111 | + <TableCell colSpan={3} className="text-slate-500"> |
| 112 | + *This property is experimental and may not have been adopted in |
| 113 | + clients yet |
| 114 | + </TableCell> |
| 115 | + </TableRow> |
| 116 | + )} |
| 117 | + </TableBody> |
| 118 | + </Table> |
| 119 | + ); |
| 120 | +} |
0 commit comments