CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/compute/cloud-filesystem/hooks.ts
Views: 687
1
import { useState, useEffect } from "react";
2
import type { CloudFilesystem } from "@cocalc/util/db-schema/cloud-filesystems";
3
import { getCloudFilesystems } from "./api";
4
5
// This uses potentially 30 second stale cached data if available, doesn't update automatically.
6
// We should improve that later, probably switching a changefeed like with compute servers, etc.
7
// But for v0 this is fine.
8
export function useCloudFilesystem({
9
cloud_filesystem_id,
10
refresh,
11
}: {
12
cloud_filesystem_id: number;
13
refresh?: number;
14
}): [CloudFilesystem | null, string, (string) => void] {
15
const [filsystem, setFilesystem] = useState<CloudFilesystem | null>(null);
16
const [error, setError] = useState<string>("");
17
useEffect(() => {
18
(async () => {
19
let v;
20
try {
21
setError("");
22
v = await getCloudFilesystems({
23
id: cloud_filesystem_id,
24
cache: true,
25
});
26
} catch (err) {
27
setError(`${err}`);
28
return;
29
}
30
if (v.length != 1) {
31
setError(`no cloud file system with global id ${cloud_filesystem_id}`);
32
} else {
33
setFilesystem(v[0]);
34
}
35
})();
36
}, [refresh]);
37
return [filsystem, error, setError];
38
}
39
40