@arronqzy/react-blueprint 1.0.19 → 1.0.22

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.
@@ -0,0 +1,448 @@
1
+ import { useEffect, useMemo, useRef, useState } from "react";
2
+ import {
3
+ Button,
4
+ Dialog,
5
+ DialogContent,
6
+ DialogDescription,
7
+ DialogFooter,
8
+ DialogHeader,
9
+ DialogTitle,
10
+ Label,
11
+ Textarea,
12
+ cn,
13
+ } from "@arronqzy/ui";
14
+ import {
15
+ downloadApiCollectionTemplate,
16
+ getApiCollectionTemplateJson,
17
+ type ApiCollectionRecord,
18
+ type ApiCollectionSyncResult,
19
+ } from "@arronqzy/blueprint-dsl";
20
+ import { useI18nOptional as useI18n } from "@arronqzy/i18n/react";
21
+
22
+ import {
23
+ getApiCollectionRecords,
24
+ removeApiCollection,
25
+ subscribeApiCollections,
26
+ upsertApiCollectionFromJson,
27
+ } from "../library/api-collection-store";
28
+ import { ConfigHintIcon } from "./ConfigHintIcon";
29
+
30
+ export type ApiCollectionUploadStartInfo = {
31
+ fileNames: string[];
32
+ count: number;
33
+ };
34
+
35
+ export type ApiCollectionUploadSuccessInfo = {
36
+ collections: Array<{
37
+ id: string;
38
+ name: string;
39
+ apiCount: number;
40
+ persistLocal: boolean;
41
+ }>;
42
+ };
43
+
44
+ export type ApiCollectionManagerDialogProps = {
45
+ open: boolean;
46
+ onOpenChange: (open: boolean) => void;
47
+ onUploadStart?: (info: ApiCollectionUploadStartInfo) => void;
48
+ onUploadSuccess?: (info: ApiCollectionUploadSuccessInfo) => void;
49
+ /** 按 URL 同步当前蓝图里的 Fetch 节点 */
50
+ onSyncCollection?: (
51
+ record: ApiCollectionRecord
52
+ ) => ApiCollectionSyncResult | Promise<ApiCollectionSyncResult>;
53
+ };
54
+
55
+ function formatSyncResultMessage(
56
+ t: (key: string, params?: Record<string, string | number>) => string,
57
+ result: ApiCollectionSyncResult
58
+ ): string {
59
+ if (result.updatedCount <= 0) {
60
+ return t("blueprint.apiCollection.syncNone");
61
+ }
62
+ const names = result.nodes.map((node) => node.label).join("、");
63
+ return t("blueprint.apiCollection.syncSuccessDetail", {
64
+ count: result.updatedCount,
65
+ names,
66
+ });
67
+ }
68
+
69
+ export function ApiCollectionManagerDialog({
70
+ open,
71
+ onOpenChange,
72
+ onUploadStart,
73
+ onUploadSuccess,
74
+ onSyncCollection,
75
+ }: ApiCollectionManagerDialogProps) {
76
+ const { t } = useI18n();
77
+ const fileInputRef = useRef<HTMLInputElement | null>(null);
78
+ const [records, setRecords] = useState<ApiCollectionRecord[]>(() =>
79
+ getApiCollectionRecords()
80
+ );
81
+ const [persistLocalDefault, setPersistLocalDefault] = useState(true);
82
+ const [error, setError] = useState<string | null>(null);
83
+ const [busy, setBusy] = useState(false);
84
+
85
+ const [editorOpen, setEditorOpen] = useState(false);
86
+ const [editingId, setEditingId] = useState<string | null>(null);
87
+ const [editorJson, setEditorJson] = useState("");
88
+ const [editorPersistLocal, setEditorPersistLocal] = useState(true);
89
+ const [editorError, setEditorError] = useState<string | null>(null);
90
+
91
+ useEffect(() => {
92
+ if (!open) return;
93
+ setRecords([...getApiCollectionRecords()]);
94
+ return subscribeApiCollections(() => setRecords([...getApiCollectionRecords()]));
95
+ }, [open]);
96
+
97
+ const sorted = useMemo(
98
+ () => [...records].sort((a, b) => b.updatedAt - a.updatedAt),
99
+ [records]
100
+ );
101
+
102
+ const openCreateEditor = () => {
103
+ setEditingId(null);
104
+ setEditorJson(getApiCollectionTemplateJson());
105
+ setEditorPersistLocal(persistLocalDefault);
106
+ setEditorError(null);
107
+ setEditorOpen(true);
108
+ };
109
+
110
+ const openEditEditor = (record: ApiCollectionRecord) => {
111
+ setEditingId(record.id);
112
+ setEditorJson(record.rawJson);
113
+ setEditorPersistLocal(record.persistLocal);
114
+ setEditorError(null);
115
+ setEditorOpen(true);
116
+ };
117
+
118
+ const handleSaveEditor = async () => {
119
+ setBusy(true);
120
+ setEditorError(null);
121
+ try {
122
+ const result = await upsertApiCollectionFromJson({
123
+ id: editingId ?? undefined,
124
+ rawJson: editorJson,
125
+ persistLocal: editorPersistLocal,
126
+ });
127
+ if (!result.ok) {
128
+ setEditorError(result.error);
129
+ return;
130
+ }
131
+ setEditorOpen(false);
132
+ } finally {
133
+ setBusy(false);
134
+ }
135
+ };
136
+
137
+ const handleDelete = async (id: string) => {
138
+ const ok = window.confirm(t("blueprint.apiCollection.confirmDelete"));
139
+ if (!ok) return;
140
+ setBusy(true);
141
+ setError(null);
142
+ try {
143
+ await removeApiCollection(id);
144
+ } catch (err) {
145
+ setError(err instanceof Error ? err.message : String(err));
146
+ } finally {
147
+ setBusy(false);
148
+ }
149
+ };
150
+
151
+ const handleSync = async (record: ApiCollectionRecord) => {
152
+ if (!onSyncCollection) {
153
+ setError(t("blueprint.apiCollection.syncUnavailable"));
154
+ return;
155
+ }
156
+ setBusy(true);
157
+ setError(null);
158
+ try {
159
+ const result = await onSyncCollection(record);
160
+ window.alert(formatSyncResultMessage(t, result));
161
+ } catch (err) {
162
+ setError(err instanceof Error ? err.message : String(err));
163
+ } finally {
164
+ setBusy(false);
165
+ }
166
+ };
167
+
168
+ const handleSyncFromEditor = async () => {
169
+ setBusy(true);
170
+ setEditorError(null);
171
+ try {
172
+ const result = await upsertApiCollectionFromJson({
173
+ id: editingId ?? undefined,
174
+ rawJson: editorJson,
175
+ persistLocal: editorPersistLocal,
176
+ });
177
+ if (!result.ok) {
178
+ setEditorError(result.error);
179
+ return;
180
+ }
181
+ setEditingId(result.record.id);
182
+ if (!onSyncCollection) {
183
+ setEditorError(t("blueprint.apiCollection.syncUnavailable"));
184
+ return;
185
+ }
186
+ const syncResult = await onSyncCollection(result.record);
187
+ window.alert(formatSyncResultMessage(t, syncResult));
188
+ } catch (err) {
189
+ setEditorError(err instanceof Error ? err.message : String(err));
190
+ } finally {
191
+ setBusy(false);
192
+ }
193
+ };
194
+
195
+ const handleFiles = async (files: FileList | null) => {
196
+ if (!files || files.length === 0) return;
197
+ const list = Array.from(files);
198
+ setBusy(true);
199
+ setError(null);
200
+ onUploadStart?.({
201
+ fileNames: list.map((file) => file.name),
202
+ count: list.length,
203
+ });
204
+
205
+ const uploaded: ApiCollectionUploadSuccessInfo["collections"] = [];
206
+ const errors: string[] = [];
207
+
208
+ try {
209
+ for (const file of list) {
210
+ const text = await file.text();
211
+ const result = await upsertApiCollectionFromJson({
212
+ rawJson: text,
213
+ persistLocal: persistLocalDefault,
214
+ fallbackName: file.name,
215
+ });
216
+ if (!result.ok) {
217
+ errors.push(`${file.name}: ${result.error}`);
218
+ continue;
219
+ }
220
+ uploaded.push({
221
+ id: result.record.id,
222
+ name: result.record.name,
223
+ apiCount: result.record.document.apis.length,
224
+ persistLocal: result.record.persistLocal,
225
+ });
226
+ }
227
+ if (uploaded.length > 0) {
228
+ onUploadSuccess?.({ collections: uploaded });
229
+ }
230
+ if (errors.length > 0) {
231
+ setError(errors.join("\n"));
232
+ }
233
+ } catch (err) {
234
+ setError(err instanceof Error ? err.message : String(err));
235
+ } finally {
236
+ setBusy(false);
237
+ if (fileInputRef.current) fileInputRef.current.value = "";
238
+ }
239
+ };
240
+
241
+ return (
242
+ <>
243
+ <Dialog open={open} onOpenChange={onOpenChange}>
244
+ <DialogContent className="flex max-h-[90vh] w-[min(96vw,960px)] flex-col sm:max-w-[960px]">
245
+ <DialogHeader>
246
+ <DialogTitle>{t("blueprint.apiCollection.managerTitle")}</DialogTitle>
247
+ <DialogDescription>
248
+ {t("blueprint.apiCollection.managerDescription")}
249
+ </DialogDescription>
250
+ </DialogHeader>
251
+
252
+ <div className="flex flex-wrap items-center gap-2 border-b border-border/60 pb-3">
253
+ <Button
254
+ type="button"
255
+ size="sm"
256
+ disabled={busy}
257
+ onClick={() => fileInputRef.current?.click()}
258
+ >
259
+ {t("blueprint.apiCollection.upload")}
260
+ </Button>
261
+ <Button
262
+ type="button"
263
+ size="sm"
264
+ variant="outline"
265
+ disabled={busy}
266
+ onClick={openCreateEditor}
267
+ >
268
+ {t("blueprint.apiCollection.create")}
269
+ </Button>
270
+ <Button
271
+ type="button"
272
+ size="sm"
273
+ variant="outline"
274
+ onClick={() => downloadApiCollectionTemplate()}
275
+ >
276
+ {t("blueprint.apiCollection.downloadTemplate")}
277
+ </Button>
278
+ <label className="ml-auto flex cursor-pointer items-center gap-2 text-[11px] text-muted-foreground">
279
+ <input
280
+ type="checkbox"
281
+ className="h-3.5 w-3.5 rounded border border-input"
282
+ checked={persistLocalDefault}
283
+ onChange={(e) => setPersistLocalDefault(e.target.checked)}
284
+ />
285
+ {t("blueprint.apiCollection.persistLocal")}
286
+ <ConfigHintIcon label={t("blueprint.apiCollection.persistLocal")}>
287
+ {t("blueprint.apiCollection.persistLocalHint")}
288
+ </ConfigHintIcon>
289
+ </label>
290
+ <input
291
+ ref={fileInputRef}
292
+ type="file"
293
+ accept="application/json,.json"
294
+ multiple
295
+ className="hidden"
296
+ onChange={(e) => void handleFiles(e.target.files)}
297
+ />
298
+ </div>
299
+
300
+ {error ? (
301
+ <pre className="max-h-24 overflow-auto whitespace-pre-wrap rounded border border-destructive/40 bg-destructive/5 p-2 text-[11px] text-destructive">
302
+ {error}
303
+ </pre>
304
+ ) : null}
305
+
306
+ <div className="min-h-0 flex-1 space-y-2 overflow-auto py-1">
307
+ {sorted.length === 0 ? (
308
+ <p className="py-8 text-center text-xs text-muted-foreground">
309
+ {t("blueprint.apiCollection.empty")}
310
+ </p>
311
+ ) : (
312
+ sorted.map((record) => (
313
+ <div
314
+ key={record.id}
315
+ className="flex items-start gap-2 rounded-md border border-border/70 bg-muted/20 px-2.5 py-2"
316
+ >
317
+ <div className="min-w-0 flex-1">
318
+ <div className="truncate text-xs font-medium text-foreground">
319
+ {record.name}
320
+ </div>
321
+ <div className="mt-0.5 flex flex-wrap gap-x-2 text-[10px] text-muted-foreground">
322
+ <span>
323
+ {t("blueprint.apiCollection.apiCount", {
324
+ count: record.document.apis.length,
325
+ })}
326
+ </span>
327
+ <span>
328
+ {record.persistLocal
329
+ ? t("blueprint.apiCollection.persisted")
330
+ : t("blueprint.apiCollection.memoryOnly")}
331
+ </span>
332
+ </div>
333
+ </div>
334
+ <Button
335
+ type="button"
336
+ size="sm"
337
+ variant="outline"
338
+ className="h-7 shrink-0 px-2 text-[11px]"
339
+ disabled={busy || !onSyncCollection}
340
+ title={t("blueprint.apiCollection.syncHint")}
341
+ onClick={() => void handleSync(record)}
342
+ >
343
+ {t("blueprint.apiCollection.sync")}
344
+ </Button>
345
+ <Button
346
+ type="button"
347
+ size="sm"
348
+ variant="outline"
349
+ className="h-7 shrink-0 px-2 text-[11px]"
350
+ disabled={busy}
351
+ onClick={() => openEditEditor(record)}
352
+ >
353
+ {t("common.edit")}
354
+ </Button>
355
+ <Button
356
+ type="button"
357
+ size="sm"
358
+ variant="outline"
359
+ className="h-7 shrink-0 px-2 text-[11px] text-destructive hover:bg-destructive/10"
360
+ disabled={busy}
361
+ onClick={() => void handleDelete(record.id)}
362
+ >
363
+ {t("common.delete")}
364
+ </Button>
365
+ </div>
366
+ ))
367
+ )}
368
+ </div>
369
+
370
+ <DialogFooter>
371
+ <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
372
+ {t("common.close")}
373
+ </Button>
374
+ </DialogFooter>
375
+ </DialogContent>
376
+ </Dialog>
377
+
378
+ <Dialog open={editorOpen} onOpenChange={setEditorOpen}>
379
+ <DialogContent className="flex max-h-[90vh] w-[min(96vw,1100px)] flex-col sm:max-w-[1100px]">
380
+ <DialogHeader>
381
+ <DialogTitle>
382
+ {editingId
383
+ ? t("blueprint.apiCollection.editTitle")
384
+ : t("blueprint.apiCollection.createTitle")}
385
+ </DialogTitle>
386
+ <DialogDescription>
387
+ {t("blueprint.apiCollection.editorDescription")}
388
+ </DialogDescription>
389
+ </DialogHeader>
390
+
391
+ <div className="space-y-2">
392
+ <div className="flex items-center justify-between gap-2">
393
+ <Label className="text-xs">
394
+ {t("blueprint.apiCollection.jsonEditor")}
395
+ </Label>
396
+ <label className="flex cursor-pointer items-center gap-2 text-[11px] text-muted-foreground">
397
+ <input
398
+ type="checkbox"
399
+ className="h-3.5 w-3.5 rounded border border-input"
400
+ checked={editorPersistLocal}
401
+ onChange={(e) => setEditorPersistLocal(e.target.checked)}
402
+ />
403
+ {t("blueprint.apiCollection.persistLocal")}
404
+ </label>
405
+ </div>
406
+ <Textarea
407
+ value={editorJson}
408
+ onChange={(e) => setEditorJson(e.target.value)}
409
+ className={cn(
410
+ "min-h-[min(62vh,640px)] flex-1 font-mono text-[12px] leading-relaxed",
411
+ editorError && "border-destructive"
412
+ )}
413
+ spellCheck={false}
414
+ />
415
+ {editorError ? (
416
+ <p className="text-[11px] text-destructive">{editorError}</p>
417
+ ) : null}
418
+ </div>
419
+
420
+ <DialogFooter>
421
+ <Button
422
+ type="button"
423
+ variant="outline"
424
+ onClick={() => downloadApiCollectionTemplate()}
425
+ >
426
+ {t("blueprint.apiCollection.downloadTemplate")}
427
+ </Button>
428
+ <Button
429
+ type="button"
430
+ variant="outline"
431
+ disabled={busy || !onSyncCollection}
432
+ title={t("blueprint.apiCollection.syncHint")}
433
+ onClick={() => void handleSyncFromEditor()}
434
+ >
435
+ {t("blueprint.apiCollection.sync")}
436
+ </Button>
437
+ <Button type="button" variant="outline" onClick={() => setEditorOpen(false)}>
438
+ {t("common.cancel")}
439
+ </Button>
440
+ <Button type="button" disabled={busy} onClick={() => void handleSaveEditor()}>
441
+ {t("common.save")}
442
+ </Button>
443
+ </DialogFooter>
444
+ </DialogContent>
445
+ </Dialog>
446
+ </>
447
+ );
448
+ }
@@ -5,7 +5,7 @@ import { useI18nOptional as useI18n } from "@arronqzy/i18n/react";
5
5
 
