-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRelationshipSettings.tsx
More file actions
349 lines (320 loc) · 11.4 KB
/
RelationshipSettings.tsx
File metadata and controls
349 lines (320 loc) · 11.4 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
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
import { useState } from "react";
import { DiscourseRelation, DiscourseRelationType } from "~/types";
import { Notice } from "obsidian";
import { usePlugin } from "./PluginContext";
import { ConfirmationModal } from "./ConfirmationModal";
import {
getNodeTypeById,
getImportInfo,
formatImportSource,
isAcceptedSchema,
isProvisionalSchema,
} from "~/utils/typeUtils";
import generateUid from "~/utils/generateUid";
const RelationshipSettings = () => {
const plugin = usePlugin();
const [discourseRelations, setDiscourseRelations] = useState<
DiscourseRelation[]
>(() => plugin.settings.discourseRelations ?? []);
const [errors, setErrors] = useState<Record<number, string>>({});
const findRelationTypeById = (
id: string,
): DiscourseRelationType | undefined => {
return plugin.settings.relationTypes.find((relType) => relType.id === id);
};
type EditableFieldKey = keyof Omit<
DiscourseRelation,
"id" | "modified" | "created" | "importedFromRid" | "status"
>;
const saveSettings = (relations: DiscourseRelation[]): void => {
const newErrors: Record<number, string> = {};
const completeRelations = relations.filter(
(r) => r.relationshipTypeId && r.sourceId && r.destinationId,
);
// Check for duplicates among complete relations
const seenKeys = new Map<string, number>();
for (const r of completeRelations) {
const idx = relations.indexOf(r);
const key = `${r.relationshipTypeId}-${r.sourceId}-${r.destinationId}`;
const prev = seenKeys.get(key);
if (prev !== undefined) {
newErrors[idx] = "Duplicate relation";
if (!newErrors[prev]) newErrors[prev] = "Duplicate relation";
}
seenKeys.set(key, idx);
}
setErrors(newErrors);
if (Object.keys(newErrors).length > 0) return;
// Persist complete local relations + all imported relations
const importedRelations = relations.filter((r) => r.importedFromRid);
plugin.settings.discourseRelations = [
...completeRelations.filter((r) => !r.importedFromRid),
...importedRelations,
];
void plugin.saveSettings();
};
const handleRelationChange = (
index: number,
field: EditableFieldKey,
value: string,
): void => {
const updatedRelations = [...discourseRelations];
const now = new Date().getTime();
if (!updatedRelations[index]) {
const newId = generateUid("rel3");
updatedRelations[index] = {
id: newId,
sourceId: "",
destinationId: "",
relationshipTypeId: "",
created: now,
modified: now,
};
}
updatedRelations[index] = {
...updatedRelations[index],
[field]: value,
modified: now,
};
setDiscourseRelations(updatedRelations);
saveSettings(updatedRelations);
};
const handleAddRelation = (): void => {
const newId = generateUid("rel3");
const now = new Date().getTime();
const updatedRelations = [
...discourseRelations,
{
id: newId,
sourceId: "",
destinationId: "",
relationshipTypeId: "",
created: now,
modified: now,
},
];
setDiscourseRelations(updatedRelations);
};
const confirmDeleteRelation = (index: number): void => {
const relation = discourseRelations[index] || {
sourceId: "",
destinationId: "",
relationshipTypeId: "",
};
let message = "Are you sure you want to delete this relation?";
// If the relation has source and target nodes, provide more context
if (
relation.sourceId &&
relation.destinationId &&
relation.relationshipTypeId
) {
const sourceNode = getNodeTypeById(plugin, relation.sourceId);
const targetNode = getNodeTypeById(plugin, relation.destinationId);
const relationType = findRelationTypeById(relation.relationshipTypeId);
if (sourceNode && targetNode && relationType) {
message = `Are you sure you want to delete the relation between "${sourceNode.name}" and "${targetNode.name}" (${relationType.label})?`;
}
}
const modal = new ConfirmationModal(plugin.app, {
title: "Delete Relation",
message,
onConfirm: () => handleDeleteRelation(index),
});
modal.open();
};
const handleAcceptRelation = async (index: number): Promise<void> => {
const updatedRelations = [...discourseRelations];
const relation = updatedRelations[index];
if (!relation) return;
updatedRelations[index] = { ...relation, status: "accepted" };
// Cascade: also accept the relation type if it is still provisional
const updatedRelationTypes = [...plugin.settings.relationTypes];
const relTypeIndex = updatedRelationTypes.findIndex(
(rt) => rt.id === relation.relationshipTypeId,
);
if (
relTypeIndex >= 0 &&
isProvisionalSchema(updatedRelationTypes[relTypeIndex]!)
) {
updatedRelationTypes[relTypeIndex] = {
...updatedRelationTypes[relTypeIndex]!,
status: "accepted",
};
plugin.settings.relationTypes = updatedRelationTypes;
}
setDiscourseRelations(updatedRelations);
plugin.settings.discourseRelations = updatedRelations;
await plugin.saveSettings();
};
const handleDeleteRelation = async (index: number): Promise<void> => {
const updatedRelations = discourseRelations.filter((_, i) => i !== index);
setDiscourseRelations(updatedRelations);
plugin.settings.discourseRelations = updatedRelations;
await plugin.saveSettings();
new Notice("Relation deleted");
};
const localRelations = discourseRelations.filter(
(relation) => !relation.importedFromRid,
);
const importedRelations = discourseRelations.filter(
(relation) => relation.importedFromRid,
);
const renderRelationItem = (relation: DiscourseRelation, index: number) => {
const importInfo = getImportInfo(relation.importedFromRid);
const isImported = importInfo.isImported;
const isProvisional = isProvisionalSchema(relation);
const spaceName = importInfo.spaceUri
? formatImportSource(importInfo.spaceUri, plugin.settings.spaceNames)
: "imported space";
const error = errors[index];
return (
<div key={index} className="setting-item">
<div className="flex w-full flex-col gap-1">
<div className="flex gap-2">
<select
value={relation.sourceId}
onChange={(e) =>
handleRelationChange(index, "sourceId", e.target.value)
}
className={`flex-1 pl-2 ${error ? "input-error" : ""}`}
disabled={isImported}
>
<option value="">Source Node Type</option>
{plugin.settings.nodeTypes.map((nodeType) => (
<option key={nodeType.id} value={nodeType.id}>
{nodeType.name}
</option>
))}
</select>
<select
value={relation.relationshipTypeId}
onChange={(e) =>
handleRelationChange(
index,
"relationshipTypeId",
e.target.value,
)
}
className={`flex-1 pl-2 ${error ? "input-error" : ""}`}
disabled={isImported}
>
<option value="">Relation Type</option>
{(isImported
? plugin.settings.relationTypes
: plugin.settings.relationTypes.filter(isAcceptedSchema)
).map((relType) => (
<option key={relType.id} value={relType.id}>
{relType.label} / {relType.complement}
</option>
))}
</select>
<select
value={relation.destinationId}
onChange={(e) =>
handleRelationChange(index, "destinationId", e.target.value)
}
className={`flex-1 pl-2 ${error ? "input-error" : ""}`}
disabled={isImported}
>
<option value="">Target Node Type</option>
{plugin.settings.nodeTypes.map((nodeType) => (
<option key={nodeType.id} value={nodeType.id}>
{nodeType.name}
</option>
))}
</select>
{isImported ? (
<div className="flex gap-2">
{isProvisional && (
<button
onClick={() => void handleAcceptRelation(index)}
className="p-2"
title={`Accept this relation triplet from ${spaceName} to create instances of this relation`}
>
Accept
</button>
)}
<button
onClick={() => confirmDeleteRelation(index)}
className="mod-warning p-2"
>
Delete
</button>
</div>
) : (
<button
onClick={() => confirmDeleteRelation(index)}
className="mod-warning p-2"
>
Delete
</button>
)}
</div>
{error && <div className="text-error text-xs">{error}</div>}
{isImported && (
<div className="text-muted flex items-center gap-2 text-xs">
{isProvisional && (
<span className="rounded bg-yellow-100 px-1.5 py-0.5 text-xs font-medium text-yellow-800">
Provisional
</span>
)}
{importInfo.spaceUri && (
<span>
from{" "}
{formatImportSource(
importInfo.spaceUri,
plugin.settings.spaceNames,
)}
</span>
)}
</div>
)}
</div>
</div>
);
};
return (
<div className="discourse-relations">
{plugin.settings.nodeTypes.length === 0 ? (
<div>You need to create some node types first.</div>
) : plugin.settings.relationTypes.length === 0 ? (
<div>You need to create some relation types first.</div>
) : (
<>
{localRelations.length > 0 && (
<div>
<h4 className="text-muted mb-2 text-sm font-semibold uppercase tracking-wide">
Local
</h4>
{localRelations.map((relation) => {
const index = discourseRelations.indexOf(relation);
return renderRelationItem(relation, index);
})}
</div>
)}
{importedRelations.length > 0 && (
<div className="border-modifier-border mt-6 border-t pt-4">
<h4 className="text-muted mb-2 text-sm font-semibold uppercase tracking-wide">
Imported
</h4>
<div className="border-modifier-border rounded border bg-secondary p-2">
{importedRelations.map((relation) => {
const index = discourseRelations.indexOf(relation);
return renderRelationItem(relation, index);
})}
</div>
</div>
)}
<div className="setting-item mt-4">
<div className="flex gap-2">
<button onClick={handleAddRelation} className="p-2">
Add Relation
</button>
</div>
</div>
</>
)}
</div>
);
};
export default RelationshipSettings;