|
| 1 | +import { useState, useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +interface SchemaResult { |
| 4 | + id: string; |
| 5 | + schema: Record<string, unknown>; |
| 6 | + schemaHash: string; |
| 7 | + createdAt: string; |
| 8 | + labels: string[]; |
| 9 | +} |
| 10 | + |
| 11 | +export default function SchemaBrowser() { |
| 12 | + const [query, setQuery] = useState(""); |
| 13 | + const [filterType, setFilterType] = useState<"q" | "label" | "slug">("q"); |
| 14 | + const [schemas, setSchemas] = useState<SchemaResult[]>([]); |
| 15 | + const [loading, setLoading] = useState(true); |
| 16 | + const timerRef = useRef<ReturnType<typeof setTimeout>>(); |
| 17 | + |
| 18 | + async function load(q = "", type = filterType) { |
| 19 | + setLoading(true); |
| 20 | + const params = new URLSearchParams(); |
| 21 | + if (q) params.set(type, q); |
| 22 | + params.set("limit", "50"); |
| 23 | + try { |
| 24 | + const res = await fetch(`/api/schemas?${params}`); |
| 25 | + const data = await res.json(); |
| 26 | + setSchemas(Array.isArray(data) ? data : data.id ? [data] : []); |
| 27 | + } catch { |
| 28 | + setSchemas([]); |
| 29 | + } |
| 30 | + setLoading(false); |
| 31 | + } |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + load(); |
| 35 | + }, []); |
| 36 | + |
| 37 | + function handleInput(value: string) { |
| 38 | + setQuery(value); |
| 39 | + clearTimeout(timerRef.current); |
| 40 | + timerRef.current = setTimeout(() => load(value, filterType), 300); |
| 41 | + } |
| 42 | + |
| 43 | + function handleFilterChange(type: "q" | "label" | "slug") { |
| 44 | + setFilterType(type); |
| 45 | + if (query) { |
| 46 | + clearTimeout(timerRef.current); |
| 47 | + timerRef.current = setTimeout(() => load(query, type), 100); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <div className="flex gap-2 mb-6"> |
| 54 | + <input |
| 55 | + type="search" |
| 56 | + placeholder={ |
| 57 | + filterType === "q" |
| 58 | + ? "Search schema content..." |
| 59 | + : filterType === "label" |
| 60 | + ? "Search by label..." |
| 61 | + : "Search by type name..." |
| 62 | + } |
| 63 | + className="flex-1 bg-parchment border border-rule px-3 py-2 text-sm font-mono placeholder:text-ink-muted focus:outline-none focus:border-ink" |
| 64 | + value={query} |
| 65 | + onChange={(e) => handleInput(e.target.value)} |
| 66 | + /> |
| 67 | + <div className="flex border border-rule rounded overflow-hidden text-xs"> |
| 68 | + <button |
| 69 | + onClick={() => handleFilterChange("q")} |
| 70 | + className={`px-3 py-2 transition-colors ${filterType === "q" ? "bg-ink text-parchment" : "hover:bg-parchment-dark"}`} |
| 71 | + > |
| 72 | + Content |
| 73 | + </button> |
| 74 | + <button |
| 75 | + onClick={() => handleFilterChange("slug")} |
| 76 | + className={`px-3 py-2 border-l border-rule transition-colors ${filterType === "slug" ? "bg-ink text-parchment" : "hover:bg-parchment-dark"}`} |
| 77 | + > |
| 78 | + Type |
| 79 | + </button> |
| 80 | + <button |
| 81 | + onClick={() => handleFilterChange("label")} |
| 82 | + className={`px-3 py-2 border-l border-rule transition-colors ${filterType === "label" ? "bg-ink text-parchment" : "hover:bg-parchment-dark"}`} |
| 83 | + > |
| 84 | + Label |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + |
| 89 | + <div className="space-y-2"> |
| 90 | + {loading ? ( |
| 91 | + <p className="text-sm text-ink-muted py-8 text-center">Loading...</p> |
| 92 | + ) : schemas.length === 0 ? ( |
| 93 | + <p className="text-sm text-ink-muted py-8 text-center">No schemas found.</p> |
| 94 | + ) : ( |
| 95 | + schemas.map((s) => { |
| 96 | + const properties = (s.schema as any)?.properties ?? {}; |
| 97 | + const fieldNames = Object.keys(properties); |
| 98 | + const isPrivate = (s.schema as any)?.private === true; |
| 99 | + |
| 100 | + return ( |
| 101 | + <a |
| 102 | + key={s.id} |
| 103 | + href={`/schemas/${s.id}`} |
| 104 | + className="block border border-rule p-4 hover:bg-parchment-dark/50 transition-colors" |
| 105 | + > |
| 106 | + <div className="flex items-center justify-between mb-2"> |
| 107 | + <div className="flex items-center gap-2"> |
| 108 | + <code className="font-mono text-xs text-ink-muted"> |
| 109 | + {s.schemaHash.slice(0, 12)}… |
| 110 | + </code> |
| 111 | + {isPrivate && ( |
| 112 | + <span className="text-[11px] border border-rule px-1.5 py-0.5 text-ink-muted"> |
| 113 | + private |
| 114 | + </span> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + <span className="text-[11px] text-ink-muted"> |
| 118 | + {new Date(s.createdAt).toLocaleDateString("en-US", { |
| 119 | + month: "short", |
| 120 | + day: "numeric", |
| 121 | + year: "numeric", |
| 122 | + })} |
| 123 | + </span> |
| 124 | + </div> |
| 125 | + |
| 126 | + {/* Field summary */} |
| 127 | + <div className="flex flex-wrap gap-1.5 mb-2"> |
| 128 | + {fieldNames.slice(0, 8).map((name) => ( |
| 129 | + <span |
| 130 | + key={name} |
| 131 | + className="text-[11px] font-mono bg-parchment-dark border border-rule px-1.5 py-0.5 rounded" |
| 132 | + > |
| 133 | + {name} |
| 134 | + </span> |
| 135 | + ))} |
| 136 | + {fieldNames.length > 8 && ( |
| 137 | + <span className="text-[11px] text-ink-muted px-1.5 py-0.5"> |
| 138 | + +{fieldNames.length - 8} more |
| 139 | + </span> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + |
| 143 | + {/* Labels */} |
| 144 | + {s.labels.length > 0 && ( |
| 145 | + <div className="flex flex-wrap gap-1"> |
| 146 | + {s.labels.map((label) => ( |
| 147 | + <span |
| 148 | + key={label} |
| 149 | + className="text-[11px] text-link bg-blue-50 border border-blue-200 px-1.5 py-0.5 rounded" |
| 150 | + > |
| 151 | + {label} |
| 152 | + </span> |
| 153 | + ))} |
| 154 | + </div> |
| 155 | + )} |
| 156 | + </a> |
| 157 | + ); |
| 158 | + }) |
| 159 | + )} |
| 160 | + </div> |
| 161 | + </> |
| 162 | + ); |
| 163 | +} |
0 commit comments