mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-17 20:29:42 +02:00
Projects included: - maerchenzauber (NestJS backend + Expo mobile + SvelteKit web + Astro landing) - manacore (Expo mobile + SvelteKit web + Astro landing) - manadeck (NestJS backend + Expo mobile + SvelteKit web) - memoro (Expo mobile + SvelteKit web + Astro landing) This commit preserves the current state before monorepo restructuring. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { useState } from 'react';
|
|
import { extractCreditError, type InsufficientCreditsError } from '../types/credits';
|
|
|
|
interface InsufficientCreditsState {
|
|
visible: boolean;
|
|
requiredCredits: number;
|
|
availableCredits: number;
|
|
operation: string;
|
|
}
|
|
|
|
/**
|
|
* Hook to manage insufficient credits modal state
|
|
*/
|
|
export function useInsufficientCredits() {
|
|
const [state, setState] = useState<InsufficientCreditsState>({
|
|
visible: false,
|
|
requiredCredits: 0,
|
|
availableCredits: 0,
|
|
operation: 'this operation',
|
|
});
|
|
|
|
/**
|
|
* Show insufficient credits modal
|
|
*/
|
|
const showInsufficientCredits = (error: InsufficientCreditsError) => {
|
|
setState({
|
|
visible: true,
|
|
requiredCredits: error.requiredCredits,
|
|
availableCredits: error.availableCredits,
|
|
operation: error.operation || 'this operation',
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Hide insufficient credits modal
|
|
*/
|
|
const hideInsufficientCredits = () => {
|
|
setState((prev) => ({ ...prev, visible: false }));
|
|
};
|
|
|
|
/**
|
|
* Handle API error and show modal if it's a credit error
|
|
* Returns true if it was a credit error, false otherwise
|
|
*/
|
|
const handleCreditError = (error: any): boolean => {
|
|
const creditError = extractCreditError(error);
|
|
if (creditError) {
|
|
showInsufficientCredits(creditError);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
return {
|
|
...state,
|
|
showInsufficientCredits,
|
|
hideInsufficientCredits,
|
|
handleCreditError,
|
|
};
|
|
}
|