feat: implement useBucketsWithDetails hook for enhanced bucket data retrieval

- Added a new hook, `useBucketsWithDetails`, to fetch detailed information for all buckets using concurrent queries.
- Updated the `HomePage` component to utilize the new hook, allowing for improved calculation of total bucket usage based on detailed data.
- Refactored usage calculation logic to handle multiple asynchronous queries effectively.
This commit is contained in:
Adekabang 2025-07-31 19:12:14 -04:00
parent 4ef8157aef
commit a99e8bba83
2 changed files with 22 additions and 6 deletions

View File

@ -3,6 +3,7 @@ import {
useMutation, useMutation,
UseMutationOptions, UseMutationOptions,
useQuery, useQuery,
useQueries,
} from "@tanstack/react-query"; } from "@tanstack/react-query";
import { GetBucketRes, Bucket } from "./types"; import { GetBucketRes, Bucket } from "./types";
import { CreateBucketSchema } from "./schema"; import { CreateBucketSchema } from "./schema";
@ -22,6 +23,18 @@ export const useBucketDetails = (id?: string | null) => {
}); });
}; };
export const useBucketsWithDetails = () => {
const { data: buckets } = useBuckets();
return useQueries({
queries: (buckets || []).map((bucket) => ({
queryKey: ["bucket-details", bucket.id],
queryFn: () => api.get<Bucket>("/v2/GetBucketInfo", { params: { id: bucket.id } }),
enabled: !!bucket.id,
})),
});
};
export const useCreateBucket = ( export const useCreateBucket = (
options?: UseMutationOptions<unknown, Error, CreateBucketSchema> options?: UseMutationOptions<unknown, Error, CreateBucketSchema>
) => { ) => {

View File

@ -13,16 +13,19 @@ import {
PieChart, PieChart,
} from "lucide-react"; } from "lucide-react";
import { cn, readableBytes, ucfirst } from "@/lib/utils"; import { cn, readableBytes, ucfirst } from "@/lib/utils";
import { useBuckets } from "../buckets/hooks"; import { useBucketsWithDetails } from "../buckets/hooks";
import { useMemo } from "react"; import { useMemo } from "react";
const HomePage = () => { const HomePage = () => {
const { data: health } = useNodesHealth(); const { data: health } = useNodesHealth();
const { data: buckets } = useBuckets(); const bucketDetailsQueries = useBucketsWithDetails();
const totalUsage = useMemo(() => { const totalUsage = useMemo(() => {
return buckets?.reduce((acc, bucket) => acc + bucket.bytes, 0); return bucketDetailsQueries
}, [buckets]); .map(query => query.data?.bytes)
.filter(bytes => bytes != null)
.reduce((acc, bytes) => acc + bytes, 0);
}, [bucketDetailsQueries]);
return ( return (
<div className="container"> <div className="container">
@ -38,8 +41,8 @@ const HomePage = () => {
health?.status === "healthy" health?.status === "healthy"
? "text-success" ? "text-success"
: health?.status === "degraded" : health?.status === "degraded"
? "text-warning" ? "text-warning"
: "text-error" : "text-error"
)} )}
/> />
<StatsCard title="Nodes" icon={HardDrive} value={health?.knownNodes} /> <StatsCard title="Nodes" icon={HardDrive} value={health?.knownNodes} />