mirror of
https://github.com/khairul169/garage-webui.git
synced 2025-10-14 23:09:32 +07:00
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:
parent
4ef8157aef
commit
a99e8bba83
@ -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>
|
||||||
) => {
|
) => {
|
||||||
|
@ -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">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user