AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
"use client";
import { useGetSubscriptionStatus } from "@/app/api/__generated__/endpoints/credits/credits";
import { useCopilotUsage } from "@/app/(platform)/copilot/components/UsageLimits/useCopilotUsage";
import { useTrialStatus } from "@/services/trials/useTrialStatus";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import { getUsageBudget } from "./budget";
import { getUsageExperience } from "./helpers";
export type UsageSnapshot = Pick<
ReturnType<typeof useUsageExperience>,
"budget" | "usage" | "experience"
>;
export function useUsageExperience() {
const usage = useCopilotUsage();
const trial = useTrialStatus();
const subscription = useGetSubscriptionStatus({
query: {
select: (response) =>
response.status === 200 ? response.data : undefined,
},
});
const isBillingEnabled = useGetFlag(Flag.ENABLE_PLATFORM_PAYMENT);
const experience = getUsageExperience(
usage.data,
trial.data,
subscription.data?.tier,
);
const trialUnavailable =
experience.tier === "TRIAL" && (trial.isError || !trial.data);
return {
experience,
budget: getUsageBudget(usage.data),
usage: usage.data,
trial: trial.data,
subscription: subscription.data,
isBillingEnabled,
isLoading:
usage.isLoading || (experience.tier === "TRIAL" && trial.isLoading),
isError:
usage.isError || (!usage.isLoading && !usage.data) || trialUnavailable,
retry: (throwOnError = false) =>
Promise.all([
usage.refetch({ throwOnError }),
trial.refetch({ throwOnError }),
subscription.refetch({ throwOnError }),
]),
};
}