@graphiql/plugin-collections 1.0.0-beta.1 → 1.0.0-beta.3

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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -6
  3. package/dist/components/collection-item-row.js +212 -282
  4. package/dist/components/collection-item-row.js.map +1 -1
  5. package/dist/components/collection-row.js +139 -159
  6. package/dist/components/collection-row.js.map +1 -1
  7. package/dist/components/collections-panel.js +408 -496
  8. package/dist/components/collections-panel.js.map +1 -1
  9. package/dist/components/conflict-dialog.js +148 -164
  10. package/dist/components/conflict-dialog.js.map +1 -1
  11. package/dist/components/export-dialog.js +52 -52
  12. package/dist/components/export-dialog.js.map +1 -1
  13. package/dist/components/import-dialog.js +100 -112
  14. package/dist/components/import-dialog.js.map +1 -1
  15. package/dist/components/save-dialog.js +116 -124
  16. package/dist/components/save-dialog.js.map +1 -1
  17. package/dist/components/session-actions.js +23 -31
  18. package/dist/components/session-actions.js.map +1 -1
  19. package/dist/icons/check.js +18 -9
  20. package/dist/icons/check.js.map +1 -1
  21. package/dist/icons/collections.js +16 -9
  22. package/dist/icons/collections.js.map +1 -1
  23. package/dist/icons/download.js +16 -9
  24. package/dist/icons/download.js.map +1 -1
  25. package/dist/icons/upload.js +16 -9
  26. package/dist/icons/upload.js.map +1 -1
  27. package/dist/index.d.ts +1 -0
  28. package/dist/index.js +32 -41
  29. package/dist/index.js.map +1 -1
  30. package/dist/operation-method.js +24 -24
  31. package/dist/operation-method.js.map +1 -1
  32. package/dist/storage/local-storage.js +32 -37
  33. package/dist/storage/local-storage.js.map +1 -1
  34. package/dist/storage/tab-links.js +27 -30
  35. package/dist/storage/tab-links.js.map +1 -1
  36. package/dist/store.js +365 -402
  37. package/dist/store.js.map +1 -1
  38. package/dist/style.css +6 -27
  39. package/dist/types.js +9 -8
  40. package/dist/types.js.map +1 -1
  41. package/dist/utility/uuid.js +11 -9
  42. package/dist/utility/uuid.js.map +1 -1
  43. package/package.json +19 -15
@@ -1,508 +1,420 @@
1
- import { jsxs, jsx } from "react/jsx-runtime";
2
- import { useState, useRef, useEffect } from "react";
3
- import { useGraphiQLActions, useGraphiQL, pick, PanelHeader, Dialog, Button } from "@graphiql/react";
4
- import { useCollectionsStore, collectionsStore } from "../store.js";
1
+ import { collectionsStore, useCollectionsStore } from "../store.js";
2
+ import SvgUpload from "../icons/upload.js";
5
3
  import { CollectionRow } from "./collection-row.js";
6
4
  import { ImportDialog } from "./import-dialog.js";
7
5
  import { ExportDialog } from "./export-dialog.js";
8
6
  import { ConflictDialog } from "./conflict-dialog.js";
9
7
  import SvgDownload from "../icons/download.js";
10
- import SvgUpload from "../icons/upload.js";
8
+ import { useEffect, useRef, useState } from "react";
9
+ import { Button, Dialog, PanelHeader, pick, useGraphiQL, useGraphiQLActions } from "@graphiql/react";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ //#region src/components/collections-panel.tsx
12
+ /**
13
+ * Returns how many collections `text` holds if it is a valid collections
14
+ * export, otherwise `null`. Used to guard paste/drop so non-collections content
15
+ * is ignored rather than mangled.
16
+ */
11
17
  function parseCollectionsExport(text) {
12
- try {
13
- const parsed = JSON.parse(text);
14
- const list = Array.isArray(parsed == null ? void 0 : parsed.collections) ? parsed.collections : Array.isArray(parsed) ? parsed : null;
15
- if (!(list == null ? void 0 : list.length) || !list.every(
16
- (c) => c && typeof c === "object" && Array.isArray(c.items)
17
- )) {
18
- return null;
19
- }
20
- return list.length;
21
- } catch {
22
- return null;
23
- }
18
+ try {
19
+ const parsed = JSON.parse(text);
20
+ const list = Array.isArray(parsed?.collections) ? parsed.collections : Array.isArray(parsed) ? parsed : null;
21
+ if (!list?.length || !list.every((c) => c && typeof c === "object" && Array.isArray(c.items))) return null;
22
+ return list.length;
23
+ } catch {
24
+ return null;
25
+ }
24
26
  }
25
27
  function isEditableTarget(el) {
26
- if (!el) {
27
- return false;
28
- }
29
- return el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.isContentEditable || Boolean(el.closest(".monaco-editor"));
28
+ if (!el) return false;
29
+ return el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.isContentEditable || Boolean(el.closest(".monaco-editor"));
30
30
  }
31
+ /** Derive a human-readable label from the parsed incoming collections. */
31
32
  function deriveSourceLabel(incoming) {
32
- if (incoming.length === 1 && incoming[0]) {
33
- return incoming[0].name;
34
- }
35
- return `${incoming.length} collections`;
33
+ if (incoming.length === 1 && incoming[0]) return incoming[0].name;
34
+ return `${incoming.length} collections`;
36
35
  }
