38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
import { api } from "@/lib/api";
|
|
import { useAuthStore } from "@/stores/auth";
|
|
import type { OrganizationBillingState } from "@/types/organization.types";
|
|
|
|
export type { OrganizationBillingState } from "@/types/organization.types";
|
|
|
|
export const organizationBillingQueryKey = ["organization-billing"] as const;
|
|
|
|
export const isOrganizationBillingQueryEnabled = (accessToken?: string | null) =>
|
|
Boolean(accessToken);
|
|
|
|
export const fetchOrganizationBillingState = async (accessToken: string) => {
|
|
const { data } = await api.get<OrganizationBillingState>("/api/v1/users/organization", {
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
|
|
return data;
|
|
};
|
|
|
|
export const hasOrganizationBillingAccess = (state: OrganizationBillingState) =>
|
|
Boolean(state.active_subscription_plan) || !state.is_trial_expired;
|
|
|
|
export const shouldShowInAppBillingPaywall = (state: OrganizationBillingState) =>
|
|
state.is_billing_owner && state.is_trial_expired && !state.active_subscription_plan;
|
|
|
|
export const useOrganizationBilling = () => {
|
|
const accessToken = useAuthStore((state) => state.session?.access_token ?? null);
|
|
|
|
return useQuery({
|
|
queryKey: organizationBillingQueryKey,
|
|
queryFn: () => fetchOrganizationBillingState(accessToken as string),
|
|
enabled: isOrganizationBillingQueryEnabled(accessToken),
|
|
staleTime: 30_000,
|
|
});
|
|
};
|