1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

fixed 'show more' status while initial comment load

This commit is contained in:
Fedor Katurov 2022-12-15 11:18:45 +06:00
parent 585f9b57cb
commit 60fdced237
2 changed files with 20 additions and 9 deletions

View file

@ -34,15 +34,21 @@ const NodeComments: FC<IProps> = memo(({ order }) => {
const groupped: ICommentGroup[] = useGrouppedComments( const groupped: ICommentGroup[] = useGrouppedComments(
comments, comments,
order, order,
lastSeenCurrent ?? undefined lastSeenCurrent ?? undefined,
); );
const more = useMemo( const more = useMemo(
() => () =>
hasMore && <div className={styles.more}> hasMore &&
<LoadMoreButton isLoading={isLoadingMore} onClick={onLoadMoreComments} /> !isLoading && (
</div>, <div className={styles.more}>
[hasMore, onLoadMoreComments, isLoadingMore] <LoadMoreButton
isLoading={isLoadingMore}
onClick={onLoadMoreComments}
/>
</div>
),
[hasMore, onLoadMoreComments, isLoadingMore, isLoading],
); );
if (!node?.id) { if (!node?.id) {
@ -53,7 +59,7 @@ const NodeComments: FC<IProps> = memo(({ order }) => {
<div className={styles.wrap}> <div className={styles.wrap}>
{order === 'DESC' && more} {order === 'DESC' && more}
{groupped.map(group => ( {groupped.map((group) => (
<Comment <Comment
nodeId={node.id!} nodeId={node.id!}
key={group.ids.join()} key={group.ids.join()}

View file

@ -1,4 +1,4 @@
import { useCallback, useMemo } from 'react'; import { useCallback, useMemo, useState } from 'react';
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite'; import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
@ -26,6 +26,8 @@ const extractKey = (key: string) => {
}; };
export const useGetComments = (nodeId: number, fallbackData?: IComment[]) => { export const useGetComments = (nodeId: number, fallbackData?: IComment[]) => {
const [isInitialLoaded, setIsInitialLoaded] = useState(false);
const { data, isValidating, setSize, size, mutate } = useSWRInfinite( const { data, isValidating, setSize, size, mutate } = useSWRInfinite(
getKey(nodeId), getKey(nodeId),
async (key: string) => { async (key: string) => {
@ -39,6 +41,7 @@ export const useGetComments = (nodeId: number, fallbackData?: IComment[]) => {
}, },
{ {
fallbackData: fallbackData && [fallbackData], fallbackData: fallbackData && [fallbackData],
onSuccess: () => setIsInitialLoaded(true),
}, },
); );
@ -52,13 +55,15 @@ export const useGetComments = (nodeId: number, fallbackData?: IComment[]) => {
[setSize, size], [setSize, size],
); );
const isLoading = isValidating && !isInitialLoaded;
return { return {
comments, comments,
hasMore, hasMore,
onLoadMoreComments, onLoadMoreComments,
isLoading: !data && isValidating, isLoading,
mutate, mutate,
data, data,
isLoadingMore: !!data?.length && isValidating, isLoadingMore: !!data?.length && isValidating && isInitialLoaded,
}; };
}; };