@graphiql/plugin-collections 1.0.0-beta.1 → 1.0.0-beta.2
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 +21 -0
- package/dist/components/collection-item-row.js +212 -282
- package/dist/components/collection-item-row.js.map +1 -1
- package/dist/components/collection-row.js +139 -159
- package/dist/components/collection-row.js.map +1 -1
- package/dist/components/collections-panel.js +408 -496
- package/dist/components/collections-panel.js.map +1 -1
- package/dist/components/conflict-dialog.js +148 -164
- package/dist/components/conflict-dialog.js.map +1 -1
- package/dist/components/export-dialog.js +52 -52
- package/dist/components/export-dialog.js.map +1 -1
- package/dist/components/import-dialog.js +100 -112
- package/dist/components/import-dialog.js.map +1 -1
- package/dist/components/save-dialog.js +116 -124
- package/dist/components/save-dialog.js.map +1 -1
- package/dist/components/session-actions.js +23 -31
- package/dist/components/session-actions.js.map +1 -1
- package/dist/icons/check.js +18 -9
- package/dist/icons/check.js.map +1 -1
- package/dist/icons/collections.js +16 -9
- package/dist/icons/collections.js.map +1 -1
- package/dist/icons/download.js +16 -9
- package/dist/icons/download.js.map +1 -1
- package/dist/icons/upload.js +16 -9
- package/dist/icons/upload.js.map +1 -1
- package/dist/index.js +31 -41
- package/dist/index.js.map +1 -1
- package/dist/operation-method.js +24 -24
- package/dist/operation-method.js.map +1 -1
- package/dist/storage/local-storage.js +32 -37
- package/dist/storage/local-storage.js.map +1 -1
- package/dist/storage/tab-links.js +27 -30
- package/dist/storage/tab-links.js.map +1 -1
- package/dist/store.js +365 -402
- package/dist/store.js.map +1 -1
- package/dist/style.css +6 -27
- package/dist/types.js +9 -8
- package/dist/types.js.map +1 -1
- package/dist/utility/uuid.js +11 -9
- package/dist/utility/uuid.js.map +1 -1
- 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 {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
};
|
|
413
|
-
//# sourceMappingURL=store.js.map
|
|
372
|
+
var useCollectionsStore = createBoundedUseStore(collectionsStore);
|
|
373
|
+
//#endregion
|
|
374
|
+
export { collectionsStore, deriveOperationName, useCollectionsStore };
|
|
375
|
+
|
|
376
|
+
//# sourceMappingURL=store.js.map
|