-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDiscoverSharedNodesDialog.tsx
More file actions
197 lines (188 loc) · 5.86 KB
/
Copy pathDiscoverSharedNodesDialog.tsx
File metadata and controls
197 lines (188 loc) · 5.86 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
import React, { useEffect, useState } from "react";
import {
Button,
Callout,
Classes,
Dialog,
Spinner,
Tag,
} from "@blueprintjs/core";
import renderOverlay, {
RoamOverlayProps,
} from "roamjs-components/util/renderOverlay";
import { getLoggedInClient, getSupabaseContext } from "~/utils/supabaseContext";
import {
discoverSharedNodes,
getMyGroups,
type DiscoverableGroup,
type DiscoveredSharedNode,
} from "~/utils/discoverSharedNodes";
/**
* Source RIDs of shared nodes already imported into this Roam graph. ENG-1855 ships this
* as an empty seam: Roam has no imported-node store yet (that is ENG-1856), so nothing is
* marked as imported. ENG-1859 points this at the real reader once ENG-1856 persists
* imported source identity. See ENG-1855 Decisions in the project worklog.
*/
const getImportedSourceRids = (): Set<string> => new Set<string>();
type LoadState =
| { status: "loading" }
| { status: "error"; message: string }
| {
status: "ready";
groups: DiscoverableGroup[];
nodes: DiscoveredSharedNode[];
};
const formatModified = (iso: string): string => {
const ms = new Date(iso).valueOf();
if (Number.isNaN(ms) || ms === 0) return "Unknown";
return new Date(ms).toLocaleString();
};
const SharedNodeList = ({ nodes }: { nodes: DiscoveredSharedNode[] }) => {
const bySpace = new Map<
string,
{
sourceApp: DiscoveredSharedNode["sourceApp"];
nodes: DiscoveredSharedNode[];
}
>();
for (const node of nodes) {
const existing = bySpace.get(node.sourceSpaceName);
if (existing) existing.nodes.push(node);
else
bySpace.set(node.sourceSpaceName, {
sourceApp: node.sourceApp,
nodes: [node],
});
}
return (
<div className="flex max-h-96 flex-col gap-4 overflow-y-auto">
{[...bySpace.entries()].map(([spaceName, group]) => (
<div key={spaceName}>
<div className="mb-1 flex items-center gap-2">
<Tag minimal>{group.sourceApp}</Tag>
<strong>{spaceName}</strong>
</div>
<ul className="m-0 list-none p-0">
{group.nodes.map((node) => (
<li
key={node.sourceNodeRid}
className="flex items-center justify-between gap-2 py-1"
>
<span className="truncate">{node.title}</span>
<span className="flex flex-shrink-0 items-center gap-2">
{node.alreadyImported && (
<Tag intent="success" minimal>
Imported
</Tag>
)}
<span className="text-xs opacity-60">
{formatModified(node.sourceModifiedAt)}
</span>
</span>
</li>
))}
</ul>
</div>
))}
</div>
);
};
const DiscoverSharedNodesDialog = ({
isOpen,
onClose,
}: RoamOverlayProps<Record<string, never>>) => {
const [state, setState] = useState<LoadState>({ status: "loading" });
useEffect(() => {
let cancelled = false;
const load = async () => {
try {
const client = await getLoggedInClient();
if (!client) {
if (!cancelled)
setState({
status: "error",
message: "Could not access the Discourse Graph database.",
});
return;
}
const context = await getSupabaseContext();
if (!context) {
if (!cancelled)
setState({
status: "error",
message: "Could not load this graph's workspace context.",
});
return;
}
const [groups, nodes] = await Promise.all([
getMyGroups(client),
discoverSharedNodes({
client,
currentSpaceId: context.spaceId,
importedRids: getImportedSourceRids(),
}),
]);
if (!cancelled) setState({ status: "ready", groups, nodes });
} catch (error) {
if (!cancelled)
setState({
status: "error",
message:
error instanceof Error
? error.message
: "Unexpected error loading shared nodes.",
});
}
};
void load();
return () => {
cancelled = true;
};
}, []);
return (
<Dialog isOpen={isOpen} onClose={onClose} title="Discover shared nodes">
<div className={Classes.DIALOG_BODY}>
{state.status === "loading" && (
<div className="flex items-center gap-2">
<Spinner size={20} />
<span>Loading shared nodes…</span>
</div>
)}
{state.status === "error" && (
<Callout intent="danger" title="Could not load shared nodes">
{state.message}
</Callout>
)}
{state.status === "ready" &&
state.groups.length === 0 &&
state.nodes.length === 0 && (
<Callout intent="primary" title="No sharing groups">
You are not a member of any sharing group yet, so there are no
shared nodes to import.
</Callout>
)}
{state.status === "ready" &&
state.groups.length > 0 &&
state.nodes.length === 0 && (
<Callout intent="primary" title="No shared nodes">
No shared nodes from other spaces are available to import yet.
</Callout>
)}
{state.status === "ready" && state.nodes.length > 0 && (
<SharedNodeList nodes={state.nodes} />
)}
</div>
<div className={Classes.DIALOG_FOOTER}>
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
<Button onClick={onClose}>Close</Button>
</div>
</div>
</Dialog>
);
};
export const renderDiscoverSharedNodesDialog = (): void => {
renderOverlay({
id: "discover-shared-nodes",
Overlay: DiscoverSharedNodesDialog,
});
};