-
Notifications
You must be signed in to change notification settings - Fork 536
Integrate billing to new desktop bundle #1668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
729752c
wip
yujonglee 1ca1df5
formatting
yujonglee edf8148
wip
yujonglee 8dd1d97
unrelated
yujonglee 816d6eb
wip
yujonglee c5b7520
fix stripe webhook local
yujonglee 493a60c
wip
yujonglee 63e8521
ai settings wip
yujonglee 6ca92f3
override dg model
yujonglee 25fb047
wip
yujonglee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { openUrl } from "@tauri-apps/plugin-opener"; | ||
| import { | ||
| createContext, | ||
| type ReactNode, | ||
| useCallback, | ||
| useContext, | ||
| useMemo, | ||
| } from "react"; | ||
| import type Stripe from "stripe"; | ||
|
|
||
| import { useAuth } from "./auth"; | ||
| import { env } from "./env"; | ||
|
|
||
| type BillingRow = { | ||
| id: string; | ||
| user_id: string; | ||
| created_at: string; | ||
| updated_at: string; | ||
| stripe_customer: Stripe.Customer | null; | ||
| stripe_subscription: Stripe.Subscription | null; | ||
| }; | ||
|
|
||
| type BillingData = (BillingRow & { isPro: boolean }) | null; | ||
|
|
||
| type BillingContextValue = { | ||
| data: BillingData; | ||
| isPro: boolean; | ||
| isLoading: boolean; | ||
| isPending: boolean; | ||
| isFetching: boolean; | ||
| isRefetching: boolean; | ||
| isError: boolean; | ||
| error: unknown; | ||
| refetch: () => Promise<unknown>; | ||
| upgradeToPro: () => void; | ||
| }; | ||
|
|
||
| export type BillingAccess = BillingContextValue; | ||
|
|
||
| const BillingContext = createContext<BillingContextValue | null>(null); | ||
|
|
||
| export function BillingProvider({ children }: { children: ReactNode }) { | ||
| const auth = useAuth(); | ||
|
|
||
| const { | ||
| data: queryData, | ||
| isLoading, | ||
| isPending, | ||
| isFetching, | ||
| isRefetching, | ||
| isError, | ||
| error, | ||
| refetch, | ||
| } = useQuery({ | ||
| enabled: !!auth?.supabase && !!auth?.session?.user?.id, | ||
| queryKey: ["billing", auth?.session?.user?.id], | ||
| queryFn: async (): Promise<BillingData> => { | ||
| if (!auth?.supabase || !auth?.session?.user?.id) { | ||
| return null; | ||
| } | ||
|
|
||
| const { data, error } = await auth.supabase | ||
| .from("billings") | ||
| .select("*") | ||
| .eq("user_id", auth.session.user.id) | ||
| .maybeSingle(); | ||
|
|
||
| if (error) { | ||
| throw error; | ||
| } | ||
|
|
||
| if (!data) { | ||
| return null; | ||
| } | ||
|
|
||
| const billing = data as BillingRow; | ||
| return { | ||
| ...billing, | ||
| isPro: computeIsPro(billing.stripe_subscription), | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const data = queryData ?? null; | ||
|
|
||
yujonglee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const upgradeToPro = useCallback(() => { | ||
| openUrl(`${env.VITE_APP_URL}/app/checkout?period=monthly`); | ||
| }, [auth]); | ||
|
|
||
| const value = useMemo<BillingContextValue>( | ||
| () => ({ | ||
| data, | ||
| isPro: !!data?.isPro, | ||
| isLoading, | ||
| isPending, | ||
| isFetching, | ||
| isRefetching, | ||
| isError, | ||
| error, | ||
| refetch: () => refetch(), | ||
| upgradeToPro, | ||
| }), | ||
| [ | ||
| data, | ||
| error, | ||
| isError, | ||
| isFetching, | ||
| isLoading, | ||
| isPending, | ||
| isRefetching, | ||
| refetch, | ||
| upgradeToPro, | ||
| ], | ||
| ); | ||
|
|
||
| return ( | ||
| <BillingContext.Provider value={value}>{children}</BillingContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useBillingAccess() { | ||
| const context = useContext(BillingContext); | ||
|
|
||
| if (!context) { | ||
| throw new Error("useBillingAccess must be used within BillingProvider"); | ||
| } | ||
|
|
||
| return context; | ||
| } | ||
|
|
||
| function computeIsPro( | ||
| subscription: Stripe.Subscription | null | undefined, | ||
| ): boolean { | ||
| if (!subscription) { | ||
| return false; | ||
| } | ||
|
|
||
| const hasValidStatus = ["active", "trialing"].includes(subscription.status); | ||
|
|
||
| const hasProProduct = subscription.items.data.some((item) => { | ||
| const product = item.price.product; | ||
|
|
||
| return typeof product === "string" | ||
| ? product === env.VITE_PRO_PRODUCT_ID | ||
| : product.id === env.VITE_PRO_PRODUCT_ID; | ||
| }); | ||
|
|
||
| return hasValidStatus && hasProProduct; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.