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

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 (57) hide show
  1. package/README.md +162 -0
  2. package/dist/collections.stories.d.ts +14 -0
  3. package/dist/components/collection-item-row.d.ts +25 -0
  4. package/dist/components/collection-item-row.js +286 -0
  5. package/dist/components/collection-item-row.js.map +1 -0
  6. package/dist/components/collection-row.d.ts +32 -0
  7. package/dist/components/collection-row.js +162 -0
  8. package/dist/components/collection-row.js.map +1 -0
  9. package/dist/components/collections-panel.d.ts +9 -0
  10. package/dist/components/collections-panel.js +508 -0
  11. package/dist/components/collections-panel.js.map +1 -0
  12. package/dist/components/conflict-dialog.d.ts +11 -0
  13. package/dist/components/conflict-dialog.js +169 -0
  14. package/dist/components/conflict-dialog.js.map +1 -0
  15. package/dist/components/export-dialog.d.ts +7 -0
  16. package/dist/components/export-dialog.js +54 -0
  17. package/dist/components/export-dialog.js.map +1 -0
  18. package/dist/components/import-dialog.d.ts +10 -0
  19. package/dist/components/import-dialog.js +114 -0
  20. package/dist/components/import-dialog.js.map +1 -0
  21. package/dist/components/save-dialog.d.ts +7 -0
  22. package/dist/components/save-dialog.js +125 -0
  23. package/dist/components/save-dialog.js.map +1 -0
  24. package/dist/components/session-actions.d.ts +9 -0
  25. package/dist/components/session-actions.js +34 -0
  26. package/dist/components/session-actions.js.map +1 -0
  27. package/dist/icons/check.js +10 -0
  28. package/dist/icons/check.js.map +1 -0
  29. package/dist/icons/collections.js +10 -0
  30. package/dist/icons/collections.js.map +1 -0
  31. package/dist/icons/download.js +10 -0
  32. package/dist/icons/download.js.map +1 -0
  33. package/dist/icons/upload.js +10 -0
  34. package/dist/icons/upload.js.map +1 -0
  35. package/dist/index.d.ts +17 -0
  36. package/dist/index.js +46 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/operation-method.d.ts +10 -0
  39. package/dist/operation-method.js +28 -0
  40. package/dist/operation-method.js.map +1 -0
  41. package/dist/storage/local-storage.d.ts +3 -0
  42. package/dist/storage/local-storage.js +39 -0
  43. package/dist/storage/local-storage.js.map +1 -0
  44. package/dist/storage/tab-links.d.ts +7 -0
  45. package/dist/storage/tab-links.js +36 -0
  46. package/dist/storage/tab-links.js.map +1 -0
  47. package/dist/store.d.ts +78 -0
  48. package/dist/store.js +413 -0
  49. package/dist/store.js.map +1 -0
  50. package/dist/style.css +442 -0
  51. package/dist/types.d.ts +80 -0
  52. package/dist/types.js +9 -0
  53. package/dist/types.js.map +1 -0
  54. package/dist/utility/uuid.d.ts +4 -0
  55. package/dist/utility/uuid.js +11 -0
  56. package/dist/utility/uuid.js.map +1 -0
  57. package/package.json +60 -1
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # @graphiql/plugin-collections
2
+
3
+ A first-party operation collections plugin for GraphiQL. Save named GraphQL operations into collections and reuse them later, with drag-and-drop reordering, clipboard sharing, and JSON import/export that merges by id instead of duplicating.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @graphiql/plugin-collections
9
+ ```
10
+
11
+ Make sure the peer dependencies are installed too:
12
+
13
+ ```sh
14
+ npm install react react-dom graphql @graphiql/react
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ The collections plugin is installed by default in GraphiQL, so an unmodified `<GraphiQL>` already shows a Collections rail icon. You only need to register it explicitly to configure it, for example to supply a custom storage backend or restrict what users can do.
20
+
21
+ > **Note:** passing the `plugins` prop replaces the default plugin set, so include the others you want alongside it.
22
+
23
+ ```jsx
24
+ import { GraphiQL } from 'graphiql';
25
+ import { createTransport } from '@graphiql/toolkit';
26
+ import { collectionsPlugin } from '@graphiql/plugin-collections';
27
+ import 'graphiql/style.css';
28
+ import '@graphiql/plugin-collections/style.css';
29
+
30
+ const transport = createTransport({
31
+ url: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
32
+ });
33
+
34
+ const collections = collectionsPlugin();
35
+
36
+ function GraphiQLWithCollections() {
37
+ return <GraphiQL transport={transport} plugins={[collections]} />;
38
+ }
39
+ ```
40
+
41
+ ## Options
42
+
43
+ ```ts
44
+ collectionsPlugin({
45
+ storage, // custom persistence backend; see below
46
+ readOnly, // default false
47
+ allowImportExport, // default true
48
+ allowReplace, // default true
49
+ });
50
+ ```
51
+
52
+ | Option | Default | Effect |
53
+ | ------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
54
+ | `storage` | localStorage adapter | A custom `CollectionsStorage` (see [Custom storage](#custom-storage)). |
55
+ | `readOnly` | `false` | Hides every write affordance (create, rename, delete, reorder, save, import). Export and copy stay available so users can still share. |
56
+ | `allowImportExport` | `true` | When `false`, hides the import/export dialog button and turns off paste/drop import. |
57
+ | `allowReplace` | `true` | When `false`, hides the destructive "replace everything" import mode and leaves only merge. |
58
+
59
+ ## Import and merge behavior
60
+
61
+ Collections and the operations inside them carry stable UUIDs that survive export and import. On import, the plugin reconciles by id instead of duplicating:
62
+
63
+ - A new operation is added. An operation you already have is updated in place wherever you keep it, even if you've moved it to a different collection. A missing parent collection is recreated.
64
+ - Re-importing an unchanged export does nothing, so you never get duplicates.
65
+ - When an incoming operation differs from your local copy, a dialog lets you apply the changes, keep your versions, or review each operation one at a time. Merge never deletes.
66
+
67
+ An operation can also be shared on its own with "Copy operation". It travels as a one-item collection envelope stamped with its parent collection, so the recipient can re-home it on import.
68
+
69
+ ## Custom storage
70
+
71
+ Persistence is pluggable through the `CollectionsStorage` interface:
72
+
73
+ ```ts
74
+ type CollectionsStorage = {
75
+ storageKey?: string;
76
+ load(): Promise<Collection[]>;
77
+ save(collections: Collection[]): Promise<void>;
78
+ };
79
+ ```
80
+
81
+ The default writes to `localStorage` under the key `graphiql:collections`. To use a different key, pass an adapter from the exported `createLocalStorageAdapter` factory:
82
+
83
+ ```ts
84
+ import {
85
+ collectionsPlugin,
86
+ createLocalStorageAdapter,
87
+ } from '@graphiql/plugin-collections';
88
+
89
+ const collections = collectionsPlugin({
90
+ storage: createLocalStorageAdapter('my-key'),
91
+ });
92
+ ```
93
+
94
+ To back collections with your own service (REST, a database, a team-shared store), implement the interface and pass it as `storage`:
95
+
96
+ ```ts
97
+ import { collectionsPlugin } from '@graphiql/plugin-collections';
98
+ import type {
99
+ Collection,
100
+ CollectionsStorage,
101
+ } from '@graphiql/plugin-collections';
102
+
103
+ const apiStorage: CollectionsStorage = {
104
+ async load() {
105
+ const res = await fetch('/api/collections');
106
+ return (await res.json()) as Collection[];
107
+ },
108
+ async save(collections) {
109
+ await fetch('/api/collections', {
110
+ method: 'PUT',
111
+ headers: { 'content-type': 'application/json' },
112
+ body: JSON.stringify(collections),
113
+ });
114
+ },
115
+ };
116
+
117
+ const collections = collectionsPlugin({ storage: apiStorage });
118
+ ```
119
+
120
+ `load()` runs once when the panel first mounts. `save()` runs after every mutation with the full collection list.
121
+
122
+ ### Team-shared backends and sync
123
+
124
+ The plugin has no opinion about conflict resolution or synchronization; that lives in your storage adapter. Because every operation and collection has a stable id, your adapter can diff successive `save()` snapshots and reconcile them however it needs to, whether that is a plain backend write, a last-write-wins policy, or a CRDT such as Yjs or Automerge. The plugin itself stays a CRUD UI over a serializable model.
125
+
126
+ The plugin does not react to remote changes on its own. When your backend reports an external change, call `reload()` to re-read storage and refresh the UI:
127
+
128
+ ```ts
129
+ import { collectionsStore } from '@graphiql/plugin-collections';
130
+
131
+ await collectionsStore.getState().actions.reload();
132
+ ```
133
+
134
+ ## Programmatic access
135
+
136
+ The store and its React hook are exported for programmatic access:
137
+
138
+ ```ts
139
+ import {
140
+ collectionsStore, // the vanilla zustand store
141
+ useCollectionsStore, // React hook: useCollectionsStore(selector)
142
+ } from '@graphiql/plugin-collections';
143
+
144
+ const { actions } = collectionsStore.getState();
145
+
146
+ // Replace the in-memory collections (e.g. after merging remote state).
147
+ // Does NOT write back to storage.
148
+ actions.setCollections(nextCollections);
149
+
150
+ // Re-read from the configured storage and refresh the UI.
151
+ await actions.reload();
152
+ ```
153
+
154
+ `actions` also exposes the full CRUD surface (`createCollection`, `addItem`, `updateItem`, `deleteItem`, `moveItem`, `renameCollection`, and so on) plus `exportCollections` / `exportCollection` / `exportItem` and the two-phase `analyzeImport` / `applyImport`.
155
+
156
+ ## Exports
157
+
158
+ - `collectionsPlugin(options?)` — the plugin factory
159
+ - `collectionsStore`, `useCollectionsStore` — the store and its React hook
160
+ - `createLocalStorageAdapter(key)`, `localStorageAdapter` — built-in storage
161
+ - `CollectionsSaveDialog` — the save dialog component
162
+ - Types: `Collection`, `CollectionItem`, `CollectionsStorage`, `CollectionsConfig`, `ActiveOperation`
@@ -0,0 +1,14 @@
1
+ import { Meta, StoryObj } from '@storybook/react-vite';
2
+ declare const meta: Meta;
3
+ export default meta;
4
+ type Story = StoryObj;
5
+ export declare const MethodPillQuery: Story;
6
+ export declare const MethodPillMutation: Story;
7
+ export declare const MethodPillSubscription: Story;
8
+ export declare const MethodPillMix: Story;
9
+ export declare const CollectionCollapsed: Story;
10
+ export declare const CollectionExpanded: Story;
11
+ export declare const TwoCollectionsExpanded: Story;
12
+ export declare const CollectionEmpty: Story;
13
+ export declare const SaveDialogOpen: Story;
14
+ export declare const ConflictDialogSummary: Story;
@@ -0,0 +1,25 @@
1
+ import { FC } from 'react';
2
+ import { CollectionItem } from '../types';
3
+ type CollectionItemRowProps = {
4
+ item: CollectionItem;
5
+ collectionId: string;
6
+ /** The item's position within its collection, used for drag reorder. */
7
+ index: number;
8
+ /** Hide write affordances (Move, Delete) and disable drag-reorder when true. */
9
+ readOnly?: boolean;
10
+ isGrabbed: boolean;
11
+ onGrabToggle(): void;
12
+ onGrabMove(direction: 'up' | 'down'): void;
13
+ onGrabCancel(): void;
14
+ onOpen(item: CollectionItem): void;
15
+ onShare(itemId: string): Promise<void>;
16
+ onAnnounce(message: string): void;
17
+ onDelete(collectionId: string, itemId: string): void;
18
+ onMove(fromCollectionId: string, fromIndex: number, toCollectionId: string, toIndex: number): void;
19
+ onRenameItem(collectionId: string, itemId: string, updates: {
20
+ name: string;
21
+ description: string;
22
+ }): void;
23
+ };
24
+ export declare const CollectionItemRow: FC<CollectionItemRowProps>;
25
+ export {};
@@ -0,0 +1,286 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { useState, useRef, useEffect } from "react";
3
+ import { MethodPill, PenIcon, CopyIcon, TrashIcon } from "@graphiql/react";
4
+ import { getDocumentMethod } from "../operation-method.js";
5
+ import SvgUpload from "../icons/upload.js";
6
+ import SvgCheck from "../icons/check.js";
7
+ const CollectionItemRow = ({
8
+ item,
9
+ collectionId,
10
+ index,
11
+ readOnly = false,
12
+ isGrabbed,
13
+ onGrabToggle,
14
+ onGrabMove,
15
+ onGrabCancel,
16
+ onOpen,
17
+ onShare,
18
+ onAnnounce,
19
+ onDelete,
20
+ onMove,
21
+ onRenameItem
22
+ }) => {
23
+ const [isDragOver, setIsDragOver] = useState(false);
24
+ const [isEditing, setIsEditing] = useState(false);
25
+ const [confirmed, setConfirmed] = useState(null);
26
+ const [confirmToken, setConfirmToken] = useState(0);
27
+ const editFormRef = useRef(null);
28
+ const [editName, setEditName] = useState(item.name);
29
+ const [editDescription, setEditDescription] = useState(
30
+ item.description ?? ""
31
+ );
32
+ const method = getDocumentMethod(item.query);
33
+ const startEdit = (e) => {
34
+ e.stopPropagation();
35
+ setEditName(item.name);
36
+ setEditDescription(item.description ?? "");
37
+ setIsEditing(true);
38
+ };
39
+ useEffect(() => {
40
+ if (!confirmed) {
41
+ return;
42
+ }
43
+ const timer = setTimeout(() => setConfirmed(null), 1500);
44
+ return () => clearTimeout(timer);
45
+ }, [confirmed, confirmToken]);
46
+ const runAction = async (kind, write, message, failureMessage) => {
47
+ try {
48
+ await write();
49
+ onAnnounce(message);
50
+ setConfirmed(kind);
51
+ setConfirmToken((t) => t + 1);
52
+ } catch {
53
+ onAnnounce(failureMessage);
54
+ }
55
+ };
56
+ const handleDelete = (e) => {
57
+ e.stopPropagation();
58
+ onDelete(collectionId, item.id);
59
+ };
60
+ const commitEdit = () => {
61
+ const name = editName.trim() || item.name;
62
+ onRenameItem(collectionId, item.id, {
63
+ name,
64
+ description: editDescription
65
+ });
66
+ setIsEditing(false);
67
+ };
68
+ const handleBlur = (e) => {
69
+ var _a;
70
+ if ((_a = editFormRef.current) == null ? void 0 : _a.contains(e.relatedTarget)) {
71
+ return;
72
+ }
73
+ commitEdit();
74
+ };
75
+ const cancelEdit = () => {
76
+ setEditName(item.name);
77
+ setEditDescription(item.description ?? "");
78
+ setIsEditing(false);
79
+ };
80
+ return /* @__PURE__ */ jsxs(
81
+ "div",
82
+ {
83
+ className: `graphiql-collection-item-row${isDragOver ? " graphiql-collection-drop-target" : ""}`,
84
+ "data-grabbed": isGrabbed || void 0,
85
+ draggable: !readOnly && !isEditing,
86
+ onDragStart: readOnly || isEditing ? void 0 : (e) => {
87
+ e.dataTransfer.setData(
88
+ "application/x-graphiql-item",
89
+ JSON.stringify({ collectionId, index })
90
+ );
91
+ e.dataTransfer.effectAllowed = "move";
92
+ },
93
+ onDragOver: readOnly ? void 0 : (e) => {
94
+ e.preventDefault();
95
+ e.dataTransfer.dropEffect = "move";
96
+ setIsDragOver(true);
97
+ },
98
+ onDragLeave: readOnly ? void 0 : () => setIsDragOver(false),
99
+ onDrop: readOnly ? void 0 : (e) => {
100
+ e.preventDefault();
101
+ setIsDragOver(false);
102
+ try {
103
+ const data = JSON.parse(
104
+ e.dataTransfer.getData("application/x-graphiql-item")
105
+ );
106
+ onMove(data.collectionId, data.index, collectionId, index);
107
+ } catch {
108
+ }
109
+ },
110
+ onClick: () => {
111
+ if (!isEditing) {
112
+ onOpen(item);
113
+ }
114
+ },
115
+ children: [
116
+ /* @__PURE__ */ jsx(
117
+ "button",
118
+ {
119
+ type: "button",
120
+ className: "graphiql-collection-drag-handle",
121
+ "data-collection-drag-handle": item.id,
122
+ "aria-label": `Reorder ${item.name}. Press space to grab, arrow keys to move, escape to cancel.`,
123
+ "aria-pressed": isGrabbed,
124
+ disabled: readOnly,
125
+ onClick: (e) => e.stopPropagation(),
126
+ onKeyDown: (e) => {
127
+ if (e.key === " " || e.key === "Enter") {
128
+ e.preventDefault();
129
+ e.stopPropagation();
130
+ onGrabToggle();
131
+ } else if (isGrabbed && e.key === "ArrowDown") {
132
+ e.preventDefault();
133
+ e.stopPropagation();
134
+ onGrabMove("down");
135
+ } else if (isGrabbed && e.key === "ArrowUp") {
136
+ e.preventDefault();
137
+ e.stopPropagation();
138
+ onGrabMove("up");
139
+ } else if (isGrabbed && e.key === "Escape") {
140
+ e.preventDefault();
141
+ e.stopPropagation();
142
+ onGrabCancel();
143
+ }
144
+ },
145
+ children: "⠿"
146
+ }
147
+ ),
148
+ method === "mix" ? /* @__PURE__ */ jsx(
149
+ "span",
150
+ {
151
+ className: "graphiql-method-pill graphiql-collection-method-pill-mix",
152
+ "aria-hidden": true,
153
+ children: "MIX"
154
+ }
155
+ ) : /* @__PURE__ */ jsx(MethodPill, { operation: method, "aria-hidden": true }),
156
+ isEditing ? /* @__PURE__ */ jsxs(
157
+ "div",
158
+ {
159
+ ref: editFormRef,
160
+ className: "graphiql-collection-item-edit-form",
161
+ onClick: (e) => e.stopPropagation(),
162
+ children: [
163
+ /* @__PURE__ */ jsx(
164
+ "input",
165
+ {
166
+ className: "graphiql-collection-item-edit-input",
167
+ value: editName,
168
+ autoFocus: true,
169
+ placeholder: item.name,
170
+ onChange: (e) => setEditName(e.target.value),
171
+ onBlur: handleBlur,
172
+ onKeyDown: (e) => {
173
+ if (e.key === "Enter") {
174
+ commitEdit();
175
+ }
176
+ if (e.key === "Escape") {
177
+ cancelEdit();
178
+ }
179
+ e.stopPropagation();
180
+ }
181
+ }
182
+ ),
183
+ /* @__PURE__ */ jsx(
184
+ "input",
185
+ {
186
+ className: "graphiql-collection-item-edit-input",
187
+ value: editDescription,
188
+ placeholder: "Description (optional)",
189
+ onChange: (e) => setEditDescription(e.target.value),
190
+ onBlur: handleBlur,
191
+ onKeyDown: (e) => {
192
+ if (e.key === "Enter") {
193
+ commitEdit();
194
+ }
195
+ if (e.key === "Escape") {
196
+ cancelEdit();
197
+ }
198
+ e.stopPropagation();
199
+ }
200
+ }
201
+ )
202
+ ]
203
+ }
204
+ ) : /* @__PURE__ */ jsxs("div", { className: "graphiql-collection-item-main", children: [
205
+ /* @__PURE__ */ jsx("span", { className: "graphiql-collection-item-name", title: item.name, children: item.name }),
206
+ item.description && /* @__PURE__ */ jsx(
207
+ "span",
208
+ {
209
+ className: "graphiql-collection-item-description",
210
+ title: item.description,
211
+ children: item.description
212
+ }
213
+ )
214
+ ] }),
215
+ !isEditing && /* @__PURE__ */ jsxs("div", { className: "graphiql-collection-item-actions", children: [
216
+ !readOnly && /* @__PURE__ */ jsx(
217
+ "button",
218
+ {
219
+ type: "button",
220
+ className: "graphiql-collection-item-action",
221
+ "aria-label": `Rename ${item.name}`,
222
+ title: "Rename",
223
+ onClick: startEdit,
224
+ children: /* @__PURE__ */ jsx(PenIcon, { "aria-hidden": "true" })
225
+ }
226
+ ),
227
+ /* @__PURE__ */ jsx(
228
+ "button",
229
+ {
230
+ type: "button",
231
+ className: "graphiql-collection-item-action",
232
+ "aria-label": `Copy operation ${item.name}`,
233
+ title: "Copy operation",
234
+ "data-confirmed": confirmed === "copy" || void 0,
235
+ onClick: (e) => {
236
+ e.stopPropagation();
237
+ void runAction(
238
+ "copy",
239
+ () => navigator.clipboard.writeText(item.query),
240
+ "Copied query to clipboard.",
241
+ "Could not copy to clipboard."
242
+ );
243
+ },
244
+ children: confirmed === "copy" ? /* @__PURE__ */ jsx(SvgCheck, { "aria-hidden": "true" }) : /* @__PURE__ */ jsx(CopyIcon, { "aria-hidden": "true" })
245
+ }
246
+ ),
247
+ /* @__PURE__ */ jsx(
248
+ "button",
249
+ {
250
+ type: "button",
251
+ className: "graphiql-collection-item-action",
252
+ "aria-label": `Share ${item.name} as collection`,
253
+ title: "Share as collection",
254
+ "data-confirmed": confirmed === "share" || void 0,
255
+ onClick: (e) => {
256
+ e.stopPropagation();
257
+ void runAction(
258
+ "share",
259
+ () => onShare(item.id),
260
+ "Shared operation to clipboard.",
261
+ "Could not share to clipboard."
262
+ );
263
+ },
264
+ children: confirmed === "share" ? /* @__PURE__ */ jsx(SvgCheck, { "aria-hidden": "true" }) : /* @__PURE__ */ jsx(SvgUpload, { "aria-hidden": "true" })
265
+ }
266
+ ),
267
+ !readOnly && /* @__PURE__ */ jsx(
268
+ "button",
269
+ {
270
+ type: "button",
271
+ className: "graphiql-collection-item-action",
272
+ "aria-label": `Delete ${item.name}`,
273
+ title: "Delete",
274
+ onClick: handleDelete,
275
+ children: /* @__PURE__ */ jsx(TrashIcon, { "aria-hidden": "true" })
276
+ }
277
+ )
278
+ ] })
279
+ ]
280
+ }
281
+ );
282
+ };
283
+ export {
284
+ CollectionItemRow
285
+ };
286
+ //# sourceMappingURL=collection-item-row.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection-item-row.js","sources":["../../src/components/collection-item-row.tsx"],"sourcesContent":["import { FC, useEffect, useRef, useState } from 'react';\nimport { MethodPill, PenIcon, CopyIcon, TrashIcon } from '@graphiql/react';\nimport type { CollectionItem } from '../types';\nimport { getDocumentMethod } from '../operation-method';\nimport UploadIcon from '../icons/upload.svg?react';\nimport CheckIcon from '../icons/check.svg?react';\n\ntype CollectionItemRowProps = {\n item: CollectionItem;\n collectionId: string;\n /** The item's position within its collection, used for drag reorder. */\n index: number;\n /** Hide write affordances (Move, Delete) and disable drag-reorder when true. */\n readOnly?: boolean;\n isGrabbed: boolean;\n onGrabToggle(): void;\n onGrabMove(direction: 'up' | 'down'): void;\n onGrabCancel(): void;\n onOpen(item: CollectionItem): void;\n onShare(itemId: string): Promise<void>;\n onAnnounce(message: string): void;\n onDelete(collectionId: string, itemId: string): void;\n onMove(\n fromCollectionId: string,\n fromIndex: number,\n toCollectionId: string,\n toIndex: number,\n ): void;\n onRenameItem(\n collectionId: string,\n itemId: string,\n updates: { name: string; description: string },\n ): void;\n};\n\nexport const CollectionItemRow: FC<CollectionItemRowProps> = ({\n item,\n collectionId,\n index,\n readOnly = false,\n isGrabbed,\n onGrabToggle,\n onGrabMove,\n onGrabCancel,\n onOpen,\n onShare,\n onAnnounce,\n onDelete,\n onMove,\n onRenameItem,\n}) => {\n const [isDragOver, setIsDragOver] = useState(false);\n const [isEditing, setIsEditing] = useState(false);\n const [confirmed, setConfirmed] = useState<'copy' | 'share' | null>(null);\n const [confirmToken, setConfirmToken] = useState(0);\n const editFormRef = useRef<HTMLDivElement>(null);\n const [editName, setEditName] = useState(item.name);\n const [editDescription, setEditDescription] = useState(\n item.description ?? '',\n );\n const method = getDocumentMethod(item.query);\n\n // Action buttons stop propagation so their clicks don't also trigger the\n // row's open handler.\n const startEdit = (e: React.MouseEvent) => {\n e.stopPropagation();\n setEditName(item.name);\n setEditDescription(item.description ?? '');\n setIsEditing(true);\n };\n\n useEffect(() => {\n if (!confirmed) {\n return;\n }\n const timer = setTimeout(() => setConfirmed(null), 1500);\n return () => clearTimeout(timer);\n }, [confirmed, confirmToken]);\n\n const runAction = async (\n kind: 'copy' | 'share',\n write: () => Promise<void>,\n message: string,\n failureMessage: string,\n ) => {\n try {\n await write();\n onAnnounce(message);\n setConfirmed(kind);\n setConfirmToken(t => t + 1);\n } catch {\n onAnnounce(failureMessage);\n }\n };\n\n const handleDelete = (e: React.MouseEvent) => {\n e.stopPropagation();\n onDelete(collectionId, item.id);\n };\n\n const commitEdit = () => {\n const name = editName.trim() || item.name;\n onRenameItem(collectionId, item.id, {\n name,\n description: editDescription,\n });\n setIsEditing(false);\n };\n\n // Commit only when focus leaves the whole form; moving between the name and\n // description inputs must not tear down the edit UI mid-edit.\n const handleBlur = (e: React.FocusEvent) => {\n if (editFormRef.current?.contains(e.relatedTarget as Node | null)) {\n return;\n }\n commitEdit();\n };\n\n const cancelEdit = () => {\n setEditName(item.name);\n setEditDescription(item.description ?? '');\n setIsEditing(false);\n };\n\n return (\n <div\n className={`graphiql-collection-item-row${isDragOver ? ' graphiql-collection-drop-target' : ''}`}\n data-grabbed={isGrabbed || undefined}\n draggable={!readOnly && !isEditing}\n onDragStart={\n readOnly || isEditing\n ? undefined\n : e => {\n e.dataTransfer.setData(\n 'application/x-graphiql-item',\n JSON.stringify({ collectionId, index }),\n );\n e.dataTransfer.effectAllowed = 'move';\n }\n }\n onDragOver={\n readOnly\n ? undefined\n : e => {\n e.preventDefault();\n e.dataTransfer.dropEffect = 'move';\n setIsDragOver(true);\n }\n }\n onDragLeave={readOnly ? undefined : () => setIsDragOver(false)}\n onDrop={\n readOnly\n ? undefined\n : e => {\n e.preventDefault();\n setIsDragOver(false);\n try {\n const data = JSON.parse(\n e.dataTransfer.getData('application/x-graphiql-item'),\n );\n onMove(data.collectionId, data.index, collectionId, index);\n } catch {\n // malformed drag data\n }\n }\n }\n onClick={() => {\n if (!isEditing) {\n onOpen(item);\n }\n }}\n >\n <button\n type=\"button\"\n className=\"graphiql-collection-drag-handle\"\n data-collection-drag-handle={item.id}\n aria-label={`Reorder ${item.name}. Press space to grab, arrow keys to move, escape to cancel.`}\n aria-pressed={isGrabbed}\n disabled={readOnly}\n onClick={e => e.stopPropagation()}\n onKeyDown={e => {\n if (e.key === ' ' || e.key === 'Enter') {\n e.preventDefault();\n e.stopPropagation();\n onGrabToggle();\n } else if (isGrabbed && e.key === 'ArrowDown') {\n e.preventDefault();\n e.stopPropagation();\n onGrabMove('down');\n } else if (isGrabbed && e.key === 'ArrowUp') {\n e.preventDefault();\n e.stopPropagation();\n onGrabMove('up');\n } else if (isGrabbed && e.key === 'Escape') {\n e.preventDefault();\n e.stopPropagation();\n onGrabCancel();\n }\n }}\n >\n ⠿\n </button>\n {method === 'mix' ? (\n <span\n className=\"graphiql-method-pill graphiql-collection-method-pill-mix\"\n aria-hidden\n >\n MIX\n </span>\n ) : (\n <MethodPill operation={method} aria-hidden />\n )}\n {isEditing ? (\n <div\n ref={editFormRef}\n className=\"graphiql-collection-item-edit-form\"\n onClick={e => e.stopPropagation()}\n >\n <input\n className=\"graphiql-collection-item-edit-input\"\n value={editName}\n autoFocus\n placeholder={item.name}\n onChange={e => setEditName(e.target.value)}\n onBlur={handleBlur}\n onKeyDown={e => {\n if (e.key === 'Enter') {\n commitEdit();\n }\n if (e.key === 'Escape') {\n cancelEdit();\n }\n e.stopPropagation();\n }}\n />\n <input\n className=\"graphiql-collection-item-edit-input\"\n value={editDescription}\n placeholder=\"Description (optional)\"\n onChange={e => setEditDescription(e.target.value)}\n onBlur={handleBlur}\n onKeyDown={e => {\n if (e.key === 'Enter') {\n commitEdit();\n }\n if (e.key === 'Escape') {\n cancelEdit();\n }\n e.stopPropagation();\n }}\n />\n </div>\n ) : (\n <div className=\"graphiql-collection-item-main\">\n <span className=\"graphiql-collection-item-name\" title={item.name}>\n {item.name}\n </span>\n {item.description && (\n <span\n className=\"graphiql-collection-item-description\"\n title={item.description}\n >\n {item.description}\n </span>\n )}\n </div>\n )}\n {!isEditing && (\n <div className=\"graphiql-collection-item-actions\">\n {!readOnly && (\n <button\n type=\"button\"\n className=\"graphiql-collection-item-action\"\n aria-label={`Rename ${item.name}`}\n title=\"Rename\"\n onClick={startEdit}\n >\n <PenIcon aria-hidden=\"true\" />\n </button>\n )}\n <button\n type=\"button\"\n className=\"graphiql-collection-item-action\"\n aria-label={`Copy operation ${item.name}`}\n title=\"Copy operation\"\n data-confirmed={confirmed === 'copy' || undefined}\n onClick={e => {\n e.stopPropagation();\n void runAction(\n 'copy',\n () => navigator.clipboard.writeText(item.query),\n 'Copied query to clipboard.',\n 'Could not copy to clipboard.',\n );\n }}\n >\n {confirmed === 'copy' ? (\n <CheckIcon aria-hidden=\"true\" />\n ) : (\n <CopyIcon aria-hidden=\"true\" />\n )}\n </button>\n <button\n type=\"button\"\n className=\"graphiql-collection-item-action\"\n aria-label={`Share ${item.name} as collection`}\n title=\"Share as collection\"\n data-confirmed={confirmed === 'share' || undefined}\n onClick={e => {\n e.stopPropagation();\n void runAction(\n 'share',\n () => onShare(item.id),\n 'Shared operation to clipboard.',\n 'Could not share to clipboard.',\n );\n }}\n >\n {confirmed === 'share' ? (\n <CheckIcon aria-hidden=\"true\" />\n ) : (\n <UploadIcon aria-hidden=\"true\" />\n )}\n </button>\n {!readOnly && (\n <button\n type=\"button\"\n className=\"graphiql-collection-item-action\"\n aria-label={`Delete ${item.name}`}\n title=\"Delete\"\n onClick={handleDelete}\n >\n <TrashIcon aria-hidden=\"true\" />\n </button>\n )}\n </div>\n )}\n </div>\n );\n};\n"],"names":["CheckIcon","UploadIcon"],"mappings":";;;;;;AAmCO,MAAM,oBAAgD,CAAC;AAAA,EAC5D;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAAM;AACJ,QAAM,CAAC,YAAY,aAAa,IAAI,SAAS,KAAK;AAClD,QAAM,CAAC,WAAW,YAAY,IAAI,SAAS,KAAK;AAChD,QAAM,CAAC,WAAW,YAAY,IAAI,SAAkC,IAAI;AACxE,QAAM,CAAC,cAAc,eAAe,IAAI,SAAS,CAAC;AAC5C,QAAA,cAAc,OAAuB,IAAI;AAC/C,QAAM,CAAC,UAAU,WAAW,IAAI,SAAS,KAAK,IAAI;AAC5C,QAAA,CAAC,iBAAiB,kBAAkB,IAAI;AAAA,IAC5C,KAAK,eAAe;AAAA,EACtB;AACM,QAAA,SAAS,kBAAkB,KAAK,KAAK;AAIrC,QAAA,YAAY,CAAC,MAAwB;AACzC,MAAE,gBAAgB;AAClB,gBAAY,KAAK,IAAI;AACF,uBAAA,KAAK,eAAe,EAAE;AACzC,iBAAa,IAAI;AAAA,EACnB;AAEA,YAAU,MAAM;AACd,QAAI,CAAC,WAAW;AACd;AAAA,IAAA;AAEF,UAAM,QAAQ,WAAW,MAAM,aAAa,IAAI,GAAG,IAAI;AAChD,WAAA,MAAM,aAAa,KAAK;AAAA,EAAA,GAC9B,CAAC,WAAW,YAAY,CAAC;AAE5B,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AAY,UAAU,KAAK,IAAI;AAAA,cAC/B,OAAM;AAAA,cACN,SAAS;AAAA,cAET,UAAA,oBAAC,WAAU,EAAA,eAAY,OAAO,CAAA;AAAA,YAAA;AAAA,UAAA;AAAA,QAChC,EAEJ,CAAA;AAAA,MAAA;AAAA,IAAA;AAAA,EAEJ;AAEJ;"}
@@ -0,0 +1,32 @@
1
+ import { FC } from 'react';
2
+ import { Collection, CollectionItem } from '../types';
3
+ type CollectionRowProps = {
4
+ collection: Collection;
5
+ expanded: boolean;
6
+ onToggleExpand(): void;
7
+ /** Hide write affordances (Rename, Delete, add-item) when true. */
8
+ readOnly?: boolean;
9
+ grabbed: {
10
+ collectionId: string;
11
+ index: number;
12
+ itemId: string;
13
+ } | null;
14
+ onGrabToggle(collectionId: string, index: number, itemId: string): void;
15
+ onGrabMove(direction: 'up' | 'down'): void;
16
+ onGrabCancel(): void;
17
+ onRename(id: string, name: string): void;
18
+ onDelete(id: string): void;
19
+ onShareCollection(id: string): Promise<void>;
20
+ onOpenItem(item: CollectionItem): void;
21
+ onShare(itemId: string): Promise<void>;
22
+ onAnnounce(message: string): void;
23
+ onDeleteItem(collectionId: string, itemId: string): void;
24
+ onMoveItem(fromCollectionId: string, fromIndex: number, toCollectionId: string, toIndex: number): void;
25
+ onAddItem(collectionId: string, item: Omit<CollectionItem, 'id' | 'createdAt' | 'updatedAt'>): CollectionItem;
26
+ onRenameItem(collectionId: string, itemId: string, updates: {
27
+ name: string;
28
+ description: string;
29
+ }): void;
30
+ };
31
+ export declare const CollectionRow: FC<CollectionRowProps>;
32
+ export {};