37
- const CollectionsPanel = ({
38
- readOnly = false,
39
- allowImportExport = true,
40
- allowReplace = true
41
- }) => {
42
- const allowImport = !readOnly && allowImportExport;
43
- const actions = useCollectionsStore((s) => s.actions);
44
- const collections = useCollectionsStore((s) => s.collections);
45
- const loaded = useCollectionsStore((s) => s.loaded);
46
- const [openDialog, setOpenDialog] = useState(
47
- null
48
- );
49
- const [isDragOver, setIsDragOver] = useState(false);
50
- const [status, setStatus] = useState(
51
- null
52
- );
53
- const [pendingConflict, setPendingConflict] = useState(null);
54
- const [pendingReplace, setPendingReplace] = useState(null);
55
- const [collapsed, setCollapsed] = useState(/* @__PURE__ */ new Set());
56
- const [grabbed, setGrabbed] = useState(null);
57
- const [origin, setOrigin] = useState(null);
58
- const [liveMessage, setLiveMessage] = useState("");
59
- const listRef = useRef(null);
60
- const cancelledItemRef = useRef(null);
61
- useEffect(() => {
62
- if (!status) {
63
- return;
64
- }
65
- const timer = setTimeout(() => setStatus(null), 4e3);
66
- return () => clearTimeout(timer);
67
- }, [status]);
68
- const applyReplace = (text) => {
69
- const analysis = collectionsStore.getState().actions.analyzeImport(text);
70
- collectionsStore.getState().actions.applyImport(analysis, { mode: "replace" });
71
- };
72
- const runImport = (text, mode) => {
73
- if (mode === "replace") {
74
- setPendingReplace(text);
75
- return;
76
- }
77
- const count = parseCollectionsExport(text);
78
- if (count === null) {
79
- setStatus({
80
- ok: false,
81
- message: "That file isn't a collections export."
82
- });
83
- return;
84
- }
85
- const analysis = collectionsStore.getState().actions.analyzeImport(text);
86
- if (!analysis.ok) {
87
- setStatus({ ok: false, message: "Invalid collections export." });
88
- return;
89
- }
90
- if (analysis.changedItems.length === 0) {
91
- collectionsStore.getState().actions.applyImport(analysis, { mode: "merge", applyChanges: true });
92
- return;
93
- }
94
- setPendingConflict({
95
- analysis,
96
- sourceLabel: deriveSourceLabel(analysis._incoming)
97
- });
98
- };
99
- const handleConflictResolve = (resolution) => {
100
- if (!pendingConflict) {
101
- return;
102
- }
103
- const { analysis } = pendingConflict;
104
- collectionsStore.getState().actions.applyImport(analysis, resolution);
105
- setPendingConflict(null);
106
- };
107
- const importDroppedFile = async (file) => {
108
- const text = await file.text();
109
- if (parseCollectionsExport(text) === null) {
110
- setStatus({
111
- ok: false,
112
- message: "That file isn't a collections export."
113
- });
114
- return;
115
- }
116
- runImport(text, "merge");
117
- };
118
- const allowImportRef = useRef(allowImport);
119
- allowImportRef.current = allowImport;
120
- useEffect(() => {
121
- const onPaste = (event) => {
122
- var _a;
123
- if (!allowImportRef.current) {
124
- return;
125
- }
126
- if (isEditableTarget(document.activeElement)) {
127
- return;
128
- }
129
- const text = ((_a = event.clipboardData) == null ? void 0 : _a.getData("text")) ?? "";
130
- if (parseCollectionsExport(text) === null) {
131
- return;
132
- }
133
- event.preventDefault();
134
- runImport(text, "merge");
135
- };
136
- document.addEventListener("paste", onPaste);
137
- return () => document.removeEventListener("paste", onPaste);
138
- }, []);
139
- const { addTab, changeTab, updateActiveTabValues } = useGraphiQLActions();
140
- const { queryEditor, variableEditor, headerEditor } = useGraphiQL(
141
- pick("queryEditor", "variableEditor", "headerEditor")
142
- );
143
- const tabs = useGraphiQL((s) => s.tabs);
144
- const activeTabId = useGraphiQL((s) => {
145
- var _a;
146
- return (_a = s.tabs[s.activeTabIndex]) == null ? void 0 : _a.id;
147
- });
148
- const pendingLink = useRef(
149
- null
150
- );
151
- useEffect(() => {
152
- const link = pendingLink.current;
153
- if (link && activeTabId) {
154
- actions.linkTab(activeTabId, link.collectionId, link.itemId);
155
- pendingLink.current = null;
156
- }
157
- }, [activeTabId, actions]);
158
- const handleOpen = (item) => {
159
- var _a;
160
- const { links } = collectionsStore.getState();
161
- const openIndex = tabs.findIndex(
162
- (t) => {
163
- var _a2;
164
- return t.id && ((_a2 = links[t.id]) == null ? void 0 : _a2.itemId) === item.id;
165
- }
166
- );
167
- if (openIndex !== -1) {
168
- changeTab(openIndex);
169
- return;
170
- }
171
- const collectionId = (_a = collections.find(
172
- (c) => c.items.some((i) => i.id === item.id)
173
- )) == null ? void 0 : _a.id;
174
- if (collectionId) {
175
- pendingLink.current = { collectionId, itemId: item.id };
176
- }
177
- addTab();
178
- queryEditor == null ? void 0 : queryEditor.setValue(item.query ?? "");
179
- variableEditor == null ? void 0 : variableEditor.setValue(item.variables ?? "");
180
- headerEditor == null ? void 0 : headerEditor.setValue(item.headers ?? "");
181
- updateActiveTabValues({
182
- query: item.query,
183
- variables: item.variables ?? "",
184
- headers: item.headers ?? "",
185
- lastSavedQuery: item.query ?? null
186
- });
187
- };
188
- const announce = (message) => setLiveMessage(message);
189
- const handleShareCollection = async (collectionId) => {
190
- const json = collectionsStore.getState().actions.exportCollection(collectionId);
191
- await navigator.clipboard.writeText(json);
192
- };
193
- const handleShareItem = async (itemId) => {
194
- const json = collectionsStore.getState().actions.exportItem(itemId);
195
- await navigator.clipboard.writeText(json);
196
- };
197
- const handleRename = actions.renameCollection;
198
- const handleDelete = actions.deleteCollection;
199
- const handleDeleteItem = actions.deleteItem;
200
- const handleMoveItem = actions.moveItem;
201
- const handleAddItem = actions.addItem;
202
- const handleRenameItem = (collectionId, itemId, updates) => {
203
- collectionsStore.getState().actions.updateItem(collectionId, itemId, {
204
- name: updates.name,
205
- description: updates.description || void 0
206
- });
207
- };
208
- const toggleExpand = (id) => setCollapsed((prev) => {
209
- const next = new Set(prev);
210
- if (next.has(id)) {
211
- next.delete(id);
212
- } else {
213
- next.add(id);
214
- }
215
- return next;
216
- });
217
- const expand = (id) => setCollapsed((prev) => {
218
- if (!prev.has(id)) {
219
- return prev;
220
- }
221
- const next = new Set(prev);
222
- next.delete(id);
223
- return next;
224
- });
225
- const handleGrabToggle = (collectionId, index, itemId) => {
226
- var _a;
227
- if (grabbed) {
228
- const cols = collectionsStore.getState().collections;
229
- const col = cols.find((c) => c.id === grabbed.collectionId);
230
- const droppedItem = col == null ? void 0 : col.items[grabbed.index];
231
- setLiveMessage(
232
- `Dropped ${(droppedItem == null ? void 0 : droppedItem.name) ?? grabbed.itemId} in ${(col == null ? void 0 : col.name) ?? ""} at position ${grabbed.index + 1}.`
233
- );
234
- setGrabbed(null);
235
- setOrigin(null);
236
- return;
237
- }
238
- setGrabbed({ collectionId, index, itemId });
239
- setOrigin({ collectionId, index });
240
- const item = (_a = collectionsStore.getState().collections.find((c) => c.id === collectionId)) == null ? void 0 : _a.items[index];
241
- setLiveMessage(
242
- `Grabbed ${(item == null ? void 0 : item.name) ?? itemId}. Use arrow keys to move, space to drop, escape to cancel.`
243
- );
244
- };
245
- const handleGrabMove = (direction) => {
246
- if (!grabbed) {
247
- return;
248
- }
249
- const cols = collectionsStore.getState().collections;
250
- const cIdx = cols.findIndex((c) => c.id === grabbed.collectionId);
251
- const col = cols[cIdx];
252
- if (!col) {
253
- return;
254
- }
255
- const { moveItem } = collectionsStore.getState().actions;
256
- if (direction === "down") {
257
- if (grabbed.index < col.items.length - 1) {
258
- moveItem(col.id, grabbed.index, col.id, grabbed.index + 1);
259
- const next = { ...grabbed, index: grabbed.index + 1 };
260
- setGrabbed(next);
261
- setLiveMessage(
262
- `Position ${next.index + 1} of ${col.items.length} in ${col.name}.`
263
- );
264
- } else if (cIdx < cols.length - 1) {
265
- const dest = cols[cIdx + 1];
266
- expand(dest.id);
267
- moveItem(col.id, grabbed.index, dest.id, 0);
268
- setGrabbed({ collectionId: dest.id, index: 0, itemId: grabbed.itemId });
269
- setLiveMessage(
270
- `Moved to ${dest.name}, position 1 of ${dest.items.length + 1}.`
271
- );
272
- } else {
273
- setLiveMessage("Already at the end.");
274
- }
275
- } else if (grabbed.index > 0) {
276
- moveItem(col.id, grabbed.index, col.id, grabbed.index - 1);
277
- const next = { ...grabbed, index: grabbed.index - 1 };
278
- setGrabbed(next);
279
- setLiveMessage(
280
- `Position ${next.index + 1} of ${col.items.length} in ${col.name}.`
281
- );
282
- } else if (cIdx > 0) {
283
- const dest = cols[cIdx - 1];
284
- expand(dest.id);
285
- const toIndex = dest.items.length;
286
- moveItem(col.id, grabbed.index, dest.id, toIndex);
287
- setGrabbed({
288
- collectionId: dest.id,
289
- index: toIndex,
290
- itemId: grabbed.itemId
291
- });
292
- setLiveMessage(
293
- `Moved to ${dest.name}, position ${toIndex + 1} of ${dest.items.length + 1}.`
294
- );
295
- } else {
296
- setLiveMessage("Already at the start.");
297
- }
298
- };
299
- const handleGrabCancel = () => {
300
- if (!grabbed || !origin) {
301
- return;
302
- }
303
- collectionsStore.getState().actions.moveItem(
304
- grabbed.collectionId,
305
- grabbed.index,
306
- origin.collectionId,
307
- origin.index
308
- );
309
- cancelledItemRef.current = grabbed.itemId;
310
- setGrabbed(null);
311
- setOrigin(null);
312
- setLiveMessage("Move cancelled.");
313
- };
314
- useEffect(() => {
315
- var _a, _b, _c, _d;
316
- if (grabbed) {
317
- (_b = (_a = listRef.current) == null ? void 0 : _a.querySelector(
318
- `[data-collection-drag-handle="${grabbed.itemId}"]`
319
- )) == null ? void 0 : _b.focus();
320
- } else if (cancelledItemRef.current) {
321
- (_d = (_c = listRef.current) == null ? void 0 : _c.querySelector(
322
- `[data-collection-drag-handle="${cancelledItemRef.current}"]`
323
- )) == null ? void 0 : _d.focus();
324
- cancelledItemRef.current = null;
325
- }
326
- }, [grabbed]);
327
- return /* @__PURE__ */ jsxs(
328
- "div",
329
- {
330
- className: `graphiql-collections-panel${isDragOver ? " graphiql-collections-drop-active" : ""}`,
331
- onDragOver: (e) => {
332
- if (!allowImport || !e.dataTransfer.types.includes("Files")) {
333
- return;
334
- }
335
- e.preventDefault();
336
- setIsDragOver(true);
337
- },
338
- onDragLeave: (e) => {
339
- if (e.currentTarget.contains(e.relatedTarget)) {
340
- return;
341
- }
342
- setIsDragOver(false);
343
- },
344
- onDrop: (e) => {
345
- if (!allowImport) {
346
- return;
347
- }
348
- const file = e.dataTransfer.files[0];
349
- if (!file) {
350
- return;
351
- }
352
- e.preventDefault();
353
- setIsDragOver(false);
354
- void importDroppedFile(file);
355
- },
356
- children: [
357
- /* @__PURE__ */ jsx(
358
- PanelHeader,
359
- {
360
- title: "Operation Collections",
361
- subtitle: "Save and organize operations into collections."
362
- }
363
- ),
364
- (!readOnly || allowImportExport) && /* @__PURE__ */ jsxs("div", { className: "graphiql-collections-toolbar", children: [
365
- /* @__PURE__ */ jsx("div", { className: "graphiql-collections-toolbar-left", children: !readOnly && /* @__PURE__ */ jsx(
366
- "button",
367
- {
368
- type: "button",
369
- className: "graphiql-collections-action",
370
- onClick: () => collectionsStore.getState().actions.createCollection("New Collection"),
371
- "aria-label": "New collection",
372
- title: "New collection",
373
- children: "+ New Collection"
374
- }
375
- ) }),
376
- /* @__PURE__ */ jsxs("div", { className: "graphiql-collections-toolbar-right", children: [
377
- allowImport && /* @__PURE__ */ jsx(
378
- "button",
379
- {
380
- type: "button",
381
- className: "graphiql-collections-action",
382
- onClick: () => setOpenDialog("import"),
383
- "aria-label": "Import collections",
384
- title: "Import collections",
385
- children: /* @__PURE__ */ jsx(SvgDownload, {})
386
- }
387
- ),
388
- allowImportExport && /* @__PURE__ */ jsx(
389
- "button",
390
- {
391
- type: "button",
392
- className: "graphiql-collections-action",
393
- onClick: () => setOpenDialog("export"),
394
- "aria-label": "Export collections",
395
- title: "Export collections",
396
- children: /* @__PURE__ */ jsx(SvgUpload, {})
397
- }
398
- )
399
- ] })
400
- ] }),
401
- status && /* @__PURE__ */ jsx(
402
- "div",
403
- {
404
- className: `graphiql-collections-status${status.ok ? "" : " graphiql-collections-status-error"}`,
405
- role: "status",
406
- children: status.message
407
- }
408
- ),
409
- !loaded && /* @__PURE__ */ jsx("div", { className: "graphiql-collections-loading", children: "Loading…" }),
410
- loaded && collections.length === 0 && /* @__PURE__ */ jsx("div", { className: "graphiql-collections-empty", children: readOnly ? "No collections." : allowImport ? "No collections yet. Save an operation to get started, or drop or paste a collections export to import one." : "No collections yet. Save an operation to get started." }),
411
- /* @__PURE__ */ jsx(
412
- "div",
413
- {
414
- className: "graphiql-sr-only",
415
- "aria-live": "assertive",
416
- "aria-atomic": "true",
417
- children: liveMessage
418
- }
419
- ),
420
- /* @__PURE__ */ jsx("div", { className: "graphiql-collections-list", ref: listRef, children: collections.map((collection) => /* @__PURE__ */ jsx(
421
- CollectionRow,
422
- {
423
- collection,
424
- readOnly,
425
- expanded: !collapsed.has(collection.id),
426
- onToggleExpand: () => toggleExpand(collection.id),
427
- grabbed,
428
- onGrabToggle: handleGrabToggle,
429
- onGrabMove: handleGrabMove,
430
- onGrabCancel: handleGrabCancel,
431
- onRename: handleRename,
432
- onDelete: handleDelete,
433
- onShareCollection: handleShareCollection,
434
- onOpenItem: handleOpen,
435
- onShare: handleShareItem,
436
- onAnnounce: announce,
437
- onDeleteItem: handleDeleteItem,
438
- onMoveItem: handleMoveItem,
439
- onAddItem: handleAddItem,
440
- onRenameItem: handleRenameItem
441
- },
442
- collection.id
443
- )) }),
444
- allowImportExport && /* @__PURE__ */ jsx(
445
- ExportDialog,
446
- {
447
- open: openDialog === "export",
448
- onClose: () => setOpenDialog(null)
449
- }
450
- ),
451
- allowImport && /* @__PURE__ */ jsx(
452
- ImportDialog,
453
- {
454
- open: openDialog === "import",
455
- onClose: () => setOpenDialog(null),
456
- onImport: runImport,
457
- allowReplace
458
- }
459
- ),
460
- pendingConflict && /* @__PURE__ */ jsx(
461
- ConflictDialog,
462
- {
463
- analysis: pendingConflict.analysis,
464
- sourceLabel: pendingConflict.sourceLabel,
465
- open: true,
466
- onClose: () => setPendingConflict(null),
467
- onResolve: handleConflictResolve
468
- }
469
- ),
470
- /* @__PURE__ */ jsxs(
471
- Dialog,
472
- {
473
- open: pendingReplace !== null,
474
- onOpenChange: (o) => !o && setPendingReplace(null),
475
- children: [
476
- /* @__PURE__ */ jsx(Dialog.Header, { children: "Replace all collections?" }),
477
- /* @__PURE__ */ jsxs(Dialog.Body, { children: [
478
- /* @__PURE__ */ jsx("p", { className: "graphiql-conflict-summary", children: "This will remove all existing collections and replace them with the imported data. This cannot be undone." }),
479
- /* @__PURE__ */ jsxs("div", { className: "graphiql-conflict-actions", children: [
480
- /* @__PURE__ */ jsx(
481
- Button,
482
- {
483
- type: "button",
484
- variant: "primary",
485
- onClick: () => {
486
- const text = pendingReplace;
487
- setPendingReplace(null);
488
- if (text !== null) {
489
- applyReplace(text);
490
- }
491
- },
492
- children: "Replace"
493
- }
494
- ),
495
- /* @__PURE__ */ jsx(Button, { type: "button", onClick: () => setPendingReplace(null), children: "Cancel" })
496
- ] })
497
- ] })
498
- ]
499
- }
500
- )
501
- ]
502
- }
503
- );
504
- };
505
- export {
506
- CollectionsPanel
36
+ var CollectionsPanel = ({ readOnly = false, allowImportExport = true, allowReplace = true }) => {
37
+ const allowImport = !readOnly && allowImportExport;
38
+ const actions = useCollectionsStore((s) => s.actions);
39
+ const collections = useCollectionsStore((s) => s.collections);
40
+ const loaded = useCollectionsStore((s) => s.loaded);
41
+ const [openDialog, setOpenDialog] = useState(null);
42
+ const [isDragOver, setIsDragOver] = useState(false);
43
+ const [status, setStatus] = useState(null);
44
+ const [pendingConflict, setPendingConflict] = useState(null);
45
+ const [pendingReplace, setPendingReplace] = useState(null);
46
+ const [collapsed, setCollapsed] = useState(/* @__PURE__ */ new Set());
47
+ const [grabbed, setGrabbed] = useState(null);
48
+ const [origin, setOrigin] = useState(null);
49
+ const [liveMessage, setLiveMessage] = useState("");
50
+ const listRef = useRef(null);
51
+ const cancelledItemRef = useRef(null);
52
+ useEffect(() => {
53
+ if (!status) return;
54
+ const timer = setTimeout(() => setStatus(null), 4e3);
55
+ return () => clearTimeout(timer);
56
+ }, [status]);
57
+ const applyReplace = (text) => {
58
+ const analysis = collectionsStore.getState().actions.analyzeImport(text);
59
+ collectionsStore.getState().actions.applyImport(analysis, { mode: "replace" });
60
+ };
61
+ const runImport = (text, mode) => {
62
+ if (mode === "replace") {
63
+ setPendingReplace(text);
64
+ return;
65
+ }
66
+ if (parseCollectionsExport(text) === null) {
67
+ setStatus({
68
+ ok: false,
69
+ message: "That file isn't a collections export."
70
+ });
71
+ return;
72
+ }
73
+ const analysis = collectionsStore.getState().actions.analyzeImport(text);
74
+ if (!analysis.ok) {
75
+ setStatus({
76
+ ok: false,
77
+ message: "Invalid collections export."
78
+ });
79
+ return;
80
+ }
81
+ if (analysis.changedItems.length === 0) {
82
+ collectionsStore.getState().actions.applyImport(analysis, {
83
+ mode: "merge",
84
+ applyChanges: true
85
+ });
86
+ return;
87
+ }
88
+ setPendingConflict({
89
+ analysis,
90
+ sourceLabel: deriveSourceLabel(analysis._incoming)
91
+ });
92
+ };
93
+ const handleConflictResolve = (resolution) => {
94
+ if (!pendingConflict) return;
95
+ const { analysis } = pendingConflict;
96
+ collectionsStore.getState().actions.applyImport(analysis, resolution);
97
+ setPendingConflict(null);
98
+ };
99
+ const importDroppedFile = async (file) => {
100
+ const text = await file.text();
101
+ if (parseCollectionsExport(text) === null) {
102
+ setStatus({
103
+ ok: false,
104
+ message: "That file isn't a collections export."
105
+ });
106
+ return;
107
+ }
108
+ runImport(text, "merge");
109
+ };
110
+ const allowImportRef = useRef(allowImport);
111
+ allowImportRef.current = allowImport;
112
+ useEffect(() => {
113
+ const onPaste = (event) => {
114
+ if (!allowImportRef.current) return;
115
+ if (isEditableTarget(document.activeElement)) return;
116
+ const text = event.clipboardData?.getData("text") ?? "";
117
+ if (parseCollectionsExport(text) === null) return;
118
+ event.preventDefault();
119
+ runImport(text, "merge");
120
+ };
121
+ document.addEventListener("paste", onPaste);
122
+ return () => document.removeEventListener("paste", onPaste);
123
+ }, []);
124
+ const { addTab, changeTab, updateActiveTabValues } = useGraphiQLActions();
125
+ const { queryEditor, variableEditor, headerEditor } = useGraphiQL(pick("queryEditor", "variableEditor", "headerEditor"));
126
+ const tabs = useGraphiQL((s) => s.tabs);
127
+ const activeTabId = useGraphiQL((s) => s.tabs[s.activeTabIndex]?.id);
128
+ const pendingLink = useRef(null);
129
+ useEffect(() => {
130
+ const link = pendingLink.current;
131
+ if (link && activeTabId) {
132
+ actions.linkTab(activeTabId, link.collectionId, link.itemId);
133
+ pendingLink.current = null;
134
+ }
135
+ }, [activeTabId, actions]);
136
+ const handleOpen = (item) => {
137
+ const { links } = collectionsStore.getState();
138
+ const openIndex = tabs.findIndex((t) => t.id && links[t.id]?.itemId === item.id);
139
+ if (openIndex !== -1) {
140
+ changeTab(openIndex);
141
+ return;
142
+ }
143
+ const collectionId = collections.find((c) => c.items.some((i) => i.id === item.id))?.id;
144
+ if (collectionId) pendingLink.current = {
145
+ collectionId,
146
+ itemId: item.id
147
+ };
148
+ addTab();
149
+ queryEditor?.setValue(item.query ?? "");
150
+ variableEditor?.setValue(item.variables ?? "");
151
+ headerEditor?.setValue(item.headers ?? "");
152
+ updateActiveTabValues({
153
+ query: item.query,
154
+ variables: item.variables ?? "",
155
+ headers: item.headers ?? "",
156
+ lastSavedQuery: item.query ?? null
157
+ });
158
+ };
159
+ const announce = (message) => setLiveMessage(message);
160
+ const handleShareCollection = async (collectionId) => {
161
+ const json = collectionsStore.getState().actions.exportCollection(collectionId);
162
+ await navigator.clipboard.writeText(json);
163
+ };
164
+ const handleShareItem = async (itemId) => {
165
+ const json = collectionsStore.getState().actions.exportItem(itemId);
166
+ await navigator.clipboard.writeText(json);
167
+ };
168
+ const handleRename = actions.renameCollection;
169
+ const handleDelete = actions.deleteCollection;
170
+ const handleDeleteItem = actions.deleteItem;
171
+ const handleMoveItem = actions.moveItem;
172
+ const handleAddItem = actions.addItem;
173
+ const handleRenameItem = (collectionId, itemId, updates) => {
174
+ collectionsStore.getState().actions.updateItem(collectionId, itemId, {
175
+ name: updates.name,
176
+ description: updates.description || void 0
177
+ });
178
+ };
179
+ const toggleExpand = (id) => setCollapsed((prev) => {
180
+ const next = new Set(prev);
181
+ if (next.has(id)) next.delete(id);
182
+ else next.add(id);
183
+ return next;
184
+ });
185
+ const expand = (id) => setCollapsed((prev) => {
186
+ if (!prev.has(id)) return prev;
187
+ const next = new Set(prev);
188
+ next.delete(id);
189
+ return next;
190
+ });
191
+ const handleGrabToggle = (collectionId, index, itemId) => {
192
+ if (grabbed) {
193
+ const col = collectionsStore.getState().collections.find((c) => c.id === grabbed.collectionId);
194
+ const droppedItem = col?.items[grabbed.index];
195
+ setLiveMessage(`Dropped ${droppedItem?.name ?? grabbed.itemId} in ${col?.name ?? ""} at position ${grabbed.index + 1}.`);
196
+ setGrabbed(null);
197
+ setOrigin(null);
198
+ return;
199
+ }
200
+ setGrabbed({
201
+ collectionId,
202
+ index,
203
+ itemId
204
+ });
205
+ setOrigin({
206
+ collectionId,
207
+ index
208
+ });
209
+ const item = collectionsStore.getState().collections.find((c) => c.id === collectionId)?.items[index];
210
+ setLiveMessage(`Grabbed ${item?.name ?? itemId}. Use arrow keys to move, space to drop, escape to cancel.`);
211
+ };
212
+ const handleGrabMove = (direction) => {
213
+ if (!grabbed) return;
214
+ const cols = collectionsStore.getState().collections;
215
+ const cIdx = cols.findIndex((c) => c.id === grabbed.collectionId);
216
+ const col = cols[cIdx];
217
+ if (!col) return;
218
+ const { moveItem } = collectionsStore.getState().actions;
219
+ if (direction === "down") {
220
+ if (grabbed.index < col.items.length - 1) {
221
+ moveItem(col.id, grabbed.index, col.id, grabbed.index + 1);
222
+ const next = {
223
+ ...grabbed,
224
+ index: grabbed.index + 1
225
+ };
226
+ setGrabbed(next);
227
+ setLiveMessage(`Position ${next.index + 1} of ${col.items.length} in ${col.name}.`);
228
+ } else if (cIdx < cols.length - 1) {
229
+ const dest = cols[cIdx + 1];
230
+ expand(dest.id);
231
+ moveItem(col.id, grabbed.index, dest.id, 0);
232
+ setGrabbed({
233
+ collectionId: dest.id,
234
+ index: 0,
235
+ itemId: grabbed.itemId
236
+ });
237
+ setLiveMessage(`Moved to ${dest.name}, position 1 of ${dest.items.length + 1}.`);
238
+ } else setLiveMessage("Already at the end.");
239
+ } else if (grabbed.index > 0) {
240
+ moveItem(col.id, grabbed.index, col.id, grabbed.index - 1);
241
+ const next = {
242
+ ...grabbed,
243
+ index: grabbed.index - 1
244
+ };
245
+ setGrabbed(next);
246
+ setLiveMessage(`Position ${next.index + 1} of ${col.items.length} in ${col.name}.`);
247
+ } else if (cIdx > 0) {
248
+ const dest = cols[cIdx - 1];
249
+ expand(dest.id);
250
+ const toIndex = dest.items.length;
251
+ moveItem(col.id, grabbed.index, dest.id, toIndex);
252
+ setGrabbed({
253
+ collectionId: dest.id,
254
+ index: toIndex,
255
+ itemId: grabbed.itemId
256
+ });
257
+ setLiveMessage(`Moved to ${dest.name}, position ${toIndex + 1} of ${dest.items.length + 1}.`);
258
+ } else setLiveMessage("Already at the start.");
259
+ };
260
+ const handleGrabCancel = () => {
261
+ if (!grabbed || !origin) return;
262
+ collectionsStore.getState().actions.moveItem(grabbed.collectionId, grabbed.index, origin.collectionId, origin.index);
263
+ cancelledItemRef.current = grabbed.itemId;
264
+ setGrabbed(null);
265
+ setOrigin(null);
266
+ setLiveMessage("Move cancelled.");
267
+ };
268
+ useEffect(() => {
269
+ if (grabbed) listRef.current?.querySelector(`[data-collection-drag-handle="${grabbed.itemId}"]`)?.focus();
270
+ else if (cancelledItemRef.current) {
271
+ listRef.current?.querySelector(`[data-collection-drag-handle="${cancelledItemRef.current}"]`)?.focus();
272
+ cancelledItemRef.current = null;
273
+ }
274
+ }, [grabbed]);
275
+ return /* @__PURE__ */ jsxs("div", {
276
+ className: `graphiql-collections-panel${isDragOver ? " graphiql-collections-drop-active" : ""}`,
277
+ onDragOver: (e) => {
278
+ if (!allowImport || !e.dataTransfer.types.includes("Files")) return;
279
+ e.preventDefault();
280
+ setIsDragOver(true);
281
+ },
282
+ onDragLeave: (e) => {
283
+ if (e.currentTarget.contains(e.relatedTarget)) return;
284
+ setIsDragOver(false);
285
+ },
286
+ onDrop: (e) => {
287
+ if (!allowImport) return;
288
+ const file = e.dataTransfer.files[0];
289
+ if (!file) return;
290
+ e.preventDefault();
291
+ setIsDragOver(false);
292
+ importDroppedFile(file);
293
+ },
294
+ children: [
295
+ /* @__PURE__ */ jsx(PanelHeader, {
296
+ title: "Operation Collections",
297
+ subtitle: "Save and organize operations into collections."
298
+ }),
299
+ (!readOnly || allowImportExport) && /* @__PURE__ */ jsxs("div", {
300
+ className: "graphiql-collections-toolbar",
301
+ children: [/* @__PURE__ */ jsx("div", {
302
+ className: "graphiql-collections-toolbar-left",
303
+ children: !readOnly && /* @__PURE__ */ jsx("button", {
304
+ type: "button",
305
+ className: "graphiql-collections-action",
306
+ onClick: () => collectionsStore.getState().actions.createCollection("New Collection"),
307
+ "aria-label": "New collection",
308
+ title: "New collection",
309
+ children: "+ New Collection"
310
+ })
311
+ }), /* @__PURE__ */ jsxs("div", {
312
+ className: "graphiql-collections-toolbar-right",
313
+ children: [allowImport && /* @__PURE__ */ jsx("button", {
314
+ type: "button",
315
+ className: "graphiql-collections-action",
316
+ onClick: () => setOpenDialog("import"),
317
+ "aria-label": "Import collections",
318
+ title: "Import collections",
319
+ children: /* @__PURE__ */ jsx(SvgDownload, {})
320
+ }), allowImportExport && /* @__PURE__ */ jsx("button", {
321
+ type: "button",
322
+ className: "graphiql-collections-action",
323
+ onClick: () => setOpenDialog("export"),
324
+ "aria-label": "Export collections",
325
+ title: "Export collections",
326
+ children: /* @__PURE__ */ jsx(SvgUpload, {})
327
+ })]
328
+ })]
329
+ }),
330
+ status && /* @__PURE__ */ jsx("div", {
331
+ className: `graphiql-collections-status${status.ok ? "" : " graphiql-collections-status-error"}`,
332
+ role: "status",
333
+ children: status.message
334
+ }),
335
+ !loaded && /* @__PURE__ */ jsx("div", {
336
+ className: "graphiql-collections-loading",
337
+ children: "Loading…"
338
+ }),
339
+ loaded && collections.length === 0 && /* @__PURE__ */ jsx("div", {
340
+ className: "graphiql-collections-empty",
341
+ children: readOnly ? "No collections." : allowImport ? "No collections yet. Save an operation to get started, or drop or paste a collections export to import one." : "No collections yet. Save an operation to get started."
342
+ }),
343
+ /* @__PURE__ */ jsx("div", {
344
+ className: "graphiql-sr-only",
345
+ "aria-live": "assertive",
346
+ "aria-atomic": "true",
347
+ children: liveMessage
348
+ }),
349
+ /* @__PURE__ */ jsx("div", {
350
+ className: "graphiql-collections-list",
351
+ ref: listRef,
352
+ children: collections.map((collection) => /* @__PURE__ */ jsx(CollectionRow, {
353
+ collection,
354
+ readOnly,
355
+ expanded: !collapsed.has(collection.id),
356
+ onToggleExpand: () => toggleExpand(collection.id),
357
+ grabbed,
358
+ onGrabToggle: handleGrabToggle,
359
+ onGrabMove: handleGrabMove,
360
+ onGrabCancel: handleGrabCancel,
361
+ onRename: handleRename,
362
+ onDelete: handleDelete,
363
+ onShareCollection: handleShareCollection,
364
+ onOpenItem: handleOpen,
365
+ onShare: handleShareItem,
366
+ onAnnounce: announce,
367
+ onDeleteItem: handleDeleteItem,
368
+ onMoveItem: handleMoveItem,
369
+ onAddItem: handleAddItem,
370
+ onRenameItem: handleRenameItem
371
+ }, collection.id))
372
+ }),
373
+ allowImportExport && /* @__PURE__ */ jsx(ExportDialog, {
374
+ open: openDialog === "export",
375
+ onClose: () => setOpenDialog(null)
376
+ }),
377
+ allowImport && /* @__PURE__ */ jsx(ImportDialog, {
378
+ open: openDialog === "import",
379
+ onClose: () => setOpenDialog(null),
380
+ onImport: runImport,
381
+ allowReplace
382
+ }),
383
+ pendingConflict && /* @__PURE__ */ jsx(ConflictDialog, {
384
+ analysis: pendingConflict.analysis,
385
+ sourceLabel: pendingConflict.sourceLabel,
386
+ open: true,
387
+ onClose: () => setPendingConflict(null),
388
+ onResolve: handleConflictResolve
389
+ }),
390
+ /* @__PURE__ */ jsxs(Dialog, {
391
+ open: pendingReplace !== null,
392
+ onOpenChange: (o) => !o && setPendingReplace(null),
393
+ children: [/* @__PURE__ */ jsx(Dialog.Header, { children: "Replace all collections?" }), /* @__PURE__ */ jsxs(Dialog.Body, { children: [/* @__PURE__ */ jsx("p", {
394
+ className: "graphiql-conflict-summary",
395
+ children: "This will remove all existing collections and replace them with the imported data. This cannot be undone."
396
+ }), /* @__PURE__ */ jsxs("div", {
397
+ className: "graphiql-conflict-actions",
398
+ children: [/* @__PURE__ */ jsx(Button, {
399
+ type: "button",
400
+ variant: "primary",
401
+ onClick: () => {
402
+ const text = pendingReplace;
403
+ setPendingReplace(null);
404
+ if (text !== null) applyReplace(text);
405
+ },
406
+ children: "Replace"
407
+ }), /* @__PURE__ */ jsx(Button, {
408
+ type: "button",
409
+ onClick: () => setPendingReplace(null),
410
+ children: "Cancel"
411
+ })]
412
+ })] })]
413
+ })
414
+ ]
415
+ });
507
416
  };
508
- //# sourceMappingURL=collections-panel.js.map
417
+ //#endregion
418
+ export { CollectionsPanel };
419
+
420
+ //# sourceMappingURL=collections-panel.js.map