refactor: add user info to authData

This commit is contained in:
Tristan Yang
2022-06-29 10:56:50 +08:00
parent c5694a07b0
commit aff66ad5c8
37 changed files with 94 additions and 104 deletions
+3 -5
View File
@@ -58,15 +58,13 @@ export default function ChannelChat({ cid = "", dropFiles = [] }) {
userIds,
data,
messageData,
loginUid,
loginUser,
footprint
} = useSelector((store) => {
return {
selects: store.ui.selectMessages[`channel_${cid}`],
footprint: store.footprint,
loginUser: store.contacts.byId[store.authData.uid],
loginUid: store.authData.uid,
loginUser: store.authData.user,
// msgIds: store.channelMessage[cid] || [],
userIds: store.contacts.ids,
data: store.channels.byId[cid] || {},
@@ -93,7 +91,7 @@ export default function ChannelChat({ cid = "", dropFiles = [] }) {
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));
const addVisible = loginUser?.is_admin || owner == loginUid;
const addVisible = loginUser?.is_admin || owner == loginUser.uid;
console.log("channel message list", msgIds);
const readIndex = footprint.readChannels[cid];
const pinCount = data?.pinned_messages?.length || 0;
@@ -236,7 +234,7 @@ export default function ChannelChat({ cid = "", dropFiles = [] }) {
if (!curr) return null;
const isFirst = idx == 0;
const prev = isFirst ? null : messageData[feeds[idx - 1]];
const read = curr?.from_uid == loginUid || mid <= readIndex;
const read = curr?.from_uid == loginUser.uid || mid <= readIndex;
return renderMessageFragment({
selectMode: !!selects,
updateReadIndex: updateReadDebounced,
+2 -11
View File
@@ -29,21 +29,12 @@ const NavItem = ({ id, setFiles, toggleRemoveConfirm }) => {
handleContextMenuEvent,
hideContextMenu
} = useContextMenu();
const {
channel,
mids,
messageData,
readIndex,
muted,
loginUid
// loginUser,
} = useSelector((store) => {
const { channel, mids, messageData, readIndex, muted, loginUid } = useSelector((store) => {
return {
// loginUser: store.contacts.byId[store.authData.uid],
channel: store.channels.byId[id],
mids: store.channelMessage[id],
messageData: store.message,
loginUid: store.authData.uid,
loginUid: store.authData.user?.uid,
readIndex: store.footprint.readChannels[id],
muted: store.footprint.muteChannels[id]
};
+3 -3
View File
@@ -1,5 +1,4 @@
// import { useState, useEffect } from "react";
import { useSelector } from "react-redux";
import { useDebounce } from "rooks";
import Tippy from "@tippyjs/react";
import FavList from "../FavList";
@@ -15,6 +14,7 @@ import { StyledHeader, StyledDMChat } from "./styled";
import LoadMore from "../LoadMore";
import { renderMessageFragment } from "../utils";
import useMessageFeed from "../../../common/hook/useMessageFeed";
import { useAppSelector } from "../../../app/store";
export default function DMChat({ uid = "", dropFiles = [] }) {
const {
@@ -29,10 +29,10 @@ export default function DMChat({ uid = "", dropFiles = [] }) {
const [updateReadIndex] = useReadMessageMutation();
const updateReadDebounced = useDebounce(updateReadIndex, 300);
console.log("dm files", dropFiles);
const { currUser, messageData, footprint, loginUid, selects } = useSelector((store) => {
const { currUser, messageData, footprint, loginUid, selects } = useAppSelector((store) => {
return {
selects: store.ui.selectMessages[`user_${uid}`],
loginUid: store.authData.uid,
loginUid: store.authData.user?.uid,
footprint: store.footprint,
currUser: store.contacts.byId[uid],
messageData: store.message
+1 -1
View File
@@ -11,7 +11,7 @@ interface Props {
const DMList: FC<Props> = ({ uids, setDropFiles }) => {
const { userMessage, messageData, readUsers, loginUid } = useAppSelector((store) => {
return {
loginUid: store.authData.uid,
loginUid: store.authData.user?.uid,
readUsers: store.footprint.readUsers,
contactData: store.contacts.byId,
userMessage: store.userMessage.byId,
+1 -1
View File
@@ -55,7 +55,7 @@ export default function Session({
(store) => {
return {
mids: type == "user" ? store.userMessage.byId[id] : store.channelMessage[id],
loginUid: store.authData.uid,
loginUid: store.authData.user?.uid,
readIndex:
type == "user" ? store.footprint.readUsers[id] : store.footprint.readChannels[id],
messageData: store.message,
+1 -1
View File
@@ -12,7 +12,7 @@ export default function SessionList({ tempSession = null }) {
const { channelIDs, DMs, readChannels, readUsers, channelMessage, userMessage, loginUid } =
useAppSelector((store) => {
return {
loginUid: store.authData.uid,
loginUid: store.authData.user?.uid,
channelIDs: store.channels.ids,
DMs: store.userMessage.ids,
userMessage: store.userMessage.byId,
+3 -3
View File
@@ -1,7 +1,6 @@
// import React from 'react';
// import { useEffect } from "react";
import { Outlet, NavLink, useLocation, useMatch } from "react-router-dom";
import { useSelector } from "react-redux";
import StyledWrapper from "./styled";
import User from "./User";
// import Tools from "./Tools";
@@ -17,6 +16,7 @@ import ChatIcon from "../../assets/icons/chat.svg";
import ContactIcon from "../../assets/icons/contact.svg";
import FavIcon from "../../assets/icons/bookmark.svg";
import FolderIcon from "../../assets/icons/folder.svg";
import { useAppSelector } from "../../app/store";
// const routes = ["/setting", "/setting/channel/:cid"];
export default function HomePage() {
usePWABadge();
@@ -29,10 +29,10 @@ export default function HomePage() {
ready,
remeberedNavs: { chat: chatPath, contact: contactPath }
}
} = useSelector((store) => {
} = useAppSelector((store) => {
return {
ui: store.ui,
loginUid: store.authData.uid
loginUid: store.authData.user?.uid
};
});
const { loading } = usePreload();
+3 -1
View File
@@ -13,7 +13,7 @@ import { useAppSelector } from "../../app/store";
export default function usePreload() {
const { rehydrate, rehydrated } = useRehydrate();
const { loginUid, token } = useAppSelector((store) => {
return { loginUid: store.authData.uid, token: store.authData.token };
return { loginUid: store.authData.user?.uid, token: store.authData.token };
});
const { setStreamingReady } = useStreaming();
const [
@@ -54,6 +54,8 @@ export default function usePreload() {
}
}, [rehydrated]);
const canStreaming = loginUid && rehydrated && !!token;
console.log("ttt", loginUid, rehydrated, token);
useEffect(() => {
setStreamingReady(canStreaming);
}, [canStreaming]);
-2
View File
@@ -13,8 +13,6 @@ export default function InvitePage() {
const [samePwd, setSamePwd] = useState(true);
const [token, setToken] = useState("");
const [valid, setValid] = useState(false);
// const [sp] = useSearchParams();
// const navigateTo = useNavigate();
const [register, { data, isLoading, isSuccess, isError, error }] = useRegisterMutation();
const [checkToken, { data: isValid, isLoading: checkLoading, isSuccess: checkSuccess }] =
useCheckMagicTokenValidMutation();
+1 -1
View File
@@ -116,7 +116,7 @@ export default function MyAccount() {
const [editModal, setEditModal] = useState(null);
const [uploadAvatar, { isSuccess: uploadSuccess }] = useUpdateAvatarMutation();
const loginUser = useAppSelector((store) => {
return store.contacts.byId[store.authData.uid];
return store.authData.user;
});
useEffect(() => {
+1 -1
View File
@@ -96,7 +96,7 @@ const navs = [
const useNavs = () => {
const loginUser = useAppSelector((store) => {
return store.contacts.byId[store.authData.uid];
return store.authData.user;
});
return navs.filter((nav) => {
if (loginUser?.is_admin) {
+1 -1
View File
@@ -46,7 +46,7 @@ const StyledWrapper = styled.div`
export default function Overview({ id = 0 }) {
const { loginUser, channel } = useAppSelector((store) => {
return {
loginUser: store.contacts.byId[store.authData.uid],
loginUser: store.authData.user,
channel: store.channels.byId[id]
};
});
+3 -4
View File
@@ -10,10 +10,9 @@ let from: string | null = null;
export default function ChannelSetting() {
const { cid } = useParams();
const { isAdmin, loginUid, channel } = useAppSelector((store) => {
const { loginUser, channel } = useAppSelector((store) => {
return {
loginUid: store.authData.uid,
isAdmin: store.contacts.byId[store.authData.uid]?.is_admin,
loginUser: store.authData.user,
channel: store.channels.byId[cid]
};
});
@@ -41,7 +40,7 @@ export default function ChannelSetting() {
};
if (!cid) return null;
const currNav = flatenNavs.find((n) => n.name == navKey) || flatenNavs[0];
const canDelete = isAdmin || channel?.owner == loginUid;
const canDelete = loginUser.isAdmin || channel?.owner == loginUser.uid;
const canLeave = !channel?.is_public;
return (