fix: can not send new message at new channel

This commit is contained in:
zerosoul
2022-06-08 22:00:51 +08:00
parent 58cbd6e675
commit c3fe8152f8
4 changed files with 41 additions and 43 deletions
+23 -36
View File
@@ -1,46 +1,33 @@
import { useRef, useEffect } from "react";
// import { useDebounce } from "rooks";
function useChatScroll(dep) {
function useChatScroll(deps = []) {
const ref = useRef();
// const updateScrollTop = useDebounce(() => {
// console.log("chat scroll", ref);
// if (ref.current) {
// setTimeout(() => {
// ref.current.scrollTop = ref.current.scrollHeight;
// }, 50);
// }
// }, 100);
// const updateScrollTop = () => {
// console.log("chat scroll", ref);
// if (ref.current) {
// setTimeout(() => {
// ref.current.scrollTop = ref.current.scrollHeight;
// }, 100);
// }
// };
useEffect(() => {
console.log("chat scroll", ref);
if (ref.current) {
// setTimeout(() => {
if (ref.current) {
ref.current.scrollTop = ref.current.scrollHeight;
}
// }, 20);
}
}, [dep]);
// useEffect(() => {
// console.log("chat scroll", ref);
// if (ref.current) {
// const ele = ref.current;
// const resizeObserver = new ResizeObserver((entries) => {
// for (let entry of entries) {
// entry.target.scrollTop = entry.target.scrollHeight;
// }
// });
// resizeObserver.observe(ele);
// // setTimeout(() => {
// if (ref.current) {
// setTimeout(() => {
// ref.current.scrollTop = ref.current.scrollHeight;
// }, 500);
// }
// // }, 20);
// }
// }, []);
// }, [...deps]);
useEffect(() => {
if (ref.current) {
console.log("chat scroll", ref);
const ele = ref.current;
const resizeObserver = new ResizeObserver((entries) => {
for (let entry of entries) {
console.log("update scroll top");
entry.target.scrollTop = entry.target.scrollHeight;
}
});
resizeObserver.observe(ele);
}
}, []);
return ref;
}
+11 -4
View File
@@ -76,18 +76,25 @@ export default function useMessageFeed({ context = "channel", id = null }) {
}
}, [items, context, id]);
useEffect(() => {
if (listRef.current.length == 0 && mids.length) {
const serverMids = mids.filter((id) => {
const ts = new Date().getTime();
return Math.abs(ts - id) > 5 * 1000;
});
if (listRef.current.length == 0 && serverMids.length) {
// 初次
const pageInfo = getFeedWithPagination({ mids, isLast: true });
const pageInfo = getFeedWithPagination({
mids: serverMids,
isLast: true,
});
console.log("pull up 2", pageInfo);
pageRef.current = pageInfo;
listRef.current = pageInfo.ids;
setItems(listRef.current);
console.log("message pageInfo", mids, pageInfo);
console.log("message pageInfo", serverMids, pageInfo);
} else {
// 追加
const lastMid = listRef.current.slice(-1);
const sorteds = mids.slice(0).sort((a, b) => {
const sorteds = serverMids.slice(0).sort((a, b) => {
return Number(a) - Number(b);
});
const appends = sorteds.filter((s) => s > lastMid);