Skip to content

Commit 30c210b

Browse files
fix: Fix the session state
1 parent 35713fb commit 30c210b

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/app/(dashboard)/dashboard/billing/_components/credit-packages.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export function CreditPackages() {
3737
const [isDialogOpen, setIsDialogOpen] = useState(false);
3838
const [selectedPackage, setSelectedPackage] = useState<CreditPackage | null>(null);
3939
const [clientSecret, setClientSecret] = useState<string | null>(null);
40-
const session = useSessionStore((state) => state.session);
40+
const session = useSessionStore((state) => state);
4141
const transactionsRefresh = useTransactionStore((state) => state.triggerRefresh);
42-
const sessionIsLoading = useSessionStore((state) => state.isLoading);
42+
const sessionIsLoading = session?.isLoading;
4343

4444
const handlePurchase = async (pkg: CreditPackage) => {
4545
try {
@@ -78,7 +78,7 @@ export function CreditPackages() {
7878
</>
7979
) : (
8080
<div className="text-3xl font-bold">
81-
{session?.user.currentCredits.toLocaleString()} credits
81+
{session?.session?.user?.currentCredits.toLocaleString()} credits
8282
</div>
8383
)}
8484
</div>

src/state/session.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ export const useSessionStore = create(
88
session: null as SessionValidationResult | null,
99
isLoading: true,
1010
lastFetched: null as Date | null,
11+
isInitialized: false,
1112
},
1213
(set) => ({
13-
setSession: (session: SessionValidationResult) => set({ session, isLoading: false, lastFetched: new Date() }),
14-
clearSession: () => set({ session: null, isLoading: false, lastFetched: null }),
15-
refetchSession: () => set({ isLoading: true, lastFetched: null }),
14+
setSession: (session: SessionValidationResult) =>
15+
set({ session, isLoading: false, lastFetched: new Date(), isInitialized: true }),
16+
clearSession: () =>
17+
set({ session: null, isLoading: false, lastFetched: null, isInitialized: true }),
18+
refetchSession: () =>
19+
set((state) => ({
20+
...state,
21+
isLoading: !state.isInitialized,
22+
lastFetched: null
23+
})),
1624
})
1725
)
1826
)

0 commit comments

Comments
 (0)