Files
AeroStart/context/ToastContext.tsx
ZyphrZero 56dd6d8bf2 feat: initialize AeroStart browser start page project
Implement a modern, customizable browser start page with comprehensive features:

- Multi-theme support with 8 preset color schemes
- Custom wallpaper system supporting images and videos with multiple fit modes
- Integrated search functionality with 5 major search engines (Google, Baidu, Bing, DuckDuckGo, Bilibili)
- Real-time clock component with 12/24 hour format options
- Dynamic background blur effect during search for enhanced focus
- Complete i18n system with English and Chinese language support
- Responsive design with smooth animations and transitions
- Local storage integration for persistent user preferences
- Context menu system for quick settings access
- Toast notification system for user feedback
- Error boundary for robust error handling

Tech Stack:
- React 19 with TypeScript
- Vite 6 for build tooling
- Tailwind CSS for styling
- Local storage for data persistence

Project Structure:
- Core components: Clock, SearchBox, SettingsModal, ThemeSettings, WallpaperManager
- Utility modules: storage management, search suggestions
- Context providers: Toast notifications, i18n
- Type definitions and constants configuration

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-05 02:55:50 +08:00

80 lines
2.9 KiB
TypeScript

import React, { createContext, useContext, useState, useCallback, ReactNode } from 'react';
import { createPortal } from 'react-dom';
import { XIcon, CheckIcon, AlertCircleIcon, InfoIcon } from '../components/Icons';
export type ToastType = 'success' | 'error' | 'info' | 'warning';
interface Toast {
id: string;
message: string;
type: ToastType;
}
interface ToastContextType {
showToast: (message: string, type?: ToastType, duration?: number) => void;
}
const ToastContext = createContext<ToastContextType | undefined>(undefined);
export const useToast = () => {
const context = useContext(ToastContext);
if (!context) {
throw new Error('useToast must be used within a ToastProvider');
}
return context;
};
interface ToastProviderProps {
children: ReactNode;
}
export const ToastProvider: React.FC<ToastProviderProps> = ({ children }) => {
const [toasts, setToasts] = useState<Toast[]>([]);
const removeToast = useCallback((id: string) => {
setToasts((prev) => prev.filter((toast) => toast.id !== id));
}, []);
const showToast = useCallback((message: string, type: ToastType = 'info', duration = 3500) => {
const id = Date.now().toString() + Math.random().toString();
setToasts((prev) => [...prev, { id, message, type }]);
if (duration > 0) {
setTimeout(() => {
removeToast(id);
}, duration);
}
}, [removeToast]);
return (
<ToastContext.Provider value={{ showToast }}>
{children}
{createPortal(
<div className="fixed top-6 left-1/2 -translate-x-1/2 z-[100] flex flex-col gap-3 w-full max-w-sm pointer-events-none px-4 md:px-0">
{toasts.map((toast) => (
<div
key={toast.id}
className="pointer-events-auto flex items-start gap-3 px-4 py-3 rounded-xl bg-[#1a1a1a]/90 backdrop-blur-xl border border-white/10 shadow-[0_8px_30px_rgba(0,0,0,0.5)] animate-in slide-in-from-top-5 fade-in duration-300 text-white group"
>
<div className="flex-shrink-0 pt-0.5">
{toast.type === 'success' && <CheckIcon className="w-5 h-5 text-green-400" />}
{toast.type === 'error' && <AlertCircleIcon className="w-5 h-5 text-red-400" />}
{toast.type === 'warning' && <AlertCircleIcon className="w-5 h-5 text-yellow-400" />}
{toast.type === 'info' && <InfoIcon className="w-5 h-5 text-blue-400" />}
</div>
<p className="text-sm font-medium flex-1 break-words leading-tight opacity-90">{toast.message}</p>
<button
onClick={() => removeToast(toast.id)}
className="p-1 -mt-1 -mr-1 rounded-md hover:bg-white/10 text-white/40 hover:text-white transition-colors"
>
<XIcon className="w-4 h-4" />
</button>
</div>
))}
</div>,
document.body
)}
</ToastContext.Provider>
);
};