@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
package/dist/store.js CHANGED
@@ -1,413 +1,376 @@
1
- import { createStore } from "zustand";
2
- import { createBoundedUseStore } from "@graphiql/react";
3
- import { z } from "zod";
4
1
  import { randomId } from "./utility/uuid.js";
5
2
  import { DEFAULT_COLLECTIONS_CONFIG } from "./types.js";
6
3
  import { localStorageAdapter } from "./storage/local-storage.js";
7
- import { saveTabLinks, loadTabLinks } from "./storage/tab-links.js";
8
- const INITIAL_SAVE_DIALOG = {
9
- open: false,
10
- query: "",
11
- variables: "",
12
- headers: "",
13
- name: ""
4
+ import { loadTabLinks, saveTabLinks } from "./storage/tab-links.js";
5
+ import { createBoundedUseStore } from "@graphiql/react";
6
+ import { createStore } from "zustand";
7
+ import { z } from "zod";
8
+ //#region src/store.ts
9
+ var INITIAL_SAVE_DIALOG = {
10
+ open: false,
11
+ query: "",
12
+ variables: "",
13
+ headers: "",
14
+ name: ""
14
15
  };
15
16
  function deriveOperationName(query, fallback) {
16
- if (fallback) {
17
- return fallback;
18
- }
19
- const match = /(?:query|mutation|subscription)\s+([A-Za-z_][A-Za-z0-9_]*)/i.exec(query);
20
- return (match == null ? void 0 : match[1]) ?? "Unnamed operation";
17
+ if (fallback) return fallback;
18
+ return /(?:query|mutation|subscription)\s+([A-Za-z_][A-Za-z0-9_]*)/i.exec(query)?.[1] ?? "Unnamed operation";
21
19
  }
20
+ /** Drop every link that points at one of `itemIds`. */
22
21
  function pruneLinksForItems(links, itemIds) {
23
- const next = {};
24
- for (const [tabId, link] of Object.entries(links)) {
25
- if (!itemIds.has(link.itemId)) {
26
- next[tabId] = link;
27
- }
28
- }
29
- return next;
22
+ const next = {};
23
+ for (const [tabId, link] of Object.entries(links)) if (!itemIds.has(link.itemId)) next[tabId] = link;
24
+ return next;
30
25
  }
31
- const norm = (v) => v ?? "";
26
+ /** Normalize optional string fields so undefined and '' are treated identically. */
27
+ var norm = (v) => v ?? "";
28
+ /** Compare only the user-visible content fields of two items, ignoring id and timestamps. */
32
29
  function itemContentEqual(a, b) {
33
- return a.name === b.name && norm(a.query) === norm(b.query) && norm(a.variables) === norm(b.variables) && norm(a.headers) === norm(b.headers);
30
+ return a.name === b.name && norm(a.query) === norm(b.query) && norm(a.variables) === norm(b.variables) && norm(a.headers) === norm(b.headers);
34
31
  }
35
- const collectionsStore = createStore((set, get) => {
36
- const persist = async () => {
37
- await get().storage.save(get().collections);
38
- };
39
- return {
40
- collections: [],
41
- loaded: false,
42
- storage: localStorageAdapter,
43
- config: DEFAULT_COLLECTIONS_CONFIG,
44
- links: {},
45
- saveDialog: INITIAL_SAVE_DIALOG,
46
- actions: {
47
- async init(storage, config) {
48
- const collections = await storage.load();
49
- set({
50
- collections,
51
- loaded: true,
52
- storage,
53
- config: { ...DEFAULT_COLLECTIONS_CONFIG, ...config },
54
- // Restore tab↔item links so ⌘S on a tab reopened from the previous
55
- // session still updates its saved operation in place.
56
- links: loadTabLinks()
57
- });
58
- },
59
- setCollections(collections) {
60
- set({ collections });
61
- },
62
- async reload() {
63
- const collections = await get().storage.load();
64
- set({ collections });
65
- },
66
- createCollection(name, description) {
67
- const c = {
68
- id: randomId(),
69
- name,
70
- description,
71
- items: [],
72
- createdAt: Date.now(),
73
- updatedAt: Date.now()
74
- };
75
- set((s) => ({ collections: [...s.collections, c] }));
76
- void persist();
77
- return c;
78
- },
79
- deleteCollection(id) {
80
- const removed = get().collections.find((c) => c.id === id);
81
- const removedItemIds = new Set((removed == null ? void 0 : removed.items.map((i) => i.id)) ?? []);
82
- set((s) => ({
83
- collections: s.collections.filter((c) => c.id !== id),
84
- links: pruneLinksForItems(s.links, removedItemIds)
85
- }));
86
- void persist();
87
- saveTabLinks(get().links);
88
- },
89
- renameCollection(id, name) {
90
- set((s) => ({
91
- collections: s.collections.map(
92
- (c) => c.id === id ? { ...c, name, updatedAt: Date.now() } : c
93
- )
94
- }));
95
- void persist();
96
- },
97
- addItem(collectionId, item) {
98
- const newItem = {
99
- ...item,
100
- id: randomId(),
101
- createdAt: Date.now(),
102
- updatedAt: Date.now()
103
- };
104
- set((s) => ({
105
- collections: s.collections.map(
106
- (c) => c.id === collectionId ? { ...c, items: [...c.items, newItem], updatedAt: Date.now() } : c
107
- )
108
- }));
109
- void persist();
110
- return newItem;
111
- },
112
- updateItem(collectionId, itemId, updates) {
113
- set((s) => ({
114
- collections: s.collections.map(
115
- (c) => c.id === collectionId ? {
116
- ...c,
117
- updatedAt: Date.now(),
118
- items: c.items.map(
119
- (i) => i.id === itemId ? { ...i, ...updates, updatedAt: Date.now() } : i
120
- )
121
- } : c
122
- )
123
- }));
124
- void persist();
125
- },
126
- deleteItem(collectionId, itemId) {
127
- set((s) => ({
128
- collections: s.collections.map(
129
- (c) => c.id === collectionId ? {
130
- ...c,
131
- items: c.items.filter((i) => i.id !== itemId),
132
- updatedAt: Date.now()
133
- } : c
134
- ),
135
- links: pruneLinksForItems(s.links, /* @__PURE__ */ new Set([itemId]))
136
- }));
137
- void persist();
138
- saveTabLinks(get().links);
139
- },
140
- moveItem(fromCollectionId, fromIndex, toCollectionId, toIndex) {
141
- const { collections } = get();
142
- const fromCollection = collections.find((c) => c.id === fromCollectionId);
143
- if (!fromCollection || fromIndex < 0 || fromIndex >= fromCollection.items.length) {
144
- return;
145
- }
146
- const item = fromCollection.items[fromIndex];
147
- if (!item) {
148
- return;
149
- }
150
- const newCollections = collections.map((c) => {
151
- if (c.id === fromCollectionId && c.id === toCollectionId) {
152
- const items = [...c.items];
153
- items.splice(fromIndex, 1);
154
- items.splice(toIndex, 0, item);
155
- return { ...c, items, updatedAt: Date.now() };
156
- }
157
- if (c.id === fromCollectionId) {
158
- return {
159
- ...c,
160
- items: c.items.filter((_, i) => i !== fromIndex),
161
- updatedAt: Date.now()
162
- };
163
- }
164
- if (c.id === toCollectionId) {
165
- const items = [...c.items];
166
- items.splice(toIndex, 0, item);
167
- return { ...c, items, updatedAt: Date.now() };
168
- }
169
- return c;
170
- });
171
- set({ collections: newCollections });
172
- void persist();
173
- },
174
- analyzeImport(json) {
175
- const empty = {
176
- ok: false,
177
- newCollections: [],
178
- newItems: [],
179
- changedItems: [],
180
- unchangedCount: 0,
181
- _incoming: []
182
- };
183
- const CollectionItemSchema = z.object({
184
- id: z.string(),
185
- name: z.string(),
186
- query: z.string(),
187
- variables: z.string().optional(),
188
- headers: z.string().optional(),
189
- description: z.string().optional(),
190
- createdAt: z.number(),
191
- updatedAt: z.number()
192
- });
193
- const CollectionSchema = z.object({
194
- id: z.string(),
195
- name: z.string(),
196
- description: z.string().optional(),
197
- createdAt: z.number(),
198
- updatedAt: z.number(),
199
- items: z.array(CollectionItemSchema)
200
- });
201
- const EnvelopeSchema = z.union([
202
- z.object({
203
- version: z.number().optional(),
204
- collections: z.array(CollectionSchema)
205
- }),
206
- z.array(CollectionSchema)
207
- ]);
208
- let parsed;
209
- try {
210
- parsed = JSON.parse(json);
211
- } catch {
212
- return empty;
213
- }
214
- const result = EnvelopeSchema.safeParse(parsed);
215
- if (!result.success) {
216
- return empty;
217
- }
218
- const incoming = Array.isArray(result.data) ? result.data : result.data.collections;
219
- if (incoming.length === 0) {
220
- return empty;
221
- }
222
- const localCollections = get().collections;
223
- const localItemMap = /* @__PURE__ */ new Map();
224
- for (const col of localCollections) {
225
- for (const item of col.items) {
226
- localItemMap.set(item.id, { item, collectionId: col.id });
227
- }
228
- }
229
- const localCollectionIds = new Set(localCollections.map((c) => c.id));
230
- const newCollections = [];
231
- const newItems = [];
232
- const changedItems = [];
233
- let unchangedCount = 0;
234
- for (const incomingCol of incoming) {
235
- const genuinelyNewItems = incomingCol.items.filter(
236
- (item) => !localItemMap.has(item.id)
237
- );
238
- if (!localCollectionIds.has(incomingCol.id) && (incomingCol.items.length === 0 || genuinelyNewItems.length > 0)) {
239
- newCollections.push({ ...incomingCol, items: [] });
240
- }
241
- for (const incomingItem of incomingCol.items) {
242
- const local = localItemMap.get(incomingItem.id);
243
- if (!local) {
244
- newItems.push({
245
- item: incomingItem,
246
- // Land in the incoming parent; if that parent is new it will be
247
- // created during apply, otherwise it already exists locally.
248
- targetCollectionId: incomingCol.id
249
- });
250
- } else if (itemContentEqual(incomingItem, local.item)) {
251
- unchangedCount++;
252
- } else {
253
- changedItems.push({
254
- incoming: incomingItem,
255
- current: local.item,
256
- // Use where the recipient actually keeps it, not the incoming parent.
257
- currentCollectionId: local.collectionId
258
- });
259
- }
260
- }
261
- }
262
- return {
263
- ok: true,
264
- newCollections,
265
- newItems,
266
- changedItems,
267
- unchangedCount,
268
- _incoming: incoming
269
- };
270
- },
271
- applyImport(analysis, resolution) {
272
- if (!analysis.ok) {
273
- return;
274
- }
275
- if (resolution.mode === "replace") {
276
- set({ collections: analysis._incoming });
277
- void persist();
278
- return;
279
- }
280
- const now = Date.now();
281
- let collections = [...get().collections];
282
- for (const shell of analysis.newCollections) {
283
- collections.push({ ...shell, items: [] });
284
- }
285
- for (const { item, targetCollectionId } of analysis.newItems) {
286
- collections = collections.map(
287
- (c) => c.id === targetCollectionId ? { ...c, items: [...c.items, item], updatedAt: now } : c
288
- );
289
- }
290
- const shouldApply = (itemId) => {
291
- if ("applyChanges" in resolution) {
292
- return resolution.applyChanges;
293
- }
294
- return resolution.changedItemIds.has(itemId);
295
- };
296
- for (const conflict of analysis.changedItems) {
297
- if (!shouldApply(conflict.incoming.id)) {
298
- continue;
299
- }
300
- collections = collections.map((c) => {
301
- if (c.id !== conflict.currentCollectionId) {
302
- return c;
303
- }
304
- return {
305
- ...c,
306
- updatedAt: now,
307
- items: c.items.map(
308
- (i) => i.id === conflict.incoming.id ? {
309
- // Replace content fields; keep the local createdAt.
310
- ...conflict.incoming,
311
- createdAt: conflict.current.createdAt,
312
- updatedAt: now
313
- } : i
314
- )
315
- };
316
- });
317
- }
318
- set({ collections });
319
- void persist();
320
- },
321
- exportCollections() {
322
- return JSON.stringify(
323
- { version: 1, collections: get().collections },
324
- null,
325
- 2
326
- );
327
- },
328
- exportCollection(id) {
329
- const collection = get().collections.find((c) => c.id === id);
330
- return JSON.stringify(
331
- { version: 1, collections: collection ? [collection] : [] },
332
- null,
333
- 2
334
- );
335
- },
336
- exportItem(itemId) {
337
- for (const col of get().collections) {
338
- const item = col.items.find((i) => i.id === itemId);
339
- if (item) {
340
- const shell = {
341
- id: col.id,
342
- name: col.name,
343
- description: col.description,
344
- createdAt: col.createdAt,
345
- updatedAt: col.updatedAt,
346
- items: [item]
347
- };
348
- return JSON.stringify(
349
- { version: 1, collections: [shell] },
350
- null,
351
- 2
352
- );
353
- }
354
- }
355
- return JSON.stringify({ version: 1, collections: [] }, null, 2);
356
- },
357
- linkTab(tabId, collectionId, itemId) {
358
- set((s) => ({
359
- // An item lives in exactly one tab: drop any stale link to it (e.g. a
360
- // closed tab's) before recording the new one.
361
- links: {
362
- ...pruneLinksForItems(s.links, /* @__PURE__ */ new Set([itemId])),
363
- [tabId]: { collectionId, itemId }
364
- }
365
- }));
366
- saveTabLinks(get().links);
367
- },
368
- requestSave(operation) {
369
- if (get().config.readOnly) {
370
- return false;
371
- }
372
- const { links, collections, actions } = get();
373
- const link = operation.id ? links[operation.id] : void 0;
374
- if (link) {
375
- const collection = collections.find((c) => c.id === link.collectionId);
376
- const item = collection == null ? void 0 : collection.items.find((i) => i.id === link.itemId);
377
- if (item) {
378
- actions.updateItem(link.collectionId, link.itemId, {
379
- query: operation.query ?? "",
380
- variables: operation.variables ?? "",
381
- headers: operation.headers ?? ""
382
- });
383
- return true;
384
- }
385
- }
386
- actions.openSaveDialog({
387
- tabId: operation.id,
388
- query: operation.query ?? "",
389
- variables: operation.variables ?? "",
390
- headers: operation.headers ?? "",
391
- name: deriveOperationName(
392
- operation.query ?? "",
393
- operation.operationName
394
- )
395
- });
396
- return false;
397
- },
398
- openSaveDialog(input) {
399
- set({ saveDialog: { ...input, open: true } });
400
- },
401
- closeSaveDialog() {
402
- set((s) => ({ saveDialog: { ...s.saveDialog, open: false } }));
403
- }
404
- }
405
- };
32
+ var collectionsStore = createStore((set, get) => {
33
+ const persist = async () => {
34
+ await get().storage.save(get().collections);
35
+ };
36
+ return {
37
+ collections: [],
38
+ loaded: false,
39
+ storage: localStorageAdapter,
40
+ config: DEFAULT_COLLECTIONS_CONFIG,
41
+ links: {},
42
+ saveDialog: INITIAL_SAVE_DIALOG,
43
+ actions: {
44
+ async init(storage, config) {
45
+ set({
46
+ collections: await storage.load(),
47
+ loaded: true,
48
+ storage,
49
+ config: {
50
+ ...DEFAULT_COLLECTIONS_CONFIG,
51
+ ...config
52
+ },
53
+ links: loadTabLinks()
54
+ });
55
+ },
56
+ setCollections(collections) {
57
+ set({ collections });
58
+ },
59
+ async reload() {
60
+ set({ collections: await get().storage.load() });
61
+ },
62
+ createCollection(name, description) {
63
+ const c = {
64
+ id: randomId(),
65
+ name,
66
+ description,
67
+ items: [],
68
+ createdAt: Date.now(),
69
+ updatedAt: Date.now()
70
+ };
71
+ set((s) => ({ collections: [...s.collections, c] }));
72
+ persist();
73
+ return c;
74
+ },
75
+ deleteCollection(id) {
76
+ const removed = get().collections.find((c) => c.id === id);
77
+ const removedItemIds = new Set(removed?.items.map((i) => i.id) ?? []);
78
+ set((s) => ({
79
+ collections: s.collections.filter((c) => c.id !== id),
80
+ links: pruneLinksForItems(s.links, removedItemIds)
81
+ }));
82
+ persist();
83
+ saveTabLinks(get().links);
84
+ },
85
+ renameCollection(id, name) {
86
+ set((s) => ({ collections: s.collections.map((c) => c.id === id ? {
87
+ ...c,
88
+ name,
89
+ updatedAt: Date.now()
90
+ } : c) }));
91
+ persist();
92
+ },
93
+ addItem(collectionId, item) {
94
+ const newItem = {
95
+ ...item,
96
+ id: randomId(),
97
+ createdAt: Date.now(),
98
+ updatedAt: Date.now()
99
+ };
100
+ set((s) => ({ collections: s.collections.map((c) => c.id === collectionId ? {
101
+ ...c,
102
+ items: [...c.items, newItem],
103
+ updatedAt: Date.now()
104
+ } : c) }));
105
+ persist();
106
+ return newItem;
107
+ },
108
+ updateItem(collectionId, itemId, updates) {
109
+ set((s) => ({ collections: s.collections.map((c) => c.id === collectionId ? {
110
+ ...c,
111
+ updatedAt: Date.now(),
112
+ items: c.items.map((i) => i.id === itemId ? {
113
+ ...i,
114
+ ...updates,
115
+ updatedAt: Date.now()
116
+ } : i)
117
+ } : c) }));
118
+ persist();
119
+ },
120
+ deleteItem(collectionId, itemId) {
121
+ set((s) => ({
122
+ collections: s.collections.map((c) => c.id === collectionId ? {
123
+ ...c,
124
+ items: c.items.filter((i) => i.id !== itemId),
125
+ updatedAt: Date.now()
126
+ } : c),
127
+ links: pruneLinksForItems(s.links, /* @__PURE__ */ new Set([itemId]))
128
+ }));
129
+ persist();
130
+ saveTabLinks(get().links);
131
+ },
132
+ moveItem(fromCollectionId, fromIndex, toCollectionId, toIndex) {
133
+ const { collections } = get();
134
+ const fromCollection = collections.find((c) => c.id === fromCollectionId);
135
+ if (!fromCollection || fromIndex < 0 || fromIndex >= fromCollection.items.length) return;
136
+ const item = fromCollection.items[fromIndex];
137
+ if (!item) return;
138
+ set({ collections: collections.map((c) => {
139
+ if (c.id === fromCollectionId && c.id === toCollectionId) {
140
+ const items = [...c.items];
141
+ items.splice(fromIndex, 1);
142
+ items.splice(toIndex, 0, item);
143
+ return {
144
+ ...c,
145
+ items,
146
+ updatedAt: Date.now()
147
+ };
148
+ }
149
+ if (c.id === fromCollectionId) return {
150
+ ...c,
151
+ items: c.items.filter((_, i) => i !== fromIndex),
152
+ updatedAt: Date.now()
153
+ };
154
+ if (c.id === toCollectionId) {
155
+ const items = [...c.items];
156
+ items.splice(toIndex, 0, item);
157
+ return {
158
+ ...c,
159
+ items,
160
+ updatedAt: Date.now()
161
+ };
162
+ }
163
+ return c;
164
+ }) });
165
+ persist();
166
+ },
167
+ analyzeImport(json) {
168
+ const empty = {
169
+ ok: false,
170
+ newCollections: [],
171
+ newItems: [],
172
+ changedItems: [],
173
+ unchangedCount: 0,
174
+ _incoming: []
175
+ };
176
+ const CollectionItemSchema = z.object({
177
+ id: z.string(),
178
+ name: z.string(),
179
+ query: z.string(),
180
+ variables: z.string().optional(),
181
+ headers: z.string().optional(),
182
+ description: z.string().optional(),
183
+ createdAt: z.number(),
184
+ updatedAt: z.number()
185
+ });
186
+ const CollectionSchema = z.object({
187
+ id: z.string(),
188
+ name: z.string(),
189
+ description: z.string().optional(),
190
+ createdAt: z.number(),
191
+ updatedAt: z.number(),
192
+ items: z.array(CollectionItemSchema)
193
+ });
194
+ const EnvelopeSchema = z.union([z.object({
195
+ version: z.number().optional(),
196
+ collections: z.array(CollectionSchema)
197
+ }), z.array(CollectionSchema)]);
198
+ let parsed;
199
+ try {
200
+ parsed = JSON.parse(json);
201
+ } catch {
202
+ return empty;
203
+ }
204
+ const result = EnvelopeSchema.safeParse(parsed);
205
+ if (!result.success) return empty;
206
+ const incoming = Array.isArray(result.data) ? result.data : result.data.collections;
207
+ if (incoming.length === 0) return empty;
208
+ const localCollections = get().collections;
209
+ const localItemMap = /* @__PURE__ */ new Map();
210
+ for (const col of localCollections) for (const item of col.items) localItemMap.set(item.id, {
211
+ item,
212
+ collectionId: col.id
213
+ });
214
+ const localCollectionIds = new Set(localCollections.map((c) => c.id));
215
+ const newCollections = [];
216
+ const newItems = [];
217
+ const changedItems = [];
218
+ let unchangedCount = 0;
219
+ for (const incomingCol of incoming) {
220
+ const genuinelyNewItems = incomingCol.items.filter((item) => !localItemMap.has(item.id));
221
+ if (!localCollectionIds.has(incomingCol.id) && (incomingCol.items.length === 0 || genuinelyNewItems.length > 0)) newCollections.push({
222
+ ...incomingCol,
223
+ items: []
224
+ });
225
+ for (const incomingItem of incomingCol.items) {
226
+ const local = localItemMap.get(incomingItem.id);
227
+ if (!local) newItems.push({
228
+ item: incomingItem,
229
+ targetCollectionId: incomingCol.id
230
+ });
231
+ else if (itemContentEqual(incomingItem, local.item)) unchangedCount++;
232
+ else changedItems.push({
233
+ incoming: incomingItem,
234
+ current: local.item,
235
+ currentCollectionId: local.collectionId
236
+ });
237
+ }
238
+ }
239
+ return {
240
+ ok: true,
241
+ newCollections,
242
+ newItems,
243
+ changedItems,
244
+ unchangedCount,
245
+ _incoming: incoming
246
+ };
247
+ },
248
+ applyImport(analysis, resolution) {
249
+ if (!analysis.ok) return;
250
+ if (resolution.mode === "replace") {
251
+ set({ collections: analysis._incoming });
252
+ persist();
253
+ return;
254
+ }
255
+ const now = Date.now();
256
+ let collections = [...get().collections];
257
+ for (const shell of analysis.newCollections) collections.push({
258
+ ...shell,
259
+ items: []
260
+ });
261
+ for (const { item, targetCollectionId } of analysis.newItems) collections = collections.map((c) => c.id === targetCollectionId ? {
262
+ ...c,
263
+ items: [...c.items, item],
264
+ updatedAt: now
265
+ } : c);
266
+ const shouldApply = (itemId) => {
267
+ if ("applyChanges" in resolution) return resolution.applyChanges;
268
+ return resolution.changedItemIds.has(itemId);
269
+ };
270
+ for (const conflict of analysis.changedItems) {
271
+ if (!shouldApply(conflict.incoming.id)) continue;
272
+ collections = collections.map((c) => {
273
+ if (c.id !== conflict.currentCollectionId) return c;
274
+ return {
275
+ ...c,
276
+ updatedAt: now,
277
+ items: c.items.map((i) => i.id === conflict.incoming.id ? {
278
+ ...conflict.incoming,
279
+ createdAt: conflict.current.createdAt,
280
+ updatedAt: now
281
+ } : i)
282
+ };
283
+ });
284
+ }
285
+ set({ collections });
286
+ persist();
287
+ },
288
+ exportCollections() {
289
+ return JSON.stringify({
290
+ version: 1,
291
+ collections: get().collections
292
+ }, null, 2);
293
+ },
294
+ exportCollection(id) {
295
+ const collection = get().collections.find((c) => c.id === id);
296
+ return JSON.stringify({
297
+ version: 1,
298
+ collections: collection ? [collection] : []
299
+ }, null, 2);
300
+ },
301
+ exportItem(itemId) {
302
+ for (const col of get().collections) {
303
+ const item = col.items.find((i) => i.id === itemId);
304
+ if (item) {
305
+ const shell = {
306
+ id: col.id,
307
+ name: col.name,
308
+ description: col.description,
309
+ createdAt: col.createdAt,
310
+ updatedAt: col.updatedAt,
311
+ items: [item]
312
+ };
313
+ return JSON.stringify({
314
+ version: 1,
315
+ collections: [shell]
316
+ }, null, 2);
317
+ }
318
+ }
319
+ return JSON.stringify({
320
+ version: 1,
321
+ collections: []
322
+ }, null, 2);
323
+ },
324
+ linkTab(tabId, collectionId, itemId) {
325
+ set((s) => ({ links: {
326
+ ...pruneLinksForItems(s.links, /* @__PURE__ */ new Set([itemId])),
327
+ [tabId]: {
328
+ collectionId,
329
+ itemId
330
+ }
331
+ } }));
332
+ saveTabLinks(get().links);
333
+ },
334
+ requestSave(operation) {
335
+ if (get().config.readOnly) return false;
336
+ const { links, collections, actions } = get();
337
+ const link = operation.id ? links[operation.id] : void 0;
338
+ if (link) {
339
+ if (collections.find((c) => c.id === link.collectionId)?.items.find((i) => i.id === link.itemId)) {
340
+ actions.updateItem(link.collectionId, link.itemId, {
341
+ query: operation.query ?? "",
342
+ variables: operation.variables ?? "",
343
+ headers: operation.headers ?? ""
344
+ });
345
+ return true;
346
+ }
347
+ }
348
+ actions.openSaveDialog({
349
+ tabId: operation.id,
350
+ query: operation.query ?? "",
351
+ variables: operation.variables ?? "",
352
+ headers: operation.headers ?? "",
353
+ name: deriveOperationName(operation.query ?? "", operation.operationName)
354
+ });
355
+ return false;
356
+ },
357
+ openSaveDialog(input) {
358
+ set({ saveDialog: {
359
+ ...input,
360
+ open: true
361
+ } });
362
+ },
363
+ closeSaveDialog() {
364
+ set((s) => ({ saveDialog: {
365
+ ...s.saveDialog,
366
+ open: false
367
+ } }));
368
+ }
369
+ }
370
+ };
406
371
  });
407
- const useCollectionsStore = createBoundedUseStore(collectionsStore);
408
- export {
409
- collectionsStore,
410
- deriveOperationName,
411
- useCollectionsStore
412
- };
413
- //# sourceMappingURL=store.js.map
372
+ var useCollectionsStore = createBoundedUseStore(collectionsStore);
373
+ //#endregion
374
+ export { collectionsStore, deriveOperationName, useCollectionsStore };
375
+
376
+ //# sourceMappingURL=store.js.map