From f2e47225573c0d39e744b1c977bfdaaafcb08cf9 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Thu, 26 May 2022 12:40:10 +0800 Subject: [PATCH] client: imrpove data fetch --- .../src/components/document/collaboration/index.tsx | 4 +++- .../client/src/components/document/share/index.tsx | 2 +- .../client/src/components/document/style/index.tsx | 1 + packages/client/src/data/document.tsx | 12 +++++++----- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/client/src/components/document/collaboration/index.tsx b/packages/client/src/components/document/collaboration/index.tsx index 7ab8951..27a0492 100644 --- a/packages/client/src/components/document/collaboration/index.tsx +++ b/packages/client/src/components/document/collaboration/index.tsx @@ -51,7 +51,9 @@ export const DocumentCollaboration: React.FC = ({ wikiId, documentId, di const toastedUsersRef = useRef>([]); const { user: currentUser } = useUser(); const [visible, toggleVisible] = useToggle(false); - const { users, loading, error, addUser, updateUser, deleteUser } = useDoumentMembers(documentId); + const { users, loading, error, addUser, updateUser, deleteUser } = useDoumentMembers(documentId, { + enabled: visible, + }); const [inviteUser, setInviteUser] = useState(''); const [collaborationUsers, setCollaborationUsers] = useState([]); diff --git a/packages/client/src/components/document/share/index.tsx b/packages/client/src/components/document/share/index.tsx index 06d1a20..9420f24 100644 --- a/packages/client/src/components/document/share/index.tsx +++ b/packages/client/src/components/document/share/index.tsx @@ -18,7 +18,7 @@ const { Text } = Typography; export const DocumentShare: React.FC = ({ documentId, disabled = false, render }) => { const [visible, toggleVisible] = useToggle(false); - const { data, loading, error, toggleStatus } = useDocumentDetail(documentId); + const { data, loading, error, toggleStatus } = useDocumentDetail(documentId, { enabled: visible }); const [sharePassword, setSharePassword] = useState(''); const isPublic = useMemo(() => data && isPublicDocument(data.document.status), [data]); const shareUrl = useMemo(() => data && getDocumentShareURL(data.document.id), [data]); diff --git a/packages/client/src/components/document/style/index.tsx b/packages/client/src/components/document/style/index.tsx index 117b2ee..661b087 100644 --- a/packages/client/src/components/document/style/index.tsx +++ b/packages/client/src/components/document/style/index.tsx @@ -17,6 +17,7 @@ export const DocumentStyle = () => { return ( { - const { data, error, isLoading, refetch } = useQuery(DocumentApiDefinition.getMemberById.client(documentId), () => - getDocumentMembers(documentId) +export const useDoumentMembers = (documentId, options?: UseQueryOptions>) => { + const { data, error, isLoading, refetch } = useQuery( + DocumentApiDefinition.getMemberById.client(documentId), + () => getDocumentMembers(documentId), + options ); const addUser = useCallback( @@ -135,11 +137,11 @@ export const getDocumentDetail = (documentId, cookie = null): Promise { +export const useDocumentDetail = (documentId, options: UseQueryOptions = {}) => { const { data, error, isLoading, refetch } = useQuery( DocumentApiDefinition.getDetailById.client(documentId), () => getDocumentDetail(documentId), - { staleTime: 3000, refetchOnReconnect: true, refetchOnMount: true, refetchOnWindowFocus: true } + { ...options, staleTime: 3000 } ); /**