📦 EqualifyEverything / equalify

📄 useUser.ts · 16 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import { useQuery } from '@tanstack/react-query';
import { useGlobalStore } from '../utils';
import * as API from 'aws-amplify/api';
const apiClient = API.generateClient();

export const useUser = () => {
    const { authenticated } = useGlobalStore();
    return useQuery({
        queryKey: ['user', authenticated],
        queryFn: async () => (await apiClient.graphql({
            query: `query($id: uuid!) {users_by_pk(id: $id) {id name email type}}`,
            variables: { id: authenticated },
        }))?.data?.users_by_pk,
        enabled: !!authenticated, // Only run query when authenticated
    });
}