import React, { ReactNode } from 'react'; import { Box, Typography, Button, Alert, Dialog, DialogTitle, DialogContent, DialogActions, Card, CardContent, CardActions, Chip, LinearProgress } from '@mui/material'; import { useNavigate } from 'react-router-dom'; import { useSubscriptionGuard, SubscriptionGuardOptions } from '../hooks/useSubscriptionGuard'; import { Lock as LockIcon, Upgrade as UpgradeIcon } from '@mui/icons-material'; interface SubscriptionGuardProps extends SubscriptionGuardOptions { children: ReactNode; feature?: string; fallbackMessage?: string; showUpgradeButton?: boolean; showUsageProgress?: boolean; } export const SubscriptionGuard: React.FC = ({ children, feature, fallbackMessage, showUpgradeButton = true, showUsageProgress = false, ...guardOptions }) => { const navigate = useNavigate(); const { subscription, loading, isGuarded, checkFeatureAccess, getRemainingUsage, checkSubscription } = useSubscriptionGuard(guardOptions); if (loading) { return ( Checking subscription... ); } if (isGuarded) { if (fallbackMessage) { return ( {fallbackMessage} ); } return ( Feature Locked This feature requires an active subscription. {subscription && ( Current Plan: {subscription.reason && ( {subscription.reason} )} )} {showUpgradeButton && ( )} ); } if (feature && !checkFeatureAccess(feature)) { const remaining = getRemainingUsage(feature); return ( {fallbackMessage || `You've reached your limit for ${feature}. Upgrade to continue using this feature.`} {showUpgradeButton && ( )} ); } return <>{children}; }; // Convenience component for protecting entire sections export const ProtectedSection: React.FC<{ children: ReactNode; feature?: string; title?: string; }> = ({ children, feature, title }) => { return ( {title && ( {title} )} {children} ); }; // Hook for checking if user can perform an action export const useCanPerformAction = (action: string) => { const { subscription, isFeatureAvailable } = useSubscriptionGuard(); return { canPerform: subscription?.active && isFeatureAvailable(action), subscription, }; };