memowake-front/provider.tsx
2025-06-26 15:12:34 +08:00

94 lines
2.2 KiB
TypeScript

import { I18nextProvider } from "react-i18next";
import { Platform } from 'react-native';
import Toast, { BaseToast, ErrorToast, ToastConfig } from 'react-native-toast-message';
import { Provider as ReduxProvider } from "react-redux";
import { AuthProvider } from "./contexts/auth-context";
import i18n from "./i18n";
import { LanguageProvider } from "./i18n/LanguageContext";
import { store } from "./store";
// 自定义 Toast 配置
const toastConfig: ToastConfig = {
/*
覆盖默认 success 类型
- 使用自定义组件 BaseToast
- 可以添加任何 props 到组件
*/
success: (props) => (
<BaseToast
{...props}
style={{ borderLeftColor: '#4CAF50' }}
contentContainerStyle={{ paddingHorizontal: 15 }}
text1Style={{
fontSize: 15,
fontWeight: '600'
}}
text2Style={{
fontSize: 13,
color: '#666'
}}
/>
),
/*
覆盖默认 error 类型
*/
error: (props) => (
<ErrorToast
{...props}
style={{ borderLeftColor: '#F44336' }}
contentContainerStyle={{ paddingHorizontal: 15 }}
text1Style={{
fontSize: 15,
fontWeight: '600'
}}
text2Style={{
fontSize: 13,
color: '#666'
}}
/>
),
/*
自定义 info 类型
*/
info: (props) => (
<BaseToast
{...props}
style={{ borderLeftColor: '#2196F3' }}
contentContainerStyle={{ paddingHorizontal: 15 }}
text1Style={{
fontSize: 15,
fontWeight: '600'
}}
text2Style={{
fontSize: 13,
color: '#666'
}}
/>
),
};
export function Provider({ children }: { children: React.ReactNode }) {
return (
<I18nextProvider i18n={i18n}>
<LanguageProvider>
<ReduxProvider store={store}>
<AuthProvider>
{children}
<Toast
config={toastConfig}
position={Platform.OS === 'web' ? 'top' : 'bottom'}
topOffset={Platform.OS === 'web' ? 20 : undefined}
bottomOffset={Platform.OS === 'web' ? undefined : 40}
visibilityTime={3000}
autoHide
/>
</AuthProvider>
</ReduxProvider>
</LanguageProvider>
</I18nextProvider>
);
}