6
6
  import type { BlueprintGraphNode } from "../graph/document";
7
7
  import { resolveNodeClockConfig } from "../graph/document";
8
- import { ConfigFieldLabel, ConfigSectionTitle } from "./ConfigHintIcon";
8
+ import { ConfigFieldLabel, ConfigHintIcon, ConfigSectionTitle } from "./ConfigHintIcon";
9
9
 
10
10
  export type ClockNodeConfigPanelProps = {
11
11
  node: BlueprintGraphNode;
@@ -109,16 +109,19 @@ export function ClockNodeConfigPanel({
109
109
  />
110
110
  </label>
111
111
 
112
- <label className="flex items-start gap-2">
112
+ <label className="flex items-center gap-2">
113
113
  <input
114
114
  type="checkbox"
115
115
  checked={clockConfig.emitImmediately}
116
116
  onChange={handleEmitImmediatelyChange}
117
- className="mt-0.5 h-3.5 w-3.5 shrink-0 rounded border border-input"
117
+ className="h-3.5 w-3.5 shrink-0 rounded border border-input"
118
118
  />
119
- <span className="text-[11px] leading-relaxed text-muted-foreground">
119
+ <span className="text-[11px] text-muted-foreground">
120
120
  {t("blueprint.config.emitImmediately")}
121
121
  </span>
122
+ <ConfigHintIcon label={t("blueprint.config.emitImmediately")}>
123
+ {t("blueprint.config.emitImmediatelyHint")}
124
+ </ConfigHintIcon>
122
125
  </label>
123
126
 
124
127
  <label className="block space-y-1">
@@ -31,11 +31,24 @@ export function ConfigHintIcon({
31
31
  type="button"
32
32
  className={
33
33
  className ??
34
- "inline-flex h-4 w-4 shrink-0 items-center justify-center rounded-full border border-border text-[10px] leading-none text-muted-foreground hover:bg-accent/50"
34
+ "inline-flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
35
35
  }
36
36
  aria-label={t("common.hintAria", { label: resolvedLabel })}
37
37
  >
38
- ?
38
+ <svg
39
+ viewBox="0 0 24 24"
40
+ fill="none"
41
+ stroke="currentColor"
42
+ strokeWidth={2}
43
+ strokeLinecap="round"
44
+ strokeLinejoin="round"
45
+ className="h-3.5 w-3.5"
46
+ aria-hidden="true"
47
+ >
48
+ <circle cx="12" cy="12" r="10" />
49
+ <path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3" />
50
+ <path d="M12 17h.01" />
51
+ </svg>
39
52
  </button>
40
53
  </TooltipTrigger>
41
54
  <TooltipContent