feat: add auth fail error

This commit is contained in:
fantasticit 2022-04-04 21:42:33 +08:00
parent c39e89b81a
commit de77948d68
3 changed files with 34 additions and 14 deletions

View File

@ -35,6 +35,8 @@ export const Editor: React.FC<IProps> = ({ user, documentId, authority, classNam
if (!user) return null; if (!user) return null;
const [status, setStatus] = useState<ProviderStatus>('connecting'); const [status, setStatus] = useState<ProviderStatus>('connecting');
const { online } = useNetwork(); const { online } = useNetwork();
const [loading, toggleLoading] = useToggle(true);
const [error, setError] = useState(null);
const provider = useMemo(() => { const provider = useMemo(() => {
return getProvider({ return getProvider({
targetId: documentId, targetId: documentId,
@ -46,6 +48,13 @@ export const Editor: React.FC<IProps> = ({ user, documentId, authority, classNam
onAwarenessUpdate({ states }) { onAwarenessUpdate({ states }) {
triggerJoinUser(states); triggerJoinUser(states);
}, },
onAuthenticationFailed() {
toggleLoading(false);
setError(new Error('鉴权失败!暂时无法提供服务'));
},
onSynced() {
toggleLoading(false);
},
}, },
}); });
}, [documentId, user.token]); }, [documentId, user.token]);
@ -65,7 +74,6 @@ export const Editor: React.FC<IProps> = ({ user, documentId, authority, classNam
} catch (e) {} } catch (e) {}
}, 50), }, 50),
}); });
const [loading, toggleLoading] = useToggle(true);
useEffect(() => { useEffect(() => {
const indexdbProvider = getIndexdbProvider(documentId, provider.document); const indexdbProvider = getIndexdbProvider(documentId, provider.document);
@ -74,10 +82,6 @@ export const Editor: React.FC<IProps> = ({ user, documentId, authority, classNam
setStatus('loadCacheSuccess'); setStatus('loadCacheSuccess');
}); });
provider.on('synced', () => {
toggleLoading(false);
});
provider.on('status', async ({ status }) => { provider.on('status', async ({ status }) => {
setStatus(status); setStatus(status);
}); });
@ -100,7 +104,7 @@ export const Editor: React.FC<IProps> = ({ user, documentId, authority, classNam
return ( return (
<DataRender <DataRender
loading={loading} loading={loading}
error={null} error={error}
normalContent={() => { normalContent={() => {
return ( return (
<div className={styles.editorWrap}> <div className={styles.editorWrap}>

View File

@ -1,4 +1,4 @@
import React, { useMemo, useEffect } from 'react'; import React, { useMemo, useEffect, useState } from 'react';
import { useEditor, EditorContent } from '@tiptap/react'; import { useEditor, EditorContent } from '@tiptap/react';
import { Layout } from '@douyinfe/semi-ui'; import { Layout } from '@douyinfe/semi-ui';
import { IDocument, ILoginUser } from '@think/domains'; import { IDocument, ILoginUser } from '@think/domains';
@ -27,6 +27,8 @@ interface IProps {
} }
export const Editor: React.FC<IProps> = ({ user, documentId, document }) => { export const Editor: React.FC<IProps> = ({ user, documentId, document }) => {
const [loading, toggleLoading] = useToggle(true);
const [error, setError] = useState(null);
const provider = useMemo(() => { const provider = useMemo(() => {
return getProvider({ return getProvider({
targetId: documentId, targetId: documentId,
@ -38,6 +40,13 @@ export const Editor: React.FC<IProps> = ({ user, documentId, document }) => {
onAwarenessUpdate({ states }) { onAwarenessUpdate({ states }) {
triggerJoinUser(states); triggerJoinUser(states);
}, },
onAuthenticationFailed() {
toggleLoading(false);
setError(new Error('鉴权失败!暂时无法提供服务'));
},
onSynced() {
toggleLoading(false);
},
}, },
}); });
}, [documentId, user.token]); }, [documentId, user.token]);
@ -54,13 +63,8 @@ export const Editor: React.FC<IProps> = ({ user, documentId, document }) => {
taskItemClickable: true, taskItemClickable: true,
}, },
}); });
const [loading, toggleLoading] = useToggle(true);
useEffect(() => { useEffect(() => {
provider.on('synced', () => {
toggleLoading(false);
});
return () => { return () => {
destoryProvider(provider, 'READER'); destoryProvider(provider, 'READER');
}; };
@ -70,7 +74,7 @@ export const Editor: React.FC<IProps> = ({ user, documentId, document }) => {
<DataRender <DataRender
loading={loading} loading={loading}
loadingContent={<DocumentSkeleton />} loadingContent={<DocumentSkeleton />}
error={null} error={error}
normalContent={() => { normalContent={() => {
return ( return (
<Content className={styles.editorWrap}> <Content className={styles.editorWrap}>

View File

@ -20,6 +20,18 @@ export const getProvider = ({
user: IUser; user: IUser;
docType: 'document' | 'template'; docType: 'document' | 'template';
events?: { events?: {
onAuthenticated?: () => void;
onAuthenticationFailed?: ({ reason }: { reason: string }) => void;
onOpen?: (event: Event) => void;
onConnect?: () => void;
onMessage?: (event: MessageEvent) => void;
onOutgoingMessage?: (message) => void;
onStatus?: (status: any) => void;
onSynced?: () => void;
onDisconnect?: (event: CloseEvent) => void;
onClose?: (event: CloseEvent) => void;
onDestroy?: () => void;
onAwarenessChange?: (states: any) => void;
onAwarenessUpdate?: (states: any) => void; onAwarenessUpdate?: (states: any) => void;
}; };
}) => { }) => {
@ -38,7 +50,7 @@ export const getProvider = ({
maxAttempts: 5, maxAttempts: 5,
forceSyncInterval: 100, forceSyncInterval: 100,
...events, ...events,
}); } as any);
pool.set(targetId, provider); pool.set(targetId, provider);
} }
return pool.get(targetId); return pool.get(targetId);