|
| 1 | +import { useMemo, useState } from "react" |
| 2 | +import { cn } from "../utils/className" |
| 3 | +import { type IndexEntry } from "../utils/indexer/types" |
| 4 | +import { useIndexer } from "../utils/indexer/useIndexer" |
| 5 | + |
| 6 | +export function DevToolsScene() { |
| 7 | + const { index, isIndexing, indexerInstance } = useIndexer() |
| 8 | + |
| 9 | + const [selectedEntry, setSelectedEntry] = useState<IndexEntry | null>(null) |
| 10 | + |
| 11 | + const [filterQuery, setFilterQuery] = useState("") |
| 12 | + |
| 13 | + const entries = useMemo(() => Object.values(index), [index]) |
| 14 | + |
| 15 | + const filteredEntries = useMemo( |
| 16 | + () => |
| 17 | + entries.filter(entry => { |
| 18 | + if (entry.node.id === filterQuery) return true |
| 19 | + return entry.name?.toLowerCase().includes(filterQuery.toLowerCase()) |
| 20 | + }), |
| 21 | + [entries, filterQuery] |
| 22 | + ) |
| 23 | + |
| 24 | + const stats = useMemo( |
| 25 | + () => ({ |
| 26 | + total: entries.length, |
| 27 | + byType: entries.reduce( |
| 28 | + (acc, entry) => { |
| 29 | + acc[entry.type] = (acc[entry.type] ?? 0) + 1 |
| 30 | + return acc |
| 31 | + }, |
| 32 | + {} as Record<string, number> |
| 33 | + ), |
| 34 | + withText: entries.filter(e => e.text).length, |
| 35 | + }), |
| 36 | + [entries] |
| 37 | + ) |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className="flex flex-col h-full"> |
| 41 | + <div className="flex flex-col gap-2 p-2"> |
| 42 | + <div className=" flex gap-1"> |
| 43 | + <input |
| 44 | + type="text" |
| 45 | + placeholder="Filter by name or id" |
| 46 | + value={filterQuery} |
| 47 | + onChange={e => setFilterQuery(e.target.value)} |
| 48 | + className="w-full p-2 border border-gray-300 rounded flex-1" |
| 49 | + /> |
| 50 | + <button |
| 51 | + onClick={() => indexerInstance.restart()} |
| 52 | + disabled={isIndexing} |
| 53 | + className="px-3 py-1 bg-blue-500 text-white text-sm rounded disabled:opacity-50 w-auto" |
| 54 | + > |
| 55 | + {isIndexing ? "Indexing..." : "Re-index"} |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + <div className="text-xs text-gray-600 gap-x-1 flex space-between"> |
| 59 | + {Object.entries(stats.byType).map(([type, count]) => ( |
| 60 | + <span key={type} className="bg-gray-100 px-2 py-1 rounded block"> |
| 61 | + {type}: {count} |
| 62 | + </span> |
| 63 | + ))} |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + |
| 67 | + <div className="flex-1 flex overflow-hidden border-t border-t-framer-divider"> |
| 68 | + <div className="w-1/3 max-w-sm border-r border-r-framer-divider overflow-auto"> |
| 69 | + <div className="divide-y"> |
| 70 | + {filteredEntries.map(entry => ( |
| 71 | + <div |
| 72 | + key={`${entry.id}-${entry.type}`} |
| 73 | + className={cn( |
| 74 | + "p-3 cursor-pointer hover:bg-gray-50", |
| 75 | + selectedEntry === entry && "bg-blue-50" |
| 76 | + )} |
| 77 | + onClick={() => setSelectedEntry(entry)} |
| 78 | + > |
| 79 | + <div className="flex items-start justify-between"> |
| 80 | + <div className="flex-1 min-w-0"> |
| 81 | + <p className="text-sm font-medium text-gray-900 truncate"> |
| 82 | + {entry.name || "Unnamed"} |
| 83 | + </p> |
| 84 | + <p className="text-xs text-gray-500">{entry.type}</p> |
| 85 | + {entry.text && ( |
| 86 | + <p className="text-xs text-gray-600 mt-1 line-clamp-2">{entry.text}</p> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + |
| 95 | + {/* Detail Panel */} |
| 96 | + <div className="flex-1 overflow-auto"> |
| 97 | + {selectedEntry ? ( |
| 98 | + <div className="p-4 space-y-4"> |
| 99 | + <h3 className="font-semibold">Entry Details</h3> |
| 100 | + |
| 101 | + <div className="space-y-3"> |
| 102 | + <div> |
| 103 | + <label className="block text-xs font-medium text-gray-700 mb-1">ID</label> |
| 104 | + <code className="text-xs bg-gray-100 p-2 rounded block font-mono"> |
| 105 | + {selectedEntry.id} |
| 106 | + </code> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div> |
| 110 | + <label className="block text-xs font-medium text-gray-700 mb-1">Type</label> |
| 111 | + <span className="text-sm bg-blue-100 text-blue-800 px-2 py-1 rounded"> |
| 112 | + {selectedEntry.type} |
| 113 | + </span> |
| 114 | + </div> |
| 115 | + |
| 116 | + <div> |
| 117 | + <label className="block text-xs font-medium text-gray-700 mb-1">Name</label> |
| 118 | + <p className="text-sm bg-gray-50 p-2 rounded"> |
| 119 | + {selectedEntry.name || "(no name)"} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + |
| 123 | + {selectedEntry.text && ( |
| 124 | + <div> |
| 125 | + <label className="block text-xs font-medium text-gray-700 mb-1"> |
| 126 | + Text Content |
| 127 | + </label> |
| 128 | + <p className="text-sm bg-gray-50 p-2 rounded whitespace-pre-wrap"> |
| 129 | + {selectedEntry.text} |
| 130 | + </p> |
| 131 | + </div> |
| 132 | + )} |
| 133 | + |
| 134 | + <div> |
| 135 | + <label className="block text-xs font-medium text-gray-700 mb-1">Root Node</label> |
| 136 | + <p className="text-sm bg-gray-50 p-2 rounded"> |
| 137 | + {selectedEntry.rootNodeName} ({selectedEntry.rootNodeType}) |
| 138 | + </p> |
| 139 | + </div> |
| 140 | + |
| 141 | + <div> |
| 142 | + <label className="block text-xs font-medium text-gray-700 mb-1"> |
| 143 | + Raw Node Data |
| 144 | + </label> |
| 145 | + <details className="text-xs"> |
| 146 | + <summary className="cursor-pointer text-blue-600 hover:text-blue-800"> |
| 147 | + Show raw node object |
| 148 | + </summary> |
| 149 | + <pre className="mt-2 bg-gray-100 p-2 rounded overflow-auto text-xs"> |
| 150 | + {JSON.stringify( |
| 151 | + { |
| 152 | + ...selectedEntry.node, |
| 153 | + // Limit some potentially large properties |
| 154 | + ...(selectedEntry.node.__class && { |
| 155 | + __class: selectedEntry.node.__class, |
| 156 | + }), |
| 157 | + ...(selectedEntry.node.id && { id: selectedEntry.node.id }), |
| 158 | + ...("name" in selectedEntry.node && { |
| 159 | + name: selectedEntry.node.name, |
| 160 | + }), |
| 161 | + }, |
| 162 | + null, |
| 163 | + 2 |
| 164 | + )} |
| 165 | + </pre> |
| 166 | + </details> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + ) : ( |
| 171 | + <div className="p-4 text-center text-gray-500">Select an entry to view details</div> |
| 172 | + )} |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + ) |
| 177 | +} |
0 commit comments