From 5304524d75f657db9904899a5203e03923eb1b46 Mon Sep 17 00:00:00 2001 From: ngc2207 Date: Thu, 6 Feb 2025 09:21:08 +0800 Subject: [PATCH] feat(workspaces): add custom hook to fetch workspaces using React Query --- .../workspaces/api/use-get-workspaces.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/features/workspaces/api/use-get-workspaces.ts diff --git a/src/features/workspaces/api/use-get-workspaces.ts b/src/features/workspaces/api/use-get-workspaces.ts new file mode 100644 index 0000000..3a8d5f8 --- /dev/null +++ b/src/features/workspaces/api/use-get-workspaces.ts @@ -0,0 +1,21 @@ +import { client } from "@/lib/rpc"; +import { useQuery } from "@tanstack/react-query"; + +export const useGetWorkspaces = () => { + const query = useQuery({ + queryKey: ["workspaces"], + queryFn: async () => { + const response = await client.api.workspaces.$get(); + + if (!response.ok) { + throw new Error("Failed to fetch workspaces"); + } + + const { data } = await response.json(); + + return data; + }, + }); + + return query; +};