mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 11:23:10 +02:00
Desktop connection SaaS: config, billing, team support (#5768)
Co-authored-by: James Brunton <[email protected]> Co-authored-by: James Brunton <[email protected]>
This commit is contained in:
co-authored by
James Brunton
James Brunton
parent
86072ec91a
commit
5c39acecd8
@@ -0,0 +1,63 @@
|
||||
import { useState } from 'react';
|
||||
import { supabase } from '@app/auth/supabase';
|
||||
import { authService } from '@app/services/authService';
|
||||
|
||||
/**
|
||||
* Shared hook for enabling metered (overage) billing via Supabase edge function.
|
||||
* Used by CreditExhaustedModal and InsufficientCreditsModal to avoid duplicate logic.
|
||||
*
|
||||
* @param refreshBilling - Callback to refresh billing state after success
|
||||
* @param onSuccess - Callback invoked after billing is enabled and refreshed
|
||||
* @param logPrefix - Label used in console messages for easier tracing
|
||||
*/
|
||||
export function useEnableMeteredBilling(
|
||||
refreshBilling: () => Promise<void>,
|
||||
onSuccess: () => void,
|
||||
logPrefix: string
|
||||
): {
|
||||
enablingMetering: boolean;
|
||||
meteringError: string | null;
|
||||
handleEnableMetering: () => Promise<void>;
|
||||
} {
|
||||
const [enablingMetering, setEnablingMetering] = useState(false);
|
||||
const [meteringError, setMeteringError] = useState<string | null>(null);
|
||||
|
||||
const handleEnableMetering = async () => {
|
||||
console.debug(`[${logPrefix}] Enabling metered billing`);
|
||||
setEnablingMetering(true);
|
||||
setMeteringError(null);
|
||||
|
||||
try {
|
||||
const token = await authService.getAuthToken();
|
||||
if (!token) {
|
||||
throw new Error('Not authenticated');
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.functions.invoke('create-meter-subscription', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error(error.message || 'Failed to enable metered billing');
|
||||
}
|
||||
|
||||
if (!data?.success) {
|
||||
throw new Error(data?.error || data?.message || 'Failed to enable metered billing');
|
||||
}
|
||||
|
||||
console.debug(`[${logPrefix}] Metered billing enabled successfully`);
|
||||
|
||||
await refreshBilling();
|
||||
onSuccess();
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : 'Failed to enable metered billing';
|
||||
console.error(`[${logPrefix}] Failed to enable metered billing:`, err);
|
||||
setMeteringError(message);
|
||||
} finally {
|
||||
setEnablingMetering(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { enablingMetering, meteringError, handleEnableMetering };
|
||||
}
|
||||
Reference in New Issue
Block a user