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