mirror of
https://github.com/nicotsx/ironmount.git
synced 2025-12-10 12:10:51 +01:00
feat: init frontend
This commit is contained in:
37
web/app/hooks/useVolumes.ts
Normal file
37
web/app/hooks/useVolumes.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { type } from "arktype";
|
||||
|
||||
const volumesSchema = type({
|
||||
volumes: type({
|
||||
created_at: "string",
|
||||
mountpoint: "string",
|
||||
name: "string",
|
||||
}).array(),
|
||||
});
|
||||
|
||||
const getVolumes = async () => {
|
||||
const response = await fetch("/api/volumes");
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const useVolumes = () => {
|
||||
const query = useQuery({
|
||||
queryKey: ["volumes"],
|
||||
queryFn: getVolumes,
|
||||
select: (data) => {
|
||||
const result = volumesSchema(data);
|
||||
|
||||
if (result instanceof type.errors) {
|
||||
console.error("Volumes data validation failed:", result);
|
||||
return { volumes: [] };
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
});
|
||||
|
||||
return query;
|
||||
};
|
||||
Reference in New Issue
Block a user