Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

DevTools.tsx

235 lines10.6 KB

1import React, { useState } from "react"
2import { useCollections } from "@tanstack/react-optimistic"
3import { DiffView } from "./DiffView"
4import type { Transaction } from "@tanstack/react-optimistic"
5​
6export function DevTools() {
7 const collections = useCollections()
8 const [selectedCollection, setSelectedCollection] = useState<string | null>(
9 null
10 )
11 const [activeTab, setActiveTab] = useState<`state` | `transactions`>(`state`)
12 const [expandedTransactions, setExpandedTransactions] = useState<Set<string>>(
13 new Set()
14 )
15​
16 // Get the selected collection's data
17 const selectedData = selectedCollection
18 ? collections.get(selectedCollection)
19 : null
20​
21 const toggleTransaction = (id: string) => {
22 setExpandedTransactions((prev) => {
23 const next = new Set(prev)
24 if (next.has(id)) {
25 next.delete(id)
26 } else {
27 next.add(id)
28 }
29 return next
30 })
31 }
32​
33 return (
34 <div className="fixed bottom-0 left-0 right-0 h-1/2 bg-white border-t border-gray-200 flex">
35 {/* Collections List */}
36 <div className="w-64 border-r border-gray-200 overflow-auto">
37 <div className="p-4 border-b border-gray-200">
38 <h2 className="text-lg font-semibold">Collections</h2>
39 </div>
40 <div className="divide-y divide-gray-200">
41 {Array.from(collections).map(([id, { state }]) => (
42 <button
43 key={id}
44 onClick={() => setSelectedCollection(id)}
45 className={`w-full px-4 py-3 text-left hover:bg-gray-50 focus:outline-none ${
46 selectedCollection === id ? `bg-blue-50` : ``
47 }`}
48 >
49 <div className="font-medium">{id}</div>
50 <div className="text-sm text-gray-500">
51 {state.size} item{state.size !== 1 ? `s` : ``}
52 </div>
53 </button>
54 ))}
55 </div>
56 </div>
57​
58 {/* Collection Details */}
59 <div className="flex-1 overflow-hidden flex flex-col">
60 {selectedCollection ? (
61 <>
62 <div className="p-4 border-b border-gray-200">
63 <div className="flex items-center justify-between">
64 <h2 className="text-lg font-semibold">
65 Collection: {selectedCollection}
66 </h2>
67 <div className="flex space-x-1 bg-gray-100 p-1 rounded-lg">
68 <button
69 onClick={() => setActiveTab(`state`)}
70 className={`px-3 py-1 rounded ${
71 activeTab === `state`
72 ? `bg-white shadow text-gray-900`
73 : `text-gray-600 hover:bg-gray-200`
74 }`}
75 >
76 State
77 </button>
78 <button
79 onClick={() => setActiveTab(`transactions`)}
80 className={`px-3 py-1 rounded ${
81 activeTab === `transactions`
82 ? `bg-white shadow text-gray-900`
83 : `text-gray-600 hover:bg-gray-200`
84 }`}
85 >
86 Transactions
87 </button>
88 </div>
89 </div>
90 </div>
91 <div className="flex-1 overflow-auto">
92 {activeTab === `state` && selectedData ? (
93 <table className="min-w-full divide-y divide-gray-200">
94 <thead className="bg-gray-50">
95 <tr>
96 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
97 Key
98 </th>
99 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
100 Value
101 </th>
102 </tr>
103 </thead>
104 <tbody className="bg-white divide-y divide-gray-200">
105 {Array.from(selectedData.state).map(([key, value]) => (
106 <tr key={key}>
107 <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
108 {key}
109 </td>
110 <td className="px-6 py-4 whitespace-pre text-sm text-gray-500 font-mono">
111 {JSON.stringify(value, null, 2)}
112 </td>
113 </tr>
114 ))}
115 </tbody>
116 </table>
117 ) : activeTab === `transactions` && selectedData ? (
118 <table className="min-w-full divide-y divide-gray-200">
119 <thead className="bg-gray-50">
120 <tr>
121 <th className="w-8 px-6 py-3"></th>
122 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
123 Time
124 </th>
125 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
126 ID
127 </th>
128 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
129 State
130 </th>
131 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
132 Mutations
133 </th>
134 </tr>
135 </thead>
136 <tbody className="bg-white divide-y divide-gray-200">
137 {[...selectedData.transactions.values()]
138 .reverse()
139 .map((transaction: Transaction) => (
140 <React.Fragment key={transaction.id}>
141 <tr
142 className={`${
143 expandedTransactions.has(transaction.id)
144 ? `bg-gray-50`
145 : `hover:bg-gray-50`
146 } cursor-pointer`}
147 onClick={() => toggleTransaction(transaction.id)}
148 >
149 <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
150 <span className="transform inline-block transition-transform">
151 {expandedTransactions.has(transaction.id)
152 ? `▼`
153 : `▶`}
154 </span>
155 </td>
156 <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
157 {new Date(
158 transaction.createdAt
159 ).toLocaleTimeString()}
160 </td>
161 <td className="px-6 py-4 whitespace-nowrap text-sm font-mono text-gray-500">
162 {transaction.id.slice(0, 8)}...
163 </td>
164 <td className="px-6 py-4 whitespace-nowrap text-sm">
165 <span
166 className={`px-2 py-1 rounded-full text-xs font-medium ${
167 transaction.state ===
168 `persisted_awaiting_sync`
169 ? `bg-yellow-100 text-yellow-800`
170 : transaction.state === `synced`
171 ? `bg-green-100 text-green-800`
172 : `bg-gray-100 text-gray-800`
173 }`}
174 >
175 {transaction.state}
176 </span>
177 </td>
178 <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
179 {transaction.mutations.length} mutation
180 {transaction.mutations.length !== 1 ? `s` : ``}
181 </td>
182 </tr>
183 {expandedTransactions.has(transaction.id) && (
184 <tr className="bg-gray-50">
185 <td colSpan={5} className="px-6 py-4">
186 <div className="space-y-4">
187 {transaction.mutations.map(
188 (mutation, index) => (
189 <div
190 key={index}
191 className="border border-gray-200 rounded-lg bg-white p-4"
192 >
193 <div className="flex items-center justify-between mb-2">
194 <span
195 className={`px-2 py-1 rounded-full text-xs font-medium ${
196 mutation.type === `insert`
197 ? `bg-green-100 text-green-800`
198 : mutation.type === `update`
199 ? `bg-blue-100 text-blue-800`
200 : `bg-red-100 text-red-800`
201 }`}
202 >
203 {mutation.type}
204 </span>
205 <span className="text-sm font-mono text-gray-500">
206 key: {mutation.key}
207 </span>
208 </div>
209 <DiffView
210 oldValue={mutation.original}
211 newValue={mutation.modified}
212 />
213 </div>
214 )
215 )}
216 </div>
217 </td>
218 </tr>
219 )}
220 </React.Fragment>
221 ))}
222 </tbody>
223 </table>
224 ) : null}
225 </div>
226 </>
227 ) : (
228 <div className="flex items-center justify-center h-full text-gray-500">
229 Select a collection to view details
230 </div>
231 )}
232 </div>
233 </div>
234 )