mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 03:20:46 +02:00
Check if saas before blocking credit insufficiencies (#5929)
fixes #5926
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useSaaSBilling } from '@app/contexts/SaasBillingContext';
|
||||
import { useSaaSMode } from '@app/hooks/useSaaSMode';
|
||||
import { CREDIT_EVENTS } from '@app/constants/creditEvents';
|
||||
|
||||
/**
|
||||
* Desktop hook that monitors credit balance and dispatches events
|
||||
* when credits are exhausted or low
|
||||
* when credits are exhausted or low.
|
||||
* Only active in SaaS mode — self-hosted users have no credit balance.
|
||||
*/
|
||||
export function useCreditEvents() {
|
||||
const isSaaSMode = useSaaSMode();
|
||||
const { creditBalance } = useSaaSBilling();
|
||||
const prevBalanceRef = useRef(creditBalance);
|
||||
|
||||
useEffect(() => {
|
||||
const prevBalance = prevBalanceRef.current;
|
||||
|
||||
// Dispatch exhausted event when credits reach 0 from positive balance
|
||||
if (creditBalance <= 0 && prevBalance > 0) {
|
||||
// Dispatch exhausted event when credits reach 0 from positive balance.
|
||||
// Skip entirely in self-hosted mode — creditBalance defaults to 0 there.
|
||||
if (isSaaSMode && creditBalance <= 0 && prevBalance > 0) {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(CREDIT_EVENTS.EXHAUSTED, {
|
||||
detail: { previousBalance: prevBalance, currentBalance: creditBalance },
|
||||
@@ -24,5 +28,5 @@ export function useCreditEvents() {
|
||||
|
||||
// Update ref for next comparison
|
||||
prevBalanceRef.current = creditBalance;
|
||||
}, [creditBalance]);
|
||||
}, [isSaaSMode, creditBalance]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user