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
+22 -35
View File
@@ -1,46 +1,33 @@
import { useRef, useEffect } from "react"; import { useRef, useEffect } from "react";
// import { useDebounce } from "rooks"; // import { useDebounce } from "rooks";
function useChatScroll(dep) { function useChatScroll(deps = []) {
const ref = useRef(); 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(() => { // useEffect(() => {
// console.log("chat scroll", ref); // console.log("chat scroll", ref);
// if (ref.current) { // if (ref.current) {
// const ele = ref.current; // // setTimeout(() => {
// const resizeObserver = new ResizeObserver((entries) => { // if (ref.current) {
// for (let entry of entries) { // setTimeout(() => {
// entry.target.scrollTop = entry.target.scrollHeight; // 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); resizeObserver.observe(ele);
// } }
// }, []); }, []);
return ref; return ref;
} }
+11 -4
View File
@@ -76,18 +76,25 @@ export default function useMessageFeed({ context = "channel", id = null }) {
} }
}, [items, context, id]); }, [items, context, id]);
useEffect(() => { 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); console.log("pull up 2", pageInfo);
pageRef.current = pageInfo; pageRef.current = pageInfo;
listRef.current = pageInfo.ids; listRef.current = pageInfo.ids;
setItems(listRef.current); setItems(listRef.current);
console.log("message pageInfo", mids, pageInfo); console.log("message pageInfo", serverMids, pageInfo);
} else { } else {
// 追加 // 追加
const lastMid = listRef.current.slice(-1); 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); return Number(a) - Number(b);
}); });
const appends = sorteds.filter((s) => s > lastMid); const appends = sorteds.filter((s) => s > lastMid);
+6 -2
View File
@@ -8,7 +8,6 @@ import FavList from "../FavList";
import { useReadMessageMutation } from "../../../app/services/message"; import { useReadMessageMutation } from "../../../app/services/message";
import { updateRemeberedNavs } from "../../../app/slices/ui"; import { updateRemeberedNavs } from "../../../app/slices/ui";
import useMessageFeed from "../../../common/hook/useMessageFeed"; import useMessageFeed from "../../../common/hook/useMessageFeed";
import ChannelIcon from "../../../common/component/ChannelIcon"; import ChannelIcon from "../../../common/component/ChannelIcon";
import Tooltip from "../../../common/component/Tooltip"; import Tooltip from "../../../common/component/Tooltip";
import Contact from "../../../common/component/Contact"; import Contact from "../../../common/component/Contact";
@@ -32,12 +31,14 @@ import {
} from "./styled"; } from "./styled";
import InviteModal from "../../../common/component/InviteModal"; import InviteModal from "../../../common/component/InviteModal";
import LoadMore from "./LoadMore"; import LoadMore from "./LoadMore";
// import useChatScroll from "../../../common/hook/useChatScroll";
export default function ChannelChat({ cid = "", dropFiles = [] }) { export default function ChannelChat({ cid = "", dropFiles = [] }) {
const { list: msgIds, appends, hasMore, pullUp } = useMessageFeed({ const { list: msgIds, appends, hasMore, pullUp } = useMessageFeed({
context: "channel", context: "channel",
id: cid, id: cid,
}); });
// const scrollObserveRef = useChatScroll([msgIds, appends]);
const [toolVisible, setToolVisible] = useState(""); const [toolVisible, setToolVisible] = useState("");
const { pathname } = useLocation(); const { pathname } = useLocation();
const dispatch = useDispatch(); const dispatch = useDispatch();
@@ -230,7 +231,10 @@ export default function ChannelChat({ cid = "", dropFiles = [] }) {
) : null ) : null
} }
> >
<StyledChannelChat id={`RUSTCHAT_FEED_channel_${cid}`}> <StyledChannelChat
// ref={scrollObserveRef}
id={`RUSTCHAT_FEED_channel_${cid}`}
>
{/* <div className="anchor"></div> */} {/* <div className="anchor"></div> */}
{hasMore ? ( {hasMore ? (
<LoadMore pullUp={pullUp} /> <LoadMore pullUp={pullUp} />
+1 -1
View File
@@ -88,7 +88,7 @@ export const StyledChannelChat = styled.article`
height: -webkit-fill-available; height: -webkit-fill-available;
overflow-x: hidden; overflow-x: hidden;
overflow-y: auto; overflow-y: auto;
/* overflow-anchor: none; */ overflow-anchor: auto;
/* pagination start */ /* pagination start */
/* transform: rotate(180deg); /* transform: rotate(180deg);
direction: rtl; direction: rtl;