1+ import { useQuery } from "@tanstack/react-query" ;
12import { openUrl } from "@tauri-apps/plugin-opener" ;
23import { ExternalLinkIcon } from "lucide-react" ;
34import { type ReactNode , useCallback , useEffect , useState } from "react" ;
4- import type Stripe from "stripe" ;
55
66import { Button } from "@hypr/ui/components/ui/button" ;
77import { Input } from "@hypr/ui/components/ui/input" ;
88
99import { useAuth } from "../../auth" ;
10- import { useBillingAccess } from "../../billing" ;
1110import { env } from "../../env" ;
1211
1312const WEB_APP_BASE_URL = env . VITE_APP_URL ?? "http://localhost:3000" ;
1413
14+ type SubscriptionRow = {
15+ stripe_customer_id : string | null ;
16+ subscription_id : string | null ;
17+ subscription_status : string | null ;
18+ current_period_start : string | null ;
19+ current_period_end : string | null ;
20+ cancel_at_period_end : boolean | null ;
21+ } ;
22+
1523export function SettingsAccount ( ) {
1624 const auth = useAuth ( ) ;
17- const billing = useBillingAccess ( ) ;
25+
26+ const { data : subscription } = useQuery ( {
27+ enabled : ! ! auth ?. supabase && ! ! auth ?. session ?. user ?. id ,
28+ queryKey : [ "subscription" , auth ?. session ?. user ?. id ] ,
29+ queryFn : async ( ) : Promise < SubscriptionRow | null > => {
30+ if ( ! auth ?. supabase ) {
31+ return null ;
32+ }
33+
34+ const { data, error } = await auth . supabase
35+ . from ( "billing_with_subscription" )
36+ . select (
37+ "stripe_customer_id, subscription_id, subscription_status, current_period_start, current_period_end, cancel_at_period_end" ,
38+ )
39+ . maybeSingle ( ) ;
40+
41+ if ( error ) {
42+ throw error ;
43+ }
44+
45+ return data as SubscriptionRow | null ;
46+ } ,
47+ } ) ;
1848
1949 const isAuthenticated = ! ! auth ?. session ;
2050 const [ isPending , setIsPending ] = useState ( false ) ;
@@ -110,7 +140,7 @@ export function SettingsAccount() {
110140 ) ;
111141 }
112142
113- const hasStripeCustomer = ! ! billing . data ?. stripe_customer ;
143+ const hasStripeCustomer = ! ! subscription ?. stripe_customer_id ;
114144
115145 return (
116146 < div className = "flex flex-col gap-4" >
@@ -145,9 +175,11 @@ export function SettingsAccount() {
145175 ) : undefined
146176 }
147177 >
148- { billing . data ?. stripe_subscription && (
178+ { subscription ?. subscription_id && (
149179 < SubscriptionDetails
150- subscription = { billing . data . stripe_subscription }
180+ status = { subscription . subscription_status }
181+ currentPeriodStart = { subscription . current_period_start }
182+ currentPeriodEnd = { subscription . current_period_end }
151183 />
152184 ) }
153185 </ Container >
@@ -156,23 +188,32 @@ export function SettingsAccount() {
156188}
157189
158190function SubscriptionDetails ( {
159- subscription,
191+ status,
192+ currentPeriodStart,
193+ currentPeriodEnd,
160194} : {
161- subscription : Stripe . Subscription ;
195+ status : string | null ;
196+ currentPeriodStart : string | null ;
197+ currentPeriodEnd : string | null ;
162198} ) {
163- const {
164- status,
165- items : {
166- data : [ { current_period_end, current_period_start } ] ,
167- } ,
168- } = subscription ;
199+ if ( ! status ) {
200+ return null ;
201+ }
202+
203+ if ( ! currentPeriodStart || ! currentPeriodEnd ) {
204+ return (
205+ < div className = "flex flex-row gap-1 text-xs text-neutral-600" >
206+ < span className = "capitalize" > { status } </ span >
207+ </ div >
208+ ) ;
209+ }
169210
170211 return (
171212 < div className = "flex flex-row gap-1 text-xs text-neutral-600" >
172213 < span className = "capitalize" > { status } :</ span >
173- < span > { new Date ( current_period_start * 1000 ) . toLocaleDateString ( ) } </ span >
214+ < span > { new Date ( currentPeriodStart ) . toLocaleDateString ( ) } </ span >
174215 < span > ~</ span >
175- < span > { new Date ( current_period_end * 1000 ) . toLocaleDateString ( ) } </ span >
216+ < span > { new Date ( currentPeriodEnd ) . toLocaleDateString ( ) } </ span >
176217 </ div >
177218 ) ;
178219}
0 commit comments