@@ -8,7 +8,6 @@ import type { SeatData } from "@shared/types/seat";
88import { PLAN_FREE , PLAN_PRO } from "@shared/types/seat" ;
99import { logger } from "@utils/logger" ;
1010import { queryClient } from "@utils/queryClient" ;
11- import { getPostHogUrl } from "@utils/urls" ;
1211import { create } from "zustand" ;
1312
1413const log = logger . scope ( "seat-store" ) ;
@@ -18,6 +17,7 @@ interface SeatStoreState {
1817 isLoading : boolean ;
1918 error : string | null ;
2019 redirectUrl : string | null ;
20+ billingOrgId : string | null ;
2121}
2222
2323interface SeatStoreActions {
@@ -54,7 +54,7 @@ function handleSeatError(
5454 set ( {
5555 isLoading : false ,
5656 error : "Billing subscription required" ,
57- redirectUrl : getPostHogUrl ( "/organization/billing" ) ,
57+ redirectUrl : error . redirectUrl ,
5858 } ) ;
5959 return ;
6060 }
@@ -80,6 +80,7 @@ const initialState: SeatStoreState = {
8080 isLoading : false ,
8181 error : null ,
8282 redirectUrl : null ,
83+ billingOrgId : null ,
8384} ;
8485
8586export const useSeatStore = create < SeatStore > ( ) ( ( set , get ) => ( {
@@ -99,7 +100,11 @@ export const useSeatStore = create<SeatStore>()((set, get) => ({
99100 seat = await client . getMySeat ( ) ;
100101 }
101102 }
102- set ( { seat, isLoading : false } ) ;
103+ set ( {
104+ seat,
105+ isLoading : false ,
106+ billingOrgId : seat ?. organization_id ?? null ,
107+ } ) ;
103108 } catch ( error ) {
104109 const { seat : existingSeat } = get ( ) ;
105110 if ( existingSeat ) {
@@ -122,12 +127,20 @@ export const useSeatStore = create<SeatStore>()((set, get) => ({
122127 plan : existing . plan_key ,
123128 status : existing . status ,
124129 } ) ;
125- set ( { seat : existing , isLoading : false } ) ;
130+ set ( {
131+ seat : existing ,
132+ isLoading : false ,
133+ billingOrgId : existing . organization_id ?? null ,
134+ } ) ;
126135 return ;
127136 }
128137 const seat = await client . createSeat ( PLAN_FREE ) ;
129138 log . info ( "Free seat created" , { id : seat . id , plan : seat . plan_key } ) ;
130- set ( { seat, isLoading : false } ) ;
139+ set ( {
140+ seat,
141+ isLoading : false ,
142+ billingOrgId : seat . organization_id ?? null ,
143+ } ) ;
131144 invalidatePlanCache ( ) ;
132145 } catch ( error ) {
133146 log . error ( "provisionFreeSeat failed" , error ) ;
@@ -142,16 +155,28 @@ export const useSeatStore = create<SeatStore>()((set, get) => ({
142155 const existing = await client . getMySeat ( ) ;
143156 if ( existing ) {
144157 if ( existing . plan_key === PLAN_PRO ) {
145- set ( { seat : existing , isLoading : false } ) ;
158+ set ( {
159+ seat : existing ,
160+ isLoading : false ,
161+ billingOrgId : existing . organization_id ?? null ,
162+ } ) ;
146163 return ;
147164 }
148165 const seat = await client . upgradeSeat ( PLAN_PRO ) ;
149- set ( { seat, isLoading : false } ) ;
166+ set ( {
167+ seat,
168+ isLoading : false ,
169+ billingOrgId : seat . organization_id ?? null ,
170+ } ) ;
150171 invalidatePlanCache ( ) ;
151172 return ;
152173 }
153174 const seat = await client . createSeat ( PLAN_PRO ) ;
154- set ( { seat, isLoading : false } ) ;
175+ set ( {
176+ seat,
177+ isLoading : false ,
178+ billingOrgId : seat . organization_id ?? null ,
179+ } ) ;
155180 invalidatePlanCache ( ) ;
156181 } catch ( error ) {
157182 handleSeatError ( error , set ) ;
@@ -164,7 +189,11 @@ export const useSeatStore = create<SeatStore>()((set, get) => ({
164189 const client = await getClient ( ) ;
165190 await client . cancelSeat ( ) ;
166191 const seat = await client . getMySeat ( ) ;
167- set ( { seat, isLoading : false } ) ;
192+ set ( {
193+ seat,
194+ isLoading : false ,
195+ billingOrgId : seat ?. organization_id ?? null ,
196+ } ) ;
168197 invalidatePlanCache ( ) ;
169198 } catch ( error ) {
170199 handleSeatError ( error , set ) ;
@@ -176,7 +205,11 @@ export const useSeatStore = create<SeatStore>()((set, get) => ({
176205 try {
177206 const client = await getClient ( ) ;
178207 const seat = await client . reactivateSeat ( ) ;
179- set ( { seat, isLoading : false } ) ;
208+ set ( {
209+ seat,
210+ isLoading : false ,
211+ billingOrgId : seat . organization_id ?? null ,
212+ } ) ;
180213 invalidatePlanCache ( ) ;
181214 } catch ( error ) {
182215 handleSeatError ( error , set ) ;
0 commit comments