Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/hooks/use-orbital.ts
2500 views
1
/**
2
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useEffect, useState } from "react";
8
import type { orbital } from "@useorbital/client-types/types";
9
import { useFeatureFlag } from "../data/featureflag-query";
10
import { isGitpodIo } from "../utils";
11
12
declare global {
13
interface Window {
14
orbital: orbital;
15
}
16
}
17
18
export const useOrbital = (spaceId: string) => {
19
const [isLoaded, setIsLoaded] = useState<boolean>(false);
20
const [discoveryIds, setDiscoveryIds] = useState<Set<string>>(new Set());
21
22
const enabledOrbitalDiscoveries = useFeatureFlag("enabledOrbitalDiscoveries");
23
24
useEffect(() => {
25
if (!isGitpodIo()) return;
26
if (document.getElementById("orbital-client")) return;
27
if (discoveryIds.size === 0) return;
28
29
const body = document.getElementsByTagName("body")[0];
30
31
const installationScript = document.createElement("script");
32
installationScript.innerHTML = `(function(o,r,b,i,t,a,l){o[r]||(t=o[r]=function(){i.push(arguments)},t._t=new Date,t._v=1,i=t._q=[])})(window,'orbital');`;
33
body.appendChild(installationScript);
34
35
const orbitalScript = document.createElement("script");
36
orbitalScript.setAttribute("id", "orbital-client");
37
orbitalScript.setAttribute("src", `https://client.useorbital.com/api/account/${spaceId}/client.js`);
38
orbitalScript.setAttribute("async", "");
39
body.appendChild(orbitalScript);
40
orbitalScript.addEventListener(
41
"load",
42
() => {
43
if (typeof window["orbital"] === "undefined") {
44
console.error("Orbital script failed to load.");
45
return;
46
}
47
setIsLoaded(true);
48
},
49
{ once: true, capture: false },
50
);
51
}, [discoveryIds.size, spaceId]);
52
53
useEffect(() => {
54
if (!enabledOrbitalDiscoveries || enabledOrbitalDiscoveries === true) return;
55
setDiscoveryIds(new Set(enabledOrbitalDiscoveries.split(",").filter((value) => !!value)));
56
}, [enabledOrbitalDiscoveries]);
57
58
return {
59
isLoaded,
60
discoveryIds,
61
orbital: window["orbital"],
62
};
63
};
64
65