refactor: guest load more
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useState, useRef, useCallback } from "react";
|
||||||
import { useLazyGetHistoryMessagesQuery } from "../../app/services/channel";
|
import { useLazyGetHistoryMessagesQuery } from "../../app/services/channel";
|
||||||
import { useLazyGetHistoryMessagesQuery as useLazyGetDMHistoryMsg } from "../../app/services/user";
|
import { useLazyGetHistoryMessagesQuery as useLazyGetDMHistoryMsg } from "../../app/services/user";
|
||||||
import { useAppSelector } from "../../app/store";
|
import { useAppSelector } from "../../app/store";
|
||||||
@@ -83,16 +83,40 @@ export default function useMessageFeed({ context = "channel", id }: Props) {
|
|||||||
setAppends([]);
|
setAppends([]);
|
||||||
setPulling(false);
|
setPulling(false);
|
||||||
}, [context, id]);
|
}, [context, id]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// 处理自动滚动
|
const currentItems = listRef.current;
|
||||||
if (items.length > 0) {
|
// const [lastMid=Infinity]=currentItems.slice(-1)
|
||||||
let wrapper = containerRef.current = document.querySelector(`#VOCECHAT_FEED_${context}_${id}`);
|
//过滤掉本地(以及后来追加的消息?)
|
||||||
if (wrapper) {
|
const serverMids = mids.filter((id: number) => {
|
||||||
const newScroll = wrapper.scrollHeight - wrapper.clientHeight;
|
// 如果是本地消息,id是时间戳
|
||||||
wrapper.scrollTop = curScrollPos + (newScroll - oldScroll);
|
const ts = +new Date();
|
||||||
|
return Math.abs(ts - id) > 10 * 1000;
|
||||||
|
});
|
||||||
|
if (serverMids.length > 0) {
|
||||||
|
if (currentItems.length == 0) {
|
||||||
|
//初次
|
||||||
|
const pageInfo = getFeedWithPagination({
|
||||||
|
mids: serverMids,
|
||||||
|
isLast: true
|
||||||
|
});
|
||||||
|
pageRef.current = pageInfo;
|
||||||
|
listRef.current = pageInfo.ids;
|
||||||
|
setItems(pageInfo.ids);
|
||||||
|
// console.log("message pageInfo", serverMids, pageInfo);
|
||||||
|
} else {
|
||||||
|
const container = containerRef.current;
|
||||||
|
if (container) {
|
||||||
|
const loadMoreEle = container.querySelector("[data-load-more]");
|
||||||
|
console.log("effected by pull server data", loadMoreEle, isElementVisible(loadMoreEle));
|
||||||
|
if (isElementVisible(loadMoreEle)) {
|
||||||
|
// 有更新:来自于拉取历史消息,拉取下一页数据
|
||||||
|
loadMore();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [items, context, id]);
|
}, [mids]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
//处理追加:来自于其它人的实时消息以及自己发的
|
//处理追加:来自于其它人的实时消息以及自己发的
|
||||||
@@ -121,40 +145,17 @@ export default function useMessageFeed({ context = "channel", id }: Props) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [mids, messageData, loginUid]);
|
}, [mids, messageData, loginUid]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const currentItems = listRef.current;
|
// 处理自动滚动
|
||||||
// const [lastMid=Infinity]=currentItems.slice(-1)
|
if (items.length > 0) {
|
||||||
//过滤掉本地(以及后来追加的消息?)
|
let wrapper = containerRef.current = document.querySelector(`#VOCECHAT_FEED_${context}_${id}`);
|
||||||
const serverMids = mids.filter((id: number) => {
|
if (wrapper) {
|
||||||
// 如果是本地消息,id是时间戳
|
const newScroll = wrapper.scrollHeight - wrapper.clientHeight;
|
||||||
const ts = +new Date();
|
wrapper.scrollTop = curScrollPos + (newScroll - oldScroll);
|
||||||
return Math.abs(ts - id) > 10 * 1000;
|
|
||||||
});
|
|
||||||
if (serverMids.length > 0) {
|
|
||||||
if (currentItems.length == 0) {
|
|
||||||
//初次
|
|
||||||
const pageInfo = getFeedWithPagination({
|
|
||||||
mids: serverMids,
|
|
||||||
isLast: true
|
|
||||||
});
|
|
||||||
pageRef.current = pageInfo;
|
|
||||||
listRef.current = pageInfo.ids;
|
|
||||||
setItems(pageInfo.ids);
|
|
||||||
// console.log("message pageInfo", serverMids, pageInfo);
|
|
||||||
} else {
|
|
||||||
const container = containerRef.current;
|
|
||||||
if (container) {
|
|
||||||
const loadMoreEle = container.querySelector("[data-load-more]");
|
|
||||||
if (isElementVisible(loadMoreEle)) {
|
|
||||||
// 有更新:来自于拉取历史消息,拉取下一页数据
|
|
||||||
console.log("effected by pull server data");
|
|
||||||
loadMore();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [mids]);
|
}, [items, context, id]);
|
||||||
|
|
||||||
const loadMore = () => {
|
const loadMore = () => {
|
||||||
console.log("load more start", mids, listRef.current, pageRef.current);
|
console.log("load more start", mids, listRef.current, pageRef.current);
|
||||||
const currPageInfo = pageRef.current;
|
const currPageInfo = pageRef.current;
|
||||||
@@ -192,26 +193,29 @@ export default function useMessageFeed({ context = "channel", id }: Props) {
|
|||||||
);
|
);
|
||||||
setPulling(false);
|
setPulling(false);
|
||||||
};
|
};
|
||||||
const pullUp = async () => {
|
const pullUp = useCallback(
|
||||||
|
async () => {
|
||||||
|
|
||||||
setPulling(true);
|
setPulling(true);
|
||||||
const currPageInfo = pageRef.current;
|
const currPageInfo = pageRef.current;
|
||||||
console.log("pull up start", currPageInfo);
|
console.log("pull up start", currPageInfo);
|
||||||
// 本地数据的第一页
|
// 本地数据的第一页
|
||||||
if (currPageInfo && currPageInfo.isFirst || (!currPageInfo && mids.length == 0)) {
|
if (currPageInfo && currPageInfo.isFirst || (!currPageInfo && mids.length == 0)) {
|
||||||
const [firstMid] = currPageInfo ? currPageInfo.ids : [0];
|
const [firstMid] = currPageInfo ? currPageInfo.ids : [0];
|
||||||
const { data: newList } = await loadMoreMsgsFromServer({
|
const { data: newList } = await loadMoreMsgsFromServer({
|
||||||
mid: firstMid,
|
mid: firstMid,
|
||||||
id
|
id
|
||||||
});
|
});
|
||||||
if (newList?.length == 0) {
|
if (newList?.length == 0) {
|
||||||
// 只有在这里,才可以把load more去掉
|
// 只有在这里,才可以把has more去掉
|
||||||
setHasMore(false);
|
setHasMore(false);
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
loadMore();
|
||||||
}
|
},
|
||||||
loadMore();
|
[context, id],
|
||||||
};
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pulling,
|
pulling,
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ export default function GuestChannelChat({ cid = 0 }: Props) {
|
|||||||
list: msgIds,
|
list: msgIds,
|
||||||
appends,
|
appends,
|
||||||
hasMore,
|
hasMore,
|
||||||
pullUp
|
pullUp,
|
||||||
|
pulling
|
||||||
} = useMessageFeed({
|
} = useMessageFeed({
|
||||||
context: "channel",
|
context: "channel",
|
||||||
id: cid
|
id: cid
|
||||||
@@ -50,7 +51,7 @@ export default function GuestChannelChat({ cid = 0 }: Props) {
|
|||||||
>
|
>
|
||||||
<StyledChannelChat id={`VOCECHAT_FEED_channel_${cid}`}>
|
<StyledChannelChat id={`VOCECHAT_FEED_channel_${cid}`}>
|
||||||
{hasMore ? (
|
{hasMore ? (
|
||||||
<LoadMore pullUp={pullUp} />
|
<LoadMore pullUp={pullUp} pulling={pulling} />
|
||||||
) : (
|
) : (
|
||||||
<div className="info">
|
<div className="info">
|
||||||
<h2 className="title">{t("welcome_channel", { name })}</h2>
|
<h2 className="title">{t("welcome_channel", { name })}</h2>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { memo, useEffect, FC } from "react";
|
import { useEffect, FC } from "react";
|
||||||
import { Waveform } from "@uiball/loaders";
|
import { Waveform } from "@uiball/loaders";
|
||||||
import { useInViewRef } from "rooks";
|
import { useInViewRef } from "rooks";
|
||||||
|
|
||||||
@@ -19,6 +19,7 @@ const LoadMore: FC<Props> = ({ pullUp = null, pulling }) => {
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export default memo(LoadMore, (prev, next) => {
|
export default LoadMore;
|
||||||
return prev.pulling === next.pulling;
|
// export default memo(LoadMore, (prev, next) => {
|
||||||
});
|
// return prev.pulling === next.pulling;
|
||||||
|
// });
|
||||||
|
|||||||
Reference in New Issue
Block a user