swr-devtools-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +209 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +17 -0
- package/dist/production-BQanzhqs.js +1452 -0
- package/dist/production-C15b5Ipu.d.ts +30 -0
- package/dist/production.d.ts +3 -0
- package/dist/production.js +2 -0
- package/package.json +46 -3
|
@@ -0,0 +1,1452 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { Component, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { useSWRConfig } from "swr";
|
|
4
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { createPortal } from "react-dom";
|
|
6
|
+
// ../core/src/cache-actions.ts
|
|
7
|
+
/**
|
|
8
|
+
* Creates the panel's actions for one SWR cache. Pass `useSWRConfig()`.
|
|
9
|
+
*
|
|
10
|
+
* Raw cache keys work with `mutate` because SWR serialises a string key to
|
|
11
|
+
* itself, so `@"/api/posts",1,` targets the same entry.
|
|
12
|
+
*/
|
|
13
|
+
function createCacheActions({ cache, mutate, unload }) {
|
|
14
|
+
const remove = async (key) => {
|
|
15
|
+
if (cache.get(key) === void 0) return;
|
|
16
|
+
await mutate(key, void 0, { revalidate: false });
|
|
17
|
+
cache.delete(key);
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
async revalidate(key) {
|
|
21
|
+
await mutate(key);
|
|
22
|
+
},
|
|
23
|
+
remove,
|
|
24
|
+
async clearAll() {
|
|
25
|
+
if (typeof unload === "function") {
|
|
26
|
+
unload({ revalidate: false });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
await Promise.all([...cache.keys()].map(remove));
|
|
30
|
+
},
|
|
31
|
+
async setData(key, data) {
|
|
32
|
+
await mutate(key, data, { revalidate: false });
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
// ../core/src/format-key.ts
|
|
37
|
+
const PREFIXES = ["$inf$", "$sub$"];
|
|
38
|
+
/** Values `stableHash` writes without quotes that can be decoded for certain. */
|
|
39
|
+
const TOKENS = [
|
|
40
|
+
String.raw`-?\d+(\.\d+)?(e[+-]\d+)?`,
|
|
41
|
+
"NaN|-?Infinity",
|
|
42
|
+
"true|false|null|undefined",
|
|
43
|
+
String.raw`[+-]?\d{4,6}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z`
|
|
44
|
+
];
|
|
45
|
+
const TOKEN = new RegExp(`^(?:${TOKENS.join("|")})$`);
|
|
46
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
47
|
+
function formatName(name) {
|
|
48
|
+
return IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Turns an SWR cache key into something readable for the panel.
|
|
52
|
+
*
|
|
53
|
+
* SWR stores string keys as they are, and serialises array and object keys
|
|
54
|
+
* with its internal `stableHash`: `["/api/posts", 2]` becomes
|
|
55
|
+
* `@"/api/posts",2,` and `{ url, q }` becomes `#url:"/api/search",q:"ap",`.
|
|
56
|
+
* This reverses that format for display. Infinite and subscription prefixes
|
|
57
|
+
* are dropped (the panel shows the kind separately).
|
|
58
|
+
*
|
|
59
|
+
* The format isn't designed to be parsed, so the decoder only accepts values
|
|
60
|
+
* it can recognise for certain. A key containing anything else (a regex, a
|
|
61
|
+
* symbol, a Map or class instance) is shown raw rather than guessed at.
|
|
62
|
+
*
|
|
63
|
+
* Known limit: property names aren't escaped, so some different objects hash
|
|
64
|
+
* the same. `{ b: 2, a: 1 }` and `{ 'b:2,a': 1 }` both become `#b:2,a:1,`,
|
|
65
|
+
* and no decoder can tell them apart. The decoder shows the ordinary reading,
|
|
66
|
+
* `{ a: 1, b: 2 }`. Showing every object key raw instead would hurt all
|
|
67
|
+
* users to cover property names containing both "," and ":".
|
|
68
|
+
*/
|
|
69
|
+
function formatKey(key) {
|
|
70
|
+
const prefix = PREFIXES.find((p) => key.startsWith(p));
|
|
71
|
+
const body = prefix ? key.slice(prefix.length) : key;
|
|
72
|
+
if (!body.startsWith("@") && !body.startsWith("#")) return body;
|
|
73
|
+
const parser = new Parser(body);
|
|
74
|
+
const formatted = parser.value();
|
|
75
|
+
return formatted !== void 0 && parser.done() ? formatted : body;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Recursive-descent parser for `stableHash` output. Each method returns the
|
|
79
|
+
* formatted text, or `undefined` when the input doesn't match.
|
|
80
|
+
*
|
|
81
|
+
* Grammar (every item and property ends with a comma):
|
|
82
|
+
* value = array | object | string | token
|
|
83
|
+
* array = "@" (value ",")*
|
|
84
|
+
* object = "#" (name ":" value ",")*
|
|
85
|
+
* string = a JSON string
|
|
86
|
+
* token = a number, boolean, null, undefined or ISO date, up to the next comma
|
|
87
|
+
*
|
|
88
|
+
* Property names aren't escaped and may contain ":", so `#x:y:1,` is tried
|
|
89
|
+
* as name "x" (value "y:1", not a valid token) and then name "x:y" (value 1).
|
|
90
|
+
*/
|
|
91
|
+
var Parser = class {
|
|
92
|
+
input;
|
|
93
|
+
pos = 0;
|
|
94
|
+
constructor(input) {
|
|
95
|
+
this.input = input;
|
|
96
|
+
}
|
|
97
|
+
done() {
|
|
98
|
+
return this.pos === this.input.length;
|
|
99
|
+
}
|
|
100
|
+
value() {
|
|
101
|
+
const char = this.input[this.pos];
|
|
102
|
+
if (char === "@") return this.array();
|
|
103
|
+
if (char === "#") return this.object();
|
|
104
|
+
if (char === "\"") return this.string();
|
|
105
|
+
return this.token();
|
|
106
|
+
}
|
|
107
|
+
array() {
|
|
108
|
+
this.pos++;
|
|
109
|
+
const items = [];
|
|
110
|
+
while (!this.done() && this.input[this.pos] !== ",") {
|
|
111
|
+
const item = this.value();
|
|
112
|
+
if (item === void 0 || !this.consume(",")) return void 0;
|
|
113
|
+
items.push(item);
|
|
114
|
+
}
|
|
115
|
+
return `[${items.join(", ")}]`;
|
|
116
|
+
}
|
|
117
|
+
object() {
|
|
118
|
+
this.pos++;
|
|
119
|
+
const properties = [];
|
|
120
|
+
while (!this.done() && this.input[this.pos] !== ",") {
|
|
121
|
+
const property = this.property();
|
|
122
|
+
if (property === void 0) return void 0;
|
|
123
|
+
properties.push(property);
|
|
124
|
+
}
|
|
125
|
+
properties.reverse();
|
|
126
|
+
return properties.length ? `{ ${properties.join(", ")} }` : "{}";
|
|
127
|
+
}
|
|
128
|
+
/** One `name:value,`, trying each colon in turn as the separator. */
|
|
129
|
+
property() {
|
|
130
|
+
const start = this.pos;
|
|
131
|
+
let colon = this.input.indexOf(":", start);
|
|
132
|
+
while (colon !== -1) {
|
|
133
|
+
this.pos = colon + 1;
|
|
134
|
+
const value = this.value();
|
|
135
|
+
if (value !== void 0 && this.consume(",")) return `${formatName(this.input.slice(start, colon))}: ${value}`;
|
|
136
|
+
colon = this.input.indexOf(":", colon + 1);
|
|
137
|
+
}
|
|
138
|
+
this.pos = start;
|
|
139
|
+
}
|
|
140
|
+
string() {
|
|
141
|
+
let end = this.pos + 1;
|
|
142
|
+
while (end < this.input.length && this.input[end] !== "\"") end += this.input[end] === "\\" ? 2 : 1;
|
|
143
|
+
if (end >= this.input.length) return void 0;
|
|
144
|
+
end++;
|
|
145
|
+
const text = this.input.slice(this.pos, end);
|
|
146
|
+
this.pos = end;
|
|
147
|
+
return text;
|
|
148
|
+
}
|
|
149
|
+
token() {
|
|
150
|
+
const comma = this.input.indexOf(",", this.pos);
|
|
151
|
+
const end = comma === -1 ? this.input.length : comma;
|
|
152
|
+
const text = this.input.slice(this.pos, end);
|
|
153
|
+
if (!TOKEN.test(text)) return void 0;
|
|
154
|
+
this.pos = end;
|
|
155
|
+
return text;
|
|
156
|
+
}
|
|
157
|
+
consume(char) {
|
|
158
|
+
if (this.input[this.pos] !== char) return false;
|
|
159
|
+
this.pos++;
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
// ../core/src/key-kind.ts
|
|
164
|
+
function getKeyKind(key) {
|
|
165
|
+
if (key.startsWith("$inf$")) return "infinite";
|
|
166
|
+
if (key.startsWith("$sub$")) return "subscription";
|
|
167
|
+
return "standard";
|
|
168
|
+
}
|
|
169
|
+
// ../core/src/status.ts
|
|
170
|
+
function getStatus(state) {
|
|
171
|
+
if (state.isLoading) return "loading";
|
|
172
|
+
if (state.isValidating) return "validating";
|
|
173
|
+
if (state.error !== void 0) return "error";
|
|
174
|
+
if (state.data !== void 0) return "success";
|
|
175
|
+
return "idle";
|
|
176
|
+
}
|
|
177
|
+
// ../core/src/cache-reader.ts
|
|
178
|
+
/**
|
|
179
|
+
* Creates a reader that turns an SWR cache into a list of entries.
|
|
180
|
+
*
|
|
181
|
+
* SWR doesn't record when data was last fetched, so the reader remembers
|
|
182
|
+
* the `data` object and `isValidating` flag it last saw per key. The time
|
|
183
|
+
* moves on when:
|
|
184
|
+
* - a request finishes without an error (`isValidating` true → false). SWR
|
|
185
|
+
* keeps the same `data` object when a refetch returns equal data, so this
|
|
186
|
+
* is the only way to see that refetch.
|
|
187
|
+
* - the data object changes, e.g. through `mutate(key, data)`.
|
|
188
|
+
* A failed refetch keeps the old time, so old data never looks fresh. Call
|
|
189
|
+
* `read` whenever the cache changes (see the cache-observation spike) to
|
|
190
|
+
* keep timestamps accurate.
|
|
191
|
+
*/
|
|
192
|
+
function createCacheReader() {
|
|
193
|
+
const seen = /* @__PURE__ */ new Map();
|
|
194
|
+
return { read(cache, now = Date.now()) {
|
|
195
|
+
const entries = [];
|
|
196
|
+
const present = /* @__PURE__ */ new Set();
|
|
197
|
+
for (const key of cache.keys()) {
|
|
198
|
+
const state = cache.get(key);
|
|
199
|
+
if (!state) continue;
|
|
200
|
+
present.add(key);
|
|
201
|
+
const isValidating = state.isValidating ?? false;
|
|
202
|
+
const previous = seen.get(key);
|
|
203
|
+
const fetched = previous?.isValidating && !isValidating && state.error === void 0;
|
|
204
|
+
const updatedAt = !previous || previous.data !== state.data || fetched ? now : previous.updatedAt;
|
|
205
|
+
seen.set(key, {
|
|
206
|
+
data: state.data,
|
|
207
|
+
isValidating,
|
|
208
|
+
updatedAt
|
|
209
|
+
});
|
|
210
|
+
entries.push({
|
|
211
|
+
key,
|
|
212
|
+
label: formatKey(key),
|
|
213
|
+
kind: getKeyKind(key),
|
|
214
|
+
status: getStatus(state),
|
|
215
|
+
data: state.data,
|
|
216
|
+
error: state.error,
|
|
217
|
+
isLoading: state.isLoading ?? false,
|
|
218
|
+
isValidating,
|
|
219
|
+
updatedAt
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
for (const key of seen.keys()) if (!present.has(key)) seen.delete(key);
|
|
223
|
+
return entries;
|
|
224
|
+
} };
|
|
225
|
+
}
|
|
226
|
+
// ../core/src/observe-cache.ts
|
|
227
|
+
const DEFAULT_POLL_INTERVAL = 250;
|
|
228
|
+
const patches = /* @__PURE__ */ new WeakMap();
|
|
229
|
+
/**
|
|
230
|
+
* Calls `listener` after the SWR cache changes: a key added, changed or
|
|
231
|
+
* removed. Notifications are batched, so many writes in one task produce one
|
|
232
|
+
* call.
|
|
233
|
+
*
|
|
234
|
+
* Wraps `set` and `delete` on the cache object SWR already uses, which needs
|
|
235
|
+
* no setup from the app (see docs/spikes/cache-observation.md). Falls back to
|
|
236
|
+
* polling when the cache can't be patched.
|
|
237
|
+
*/
|
|
238
|
+
function observeCache(cache, listener, options = {}) {
|
|
239
|
+
const patch = patches.get(cache) ?? installPatch(cache);
|
|
240
|
+
if (!patch) return pollCache(cache, listener, options.pollInterval ?? DEFAULT_POLL_INTERVAL);
|
|
241
|
+
const subscription = () => listener();
|
|
242
|
+
patch.listeners.add(subscription);
|
|
243
|
+
let active = true;
|
|
244
|
+
return {
|
|
245
|
+
mode: "patch",
|
|
246
|
+
unsubscribe() {
|
|
247
|
+
if (!active) return;
|
|
248
|
+
active = false;
|
|
249
|
+
patch.listeners.delete(subscription);
|
|
250
|
+
if (patch.listeners.size === 0) {
|
|
251
|
+
patch.restore();
|
|
252
|
+
patches.delete(cache);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function installPatch(cache) {
|
|
258
|
+
const ownSet = Object.getOwnPropertyDescriptor(cache, "set");
|
|
259
|
+
const ownDelete = Object.getOwnPropertyDescriptor(cache, "delete");
|
|
260
|
+
const originalSet = cache.set.bind(cache);
|
|
261
|
+
const originalDelete = cache.delete.bind(cache);
|
|
262
|
+
const patch = {
|
|
263
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
264
|
+
scheduled: false,
|
|
265
|
+
restore() {
|
|
266
|
+
restoreMethod(cache, "set", ownSet);
|
|
267
|
+
restoreMethod(cache, "delete", ownDelete);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
const notify = () => {
|
|
271
|
+
if (patch.scheduled) return;
|
|
272
|
+
patch.scheduled = true;
|
|
273
|
+
queueMicrotask(() => {
|
|
274
|
+
patch.scheduled = false;
|
|
275
|
+
for (const listener of patch.listeners) listener();
|
|
276
|
+
});
|
|
277
|
+
};
|
|
278
|
+
try {
|
|
279
|
+
cache.set = (key, value) => {
|
|
280
|
+
const result = originalSet(key, value);
|
|
281
|
+
notify();
|
|
282
|
+
return result;
|
|
283
|
+
};
|
|
284
|
+
cache.delete = (key) => {
|
|
285
|
+
const result = originalDelete(key);
|
|
286
|
+
notify();
|
|
287
|
+
return result;
|
|
288
|
+
};
|
|
289
|
+
} catch {
|
|
290
|
+
patch.restore();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
patches.set(cache, patch);
|
|
294
|
+
return patch;
|
|
295
|
+
}
|
|
296
|
+
function restoreMethod(cache, name, original) {
|
|
297
|
+
try {
|
|
298
|
+
if (original) Object.defineProperty(cache, name, original);
|
|
299
|
+
else delete cache[name];
|
|
300
|
+
} catch {}
|
|
301
|
+
}
|
|
302
|
+
function pollCache(cache, listener, interval) {
|
|
303
|
+
let previous = snapshot(cache);
|
|
304
|
+
const timer = setInterval(() => {
|
|
305
|
+
const next = snapshot(cache);
|
|
306
|
+
if (changed(previous, next)) {
|
|
307
|
+
previous = next;
|
|
308
|
+
listener();
|
|
309
|
+
}
|
|
310
|
+
}, interval);
|
|
311
|
+
return {
|
|
312
|
+
mode: "poll",
|
|
313
|
+
unsubscribe() {
|
|
314
|
+
clearInterval(timer);
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function snapshot(cache) {
|
|
319
|
+
const entries = /* @__PURE__ */ new Map();
|
|
320
|
+
for (const key of cache.keys()) entries.set(key, cache.get(key));
|
|
321
|
+
return entries;
|
|
322
|
+
}
|
|
323
|
+
function changed(a, b) {
|
|
324
|
+
if (a.size !== b.size) return true;
|
|
325
|
+
for (const [key, value] of b) if (a.get(key) !== value) return true;
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
// ../core/src/cache-store.ts
|
|
329
|
+
/**
|
|
330
|
+
* Creating a store only reads the cache. The patch is installed in
|
|
331
|
+
* `subscribe`, which React calls after committing, so a render that React
|
|
332
|
+
* discards can't leave the cache patched.
|
|
333
|
+
*/
|
|
334
|
+
function createCacheStore(cache) {
|
|
335
|
+
const reader = createCacheReader();
|
|
336
|
+
let entries = reader.read(cache);
|
|
337
|
+
let mode = "patch";
|
|
338
|
+
return {
|
|
339
|
+
subscribe: (onChange) => {
|
|
340
|
+
const observer = observeCache(cache, () => {
|
|
341
|
+
entries = reader.read(cache);
|
|
342
|
+
onChange();
|
|
343
|
+
});
|
|
344
|
+
mode = observer.mode;
|
|
345
|
+
entries = reader.read(cache);
|
|
346
|
+
return observer.unsubscribe;
|
|
347
|
+
},
|
|
348
|
+
getSnapshot: () => entries,
|
|
349
|
+
getMode: () => mode
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
// ../core/src/sort-entries.ts
|
|
353
|
+
/** Problems first, then requests in flight, then settled keys. */
|
|
354
|
+
const STATUS_ORDER = {
|
|
355
|
+
error: 0,
|
|
356
|
+
loading: 1,
|
|
357
|
+
validating: 2,
|
|
358
|
+
success: 3,
|
|
359
|
+
idle: 4
|
|
360
|
+
};
|
|
361
|
+
const collator = new Intl.Collator(void 0, {
|
|
362
|
+
numeric: true,
|
|
363
|
+
sensitivity: "base"
|
|
364
|
+
});
|
|
365
|
+
const byLabel = (a, b) => collator.compare(a.label, b.label) || (a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
366
|
+
const comparators = {
|
|
367
|
+
key: byLabel,
|
|
368
|
+
status: (a, b) => STATUS_ORDER[a.status] - STATUS_ORDER[b.status] || byLabel(a, b),
|
|
369
|
+
updated: (a, b) => b.updatedAt - a.updatedAt || byLabel(a, b)
|
|
370
|
+
};
|
|
371
|
+
/**
|
|
372
|
+
* Returns the entries in a new array, sorted by key (A to Z, numbers in
|
|
373
|
+
* order), status (errors first) or last updated (newest first). Ties fall
|
|
374
|
+
* back to the key, so the order is stable as the cache changes.
|
|
375
|
+
*/
|
|
376
|
+
function sortEntries(entries, by) {
|
|
377
|
+
return entries.toSorted(comparators[by]);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Keeps the entries whose label contains the query, ignoring case and
|
|
381
|
+
* surrounding spaces. An empty query keeps every entry.
|
|
382
|
+
*/
|
|
383
|
+
function filterEntries(entries, query) {
|
|
384
|
+
const needle = query.trim().toLowerCase();
|
|
385
|
+
if (needle === "") return [...entries];
|
|
386
|
+
return entries.filter((entry) => entry.label.toLowerCase().includes(needle));
|
|
387
|
+
}
|
|
388
|
+
// ../core/src/inspect-value.ts
|
|
389
|
+
/**
|
|
390
|
+
* Describes a value for one line of a JSON tree, without walking into it.
|
|
391
|
+
* Containers report their size; read their children with `getChildren`, a
|
|
392
|
+
* slice at a time, so a large payload is only read as far as it's opened.
|
|
393
|
+
*
|
|
394
|
+
* SWR data isn't always JSON: dates, maps, sets, errors and class instances
|
|
395
|
+
* are shown for what they are, not as `{}`.
|
|
396
|
+
*/
|
|
397
|
+
function inspectValue(value) {
|
|
398
|
+
switch (typeof value) {
|
|
399
|
+
case "string": return {
|
|
400
|
+
kind: "string",
|
|
401
|
+
preview: JSON.stringify(value)
|
|
402
|
+
};
|
|
403
|
+
case "number": return {
|
|
404
|
+
kind: "number",
|
|
405
|
+
preview: String(value)
|
|
406
|
+
};
|
|
407
|
+
case "bigint": return {
|
|
408
|
+
kind: "bigint",
|
|
409
|
+
preview: `${value}n`
|
|
410
|
+
};
|
|
411
|
+
case "boolean": return {
|
|
412
|
+
kind: "boolean",
|
|
413
|
+
preview: String(value)
|
|
414
|
+
};
|
|
415
|
+
case "undefined": return {
|
|
416
|
+
kind: "undefined",
|
|
417
|
+
preview: "undefined"
|
|
418
|
+
};
|
|
419
|
+
case "symbol": return {
|
|
420
|
+
kind: "symbol",
|
|
421
|
+
preview: value.toString()
|
|
422
|
+
};
|
|
423
|
+
case "function": return {
|
|
424
|
+
kind: "function",
|
|
425
|
+
preview: `ƒ ${value.name || "anonymous"}()`
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
if (typeof value !== "object" || value === null) return {
|
|
429
|
+
kind: "null",
|
|
430
|
+
preview: "null"
|
|
431
|
+
};
|
|
432
|
+
if (value instanceof Thrown) return {
|
|
433
|
+
kind: "thrown",
|
|
434
|
+
preview: `[Threw: ${describeError(value.error)}]`
|
|
435
|
+
};
|
|
436
|
+
if (value instanceof Date) return {
|
|
437
|
+
kind: "date",
|
|
438
|
+
preview: Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString()
|
|
439
|
+
};
|
|
440
|
+
if (value instanceof Error) return {
|
|
441
|
+
kind: "error",
|
|
442
|
+
preview: describeError(value),
|
|
443
|
+
size: errorFieldNames(value).length
|
|
444
|
+
};
|
|
445
|
+
if (Array.isArray(value)) return {
|
|
446
|
+
kind: "array",
|
|
447
|
+
preview: `Array(${value.length})`,
|
|
448
|
+
size: value.length
|
|
449
|
+
};
|
|
450
|
+
if (value instanceof Map) return {
|
|
451
|
+
kind: "map",
|
|
452
|
+
preview: `Map(${value.size})`,
|
|
453
|
+
size: value.size
|
|
454
|
+
};
|
|
455
|
+
if (value instanceof Set) return {
|
|
456
|
+
kind: "set",
|
|
457
|
+
preview: `Set(${value.size})`,
|
|
458
|
+
size: value.size
|
|
459
|
+
};
|
|
460
|
+
const size = Object.keys(value).length;
|
|
461
|
+
return {
|
|
462
|
+
kind: "object",
|
|
463
|
+
preview: `${constructorName(value)}(${size})`,
|
|
464
|
+
size
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Reads the children of a container from `start` up to, not including,
|
|
469
|
+
* `end`. Leaves have no children.
|
|
470
|
+
*
|
|
471
|
+
* Only the requested slice is read: getters outside it don't run, and Maps
|
|
472
|
+
* and Sets are iterated no further than `end`. A getter that throws gives a
|
|
473
|
+
* `thrown` child rather than an exception, so one bad property can't break
|
|
474
|
+
* the panel.
|
|
475
|
+
*/
|
|
476
|
+
function getChildren(value, start = 0, end = Infinity) {
|
|
477
|
+
if (typeof value !== "object" || value === null || value instanceof Date) return [];
|
|
478
|
+
if (Array.isArray(value)) {
|
|
479
|
+
const children = [];
|
|
480
|
+
for (let index = start; index < Math.min(end, value.length); index += 1) children.push({
|
|
481
|
+
name: String(index),
|
|
482
|
+
value: readProperty(value, String(index))
|
|
483
|
+
});
|
|
484
|
+
return children;
|
|
485
|
+
}
|
|
486
|
+
if (value instanceof Map) return takeRange(value, start, end).map(([key, item]) => ({
|
|
487
|
+
name: inspectValue(key).preview,
|
|
488
|
+
value: item
|
|
489
|
+
}));
|
|
490
|
+
if (value instanceof Set) return takeRange(value, start, end).map((item, i) => ({
|
|
491
|
+
name: String(start + i),
|
|
492
|
+
value: item
|
|
493
|
+
}));
|
|
494
|
+
return (value instanceof Error ? errorFieldNames(value) : Object.keys(value)).slice(start, end).map((name) => ({
|
|
495
|
+
name,
|
|
496
|
+
value: readProperty(value, name)
|
|
497
|
+
}));
|
|
498
|
+
}
|
|
499
|
+
/** Stands in for a property whose getter threw. */
|
|
500
|
+
var Thrown = class {
|
|
501
|
+
error;
|
|
502
|
+
constructor(error) {
|
|
503
|
+
this.error = error;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
function readProperty(target, name) {
|
|
507
|
+
try {
|
|
508
|
+
return Reflect.get(target, name);
|
|
509
|
+
} catch (error) {
|
|
510
|
+
return new Thrown(error);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* An error as `Name: message`. Both are normally plain values, but either
|
|
515
|
+
* can be a getter that throws; that gives a placeholder, not an exception.
|
|
516
|
+
*/
|
|
517
|
+
function describeError(error) {
|
|
518
|
+
try {
|
|
519
|
+
return error instanceof Error ? `${error.name}: ${error.message}` : String(error);
|
|
520
|
+
} catch {
|
|
521
|
+
return "[unreadable error]";
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
/** The items of an iterable from `start` to `end`, without iterating past `end`. */
|
|
525
|
+
function takeRange(items, start, end) {
|
|
526
|
+
const taken = [];
|
|
527
|
+
if (start >= end) return taken;
|
|
528
|
+
let index = 0;
|
|
529
|
+
for (const item of items) {
|
|
530
|
+
if (index >= start) taken.push(item);
|
|
531
|
+
index += 1;
|
|
532
|
+
if (index >= end) break;
|
|
533
|
+
}
|
|
534
|
+
return taken;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* The names of an error's message, then its own fields: a fetcher often adds the HTTP
|
|
538
|
+
* status or response body (SWR's docs use `error.info` and `error.status`).
|
|
539
|
+
* The stack is left out; it points into the fetcher, not the data.
|
|
540
|
+
*/
|
|
541
|
+
function errorFieldNames(error) {
|
|
542
|
+
const own = Object.keys(error);
|
|
543
|
+
const names = ["message", ...own.filter((name) => name !== "message")];
|
|
544
|
+
if (Object.hasOwn(error, "cause") && !own.includes("cause")) names.push("cause");
|
|
545
|
+
return names;
|
|
546
|
+
}
|
|
547
|
+
function constructorName(value) {
|
|
548
|
+
const proto = Reflect.getPrototypeOf(value);
|
|
549
|
+
if (proto === null || proto === Object.prototype) return "Object";
|
|
550
|
+
const name = Reflect.get(proto, "constructor")?.name;
|
|
551
|
+
return typeof name === "string" && name !== "" ? name : "Object";
|
|
552
|
+
}
|
|
553
|
+
// ../core/src/edit-data.ts
|
|
554
|
+
const NOT_JSON = {
|
|
555
|
+
editable: false,
|
|
556
|
+
reason: "This data can’t be written as JSON, so it can’t be edited."
|
|
557
|
+
};
|
|
558
|
+
/** Formats a key's data for the JSON editor. */
|
|
559
|
+
function toEditableData(data) {
|
|
560
|
+
if (data === void 0) return {
|
|
561
|
+
editable: true,
|
|
562
|
+
text: "",
|
|
563
|
+
exact: true
|
|
564
|
+
};
|
|
565
|
+
let text;
|
|
566
|
+
try {
|
|
567
|
+
text = JSON.stringify(data, null, 2);
|
|
568
|
+
} catch {
|
|
569
|
+
return NOT_JSON;
|
|
570
|
+
}
|
|
571
|
+
if (text === void 0) return NOT_JSON;
|
|
572
|
+
let exact;
|
|
573
|
+
try {
|
|
574
|
+
exact = isExactJson(data);
|
|
575
|
+
} catch {
|
|
576
|
+
exact = false;
|
|
577
|
+
}
|
|
578
|
+
return {
|
|
579
|
+
editable: true,
|
|
580
|
+
text,
|
|
581
|
+
exact
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
/** Reads the edited text. Invalid JSON gives a message to show the user. */
|
|
585
|
+
function parseEditedData(text) {
|
|
586
|
+
if (text.trim() === "") return {
|
|
587
|
+
ok: false,
|
|
588
|
+
message: "Enter a JSON value, such as null."
|
|
589
|
+
};
|
|
590
|
+
try {
|
|
591
|
+
return {
|
|
592
|
+
ok: true,
|
|
593
|
+
data: JSON.parse(text)
|
|
594
|
+
};
|
|
595
|
+
} catch (error) {
|
|
596
|
+
return {
|
|
597
|
+
ok: false,
|
|
598
|
+
message: `Invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** Whether `JSON.parse(JSON.stringify(value))` gives back an equal value. */
|
|
603
|
+
function isExactJson(value) {
|
|
604
|
+
switch (typeof value) {
|
|
605
|
+
case "string":
|
|
606
|
+
case "boolean": return true;
|
|
607
|
+
case "number": return Number.isFinite(value);
|
|
608
|
+
case "object": {
|
|
609
|
+
if (value === null) return true;
|
|
610
|
+
if (Array.isArray(value)) return value.every((item) => isExactJson(item));
|
|
611
|
+
const proto = Object.getPrototypeOf(value);
|
|
612
|
+
if (proto !== Object.prototype && proto !== null) return false;
|
|
613
|
+
return Object.values(value).every((item) => isExactJson(item));
|
|
614
|
+
}
|
|
615
|
+
default: return false;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
// src/json-tree.tsx
|
|
619
|
+
/** Containers this shallow start open: the root only. */
|
|
620
|
+
const OPEN_DEPTH = 1;
|
|
621
|
+
/**
|
|
622
|
+
* The most children shown at one level. Bigger containers are split into
|
|
623
|
+
* ranges, `[0 … 99]` and so on, nested as deep as needed, so expanding a
|
|
624
|
+
* 100,000-item array renders a few rows rather than all of them.
|
|
625
|
+
*/
|
|
626
|
+
const PAGE_SIZE = 100;
|
|
627
|
+
/**
|
|
628
|
+
* A value as a collapsible tree. Only the root starts open, and a closed
|
|
629
|
+
* node's children aren't read or rendered, so a large payload costs only as
|
|
630
|
+
* much as is opened.
|
|
631
|
+
*/
|
|
632
|
+
function JsonTree({ value, label }) {
|
|
633
|
+
return /* @__PURE__ */ jsx(TreeBoundary, {
|
|
634
|
+
value,
|
|
635
|
+
children: /* @__PURE__ */ jsx("ul", {
|
|
636
|
+
className: "json-tree",
|
|
637
|
+
"aria-label": label,
|
|
638
|
+
children: /* @__PURE__ */ jsx(JsonNode, {
|
|
639
|
+
value,
|
|
640
|
+
depth: 0,
|
|
641
|
+
ancestors: []
|
|
642
|
+
})
|
|
643
|
+
})
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
var TreeBoundary = class extends Component {
|
|
647
|
+
state = {
|
|
648
|
+
failure: null,
|
|
649
|
+
value: this.props.value
|
|
650
|
+
};
|
|
651
|
+
static getDerivedStateFromError(error) {
|
|
652
|
+
let failure = "unknown error";
|
|
653
|
+
try {
|
|
654
|
+
failure = String(error instanceof Error ? error.message : error);
|
|
655
|
+
} catch {}
|
|
656
|
+
return { failure };
|
|
657
|
+
}
|
|
658
|
+
static getDerivedStateFromProps(props, state) {
|
|
659
|
+
return props.value === state.value ? null : {
|
|
660
|
+
failure: null,
|
|
661
|
+
value: props.value
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
render() {
|
|
665
|
+
if (this.state.failure === null) return this.props.children;
|
|
666
|
+
return /* @__PURE__ */ jsxs("p", {
|
|
667
|
+
className: "json-unreadable",
|
|
668
|
+
children: ["Couldn't show this value: ", this.state.failure]
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
function JsonNode({ name, value, depth, ancestors }) {
|
|
673
|
+
const [open, setOpen] = useState(depth < OPEN_DEPTH);
|
|
674
|
+
const { kind, preview, size } = inspectValue(value);
|
|
675
|
+
const label = name !== void 0 && /* @__PURE__ */ jsx("span", {
|
|
676
|
+
className: "json-name",
|
|
677
|
+
children: name
|
|
678
|
+
});
|
|
679
|
+
if (ancestors.includes(value)) return /* @__PURE__ */ jsxs("li", {
|
|
680
|
+
className: "json-row",
|
|
681
|
+
children: [label, /* @__PURE__ */ jsx("span", {
|
|
682
|
+
className: "json-value",
|
|
683
|
+
"data-kind": "circular",
|
|
684
|
+
children: "[Circular]"
|
|
685
|
+
})]
|
|
686
|
+
});
|
|
687
|
+
if (size === void 0) return /* @__PURE__ */ jsxs("li", {
|
|
688
|
+
className: "json-row",
|
|
689
|
+
children: [label, /* @__PURE__ */ jsx("span", {
|
|
690
|
+
className: "json-value",
|
|
691
|
+
"data-kind": kind,
|
|
692
|
+
children: preview
|
|
693
|
+
})]
|
|
694
|
+
});
|
|
695
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
696
|
+
className: "json-row",
|
|
697
|
+
children: [/* @__PURE__ */ jsxs("button", {
|
|
698
|
+
type: "button",
|
|
699
|
+
className: "json-toggle",
|
|
700
|
+
"aria-expanded": open,
|
|
701
|
+
disabled: size === 0,
|
|
702
|
+
onClick: () => setOpen(!open),
|
|
703
|
+
children: [label, /* @__PURE__ */ jsx("span", {
|
|
704
|
+
className: "json-value",
|
|
705
|
+
"data-kind": kind,
|
|
706
|
+
children: preview
|
|
707
|
+
})]
|
|
708
|
+
}), open && size > 0 && /* @__PURE__ */ jsx("ul", {
|
|
709
|
+
className: "json-children",
|
|
710
|
+
children: /* @__PURE__ */ jsx(JsonRange, {
|
|
711
|
+
value,
|
|
712
|
+
start: 0,
|
|
713
|
+
end: size,
|
|
714
|
+
depth: depth + 1,
|
|
715
|
+
ancestors: [...ancestors, value]
|
|
716
|
+
})
|
|
717
|
+
})]
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
/** The children of `value` from `start` to `end`, in ranges if there are too many. */
|
|
721
|
+
function JsonRange({ value, start, end, depth, ancestors }) {
|
|
722
|
+
const count = end - start;
|
|
723
|
+
if (count <= PAGE_SIZE) {
|
|
724
|
+
const { kind } = inspectValue(value);
|
|
725
|
+
const byName = kind === "object" || kind === "error";
|
|
726
|
+
return /* @__PURE__ */ jsx(Fragment, { children: getChildren(value, start, end).map((child, i) => /* @__PURE__ */ jsx(JsonNode, {
|
|
727
|
+
name: child.name,
|
|
728
|
+
value: child.value,
|
|
729
|
+
depth,
|
|
730
|
+
ancestors
|
|
731
|
+
}, byName ? child.name : start + i)) });
|
|
732
|
+
}
|
|
733
|
+
let step = PAGE_SIZE;
|
|
734
|
+
while (count / step > PAGE_SIZE) step *= PAGE_SIZE;
|
|
735
|
+
const ranges = [];
|
|
736
|
+
for (let from = start; from < end; from += step) ranges.push(from);
|
|
737
|
+
return /* @__PURE__ */ jsx(Fragment, { children: ranges.map((from) => /* @__PURE__ */ jsx(JsonRangeNode, {
|
|
738
|
+
value,
|
|
739
|
+
start: from,
|
|
740
|
+
end: Math.min(from + step, end),
|
|
741
|
+
depth,
|
|
742
|
+
ancestors
|
|
743
|
+
}, from)) });
|
|
744
|
+
}
|
|
745
|
+
function JsonRangeNode(props) {
|
|
746
|
+
const [open, setOpen] = useState(false);
|
|
747
|
+
return /* @__PURE__ */ jsxs("li", {
|
|
748
|
+
className: "json-row",
|
|
749
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
750
|
+
type: "button",
|
|
751
|
+
className: "json-toggle",
|
|
752
|
+
"aria-expanded": open,
|
|
753
|
+
onClick: () => setOpen(!open),
|
|
754
|
+
children: /* @__PURE__ */ jsxs("span", {
|
|
755
|
+
className: "json-range",
|
|
756
|
+
children: [
|
|
757
|
+
"[",
|
|
758
|
+
props.start,
|
|
759
|
+
" … ",
|
|
760
|
+
props.end - 1,
|
|
761
|
+
"]"
|
|
762
|
+
]
|
|
763
|
+
})
|
|
764
|
+
}), open && /* @__PURE__ */ jsx("ul", {
|
|
765
|
+
className: "json-children",
|
|
766
|
+
children: /* @__PURE__ */ jsx(JsonRange, { ...props })
|
|
767
|
+
})]
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
// src/key-list.tsx
|
|
771
|
+
const STATUS_NAMES = {
|
|
772
|
+
loading: "Loading",
|
|
773
|
+
validating: "Validating",
|
|
774
|
+
success: "Success",
|
|
775
|
+
error: "Error",
|
|
776
|
+
idle: "Idle"
|
|
777
|
+
};
|
|
778
|
+
/** Standard keys get no label; they're most of the list. */
|
|
779
|
+
const KIND_NAMES = {
|
|
780
|
+
infinite: "Infinite",
|
|
781
|
+
subscription: "Subscription"
|
|
782
|
+
};
|
|
783
|
+
/** A key's status, by name as well as colour, so it doesn't rely on colour. */
|
|
784
|
+
function StatusBadge({ status }) {
|
|
785
|
+
return /* @__PURE__ */ jsx("span", {
|
|
786
|
+
className: "badge",
|
|
787
|
+
"data-status": status,
|
|
788
|
+
children: STATUS_NAMES[status]
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
/** The given keys, each with its status and how long ago it was updated. */
|
|
792
|
+
function KeyList({ entries, selectedKey, onSelect }) {
|
|
793
|
+
const now = useNow(1e3);
|
|
794
|
+
return /* @__PURE__ */ jsx("ul", {
|
|
795
|
+
className: "key-list",
|
|
796
|
+
"aria-label": "Cache keys",
|
|
797
|
+
children: entries.map((entry) => /* @__PURE__ */ jsxs("li", {
|
|
798
|
+
className: "key-row",
|
|
799
|
+
"data-selected": entry.key === selectedKey ? "" : void 0,
|
|
800
|
+
children: [
|
|
801
|
+
/* @__PURE__ */ jsx(StatusBadge, { status: entry.status }),
|
|
802
|
+
/* @__PURE__ */ jsx("button", {
|
|
803
|
+
type: "button",
|
|
804
|
+
className: "key",
|
|
805
|
+
title: entry.label,
|
|
806
|
+
"aria-pressed": entry.key === selectedKey,
|
|
807
|
+
onClick: () => onSelect(entry.key === selectedKey ? null : entry.key),
|
|
808
|
+
children: entry.label
|
|
809
|
+
}),
|
|
810
|
+
entry.kind !== "standard" && /* @__PURE__ */ jsx("span", {
|
|
811
|
+
className: "kind",
|
|
812
|
+
children: KIND_NAMES[entry.kind]
|
|
813
|
+
}),
|
|
814
|
+
/* @__PURE__ */ jsx("time", {
|
|
815
|
+
className: "age",
|
|
816
|
+
dateTime: new Date(entry.updatedAt).toISOString(),
|
|
817
|
+
title: new Date(entry.updatedAt).toLocaleString(),
|
|
818
|
+
children: formatAge(now - entry.updatedAt)
|
|
819
|
+
})
|
|
820
|
+
]
|
|
821
|
+
}, entry.key))
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
function useNow(interval) {
|
|
825
|
+
const [now, setNow] = useState(() => Date.now());
|
|
826
|
+
useEffect(() => {
|
|
827
|
+
const timer = setInterval(() => setNow(Date.now()), interval);
|
|
828
|
+
return () => clearInterval(timer);
|
|
829
|
+
}, [interval]);
|
|
830
|
+
return now;
|
|
831
|
+
}
|
|
832
|
+
function formatAge(ms) {
|
|
833
|
+
const seconds = Math.max(0, Math.floor(ms / 1e3));
|
|
834
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
835
|
+
const minutes = Math.floor(seconds / 60);
|
|
836
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
837
|
+
return `${Math.floor(minutes / 60)}h ago`;
|
|
838
|
+
}
|
|
839
|
+
// src/key-detail.tsx
|
|
840
|
+
/**
|
|
841
|
+
* Why Revalidate can't refetch a key, if it can't. `mutate(key)` refetches
|
|
842
|
+
* through the key's mounted hook, and a subscription's hook has no fetcher.
|
|
843
|
+
*/
|
|
844
|
+
const REVALIDATE_UNAVAILABLE = { subscription: "Subscription keys have no fetcher, so they can’t be revalidated." };
|
|
845
|
+
/** One key in full: its key, status, error (if any) and data, with actions. */
|
|
846
|
+
function KeyDetail({ entry, actions, onClose }) {
|
|
847
|
+
const editable = useMemo(() => toEditableData(entry.data), [entry.data]);
|
|
848
|
+
const [editing, setEditing] = useState(null);
|
|
849
|
+
const editRef = useRef(null);
|
|
850
|
+
const hasEdited = useRef(false);
|
|
851
|
+
useEffect(() => {
|
|
852
|
+
if (hasEdited.current && editing === null) editRef.current?.focus();
|
|
853
|
+
}, [editing]);
|
|
854
|
+
const stopEditing = () => {
|
|
855
|
+
hasEdited.current = true;
|
|
856
|
+
setEditing(null);
|
|
857
|
+
};
|
|
858
|
+
return /* @__PURE__ */ jsxs("section", {
|
|
859
|
+
className: "detail",
|
|
860
|
+
"aria-label": "Key details",
|
|
861
|
+
children: [
|
|
862
|
+
/* @__PURE__ */ jsxs("header", {
|
|
863
|
+
className: "detail-header",
|
|
864
|
+
children: [
|
|
865
|
+
/* @__PURE__ */ jsx(StatusBadge, { status: entry.status }),
|
|
866
|
+
/* @__PURE__ */ jsx("h2", {
|
|
867
|
+
className: "detail-key",
|
|
868
|
+
children: entry.label
|
|
869
|
+
}),
|
|
870
|
+
/* @__PURE__ */ jsx("button", {
|
|
871
|
+
type: "button",
|
|
872
|
+
"aria-label": "Close key details",
|
|
873
|
+
onClick: onClose,
|
|
874
|
+
children: "✕"
|
|
875
|
+
})
|
|
876
|
+
]
|
|
877
|
+
}),
|
|
878
|
+
/* @__PURE__ */ jsxs("dl", {
|
|
879
|
+
className: "detail-meta",
|
|
880
|
+
children: [
|
|
881
|
+
entry.kind !== "standard" && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", { children: "Type" }), /* @__PURE__ */ jsx("dd", { children: KIND_NAMES[entry.kind] })] }),
|
|
882
|
+
entry.key !== entry.label && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", { children: "Cache key" }), /* @__PURE__ */ jsx("dd", {
|
|
883
|
+
className: "detail-raw-key",
|
|
884
|
+
children: entry.key
|
|
885
|
+
})] }),
|
|
886
|
+
/* @__PURE__ */ jsx("dt", { children: "Updated" }),
|
|
887
|
+
/* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx("time", {
|
|
888
|
+
dateTime: new Date(entry.updatedAt).toISOString(),
|
|
889
|
+
children: new Date(entry.updatedAt).toLocaleString()
|
|
890
|
+
}) })
|
|
891
|
+
]
|
|
892
|
+
}),
|
|
893
|
+
/* @__PURE__ */ jsxs("div", {
|
|
894
|
+
className: "detail-actions",
|
|
895
|
+
children: [
|
|
896
|
+
/* @__PURE__ */ jsx(ActionButton, {
|
|
897
|
+
reason: REVALIDATE_UNAVAILABLE[entry.kind],
|
|
898
|
+
onClick: () => void actions.revalidate(entry.key),
|
|
899
|
+
children: "Revalidate"
|
|
900
|
+
}),
|
|
901
|
+
/* @__PURE__ */ jsx("button", {
|
|
902
|
+
type: "button",
|
|
903
|
+
className: "danger",
|
|
904
|
+
onClick: () => void actions.remove(entry.key),
|
|
905
|
+
children: "Delete"
|
|
906
|
+
}),
|
|
907
|
+
editing === null && /* @__PURE__ */ jsx(ActionButton, {
|
|
908
|
+
buttonRef: editRef,
|
|
909
|
+
reason: editable.editable ? void 0 : editable.reason,
|
|
910
|
+
onClick: () => {
|
|
911
|
+
const current = toEditableData(entry.data);
|
|
912
|
+
if (current.editable) setEditing({
|
|
913
|
+
text: current.text,
|
|
914
|
+
exact: current.exact
|
|
915
|
+
});
|
|
916
|
+
},
|
|
917
|
+
children: "Edit"
|
|
918
|
+
})
|
|
919
|
+
]
|
|
920
|
+
}),
|
|
921
|
+
entry.error !== void 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("h3", {
|
|
922
|
+
className: "detail-heading",
|
|
923
|
+
children: "Error"
|
|
924
|
+
}), /* @__PURE__ */ jsx(JsonTree, {
|
|
925
|
+
value: entry.error,
|
|
926
|
+
label: "Error"
|
|
927
|
+
})] }),
|
|
928
|
+
/* @__PURE__ */ jsx("h3", {
|
|
929
|
+
className: "detail-heading",
|
|
930
|
+
children: "Data"
|
|
931
|
+
}),
|
|
932
|
+
editing === null ? /* @__PURE__ */ jsx(JsonTree, {
|
|
933
|
+
value: entry.data,
|
|
934
|
+
label: "Data"
|
|
935
|
+
}) : /* @__PURE__ */ jsx(DataEditor, {
|
|
936
|
+
initialText: editing.text,
|
|
937
|
+
exact: editing.exact,
|
|
938
|
+
onApply: async (data) => {
|
|
939
|
+
await actions.setData(entry.key, data);
|
|
940
|
+
stopEditing();
|
|
941
|
+
},
|
|
942
|
+
onCancel: stopEditing
|
|
943
|
+
})
|
|
944
|
+
]
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* An action button, or, with a reason, a disabled one that says why. It uses
|
|
949
|
+
* `aria-disabled` rather than `disabled`, so it stays focusable and shows its
|
|
950
|
+
* tooltip: disabled buttons don't get hover or focus in every browser.
|
|
951
|
+
*/
|
|
952
|
+
function ActionButton({ buttonRef, reason, onClick, children }) {
|
|
953
|
+
return /* @__PURE__ */ jsx("button", {
|
|
954
|
+
ref: buttonRef,
|
|
955
|
+
type: "button",
|
|
956
|
+
"aria-disabled": reason !== void 0 || void 0,
|
|
957
|
+
title: reason,
|
|
958
|
+
onClick: reason === void 0 ? onClick : void 0,
|
|
959
|
+
children
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* The JSON editor that replaces the data tree while editing. Invalid JSON
|
|
964
|
+
* shows an error under the text and isn't applied. Escape cancels, and
|
|
965
|
+
* Ctrl+Enter (⌘+Enter on a Mac) applies.
|
|
966
|
+
*/
|
|
967
|
+
function DataEditor({ initialText, exact, onApply, onCancel }) {
|
|
968
|
+
const [text, setText] = useState(initialText);
|
|
969
|
+
const [error, setError] = useState(null);
|
|
970
|
+
const errorId = useId();
|
|
971
|
+
const noteId = useId();
|
|
972
|
+
const apply = () => {
|
|
973
|
+
const parsed = parseEditedData(text);
|
|
974
|
+
if (!parsed.ok) {
|
|
975
|
+
setError(parsed.message);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
onApply(parsed.data);
|
|
979
|
+
};
|
|
980
|
+
const describedBy = [error !== null && errorId, !exact && noteId].filter(Boolean).join(" ");
|
|
981
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
982
|
+
className: "editor",
|
|
983
|
+
role: "group",
|
|
984
|
+
"aria-label": "Edit data",
|
|
985
|
+
onKeyDown: (event) => {
|
|
986
|
+
if (event.key === "Escape") {
|
|
987
|
+
event.preventDefault();
|
|
988
|
+
onCancel();
|
|
989
|
+
}
|
|
990
|
+
},
|
|
991
|
+
children: [
|
|
992
|
+
!exact && /* @__PURE__ */ jsx("p", {
|
|
993
|
+
id: noteId,
|
|
994
|
+
className: "editor-note",
|
|
995
|
+
children: "Some values, such as dates or Maps, can’t be written as JSON exactly. Applying saves them as plain JSON."
|
|
996
|
+
}),
|
|
997
|
+
/* @__PURE__ */ jsx("textarea", {
|
|
998
|
+
className: "editor-text",
|
|
999
|
+
"aria-label": "Data as JSON",
|
|
1000
|
+
"aria-invalid": error !== null || void 0,
|
|
1001
|
+
"aria-describedby": describedBy || void 0,
|
|
1002
|
+
spellCheck: false,
|
|
1003
|
+
autoFocus: true,
|
|
1004
|
+
rows: Math.min(Math.max(text.split("\n").length + 1, 4), 20),
|
|
1005
|
+
value: text,
|
|
1006
|
+
onChange: (event) => {
|
|
1007
|
+
setText(event.target.value);
|
|
1008
|
+
setError(null);
|
|
1009
|
+
},
|
|
1010
|
+
onKeyDown: (event) => {
|
|
1011
|
+
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
1012
|
+
event.preventDefault();
|
|
1013
|
+
apply();
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}),
|
|
1017
|
+
error !== null && /* @__PURE__ */ jsx("p", {
|
|
1018
|
+
id: errorId,
|
|
1019
|
+
className: "editor-error",
|
|
1020
|
+
role: "alert",
|
|
1021
|
+
children: error
|
|
1022
|
+
}),
|
|
1023
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1024
|
+
className: "detail-actions",
|
|
1025
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
1026
|
+
type: "button",
|
|
1027
|
+
onClick: apply,
|
|
1028
|
+
children: "Apply"
|
|
1029
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
1030
|
+
type: "button",
|
|
1031
|
+
onClick: onCancel,
|
|
1032
|
+
children: "Cancel"
|
|
1033
|
+
})]
|
|
1034
|
+
})
|
|
1035
|
+
]
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
// src/panel.tsx
|
|
1039
|
+
/**
|
|
1040
|
+
* Watches the nearest SWR cache for as long as the calling component is
|
|
1041
|
+
* mounted.
|
|
1042
|
+
*
|
|
1043
|
+
* SWR doesn't record when data changed, so the reader times changes itself
|
|
1044
|
+
* and can only time what it sees. Call this where it lives as long as
|
|
1045
|
+
* `<SWRDevtools />`, not inside the panel, so ages are right when the panel
|
|
1046
|
+
* opens, the way TanStack Query Devtools shows them. Watching a closed panel
|
|
1047
|
+
* is cheap: a wrapped `cache.set` and a batched re-read.
|
|
1048
|
+
*/
|
|
1049
|
+
function useCacheView() {
|
|
1050
|
+
const config = useSWRConfig();
|
|
1051
|
+
const { cache } = config;
|
|
1052
|
+
const store = useMemo(() => createCacheStore(cache), [cache]);
|
|
1053
|
+
const entries = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
1054
|
+
const actions = useMemo(() => createCacheActions(config), [config]);
|
|
1055
|
+
return {
|
|
1056
|
+
entries,
|
|
1057
|
+
mode: store.getMode(),
|
|
1058
|
+
actions
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
const SORT_OPTIONS = [
|
|
1062
|
+
{
|
|
1063
|
+
value: "key",
|
|
1064
|
+
name: "Key"
|
|
1065
|
+
},
|
|
1066
|
+
{
|
|
1067
|
+
value: "status",
|
|
1068
|
+
name: "Status"
|
|
1069
|
+
},
|
|
1070
|
+
{
|
|
1071
|
+
value: "updated",
|
|
1072
|
+
name: "Last updated"
|
|
1073
|
+
}
|
|
1074
|
+
];
|
|
1075
|
+
/**
|
|
1076
|
+
* The panel's content: a header, a filter and sort bar, the key list and,
|
|
1077
|
+
* once a key is selected, its details.
|
|
1078
|
+
*/
|
|
1079
|
+
function Panel({ view, onClose }) {
|
|
1080
|
+
const { entries, mode, actions } = view;
|
|
1081
|
+
const [sortBy, setSortBy] = useState("key");
|
|
1082
|
+
const [query, setQuery] = useState("");
|
|
1083
|
+
const visible = useMemo(() => sortEntries(filterEntries(entries, query), sortBy), [
|
|
1084
|
+
entries,
|
|
1085
|
+
query,
|
|
1086
|
+
sortBy
|
|
1087
|
+
]);
|
|
1088
|
+
const isFiltered = query.trim() !== "";
|
|
1089
|
+
const [selectedKey, setSelectedKey] = useState(null);
|
|
1090
|
+
const selected = entries.find((entry) => entry.key === selectedKey);
|
|
1091
|
+
const sortId = useId();
|
|
1092
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1093
|
+
className: "panel-content",
|
|
1094
|
+
children: [
|
|
1095
|
+
/* @__PURE__ */ jsxs("header", {
|
|
1096
|
+
className: "header",
|
|
1097
|
+
children: [
|
|
1098
|
+
/* @__PURE__ */ jsx("span", {
|
|
1099
|
+
className: "title",
|
|
1100
|
+
children: "SWR Devtools"
|
|
1101
|
+
}),
|
|
1102
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1103
|
+
className: "mode",
|
|
1104
|
+
children: [
|
|
1105
|
+
isFiltered ? `${visible.length} of ${entries.length}` : entries.length,
|
|
1106
|
+
" keys (",
|
|
1107
|
+
mode,
|
|
1108
|
+
")"
|
|
1109
|
+
]
|
|
1110
|
+
}),
|
|
1111
|
+
/* @__PURE__ */ jsxs("span", {
|
|
1112
|
+
className: "header-actions",
|
|
1113
|
+
children: [/* @__PURE__ */ jsx(ClearAll, { onConfirm: () => void actions.clearAll() }), onClose && /* @__PURE__ */ jsx("button", {
|
|
1114
|
+
type: "button",
|
|
1115
|
+
"aria-label": "Close SWR Devtools",
|
|
1116
|
+
onClick: onClose,
|
|
1117
|
+
children: "Close"
|
|
1118
|
+
})]
|
|
1119
|
+
})
|
|
1120
|
+
]
|
|
1121
|
+
}),
|
|
1122
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1123
|
+
className: "toolbar",
|
|
1124
|
+
children: [
|
|
1125
|
+
/* @__PURE__ */ jsx("input", {
|
|
1126
|
+
type: "search",
|
|
1127
|
+
className: "search",
|
|
1128
|
+
placeholder: "Filter keys",
|
|
1129
|
+
"aria-label": "Filter keys",
|
|
1130
|
+
value: query,
|
|
1131
|
+
onChange: (event) => setQuery(event.target.value)
|
|
1132
|
+
}),
|
|
1133
|
+
/* @__PURE__ */ jsx("label", {
|
|
1134
|
+
className: "sort-label",
|
|
1135
|
+
htmlFor: sortId,
|
|
1136
|
+
children: "Sort by"
|
|
1137
|
+
}),
|
|
1138
|
+
/* @__PURE__ */ jsx("select", {
|
|
1139
|
+
id: sortId,
|
|
1140
|
+
className: "sort",
|
|
1141
|
+
value: sortBy,
|
|
1142
|
+
onChange: (event) => {
|
|
1143
|
+
const option = SORT_OPTIONS.find(({ value }) => value === event.target.value);
|
|
1144
|
+
if (option) setSortBy(option.value);
|
|
1145
|
+
},
|
|
1146
|
+
children: SORT_OPTIONS.map(({ value, name }) => /* @__PURE__ */ jsx("option", {
|
|
1147
|
+
value,
|
|
1148
|
+
children: name
|
|
1149
|
+
}, value))
|
|
1150
|
+
})
|
|
1151
|
+
]
|
|
1152
|
+
}),
|
|
1153
|
+
/* @__PURE__ */ jsx("div", {
|
|
1154
|
+
className: "main",
|
|
1155
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
1156
|
+
className: "split",
|
|
1157
|
+
"data-detail": selected ? "" : void 0,
|
|
1158
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
1159
|
+
className: "body",
|
|
1160
|
+
children: entries.length === 0 ? /* @__PURE__ */ jsx("p", {
|
|
1161
|
+
className: "empty",
|
|
1162
|
+
children: "No keys in the cache yet."
|
|
1163
|
+
}) : visible.length === 0 ? /* @__PURE__ */ jsxs("p", {
|
|
1164
|
+
className: "empty",
|
|
1165
|
+
children: [
|
|
1166
|
+
"No keys match “",
|
|
1167
|
+
query.trim(),
|
|
1168
|
+
"”."
|
|
1169
|
+
]
|
|
1170
|
+
}) : /* @__PURE__ */ jsx(KeyList, {
|
|
1171
|
+
entries: visible,
|
|
1172
|
+
selectedKey: selected ? selected.key : null,
|
|
1173
|
+
onSelect: setSelectedKey
|
|
1174
|
+
})
|
|
1175
|
+
}), selected && /* @__PURE__ */ jsx(KeyDetail, {
|
|
1176
|
+
entry: selected,
|
|
1177
|
+
actions,
|
|
1178
|
+
onClose: () => setSelectedKey(null)
|
|
1179
|
+
}, selected.key)]
|
|
1180
|
+
})
|
|
1181
|
+
})
|
|
1182
|
+
]
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Clear all, behind a confirmation in the header. The panel's own, rather
|
|
1187
|
+
* than `window.confirm`, which blocks the page and can be switched off.
|
|
1188
|
+
*/
|
|
1189
|
+
function ClearAll({ onConfirm }) {
|
|
1190
|
+
const [confirming, setConfirming] = useState(false);
|
|
1191
|
+
const clearRef = useRef(null);
|
|
1192
|
+
const cancelRef = useRef(null);
|
|
1193
|
+
const hasActed = useRef(false);
|
|
1194
|
+
useEffect(() => {
|
|
1195
|
+
if (!hasActed.current) return;
|
|
1196
|
+
(confirming ? cancelRef : clearRef).current?.focus();
|
|
1197
|
+
}, [confirming]);
|
|
1198
|
+
const setAndFocus = (next) => {
|
|
1199
|
+
hasActed.current = true;
|
|
1200
|
+
setConfirming(next);
|
|
1201
|
+
};
|
|
1202
|
+
if (!confirming) return /* @__PURE__ */ jsx("button", {
|
|
1203
|
+
type: "button",
|
|
1204
|
+
ref: clearRef,
|
|
1205
|
+
onClick: () => setAndFocus(true),
|
|
1206
|
+
children: "Clear all"
|
|
1207
|
+
});
|
|
1208
|
+
return /* @__PURE__ */ jsxs("span", {
|
|
1209
|
+
className: "confirm",
|
|
1210
|
+
role: "group",
|
|
1211
|
+
"aria-label": "Confirm Clear all",
|
|
1212
|
+
onKeyDown: (event) => {
|
|
1213
|
+
if (event.key === "Escape") setAndFocus(false);
|
|
1214
|
+
},
|
|
1215
|
+
children: [
|
|
1216
|
+
/* @__PURE__ */ jsx("span", {
|
|
1217
|
+
className: "confirm-text",
|
|
1218
|
+
children: "Clear every key?"
|
|
1219
|
+
}),
|
|
1220
|
+
/* @__PURE__ */ jsx("button", {
|
|
1221
|
+
type: "button",
|
|
1222
|
+
className: "danger",
|
|
1223
|
+
onClick: () => {
|
|
1224
|
+
onConfirm();
|
|
1225
|
+
setAndFocus(false);
|
|
1226
|
+
},
|
|
1227
|
+
children: "Clear"
|
|
1228
|
+
}),
|
|
1229
|
+
/* @__PURE__ */ jsx("button", {
|
|
1230
|
+
type: "button",
|
|
1231
|
+
ref: cancelRef,
|
|
1232
|
+
onClick: () => setAndFocus(false),
|
|
1233
|
+
children: "Cancel"
|
|
1234
|
+
})
|
|
1235
|
+
]
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
// src/shadow-host.tsx
|
|
1239
|
+
/**
|
|
1240
|
+
* Renders children into a shadow root, with the panel's CSS inside it. Host
|
|
1241
|
+
* styles can't leak in, and the panel's styles can't leak out.
|
|
1242
|
+
*
|
|
1243
|
+
* By default the host is appended to `document.body` rather than placed where
|
|
1244
|
+
* `<SWRDevtools />` is, so the app's layout and stacking contexts don't
|
|
1245
|
+
* affect the floating panel. React context, including `<SWRConfig>`, still
|
|
1246
|
+
* reaches through the portal.
|
|
1247
|
+
*
|
|
1248
|
+
* Renders nothing on the server, and nothing inside the host until the shadow
|
|
1249
|
+
* root exists.
|
|
1250
|
+
*/
|
|
1251
|
+
function ShadowHost({ children, inline = false }) {
|
|
1252
|
+
const [root, setRoot] = useState(null);
|
|
1253
|
+
const inlineHost = useRef(null);
|
|
1254
|
+
useLayoutEffect(() => {
|
|
1255
|
+
let host = inlineHost.current;
|
|
1256
|
+
const owned = host === null;
|
|
1257
|
+
if (host === null) {
|
|
1258
|
+
host = document.createElement("div");
|
|
1259
|
+
host.setAttribute("data-swr-devtools", "");
|
|
1260
|
+
document.body.append(host);
|
|
1261
|
+
}
|
|
1262
|
+
const shadow = host.shadowRoot ?? host.attachShadow({ mode: "open" });
|
|
1263
|
+
setRoot(shadow);
|
|
1264
|
+
return () => {
|
|
1265
|
+
if (owned) host.remove();
|
|
1266
|
+
setRoot(null);
|
|
1267
|
+
};
|
|
1268
|
+
}, []);
|
|
1269
|
+
const content = root && createPortal(/* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("style", { children: "\n:host {\n /* Inner !important beats the host page's !important, so even an\n aggressive global reset can't restyle or hide the host element. */\n all: initial !important;\n /* `all` leaves these two out, so an RTL host page would flip the panel. */\n direction: ltr !important;\n unicode-bidi: isolate !important;\n\n /* Colours */\n --swr-dt-color-bg: #ffffff;\n --swr-dt-color-surface: #f6f7f9;\n --swr-dt-color-text: #1c2024;\n --swr-dt-color-muted: #60646c;\n --swr-dt-color-border: #e0e1e6;\n --swr-dt-color-accent: #0969da;\n --swr-dt-color-accent-text: #ffffff;\n\n /* Status colours, designed for SWR's states */\n --swr-dt-color-loading: #9a6700;\n --swr-dt-color-validating: #0969da;\n --swr-dt-color-success: #1f883d;\n --swr-dt-color-error: #d1242f;\n --swr-dt-color-idle: #60646c;\n\n /* Spacing */\n --swr-dt-space-1: 4px;\n --swr-dt-space-2: 8px;\n --swr-dt-space-3: 12px;\n --swr-dt-space-4: 16px;\n\n /* Radii */\n --swr-dt-radius-sm: 4px;\n --swr-dt-radius-md: 8px;\n --swr-dt-radius-full: 9999px;\n\n /* Fonts */\n --swr-dt-font-sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;\n --swr-dt-font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;\n --swr-dt-font-size-sm: 12px;\n --swr-dt-font-size-md: 13px;\n --swr-dt-line-height: 1.4;\n\n /* Layering and elevation */\n --swr-dt-z-index: 99999;\n --swr-dt-shadow: 0 4px 16px rgb(0 0 0 / 0.16);\n}\n\n/* <SWRDevtoolsPanel /> fills the element the app renders it in. */\n:host([data-inline]) {\n display: block !important;\n width: 100% !important;\n height: 100% !important;\n}\n\n*,\n*::before,\n*::after {\n box-sizing: border-box;\n}\n\n.toggle {\n position: fixed;\n z-index: var(--swr-dt-z-index);\n margin: var(--swr-dt-space-4);\n padding: var(--swr-dt-space-2) var(--swr-dt-space-3);\n font-family: var(--swr-dt-font-sans);\n font-size: var(--swr-dt-font-size-md);\n font-weight: 700;\n color: var(--swr-dt-color-accent-text);\n background: var(--swr-dt-color-accent);\n border-color: var(--swr-dt-color-accent);\n border-radius: var(--swr-dt-radius-full);\n box-shadow: var(--swr-dt-shadow);\n}\n\n.toggle:hover {\n border-color: var(--swr-dt-color-text);\n}\n\n.toggle[data-position^='top'] {\n top: 0;\n}\n\n.toggle[data-position^='bottom'] {\n bottom: 0;\n}\n\n.toggle[data-position$='left'] {\n left: 0;\n}\n\n.toggle[data-position$='right'] {\n right: 0;\n}\n\n.panel {\n position: fixed;\n z-index: var(--swr-dt-z-index);\n display: flex;\n background: var(--swr-dt-color-bg);\n box-shadow: var(--swr-dt-shadow);\n}\n\n.panel[data-position='bottom'] {\n left: 0;\n right: 0;\n bottom: 0;\n flex-direction: column;\n /* The size is clamped when it's set; this keeps it on screen when the\n window shrinks afterwards. 40px matches VIEWPORT_GAP. */\n max-height: calc(100vh - 40px);\n border-top: 1px solid var(--swr-dt-color-border);\n}\n\n.panel[data-position='right'] {\n top: 0;\n right: 0;\n bottom: 0;\n flex-direction: row;\n max-width: calc(100vw - 40px);\n border-left: 1px solid var(--swr-dt-color-border);\n}\n\n.resize-handle {\n flex: none;\n background: transparent;\n /* Without this, a touch drag on the edge scrolls the page instead. */\n touch-action: none;\n}\n\n.panel[data-position='bottom'] > .resize-handle {\n height: var(--swr-dt-space-1);\n cursor: ns-resize;\n}\n\n.panel[data-position='right'] > .resize-handle {\n width: var(--swr-dt-space-1);\n cursor: ew-resize;\n}\n\n.resize-handle:hover,\n.resize-handle:focus-visible {\n background: var(--swr-dt-color-accent);\n outline: none;\n}\n\n.panel-content {\n display: flex;\n flex: 1;\n flex-direction: column;\n min-width: 0;\n min-height: 0;\n height: 100%;\n color-scheme: light;\n font-family: var(--swr-dt-font-sans);\n font-size: var(--swr-dt-font-size-md);\n line-height: var(--swr-dt-line-height);\n color: var(--swr-dt-color-text);\n background: var(--swr-dt-color-bg);\n}\n\n.header {\n display: flex;\n flex: none;\n align-items: center;\n gap: var(--swr-dt-space-2);\n padding: var(--swr-dt-space-2) var(--swr-dt-space-3);\n background: var(--swr-dt-color-surface);\n border-bottom: 1px solid var(--swr-dt-color-border);\n}\n\n.title {\n font-weight: 600;\n}\n\n.mode {\n color: var(--swr-dt-color-muted);\n}\n\n.confirm {\n display: flex;\n align-items: center;\n gap: var(--swr-dt-space-1);\n}\n\n.confirm-text {\n font-size: var(--swr-dt-font-size-sm);\n}\n\n.header-actions {\n display: flex;\n gap: var(--swr-dt-space-1);\n margin-left: auto;\n}\n\n.toolbar {\n display: flex;\n flex: none;\n align-items: center;\n gap: var(--swr-dt-space-2);\n padding: var(--swr-dt-space-2) var(--swr-dt-space-3);\n border-bottom: 1px solid var(--swr-dt-color-border);\n}\n\n.search {\n flex: 1 1 160px;\n min-width: 0;\n}\n\n.sort-label {\n flex: none;\n font-size: var(--swr-dt-font-size-sm);\n color: var(--swr-dt-color-muted);\n}\n\n.sort {\n flex: none;\n}\n\n/* The panel's size, not the window's, decides whether the details sit beside\n the list or below it. */\n.main {\n flex: 1;\n min-height: 0;\n container-type: inline-size;\n}\n\n.split {\n display: grid;\n grid-template-rows: minmax(0, 1fr);\n height: 100%;\n}\n\n.split[data-detail] {\n grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);\n}\n\n@container (min-width: 640px) {\n .split[data-detail] {\n grid-template-rows: minmax(0, 1fr);\n grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);\n }\n}\n\n.body {\n min-height: 0;\n overflow: auto;\n padding: 0 var(--swr-dt-space-3) var(--swr-dt-space-3);\n}\n\nbutton,\ninput,\nselect,\ntextarea {\n font: inherit;\n font-size: var(--swr-dt-font-size-sm);\n color: var(--swr-dt-color-text);\n background: var(--swr-dt-color-surface);\n border: 1px solid var(--swr-dt-color-border);\n border-radius: var(--swr-dt-radius-sm);\n padding: var(--swr-dt-space-1) var(--swr-dt-space-2);\n}\n\nbutton,\nselect {\n cursor: pointer;\n}\n\nbutton:hover,\nselect:hover {\n border-color: var(--swr-dt-color-accent);\n}\n\nbutton[aria-disabled='true'] {\n cursor: not-allowed;\n color: var(--swr-dt-color-muted);\n}\n\nbutton[aria-disabled='true']:hover {\n border-color: var(--swr-dt-color-border);\n}\n\n/* Destructive actions: Delete, and confirming Clear all. */\nbutton.danger {\n color: var(--swr-dt-color-error);\n}\n\nbutton.danger:hover {\n border-color: var(--swr-dt-color-error);\n}\n\ninput,\ntextarea {\n background: var(--swr-dt-color-bg);\n}\n\ninput::placeholder {\n color: var(--swr-dt-color-muted);\n}\n\nbutton:focus-visible,\ninput:focus-visible,\nselect:focus-visible,\ntextarea:focus-visible {\n outline: 2px solid var(--swr-dt-color-accent);\n outline-offset: 1px;\n}\n\n.empty {\n margin: 0;\n padding: var(--swr-dt-space-4) 0;\n color: var(--swr-dt-color-muted);\n}\n\n.key-list {\n margin: 0;\n padding: 0;\n list-style: none;\n}\n\n.key-row {\n display: flex;\n flex-wrap: wrap;\n /* Whatever wraps onto a second line sits on the right, like the first. */\n justify-content: flex-end;\n align-items: center;\n gap: var(--swr-dt-space-1) var(--swr-dt-space-2);\n min-height: 32px;\n padding: var(--swr-dt-space-1) 0;\n border-bottom: 1px solid var(--swr-dt-color-border);\n}\n\n/* In a narrow panel the key keeps 128px next to the badge, and the kind\n and age wrap onto a second line. */\n.key {\n flex: 1 1 128px;\n min-width: 0;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n padding: 2px var(--swr-dt-space-1);\n text-align: left;\n font-family: var(--swr-dt-font-mono);\n font-size: var(--swr-dt-font-size-sm);\n background: transparent;\n border-color: transparent;\n}\n\n.key:hover {\n text-decoration: underline;\n border-color: transparent;\n}\n\n.key-row[data-selected] {\n background: var(--swr-dt-color-surface);\n box-shadow: inset 2px 0 var(--swr-dt-color-accent);\n}\n\n/* A fixed width keeps the keys lined up, whatever the status. */\n.badge {\n display: inline-flex;\n flex: none;\n align-items: center;\n gap: var(--swr-dt-space-1);\n width: 92px;\n padding: 2px var(--swr-dt-space-2);\n font-size: var(--swr-dt-font-size-sm);\n font-weight: 600;\n color: var(--swr-dt-badge-color);\n background: color-mix(in srgb, var(--swr-dt-badge-color) 10%, transparent);\n border: 1px solid color-mix(in srgb, var(--swr-dt-badge-color) 30%, transparent);\n border-radius: var(--swr-dt-radius-full);\n}\n\n.badge::before {\n content: '';\n flex: none;\n width: 6px;\n height: 6px;\n background: currentColor;\n border-radius: var(--swr-dt-radius-full);\n}\n\n/* Each state gets its own shape too: a hollow dot before any data arrives,\n a pulsing one while fetching in the background. */\n.badge[data-status='loading'] {\n --swr-dt-badge-color: var(--swr-dt-color-loading);\n}\n\n.badge[data-status='loading']::before {\n background: transparent;\n border: 1.5px solid currentColor;\n}\n\n.badge[data-status='validating'] {\n --swr-dt-badge-color: var(--swr-dt-color-validating);\n}\n\n.badge[data-status='validating']::before {\n animation: swr-dt-pulse 1s ease-in-out infinite alternate;\n}\n\n.badge[data-status='success'] {\n --swr-dt-badge-color: var(--swr-dt-color-success);\n}\n\n.badge[data-status='error'] {\n --swr-dt-badge-color: var(--swr-dt-color-error);\n}\n\n.badge[data-status='idle'] {\n --swr-dt-badge-color: var(--swr-dt-color-idle);\n}\n\n@keyframes swr-dt-pulse {\n to {\n opacity: 0.25;\n }\n}\n\n@media (prefers-reduced-motion: reduce) {\n .badge[data-status='validating']::before {\n animation: none;\n }\n}\n\n.kind {\n flex: none;\n padding: 0 var(--swr-dt-space-1);\n font-size: var(--swr-dt-font-size-sm);\n color: var(--swr-dt-color-muted);\n border: 1px solid var(--swr-dt-color-border);\n border-radius: var(--swr-dt-radius-sm);\n}\n\n.age {\n flex: none;\n min-width: 56px;\n text-align: right;\n font-size: var(--swr-dt-font-size-sm);\n font-variant-numeric: tabular-nums;\n color: var(--swr-dt-color-muted);\n}\n\n.detail {\n min-height: 0;\n overflow: auto;\n padding: var(--swr-dt-space-2) var(--swr-dt-space-3) var(--swr-dt-space-3);\n border-top: 1px solid var(--swr-dt-color-border);\n}\n\n@container (min-width: 640px) {\n .detail {\n border-top: none;\n border-left: 1px solid var(--swr-dt-color-border);\n }\n}\n\n.detail-header {\n display: flex;\n align-items: flex-start;\n gap: var(--swr-dt-space-2);\n}\n\n.detail-header > button {\n flex: none;\n margin-left: auto;\n}\n\n/* The full key, however long: it wraps rather than truncating. */\n.detail-key,\n.detail-raw-key {\n min-width: 0;\n margin: 0;\n overflow-wrap: anywhere;\n font-family: var(--swr-dt-font-mono);\n font-size: var(--swr-dt-font-size-sm);\n}\n\n.detail-key {\n padding-top: 2px;\n font-weight: 600;\n}\n\n.detail-meta {\n display: grid;\n grid-template-columns: max-content minmax(0, 1fr);\n gap: var(--swr-dt-space-1) var(--swr-dt-space-3);\n margin: var(--swr-dt-space-3) 0 0;\n font-size: var(--swr-dt-font-size-sm);\n}\n\n.detail-meta dt {\n color: var(--swr-dt-color-muted);\n}\n\n.detail-meta dd {\n margin: 0;\n}\n\n.detail-actions {\n display: flex;\n gap: var(--swr-dt-space-1);\n margin-top: var(--swr-dt-space-3);\n}\n\n.detail-heading {\n margin: var(--swr-dt-space-4) 0 var(--swr-dt-space-1);\n font-size: var(--swr-dt-font-size-sm);\n font-weight: 600;\n color: var(--swr-dt-color-muted);\n}\n\n.editor-text {\n display: block;\n box-sizing: border-box;\n width: 100%;\n resize: vertical;\n font-family: var(--swr-dt-font-mono);\n line-height: var(--swr-dt-line-height);\n tab-size: 2;\n}\n\n.editor-text[aria-invalid='true'] {\n border-color: var(--swr-dt-color-error);\n}\n\n.editor-error,\n.editor-note {\n margin: var(--swr-dt-space-1) 0 0;\n font-size: var(--swr-dt-font-size-sm);\n}\n\n.editor-error {\n color: var(--swr-dt-color-error);\n}\n\n.editor-note {\n margin: 0 0 var(--swr-dt-space-1);\n color: var(--swr-dt-color-muted);\n}\n\n.json-tree,\n.json-children {\n margin: 0;\n padding: 0;\n list-style: none;\n font-family: var(--swr-dt-font-mono);\n font-size: var(--swr-dt-font-size-sm);\n}\n\n.json-children {\n padding-left: var(--swr-dt-space-4);\n border-left: 1px solid var(--swr-dt-color-border);\n margin-left: 5px;\n}\n\n.json-row {\n padding: 1px 0;\n}\n\n/* A leaf lines up with the name of a container next to it, after its arrow. */\n.json-row > .json-name,\n.json-row > .json-value:first-child {\n margin-left: 14px;\n}\n\n.json-toggle {\n display: inline-flex;\n align-items: baseline;\n gap: var(--swr-dt-space-1);\n max-width: 100%;\n padding: 0;\n font: inherit;\n text-align: left;\n background: transparent;\n border: none;\n}\n\n.json-toggle::before {\n content: '▸';\n flex: none;\n width: 10px;\n color: var(--swr-dt-color-muted);\n}\n\n.json-toggle[aria-expanded='true']::before {\n content: '▾';\n}\n\n.json-toggle:disabled {\n cursor: default;\n}\n\n.json-toggle:disabled::before {\n visibility: hidden;\n}\n\n.json-toggle:hover .json-name,\n.json-toggle:hover .json-range {\n text-decoration: underline;\n}\n\n.json-name::after {\n content: ':';\n margin-right: var(--swr-dt-space-1);\n color: var(--swr-dt-color-muted);\n}\n\n.json-toggle > .json-name::after {\n margin-right: 0;\n}\n\n.json-value {\n overflow-wrap: anywhere;\n white-space: pre-wrap;\n}\n\n.json-value[data-kind='string'],\n.json-value[data-kind='date'] {\n color: var(--swr-dt-color-success);\n}\n\n.json-value[data-kind='number'],\n.json-value[data-kind='bigint'],\n.json-value[data-kind='boolean'] {\n color: var(--swr-dt-color-accent);\n}\n\n.json-value[data-kind='error'],\n.json-value[data-kind='thrown'] {\n color: var(--swr-dt-color-error);\n}\n\n.json-value[data-kind='null'],\n.json-value[data-kind='undefined'],\n.json-value[data-kind='symbol'],\n.json-value[data-kind='function'],\n.json-value[data-kind='circular'],\n.json-value[data-kind='array'],\n.json-value[data-kind='object'],\n.json-value[data-kind='map'],\n.json-value[data-kind='set'],\n.json-range {\n color: var(--swr-dt-color-muted);\n}\n\n.json-unreadable {\n margin: 0;\n font-size: var(--swr-dt-font-size-sm);\n color: var(--swr-dt-color-error);\n}\n" }), children] }), root);
|
|
1270
|
+
if (inline) return /* @__PURE__ */ jsx("div", {
|
|
1271
|
+
ref: inlineHost,
|
|
1272
|
+
"data-swr-devtools": "",
|
|
1273
|
+
"data-inline": "",
|
|
1274
|
+
children: content
|
|
1275
|
+
});
|
|
1276
|
+
return content;
|
|
1277
|
+
}
|
|
1278
|
+
// src/storage.ts
|
|
1279
|
+
const STORAGE_KEY = "swr-devtools-react";
|
|
1280
|
+
/**
|
|
1281
|
+
* Reads the saved state. Anything missing, invalid, or unreadable (storage
|
|
1282
|
+
* disabled, private mode, a sandboxed iframe) falls back to the defaults.
|
|
1283
|
+
*/
|
|
1284
|
+
function loadPanelState(fallback) {
|
|
1285
|
+
let raw;
|
|
1286
|
+
try {
|
|
1287
|
+
raw = window.localStorage.getItem(STORAGE_KEY);
|
|
1288
|
+
} catch {
|
|
1289
|
+
return fallback;
|
|
1290
|
+
}
|
|
1291
|
+
if (raw === null) return fallback;
|
|
1292
|
+
let saved;
|
|
1293
|
+
try {
|
|
1294
|
+
saved = JSON.parse(raw);
|
|
1295
|
+
} catch {
|
|
1296
|
+
return fallback;
|
|
1297
|
+
}
|
|
1298
|
+
if (typeof saved !== "object" || saved === null) return fallback;
|
|
1299
|
+
const isOpen = Reflect.get(saved, "isOpen");
|
|
1300
|
+
const height = Reflect.get(saved, "height");
|
|
1301
|
+
const width = Reflect.get(saved, "width");
|
|
1302
|
+
return {
|
|
1303
|
+
isOpen: typeof isOpen === "boolean" ? isOpen : fallback.isOpen,
|
|
1304
|
+
height: isSize(height) ? height : fallback.height,
|
|
1305
|
+
width: isSize(width) ? width : fallback.width
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
/** Saves the state. A failed write (quota, storage disabled) is ignored. */
|
|
1309
|
+
function savePanelState(state) {
|
|
1310
|
+
try {
|
|
1311
|
+
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
|
|
1312
|
+
} catch {}
|
|
1313
|
+
}
|
|
1314
|
+
function isSize(value) {
|
|
1315
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0;
|
|
1316
|
+
}
|
|
1317
|
+
/** Space always left for the app when the panel is at its largest, in pixels. */
|
|
1318
|
+
const VIEWPORT_GAP = 40;
|
|
1319
|
+
/** How far one arrow-key press resizes the panel, in pixels. */
|
|
1320
|
+
const KEY_STEP = 16;
|
|
1321
|
+
/**
|
|
1322
|
+
* A floating button that opens the devtools panel, docked to the bottom or
|
|
1323
|
+
* right of the viewport. Place it inside the app's `<SWRConfig>`.
|
|
1324
|
+
*
|
|
1325
|
+
* Whether it's open, and its size, are remembered in `localStorage`.
|
|
1326
|
+
*/
|
|
1327
|
+
function SWRDevtools({ initialIsOpen = false, buttonPosition = "bottom-right", position = "bottom" }) {
|
|
1328
|
+
return /* @__PURE__ */ jsx(ShadowHost, { children: /* @__PURE__ */ jsx(Floating, {
|
|
1329
|
+
initialIsOpen,
|
|
1330
|
+
buttonPosition,
|
|
1331
|
+
position
|
|
1332
|
+
}) });
|
|
1333
|
+
}
|
|
1334
|
+
function Floating({ initialIsOpen, buttonPosition, position }) {
|
|
1335
|
+
const view = useCacheView();
|
|
1336
|
+
const [state, setState] = useState(() => loadPanelState({
|
|
1337
|
+
isOpen: initialIsOpen,
|
|
1338
|
+
height: 320,
|
|
1339
|
+
width: 480
|
|
1340
|
+
}));
|
|
1341
|
+
const changed = useRef(false);
|
|
1342
|
+
useEffect(() => {
|
|
1343
|
+
if (changed.current) savePanelState(state);
|
|
1344
|
+
}, [state]);
|
|
1345
|
+
const update = (patch) => {
|
|
1346
|
+
changed.current = true;
|
|
1347
|
+
setState((current) => ({
|
|
1348
|
+
...current,
|
|
1349
|
+
...patch
|
|
1350
|
+
}));
|
|
1351
|
+
};
|
|
1352
|
+
const toggle = useRef(null);
|
|
1353
|
+
const focusToggle = useRef(false);
|
|
1354
|
+
useEffect(() => {
|
|
1355
|
+
if (!state.isOpen && focusToggle.current) {
|
|
1356
|
+
focusToggle.current = false;
|
|
1357
|
+
toggle.current?.focus();
|
|
1358
|
+
}
|
|
1359
|
+
}, [state.isOpen]);
|
|
1360
|
+
if (!state.isOpen) return /* @__PURE__ */ jsx("button", {
|
|
1361
|
+
ref: toggle,
|
|
1362
|
+
type: "button",
|
|
1363
|
+
className: "toggle",
|
|
1364
|
+
"data-position": buttonPosition,
|
|
1365
|
+
"aria-label": "Open SWR Devtools",
|
|
1366
|
+
onClick: () => update({ isOpen: true }),
|
|
1367
|
+
children: "SWR"
|
|
1368
|
+
});
|
|
1369
|
+
const isBottom = position === "bottom";
|
|
1370
|
+
const size = clampSize(isBottom ? state.height : state.width, isBottom);
|
|
1371
|
+
const resize = (next) => update(isBottom ? { height: clampSize(next, true) } : { width: clampSize(next, false) });
|
|
1372
|
+
return /* @__PURE__ */ jsxs("aside", {
|
|
1373
|
+
className: "panel",
|
|
1374
|
+
"data-position": position,
|
|
1375
|
+
"aria-label": "SWR Devtools",
|
|
1376
|
+
style: isBottom ? { height: size } : { width: size },
|
|
1377
|
+
children: [/* @__PURE__ */ jsx(ResizeHandle, {
|
|
1378
|
+
isBottom,
|
|
1379
|
+
size,
|
|
1380
|
+
onResize: resize
|
|
1381
|
+
}), /* @__PURE__ */ jsx(Panel, {
|
|
1382
|
+
view,
|
|
1383
|
+
onClose: () => {
|
|
1384
|
+
focusToggle.current = true;
|
|
1385
|
+
update({ isOpen: false });
|
|
1386
|
+
}
|
|
1387
|
+
})]
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
/** Drag, or focus and use the arrow keys, to resize the panel. */
|
|
1391
|
+
function ResizeHandle({ isBottom, size, onResize }) {
|
|
1392
|
+
const stopDrag = useRef(null);
|
|
1393
|
+
useEffect(() => () => stopDrag.current?.(), []);
|
|
1394
|
+
const move = (event) => onResize(isBottom ? window.innerHeight - event.clientY : window.innerWidth - event.clientX);
|
|
1395
|
+
const stop = () => {
|
|
1396
|
+
window.removeEventListener("pointermove", move);
|
|
1397
|
+
window.removeEventListener("pointerup", stop);
|
|
1398
|
+
window.removeEventListener("pointercancel", stop);
|
|
1399
|
+
stopDrag.current = null;
|
|
1400
|
+
};
|
|
1401
|
+
const startDrag = (event) => {
|
|
1402
|
+
event.preventDefault();
|
|
1403
|
+
window.addEventListener("pointermove", move);
|
|
1404
|
+
window.addEventListener("pointerup", stop);
|
|
1405
|
+
window.addEventListener("pointercancel", stop);
|
|
1406
|
+
stopDrag.current = stop;
|
|
1407
|
+
};
|
|
1408
|
+
const onKeyDown = (event) => {
|
|
1409
|
+
const grow = isBottom ? "ArrowUp" : "ArrowLeft";
|
|
1410
|
+
const shrink = isBottom ? "ArrowDown" : "ArrowRight";
|
|
1411
|
+
if (event.key !== grow && event.key !== shrink) return;
|
|
1412
|
+
event.preventDefault();
|
|
1413
|
+
onResize(size + (event.key === grow ? KEY_STEP : -16));
|
|
1414
|
+
};
|
|
1415
|
+
return /* @__PURE__ */ jsx("div", {
|
|
1416
|
+
role: "separator",
|
|
1417
|
+
tabIndex: 0,
|
|
1418
|
+
className: "resize-handle",
|
|
1419
|
+
"aria-label": "Resize SWR Devtools",
|
|
1420
|
+
"aria-orientation": isBottom ? "horizontal" : "vertical",
|
|
1421
|
+
"aria-valuemin": 120,
|
|
1422
|
+
"aria-valuemax": maxSize(isBottom),
|
|
1423
|
+
"aria-valuenow": size,
|
|
1424
|
+
onPointerDown: startDrag,
|
|
1425
|
+
onKeyDown
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
function maxSize(isBottom) {
|
|
1429
|
+
const viewport = isBottom ? window.innerHeight : window.innerWidth;
|
|
1430
|
+
return Math.max(120, viewport - VIEWPORT_GAP);
|
|
1431
|
+
}
|
|
1432
|
+
function clampSize(size, isBottom) {
|
|
1433
|
+
return Math.round(Math.min(Math.max(size, 120), maxSize(isBottom)));
|
|
1434
|
+
}
|
|
1435
|
+
// src/swr-devtools-panel.tsx
|
|
1436
|
+
/**
|
|
1437
|
+
* The devtools panel on its own, with no floating button. It fills the
|
|
1438
|
+
* element you render it in, so give that element a size. Place it inside the
|
|
1439
|
+
* app's `<SWRConfig>`.
|
|
1440
|
+
*
|
|
1441
|
+
* For embedding in your own layout or another devtools host.
|
|
1442
|
+
*/
|
|
1443
|
+
function SWRDevtoolsPanel() {
|
|
1444
|
+
return /* @__PURE__ */ jsx(ShadowHost, {
|
|
1445
|
+
inline: true,
|
|
1446
|
+
children: /* @__PURE__ */ jsx(InlinePanel, {})
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
function InlinePanel() {
|
|
1450
|
+
return /* @__PURE__ */ jsx(Panel, { view: useCacheView() });
|
|
1451
|
+
}
|
|
1452
|
+
export { SWRDevtools as n, SWRDevtoolsPanel as t };
|