fix;合并修复问题
This commit is contained in:
@@ -12,7 +12,7 @@ type PartnerSessionValue = {
|
||||
account: PartnerAccount | null;
|
||||
loading: boolean;
|
||||
loggedIn: boolean;
|
||||
refresh: () => Promise<void>;
|
||||
refresh: () => Promise<PartnerAccount | null>;
|
||||
logout: () => void;
|
||||
};
|
||||
|
||||
@@ -21,18 +21,24 @@ const PartnerSessionContext = createContext<PartnerSessionValue | null>(null);
|
||||
export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
const [account, setAccount] = useState<PartnerAccount | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loggedIn, setLoggedIn] = useState(() => isLoggedIn());
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
const refresh = useCallback(async (): Promise<PartnerAccount | null> => {
|
||||
if (!isLoggedIn()) {
|
||||
setAccount(null);
|
||||
setLoggedIn(false);
|
||||
setLoading(false);
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
setLoggedIn(true);
|
||||
setLoading(true);
|
||||
try {
|
||||
const data = await request<PartnerAccount>('PARTNER_H5', '/partner/me');
|
||||
const data = await request<PartnerAccount>('PARTNER_H5', '/partner/me', { silent: true });
|
||||
setAccount(data);
|
||||
return data;
|
||||
} catch {
|
||||
setAccount(null);
|
||||
return null;
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -41,6 +47,7 @@ export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
const logout = useCallback(() => {
|
||||
clearAuth();
|
||||
setAccount(null);
|
||||
setLoggedIn(false);
|
||||
window.location.href = toAppPath('/login');
|
||||
}, []);
|
||||
|
||||
@@ -50,7 +57,7 @@ export function PartnerSessionProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
return (
|
||||
<PartnerSessionContext.Provider
|
||||
value={{ account, loading, loggedIn: isLoggedIn(), refresh, logout }}
|
||||
value={{ account, loading, loggedIn, refresh, logout }}
|
||||
>
|
||||
{children}
|
||||
</PartnerSessionContext.Provider>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react';
|
||||
import { registerPartnerToastListener, type PartnerToastVariant } from '../lib/toast';
|
||||
|
||||
type PartnerToastContextValue = {
|
||||
showToast: (message: string, variant?: PartnerToastVariant) => void;
|
||||
};
|
||||
|
||||
const PartnerToastContext = createContext<PartnerToastContextValue | null>(null);
|
||||
|
||||
export function PartnerToastProvider({ children }: { children: ReactNode }) {
|
||||
const [toast, setToast] = useState('');
|
||||
const [variant, setVariant] = useState<PartnerToastVariant>('success');
|
||||
const timerRef = useRef<number | null>(null);
|
||||
|
||||
const showToast = useCallback((message: string, nextVariant: PartnerToastVariant = 'success') => {
|
||||
if (timerRef.current) window.clearTimeout(timerRef.current);
|
||||
setVariant(nextVariant);
|
||||
setToast(message);
|
||||
timerRef.current = window.setTimeout(() => {
|
||||
setToast('');
|
||||
timerRef.current = null;
|
||||
}, nextVariant === 'error' ? 3000 : 2000);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
registerPartnerToastListener(showToast);
|
||||
return () => {
|
||||
registerPartnerToastListener(null);
|
||||
if (timerRef.current) window.clearTimeout(timerRef.current);
|
||||
};
|
||||
}, [showToast]);
|
||||
|
||||
return (
|
||||
<PartnerToastContext.Provider value={{ showToast }}>
|
||||
{children}
|
||||
{toast ? (
|
||||
<div
|
||||
className={`partner-toast${variant === 'error' ? ' partner-toast--error' : ''}`}
|
||||
role="alert"
|
||||
aria-live="assertive"
|
||||
>
|
||||
{toast}
|
||||
</div>
|
||||
) : null}
|
||||
</PartnerToastContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function usePartnerToast(): PartnerToastContextValue {
|
||||
const ctx = useContext(PartnerToastContext);
|
||||
if (!ctx) throw new Error('usePartnerToast 必须在 PartnerToastProvider 内使用');
|
||||
return ctx;
|
||||
}
|
||||
Reference in New Issue
Block a user