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