shellphone.app/app/core/hooks/use-current-user.ts

22 lines
867 B
TypeScript
Raw Normal View History

2021-08-05 17:07:15 +00:00
import { useSession, useQuery } from "blitz";
import getCurrentUser from "../../users/queries/get-current-user";
2021-10-15 23:25:13 +00:00
import getCurrentPhoneNumber from "../../phone-numbers/queries/get-current-phone-number";
2021-08-05 17:07:15 +00:00
export default function useCurrentUser() {
const session = useSession();
2021-10-15 23:25:13 +00:00
const [user, userQuery] = useQuery(getCurrentUser, null, { enabled: Boolean(session.userId) });
2021-08-05 17:07:15 +00:00
const organization = user?.memberships[0]!.organization;
2021-10-15 23:25:13 +00:00
const hasFilledTwilioCredentials = Boolean(organization?.twilioAccountSid && organization?.twilioAuthToken);
const [phoneNumber] = useQuery(getCurrentPhoneNumber, {}, { enabled: hasFilledTwilioCredentials });
2021-08-05 17:07:15 +00:00
return {
user,
organization,
2021-10-15 23:25:13 +00:00
hasFilledTwilioCredentials,
hasPhoneNumber: Boolean(phoneNumber),
hasOngoingSubscription: organization && organization.subscriptions.length > 0,
2021-10-15 23:25:13 +00:00
refetch: userQuery.refetch,
2021-08-05 17:07:15 +00:00
};
}