bigbuffet-rw/packages/pl-hooks/lib/hooks/instance/use-instance.ts
marcin mikołajczak ad7527bd4d pl-hooks: actually improve useInstance behavior
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-10-26 19:56:53 +02:00

22 lines
634 B
TypeScript

import { useQuery } from '@tanstack/react-query';
import { instanceSchema } from 'pl-api';
import * as v from 'valibot';
import { usePlHooksApiClient } from 'pl-hooks/contexts/api-client';
import { usePlHooksQueryClient } from 'pl-hooks/contexts/query-client';
const initialData = v.parse(instanceSchema, {});
const useInstance = () => {
const { client } = usePlHooksApiClient();
const queryClient = usePlHooksQueryClient();
const query = useQuery({
queryKey: ['instance'],
queryFn: client.instance.getInstance,
}, queryClient);
return { ...query, data: query.data || initialData };
};
export { useInstance };