-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDiscourseContextView.tsx
More file actions
290 lines (255 loc) · 8.31 KB
/
Copy pathDiscourseContextView.tsx
File metadata and controls
290 lines (255 loc) · 8.31 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
import {
ItemView,
TFile,
WorkspaceLeaf,
Notice,
setIcon,
setTooltip,
} from "obsidian";
import { createRoot, Root } from "react-dom/client";
import DiscourseGraphPlugin from "~/index";
import { getDiscourseNodeFormatExpression } from "~/utils/getDiscourseNodeFormatExpression";
import { RelationshipSection } from "~/components/RelationshipSection";
import { VIEW_TYPE_DISCOURSE_CONTEXT } from "~/types";
import { PluginProvider, usePlugin } from "~/components/PluginContext";
import {
getNodeTypeById,
getAndFormatImportSource,
getUserNameById,
} from "~/utils/typeUtils";
import { refreshImportedFile } from "~/utils/importNodes";
import { PublishGroupDropdown } from "~/components/PublishGroupDropdown";
import { createBaseForNodeType } from "~/utils/baseForNodeType";
import { useState } from "react";
type DiscourseContextProps = {
activeFile: TFile | null;
};
type InfoTooltipProps = {
content: string;
};
export const InfoTooltip = ({ content }: InfoTooltipProps) => (
<button
ref={(el) => {
if (el) setTooltip(el, content);
}}
className="clickable-icon text-muted hover:text-normal flex h-4 w-4 items-center justify-center"
>
<div ref={(el) => (el && setIcon(el, "info")) || undefined} />
</button>
);
const DiscourseContext = ({ activeFile }: DiscourseContextProps) => {
const plugin = usePlugin();
const [isRefreshing, setIsRefreshing] = useState(false);
const extractContentFromTitle = (format: string, title: string): string => {
if (!format) return "";
const regex = getDiscourseNodeFormatExpression(format);
const match = title.match(regex);
return match?.[1] ?? title;
};
const handleRefresh = async () => {
if (!activeFile || isRefreshing) return;
setIsRefreshing(true);
try {
const result = await refreshImportedFile({ plugin, file: activeFile });
if (result.success) {
new Notice("File refreshed successfully", 3000);
} else {
new Notice(
`Failed to refresh file: ${result.error || "Unknown error"}`,
5000,
);
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
new Notice(`Refresh failed: ${errorMessage}`, 5000);
console.error("Refresh failed:", error);
} finally {
setIsRefreshing(false);
}
};
const renderContent = () => {
if (!activeFile) {
return <div>No file is open</div>;
}
const fileMetadata = plugin.app.metadataCache.getFileCache(activeFile);
if (!fileMetadata) {
return <div>File metadata not available</div>;
}
const frontmatter = fileMetadata.frontmatter;
if (!frontmatter) {
return <div>No discourse node data found</div>;
}
if (!frontmatter.nodeTypeId) {
return <div>Not a discourse node (no nodeTypeId)</div>;
}
const nodeType = getNodeTypeById(plugin, frontmatter.nodeTypeId as string);
if (!nodeType) {
return <div>Unknown node type: {frontmatter.nodeTypeId}</div>;
}
const isImported = !!frontmatter.importedFromRid;
const modifiedAt =
typeof frontmatter.lastModified === "number"
? frontmatter.lastModified
: activeFile.stat.mtime;
const sourceDates =
isImported && activeFile?.stat
? {
createdAt: new Date(activeFile.stat.ctime).toLocaleString(),
modifiedAt: new Date(modifiedAt).toLocaleString(),
}
: null;
const canPublish =
plugin.settings.syncModeEnabled &&
!isImported &&
!!frontmatter.nodeTypeId;
const formattedVaultName = isImported
? getAndFormatImportSource(
frontmatter.importedFromRid as string,
plugin.settings.spaceNames,
)
: "";
return (
<>
<div className="mb-6">
<div className="text-md mb-2 flex items-center gap-2 font-bold">
{nodeType.color && (
<div
className="h-4 w-4 rounded-full"
style={{ backgroundColor: nodeType.color }}
/>
)}
{nodeType.name || "Unnamed Node Type"}
<button
ref={(el) => {
if (el) {
const name = nodeType.name || "Unnamed Node Type";
setTooltip(el, `Create Base view for ${name} nodes`);
}
}}
onClick={() => {
void createBaseForNodeType(plugin, nodeType);
}}
className="clickable-icon ml-1"
>
<div
ref={(el) => (el && setIcon(el, "layout-list")) || undefined}
/>
</button>
{isImported && (
<button
onClick={() => {
void handleRefresh();
}}
disabled={isRefreshing}
className="ml-auto rounded border px-2 py-1 text-xs"
title="Refresh from source"
>
{isRefreshing ? "Refreshing..." : "🔄 Refresh"}
</button>
)}
{canPublish && (
<PublishGroupDropdown plugin={plugin} file={activeFile} />
)}
</div>
{isImported && (
<div className="mt-3 flex items-center gap-1.5">
<span className="cursor-default rounded bg-amber-300 px-2 py-1 text-xs font-semibold text-amber-950 dark:bg-amber-900/40 dark:text-amber-300">
View only
</span>
<InfoTooltip
content={
`Imported from ${formattedVaultName}. ` +
(frontmatter.authorId &&
typeof frontmatter.authorId === "number"
? `By ${getUserNameById(plugin, frontmatter.authorId)}. `
: "") +
"Direct edits will be overwritten when refreshed."
}
/>
</div>
)}
{nodeType.format && (
<div className="mb-1 mt-2">
<span className="font-bold">Content: </span>
{extractContentFromTitle(nodeType.format, activeFile.basename)}
</div>
)}
{isImported && sourceDates && (
<div className="text-modifier-text mt-2 text-xs">
<div>Created in source: {sourceDates.createdAt}</div>
<div>Last modified in source: {sourceDates.modifiedAt}</div>
</div>
)}
</div>
<div>
<h4 className="dg-h4 border-modifier-border mb-3 mt-4 border-b pb-1">
Relationships
</h4>
<RelationshipSection key={activeFile.path} activeFile={activeFile} />
</div>
</>
);
};
return (
<div>
<h3 className="dg-h3">Discourse context</h3>
{renderContent()}
</div>
);
};
export class DiscourseContextView extends ItemView {
private plugin: DiscourseGraphPlugin;
private activeFile: TFile | null = null;
private root: Root | null = null;
constructor(leaf: WorkspaceLeaf, plugin: DiscourseGraphPlugin) {
super(leaf);
this.plugin = plugin;
}
setActiveFile(file: TFile | null): void {
this.activeFile = file;
this.updateView();
}
getViewType(): string {
return VIEW_TYPE_DISCOURSE_CONTEXT;
}
getDisplayText(): string {
return "Discourse context";
}
getIcon(): string {
return "telescope";
}
// eslint-disable-next-line @typescript-eslint/require-await -- required by obsidian
async onOpen(): Promise<void> {
const container = this.containerEl.children[1];
if (container) {
container.empty();
container.addClass("discourse-context-container");
this.root = createRoot(container);
this.activeFile = this.app.workspace.getActiveFile();
this.updateView();
this.registerEvent(
this.app.workspace.on("file-open", (file) => {
this.activeFile = file;
this.updateView();
}),
);
}
}
updateView(): void {
if (this.root) {
this.root.render(
<PluginProvider plugin={this.plugin}>
<DiscourseContext activeFile={this.activeFile} />
</PluginProvider>,
);
}
}
// eslint-disable-next-line @typescript-eslint/require-await -- required by obsidian
async onClose(): Promise<void> {
if (this.root) {
this.root.unmount();
this.root = null;
}
}
}