1- import { useAuthStore } from "@features/auth/stores/authStore " ;
1+ import { getAuthenticatedClient } from "@features/auth/hooks/authClient " ;
22import type { SeatData } from "@shared/types/seat" ;
33import { PLAN_FREE , PLAN_PRO } from "@shared/types/seat" ;
4- import { electronStorage } from "@utils/electronStorage" ;
54import { logger } from "@utils/logger" ;
65import { getPostHogUrl } from "@utils/urls" ;
76import { create } from "zustand" ;
8- import { persist } from "zustand/middleware" ;
97
108const log = logger . scope ( "seat-store" ) ;
119
@@ -28,8 +26,8 @@ interface SeatStoreActions {
2826
2927type SeatStore = SeatStoreState & SeatStoreActions ;
3028
31- function getClient ( ) {
32- const client = useAuthStore . getState ( ) . client ;
29+ async function getClient ( ) {
30+ const client = await getAuthenticatedClient ( ) ;
3331 if ( ! client ) {
3432 throw new Error ( "Not authenticated" ) ;
3533 }
@@ -115,94 +113,96 @@ const initialState: SeatStoreState = {
115113 redirectUrl : null ,
116114} ;
117115
118- export const useSeatStore = create < SeatStore > ( ) (
119- persist (
120- ( set ) => ( {
121- ...initialState ,
122-
123- fetchSeat : async ( ) => {
124- set ( { isLoading : true , error : null , redirectUrl : null } ) ;
125- try {
126- const client = getClient ( ) ;
127- let seat = await client . getMySeat ( ) ;
128- if ( ! seat ) {
129- log . info ( "No seat found, auto-provisioning free plan" ) ;
130- seat = await client . createSeat ( PLAN_FREE ) ;
131- }
132- set ( { seat, isLoading : false } ) ;
133- } catch ( error ) {
134- handleSeatError ( error , set ) ;
135- }
136- } ,
137-
138- provisionFreeSeat : async ( ) => {
139- set ( { isLoading : true , error : null , redirectUrl : null } ) ;
140- try {
141- const client = getClient ( ) ;
142- const existing = await client . getMySeat ( ) ;
143- if ( existing ) {
144- set ( { seat : existing , isLoading : false } ) ;
145- return ;
146- }
147- const seat = await client . createSeat ( PLAN_FREE ) ;
148- set ( { seat, isLoading : false } ) ;
149- } catch ( error ) {
150- handleSeatError ( error , set ) ;
151- }
152- } ,
153-
154- upgradeToPro : async ( ) => {
155- set ( { isLoading : true , error : null , redirectUrl : null } ) ;
156- try {
157- const client = getClient ( ) ;
158- const existing = await client . getMySeat ( ) ;
159- if ( existing ) {
160- if ( existing . plan_key === PLAN_PRO ) {
161- set ( { seat : existing , isLoading : false } ) ;
162- return ;
163- }
164- const seat = await client . upgradeSeat ( PLAN_PRO ) ;
165- set ( { seat, isLoading : false } ) ;
166- return ;
167- }
168- const seat = await client . createSeat ( PLAN_PRO ) ;
169- set ( { seat, isLoading : false } ) ;
170- } catch ( error ) {
171- handleSeatError ( error , set ) ;
172- }
173- } ,
174-
175- cancelSeat : async ( ) => {
176- set ( { isLoading : true , error : null , redirectUrl : null } ) ;
177- try {
178- const client = getClient ( ) ;
179- await client . cancelSeat ( ) ;
180- const seat = await client . getMySeat ( ) ;
181- set ( { seat, isLoading : false } ) ;
182- } catch ( error ) {
183- handleSeatError ( error , set ) ;
184- }
185- } ,
186-
187- reactivateSeat : async ( ) => {
188- set ( { isLoading : true , error : null , redirectUrl : null } ) ;
189- try {
190- const client = getClient ( ) ;
191- const seat = await client . reactivateSeat ( ) ;
192- set ( { seat, isLoading : false } ) ;
193- } catch ( error ) {
194- handleSeatError ( error , set ) ;
116+ export const useSeatStore = create < SeatStore > ( ) ( ( set ) => ( {
117+ ...initialState ,
118+
119+ fetchSeat : async ( ) => {
120+ set ( { isLoading : true , error : null , redirectUrl : null } ) ;
121+ try {
122+ const client = await getClient ( ) ;
123+ let seat = await client . getMySeat ( ) ;
124+ if ( ! seat ) {
125+ log . info ( "No seat found, auto-provisioning free plan" ) ;
126+ seat = await client . createSeat ( PLAN_FREE ) ;
127+ }
128+ set ( { seat, isLoading : false } ) ;
129+ } catch ( error ) {
130+ handleSeatError ( error , set ) ;
131+ }
132+ } ,
133+
134+ provisionFreeSeat : async ( ) => {
135+ log . info ( "[seat] provisionFreeSeat called" ) ;
136+ set ( { isLoading : true , error : null , redirectUrl : null } ) ;
137+ try {
138+ const client = await getClient ( ) ;
139+ const existing = await client . getMySeat ( ) ;
140+ if ( existing ) {
141+ log . info ( "[seat] seat already exists on server" , {
142+ plan : existing . plan_key ,
143+ status : existing . status ,
144+ } ) ;
145+ set ( { seat : existing , isLoading : false } ) ;
146+ return ;
147+ }
148+ log . info ( "[seat] creating free seat" ) ;
149+ const seat = await client . createSeat ( PLAN_FREE ) ;
150+ log . info ( "[seat] free seat created" , {
151+ id : seat . id ,
152+ plan : seat . plan_key ,
153+ } ) ;
154+ set ( { seat, isLoading : false } ) ;
155+ } catch ( error ) {
156+ log . error ( "[seat] provisionFreeSeat failed" , error ) ;
157+ handleSeatError ( error , set ) ;
158+ }
159+ } ,
160+
161+ upgradeToPro : async ( ) => {
162+ set ( { isLoading : true , error : null , redirectUrl : null } ) ;
163+ try {
164+ const client = await getClient ( ) ;
165+ const existing = await client . getMySeat ( ) ;
166+ if ( existing ) {
167+ if ( existing . plan_key === PLAN_PRO ) {
168+ set ( { seat : existing , isLoading : false } ) ;
169+ return ;
195170 }
196- } ,
197-
198- clearError : ( ) => set ( { error : null , redirectUrl : null } ) ,
199-
200- reset : ( ) => set ( initialState ) ,
201- } ) ,
202- {
203- name : "posthog-code-seat" ,
204- storage : electronStorage ,
205- partialize : ( state ) => ( { seat : state . seat } ) ,
206- } ,
207- ) ,
208- ) ;
171+ const seat = await client . upgradeSeat ( PLAN_PRO ) ;
172+ set ( { seat, isLoading : false } ) ;
173+ return ;
174+ }
175+ const seat = await client . createSeat ( PLAN_PRO ) ;
176+ set ( { seat, isLoading : false } ) ;
177+ } catch ( error ) {
178+ handleSeatError ( error , set ) ;
179+ }
180+ } ,
181+
182+ cancelSeat : async ( ) => {
183+ set ( { isLoading : true , error : null , redirectUrl : null } ) ;
184+ try {
185+ const client = await getClient ( ) ;
186+ await client . cancelSeat ( ) ;
187+ const seat = await client . getMySeat ( ) ;
188+ set ( { seat, isLoading : false } ) ;
189+ } catch ( error ) {
190+ handleSeatError ( error , set ) ;
191+ }
192+ } ,
193+
194+ reactivateSeat : async ( ) => {
195+ set ( { isLoading : true , error : null , redirectUrl : null } ) ;
196+ try {
197+ const client = await getClient ( ) ;
198+ const seat = await client . reactivateSeat ( ) ;
199+ set ( { seat, isLoading : false } ) ;
200+ } catch ( error ) {
201+ handleSeatError ( error , set ) ;
202+ }
203+ } ,
204+
205+ clearError : ( ) => set ( { error : null , redirectUrl : null } ) ,
206+
207+ reset : ( ) => set ( initialState ) ,
208+ } ) ) ;
0 commit comments