diff --git a/src/app/slices/message.user.ts b/src/app/slices/message.user.ts index d5531950..0e11cf35 100644 --- a/src/app/slices/message.user.ts +++ b/src/app/slices/message.user.ts @@ -60,7 +60,7 @@ const userMsgSlice = createSlice({ } } }, - removeUserSession(state, action) { + removeUserSession(state, action: PayloadAction) { const ids = Array.isArray(action.payload) ? action.payload : [action.payload]; state.ids = state.ids.filter((id) => ids.findIndex((i) => i == id) == -1); } diff --git a/src/common/hook/useStreaming/index.ts b/src/common/hook/useStreaming/index.ts index 7a3cde63..4576cf3a 100644 --- a/src/common/hook/useStreaming/index.ts +++ b/src/common/hook/useStreaming/index.ts @@ -26,6 +26,8 @@ import { useAppDispatch, useAppSelector } from "../../../app/store"; import { ServerEvent, UsersStateEvent } from "../../../types/sse"; import { useRenewMutation } from "../../../app/services/auth"; import { getLocalAuthData } from "../../utils"; +import { removeUserSession } from "../../../app/slices/message.user"; +import { removeChannelSession } from "../../../app/slices/message.channel"; const getQueryString = (params: { [key: string]: string }) => { const sp = new URLSearchParams(); @@ -155,6 +157,10 @@ export default function useStreaming() { if (uid === loginUid && action === "update") { dispatch(updateLoginUser(omitBy(rest, isNull))); } + if (action === "delete") { + // 同时删掉对应的聊天记录 + dispatch(removeUserSession(uid)); + } }); break; } @@ -265,8 +271,11 @@ export default function useStreaming() { } break; } - case "kick_from_group": + case "kick_from_group": { dispatch(removeChannel(data.gid)); + // 同时清掉channel聊天记录 + dispatch(removeChannelSession(data.gid)); + } break; case "pinned_message_updated": dispatch(updatePinMessage(data)); diff --git a/src/routes/chat/ChannelChat/index.tsx b/src/routes/chat/ChannelChat/index.tsx index bf875154..8efd2bc2 100644 --- a/src/routes/chat/ChannelChat/index.tsx +++ b/src/routes/chat/ChannelChat/index.tsx @@ -1,7 +1,9 @@ import { useState, useEffect, memo } from "react"; -import { useLocation } from "react-router-dom"; +import { useLocation, useNavigate } from "react-router-dom"; import Tippy from "@tippyjs/react"; +import { useTranslation } from "react-i18next"; import { useDispatch } from "react-redux"; + import PinList from "./PinList"; import FavList from "../FavList"; import { updateRememberedNavs } from "../../../app/slices/ui"; @@ -11,9 +13,7 @@ import Layout from "../Layout"; import IconFav from "../../../assets/icons/bookmark.svg"; import IconPeople from "../../../assets/icons/people.svg"; import IconPin from "../../../assets/icons/pin.svg"; - import { useAppSelector } from "../../../app/store"; -import { useTranslation } from "react-i18next"; import GoBackNav from "../../../common/component/GoBackNav"; import Members from "./Members"; type Props = { @@ -23,6 +23,7 @@ type Props = { function ChannelChat({ cid = 0, dropFiles = [] }: Props) { const { t } = useTranslation("chat"); const { pathname } = useLocation(); + const navigate = useNavigate(); const dispatch = useDispatch(); const [membersVisible, setMembersVisible] = useState(true); @@ -37,6 +38,12 @@ function ChannelChat({ cid = 0, dropFiles = [] }: Props) { data: store.channels.byId[cid], }; }); + useEffect(() => { + if (!data) { + // channel不存在了 回首页 + navigate("/chat"); + } + }, [data]); useEffect(() => { dispatch(updateRememberedNavs()); return () => { @@ -48,6 +55,7 @@ function ChannelChat({ cid = 0, dropFiles = [] }: Props) { setMembersVisible((prev) => !prev); }; + if (!data) return null; const { name, description, is_public, members = [], owner } = data; const memberIds = is_public ? userIds : members.slice(0).sort((n) => (n == owner ? -1 : 0)); diff --git a/src/routes/chat/DMChat/index.tsx b/src/routes/chat/DMChat/index.tsx index 7bde1997..2d89ec94 100644 --- a/src/routes/chat/DMChat/index.tsx +++ b/src/routes/chat/DMChat/index.tsx @@ -1,4 +1,5 @@ -import { FC } from "react"; +import { FC, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; import Tippy from "@tippyjs/react"; import FavList from "../FavList"; import Tooltip from "../../../common/component/Tooltip"; @@ -12,11 +13,18 @@ type Props = { dropFiles?: File[]; }; const DMChat: FC = ({ uid = 0, dropFiles }) => { + const navigate = useNavigate(); const { currUser } = useAppSelector((store) => { return { currUser: store.users.byId[uid], }; }); + useEffect(() => { + if (!currUser) { + // user不存在了 回首页 + navigate("/chat"); + } + }, [currUser]); if (!currUser) return null; return (