-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconnection.tsx
More file actions
81 lines (63 loc) · 2.75 KB
/
connection.tsx
File metadata and controls
81 lines (63 loc) · 2.75 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
import type { MantisConnection, injectUIType, onMessageType, registerListenersType, setProgressType, establishLogSocketType } from "../types";
import { GenerationProgress } from "../types";
import driveIcon from "data-base64:../../../assets/drive.png";
import { getSpacePortal, reqSpaceCreation} from "../../driver";
const trigger = (url: string) => {
return url.includes("drive.google.com");
}
const getDriveFiles = async () => {
try {
const response = await new Promise<{
success: boolean;
token?: string;
driveFiles?: any[];
error?: string;
}>((resolve, reject) => {
chrome.runtime.sendMessage({ action: "initiateOAuth" }, (response) => {
if (chrome.runtime.lastError) {
return reject(new Error("Runtime error: " + chrome.runtime.lastError.message));
}
resolve(response);
});
});
if (response.success && response.driveFiles) {
console.log("Token:", response.token);
console.log("Drive files:", response.driveFiles);
return response.driveFiles;
} else {
console.error("OAuth failed:", response.error);
}
} catch (err) {
console.error("Error during Drive file fetch:", err);
}
};
const createSpace = async (injectUI: injectUIType, setProgress: setProgressType, onMessage: onMessageType, registerListeners: registerListenersType, establishLogSocket: establishLogSocketType) => {
setProgress(GenerationProgress.GATHERING_DATA);
const fileMetadata = await getDriveFiles();
if (!fileMetadata) {
console.error("Failed to retrieve file metadata from Google Drive.");
return;
}
setProgress(GenerationProgress.CREATING_SPACE);
const spaceData = await reqSpaceCreation(fileMetadata, {
"name": "semantic",
}, establishLogSocket);
setProgress(GenerationProgress.INJECTING_UI);
const spaceId = spaceData.space_id;
const createdWidget = await injectUI(spaceId, onMessage, registerListeners);
setProgress(GenerationProgress.COMPLETED);
return { spaceId, createdWidget };
};
const injectUI = async (space_id: string, onMessage: onMessageType, registerListeners: registerListenersType) => {
const iframeScalerParent = await getSpacePortal (space_id, onMessage, registerListeners);
document.body.prepend (iframeScalerParent);
return iframeScalerParent;
}
export const GoogleDriveConnection: MantisConnection = {
name: "Google Drive",
description: "Builds spaces based on the content of an entire Google Drive",
icon: driveIcon,
trigger: trigger,
createSpace: createSpace,
injectUI: injectUI,
}