Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/compute/file-hook.ts
1503 views
1
/*
2
Hook that returns the compute server that has been selected for a given document.
3
*/
4
5
import { useEffect, useMemo, useState } from "react";
6
import { webapp_client } from "@cocalc/frontend/webapp-client";
7
8
export default function useComputeServerId({
9
project_id,
10
path,
11
}): number | null {
12
const [id, setId] = useState<number | null>(null);
13
const computeServerAssociations = useMemo(() => {
14
return webapp_client.project_client.computeServers(project_id);
15
}, []);
16
useEffect(() => {
17
const handleChange = async () => {
18
try {
19
const id =
20
(await computeServerAssociations.getServerIdForPath(path)) ?? null;
21
setId(id);
22
} catch (err) {
23
console.warn(err);
24
}
25
};
26
computeServerAssociations.on("change", handleChange);
27
(async () => {
28
await handleChange();
29
})();
30
return () => {
31
computeServerAssociations.removeListener("change", handleChange);
32
};
33
}, [project_id, path]);
34
return id;
35
}
36
37