A context-based hook for displaying alert notifications with different types (error, warning, success, info) throughout the application.
src/components/app-alert.tsx
AppAlertProvider - Must wrap your app or component tree to use this hook
function useAppAlert(): {
show: (options: Options) => void;
closeModal: () => void;
}interface Options {
title: string;
description?: string;
type?: "warning" | "error" | "success" | "info";
isShowCancel?: boolean;
isShowConfirm?: boolean;
}None - Hook uses React Context
An object containing:
show: Function to display alert dialogcloseModal: Function to manually close the dialog
import { AppAlertProvider } from '@/components/app-alert';
function App() {
return (
<AppAlertProvider>
<YourAppContent />
</AppAlertProvider>
);
}import { useAppAlert } from '@/components/app-alert';
function MyComponent() {
const { show } = useAppAlert();
const handleError = () => {
show({
title: "Operation Failed",
description: "Unable to save your changes. Please try again.",
type: "error"
});
};
const handleSuccess = () => {
show({
title: "Success!",
description: "Your changes have been saved successfully.",
type: "success"
});
};
return (
<div>
<button onClick={handleError}>Trigger Error</button>
<button onClick={handleSuccess}>Trigger Success</button>
</div>
);
}const { show } = useAppAlert();
show({
title: "Error",
description: "Something went wrong. Please try again later.",
type: "error"
});show({
title: "Warning",
description: "This action may have unintended consequences.",
type: "warning"
});show({
title: "Success",
description: "Your action was completed successfully.",
type: "success"
});show({
title: "Information",
description: "Here's some helpful information for you.",
type: "info"
});function DataFetcher() {
const { show } = useAppAlert();
const fetchData = async () => {
try {
const response = await fetch('/api/data');
if (!response.ok) throw new Error('Failed to fetch');
show({
title: "Data Loaded",
description: "Successfully loaded the latest data.",
type: "success"
});
} catch (error) {
show({
title: "Loading Failed",
description: "Could not load data. Please check your connection and try again.",
type: "error"
});
}
};
return <button onClick={fetchData}>Load Data</button>;
}function FormComponent() {
const { show } = useAppAlert();
const validateAndSubmit = (formData: FormData) => {
if (!formData.email) {
show({
title: "Validation Error",
description: "Please provide a valid email address.",
type: "warning"
});
return;
}
// Submit form
show({
title: "Form Submitted",
description: "Thank you! Your form has been submitted successfully.",
type: "success"
});
};
return (
<form onSubmit={validateAndSubmit}>
{/* Form fields */}
</form>
);
}function OnboardingTips() {
const { show } = useAppAlert();
const showTip = () => {
show({
title: "Pro Tip",
description: "You can use keyboard shortcuts to navigate faster. Press 'H' for help.",
type: "info"
});
};
return <button onClick={showTip}>Show Tip</button>;
}Each alert type displays a different icon:
- Error: Red CircleX icon
- Warning: Yellow warning triangle
- Success: Green checkmark (using warning icon as placeholder)
- Info: Blue info circle
title: Alert title text
description: Additional descriptive texttype: Alert type - "error" (default), "warning", "success", "info"isShowCancel: Currently not used in implementationisShowConfirm: Currently not used in implementation
- Context-based: Uses React Context for global state management
- Type-based styling: Different icons and colors for each alert type
- Internationalization: Uses translation hook for "Close" button
- Accessible: Built with AlertDialog component for accessibility
- TypeScript support: Fully typed with proper interfaces
- Memoized rendering: Optimized icon rendering with useMemo
- Uses
useStatefor dialog state and content management - Uses
useCallbackfor memoized event handlers - Uses
useMemofor optimized icon rendering - Built on top of shadcn/ui AlertDialog component
- Integrates with i18n translation system
- Renders icons conditionally based on alert type
- API error handling: Display server error messages
- Form validation: Show validation errors and warnings
- Success notifications: Confirm successful operations
- User guidance: Provide helpful information and tips
- System status: Notify users about system states
- Feature announcements: Inform users about new features
- Maintenance notices: Alert users about system maintenance
- Use appropriate alert types for different situations
- Keep titles concise and descriptions informative
- Provide actionable information when possible
- Use error alerts for failures, warnings for cautions
- Use success alerts for confirmations
- Use info alerts for helpful tips and information
The hook will throw an error if used outside of AppAlertProvider:
Error: useAppAlert must be used within a AppAlertProvider
- Icons are styled with appropriate colors for each type
- Error icons use
text-destructiveclass - Warning icons use
text-warningclass - Success icons use
text-successclass - Info icons use default text color
- Icons are sized at
size-20(80px)
- Uses
useTranslationhook for localized text - "Close" button text is translated based on current language
- Supports Bengali and English languages
- Icon rendering is memoized with
useMemo - Provider state is isolated to prevent unnecessary re-renders
- Callbacks are memoized to prevent function recreation
- Lightweight state management with minimal overhead