This commit is contained in:
 Илья Шалонин 2025-10-29 13:43:43 +05:00
commit 8538ab969a
434 changed files with 44051 additions and 0 deletions

View file

@ -0,0 +1,80 @@
import { apiClient } from "@shared/lib/api-client";
import { useMutation, useQuery } from "@tanstack/react-query";
import type { InferRequestType } from "hono";
export const createCustomerPortalLinkMutationKey = [
"create-customer-portal-link",
] as const;
export const useCreateCustomerPortalLinkMutation = () => {
return useMutation({
mutationKey: createCustomerPortalLinkMutationKey,
mutationFn: async (
query: InferRequestType<
(typeof apiClient.payments)["create-customer-portal-link"]["$post"]
>["query"],
) => {
const response = await apiClient.payments[
"create-customer-portal-link"
].$post({
query,
});
if (!response.ok) {
throw new Error("Failed to create customer portal link");
}
return response.json();
},
});
};
export const createCheckoutLinkMutationKey = ["create-checkout-link"] as const;
export const useCreateCheckoutLinkMutation = () => {
return useMutation({
mutationKey: createCheckoutLinkMutationKey,
mutationFn: async (
query: InferRequestType<
(typeof apiClient.payments)["create-checkout-link"]["$post"]
>["query"],
) => {
const response = await apiClient.payments[
"create-checkout-link"
].$post({
query,
});
if (!response.ok) {
throw new Error("Failed to create checkout link");
}
return response.json();
},
});
};
export const purchasesQueryKey = (organizationId?: string) =>
organizationId
? ["organization", "purchases", organizationId]
: (["user", "purchases"] as const);
export const usePurchasesQuery = (
organizationId?: string,
options?: { enabled?: boolean },
) => {
return useQuery({
queryKey: purchasesQueryKey(organizationId),
queryFn: async () => {
const response = await apiClient.payments.purchases.$get({
query: {
organizationId,
},
});
if (!response.ok) {
throw new Error("Failed to fetch purchases");
}
return response.json();
},
...options,
});
};

View file

@ -0,0 +1,16 @@
import type { PlanId } from "@saas/payments/types";
import React from "react";
export const PurchasesContext = React.createContext<
| {
activeSubscription: {
id: string;
purchaseId?: string;
} | null;
loaded: boolean;
hasSubscription: (planIds?: PlanId[] | PlanId) => boolean;
refetchPurchases: () => Promise<void>;
hasPurchase: (planId: PlanId) => boolean;
}
| undefined
>(undefined);

View file

@ -0,0 +1,17 @@
import { getServerApiClient } from "@shared/lib/server";
import { cache } from "react";
export const getPurchases = cache(async (organizationId?: string) => {
const apiClient = await getServerApiClient();
const response = await apiClient.payments.purchases.$get({
query: {
organizationId,
},
});
if (!response.ok) {
throw new Error("Failed to fetch purchases");
}
return response.json();
});