-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathExportSpecsModal.tsx
More file actions
319 lines (289 loc) · 10.1 KB
/
Copy pathExportSpecsModal.tsx
File metadata and controls
319 lines (289 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
import { App, Modal, Notice } from "obsidian";
import { StrictMode, useEffect, useMemo, useState } from "react";
import { createRoot, type Root } from "react-dom/client";
import type DiscourseGraphPlugin from "~/index";
import {
exportSchemaSelectionToVault,
ExportSaveCancelledError,
} from "~/utils/specExport";
import { getDgSchemaFileName } from "~/utils/specArchive";
import { getTemplateFiles } from "~/utils/templates";
import { SchemaSelectionPanel } from "~/components/SchemaSelectionPanel";
type ExportSpecsModalProps = {
plugin: DiscourseGraphPlugin;
onClose: () => void;
};
const getAllNodeTypeIds = (plugin: DiscourseGraphPlugin): string[] => {
return plugin.settings.nodeTypes.map((nodeType) => nodeType.id);
};
const getAllRelationTypeIds = (plugin: DiscourseGraphPlugin): string[] => {
return plugin.settings.relationTypes.map((relationType) => relationType.id);
};
const getAllRelationIds = (plugin: DiscourseGraphPlugin): string[] => {
return plugin.settings.discourseRelations.map((relation) => relation.id);
};
const getReferencedTemplateNames = (
nodeTypes: DiscourseGraphPlugin["settings"]["nodeTypes"],
): Set<string> => {
return new Set(
nodeTypes
.map((nodeType) => nodeType.template)
.filter((template): template is string => !!template),
);
};
export const openExportSpecsModal = (plugin: DiscourseGraphPlugin): void => {
new ExportSpecsModal(plugin.app, plugin).open();
};
const ExportSpecsContent = ({ plugin, onClose }: ExportSpecsModalProps) => {
const [selectedNodeTypeIds, setSelectedNodeTypeIds] = useState<Set<string>>(
() => new Set(getAllNodeTypeIds(plugin)),
);
const [selectedRelationTypeIds, setSelectedRelationTypeIds] = useState<
Set<string>
>(() => new Set(getAllRelationTypeIds(plugin)));
const [selectedRelationIds, setSelectedRelationIds] = useState<Set<string>>(
() => new Set(getAllRelationIds(plugin)),
);
const [selectedTemplateNames, setSelectedTemplateNames] = useState<
Set<string>
>(() => getReferencedTemplateNames(plugin.settings.nodeTypes));
const [isExporting, setIsExporting] = useState(false);
const outputFileName = getDgSchemaFileName(plugin.app.vault.getName());
const templateNames = useMemo(() => {
return getTemplateFiles(plugin.app);
}, [plugin.app]);
const requiredRelationTypeIds = useMemo(() => {
const requiredIds = new Set<string>();
for (const relation of plugin.settings.discourseRelations) {
if (selectedRelationIds.has(relation.id)) {
requiredIds.add(relation.relationshipTypeId);
}
}
return requiredIds;
}, [plugin.settings.discourseRelations, selectedRelationIds]);
const requiredNodeTypeIds = useMemo(() => {
const requiredIds = new Set<string>();
for (const relation of plugin.settings.discourseRelations) {
if (!selectedRelationIds.has(relation.id)) continue;
requiredIds.add(relation.sourceId);
requiredIds.add(relation.destinationId);
}
return requiredIds;
}, [plugin.settings.discourseRelations, selectedRelationIds]);
useEffect(() => {
setSelectedRelationTypeIds((previousSet) => {
const nextSet = new Set(previousSet);
let didChange = false;
for (const relationTypeId of requiredRelationTypeIds) {
if (!nextSet.has(relationTypeId)) {
nextSet.add(relationTypeId);
didChange = true;
}
}
return didChange ? nextSet : previousSet;
});
}, [requiredRelationTypeIds]);
useEffect(() => {
setSelectedNodeTypeIds((previousSet) => {
const nextSet = new Set(previousSet);
let didChange = false;
for (const nodeTypeId of requiredNodeTypeIds) {
if (!nextSet.has(nodeTypeId)) {
nextSet.add(nodeTypeId);
didChange = true;
}
}
return didChange ? nextSet : previousSet;
});
}, [requiredNodeTypeIds]);
const updateSet = (
previousSet: Set<string>,
id: string,
shouldSelect: boolean,
): Set<string> => {
const nextSet = new Set(previousSet);
if (shouldSelect) {
nextSet.add(id);
} else {
nextSet.delete(id);
}
return nextSet;
};
const toggleNodeType = (nodeTypeId: string, shouldSelect: boolean): void => {
if (!shouldSelect && requiredNodeTypeIds.has(nodeTypeId)) {
new Notice(
"This node type is required by a selected relation triple. Remove the triple first.",
);
return;
}
setSelectedNodeTypeIds((previousSet) =>
updateSet(previousSet, nodeTypeId, shouldSelect),
);
};
const toggleRelationType = (
relationTypeId: string,
shouldSelect: boolean,
): void => {
if (!shouldSelect && requiredRelationTypeIds.has(relationTypeId)) {
new Notice(
"This relation type is required by a selected relation triple. Remove the triple first.",
);
return;
}
setSelectedRelationTypeIds((previousSet) =>
updateSet(previousSet, relationTypeId, shouldSelect),
);
};
const toggleRelationTriple = (
relationId: string,
shouldSelect: boolean,
): void => {
setSelectedRelationIds((previousSet) =>
updateSet(previousSet, relationId, shouldSelect),
);
};
const toggleTemplate = (
templateName: string,
shouldSelect: boolean,
): void => {
setSelectedTemplateNames((previousSet) =>
updateSet(previousSet, templateName, shouldSelect),
);
};
const handleExport = async (): Promise<void> => {
const hasSelection =
selectedNodeTypeIds.size > 0 ||
selectedRelationTypeIds.size > 0 ||
selectedRelationIds.size > 0 ||
selectedTemplateNames.size > 0;
if (!hasSelection) {
new Notice("Select at least one schema item or template to export.");
return;
}
setIsExporting(true);
try {
const result = await exportSchemaSelectionToVault({
plugin,
selection: {
nodeTypeIds: [...selectedNodeTypeIds],
relationTypeIds: [...selectedRelationTypeIds],
discourseRelationIds: [...selectedRelationIds],
templateNames: [...selectedTemplateNames],
},
});
const autoIncludedCount =
result.dependencySummary.autoIncludedNodeTypeIds.length +
result.dependencySummary.autoIncludedRelationTypeIds.length;
const warningSuffix =
result.warnings.length > 0
? ` (${result.warnings.length} warning${result.warnings.length === 1 ? "" : "s"})`
: "";
new Notice(
`Exported schema to ${result.filePath}${warningSuffix}${
autoIncludedCount > 0
? ` with ${autoIncludedCount} auto-included dependency item(s).`
: "."
}`,
6000,
);
if (result.warnings.length > 0) {
for (const warning of result.warnings) {
new Notice(warning, 6000);
}
}
onClose();
} catch (error) {
if (error instanceof ExportSaveCancelledError) {
return;
}
console.error("Failed to export schema:", error);
const message = error instanceof Error ? error.message : String(error);
new Notice(`Schema export failed: ${message}`, 6000);
} finally {
setIsExporting(false);
}
};
return (
<div>
<h3 className="mb-2">Export discourse graph schema</h3>
<p className="text-muted mb-4 text-sm">
Select the node types, relation types, relation triples, and templates
to include in <strong>{outputFileName}</strong>.
</p>
<SchemaSelectionPanel
nodeTypes={plugin.settings.nodeTypes}
relationTypes={plugin.settings.relationTypes}
relationTriples={plugin.settings.discourseRelations}
templateNames={templateNames}
selectedNodeTypeIds={selectedNodeTypeIds}
selectedRelationTypeIds={selectedRelationTypeIds}
selectedRelationIds={selectedRelationIds}
selectedTemplateNames={selectedTemplateNames}
requiredNodeTypeIds={requiredNodeTypeIds}
requiredRelationTypeIds={requiredRelationTypeIds}
onSelectAllNodeTypes={() =>
setSelectedNodeTypeIds(new Set(getAllNodeTypeIds(plugin)))
}
onDeselectOptionalNodeTypes={() =>
setSelectedNodeTypeIds(new Set([...requiredNodeTypeIds]))
}
onToggleNodeType={toggleNodeType}
onSelectAllRelationTypes={() =>
setSelectedRelationTypeIds(new Set(getAllRelationTypeIds(plugin)))
}
onDeselectOptionalRelationTypes={() =>
setSelectedRelationTypeIds(new Set([...requiredRelationTypeIds]))
}
onToggleRelationType={toggleRelationType}
onSelectAllRelationTriples={() =>
setSelectedRelationIds(new Set(getAllRelationIds(plugin)))
}
onDeselectAllRelationTriples={() => setSelectedRelationIds(new Set())}
onToggleRelationTriple={toggleRelationTriple}
onSelectAllTemplates={() =>
setSelectedTemplateNames(new Set(templateNames))
}
onDeselectAllTemplates={() => setSelectedTemplateNames(new Set())}
onToggleTemplate={toggleTemplate}
emptyTemplateText="No templates found in your Templates folder."
/>
<div className="mt-6 flex justify-between">
<button type="button" onClick={onClose} className="px-4 py-2">
Cancel
</button>
<button
type="button"
className="!bg-accent !text-on-accent rounded px-4 py-2"
onClick={() => void handleExport()}
disabled={isExporting}
>
{isExporting ? "Exporting..." : "Export schema"}
</button>
</div>
</div>
);
};
export class ExportSpecsModal extends Modal {
private plugin: DiscourseGraphPlugin;
private root: Root | null = null;
constructor(app: App, plugin: DiscourseGraphPlugin) {
super(app);
this.plugin = plugin;
}
onOpen(): void {
const { contentEl } = this;
contentEl.empty();
this.root = createRoot(contentEl);
this.root.render(
<StrictMode>
<ExportSpecsContent plugin={this.plugin} onClose={() => this.close()} />
</StrictMode>,
);
}
onClose(): void {
if (this.root) {
this.root.unmount();
this.root = null;
}
}
}