feat: guest mode
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { FC, ReactElement, useEffect } from "react";
|
||||
import { Navigate } from "react-router-dom";
|
||||
import { useGetInitializedQuery, useLazyGuestLoginQuery } from "../../app/services/auth";
|
||||
import { useGetLoginConfigQuery } from "../../app/services/server";
|
||||
import { useAppSelector } from "../../app/store";
|
||||
|
||||
interface Props {
|
||||
children: ReactElement;
|
||||
}
|
||||
|
||||
const GuestOnly: FC<Props> = ({ children }) => {
|
||||
const { data: loginConfig, isLoading: fetchingConfig } = useGetLoginConfigQuery();
|
||||
const { isLoading: initChecking } = useGetInitializedQuery();
|
||||
const [guestLogin, { isLoading: guestSigning }] = useLazyGuestLoginQuery();
|
||||
const { token, user, initialized } = useAppSelector((store) => store.authData);
|
||||
|
||||
useEffect(() => {
|
||||
// 未登录
|
||||
if (!token) {
|
||||
guestLogin();
|
||||
}
|
||||
}, [token, user]);
|
||||
|
||||
// 已登录的非guest用户
|
||||
if (token && user?.create_by !== "guest") {
|
||||
return <Navigate to={`/`} replace />;
|
||||
}
|
||||
if (initChecking || guestSigning || fetchingConfig) return null;
|
||||
// console.log("wtfff", token, user);
|
||||
// 检查有没有开启guest mode
|
||||
if (!loginConfig?.guest) return <Navigate to={`/v/off`} replace />;
|
||||
// 未初始化 则先走setup 流程
|
||||
if (!initialized) return <Navigate to={`/onboarding`} replace />;
|
||||
return token ? children : null;
|
||||
};
|
||||
|
||||
export default GuestOnly;
|
||||
@@ -155,7 +155,7 @@ const Message: FC<IProps> = ({
|
||||
edited
|
||||
})
|
||||
)}
|
||||
{reactions && <Reaction mid={mid} reactions={reactions} />}
|
||||
{reactions && <Reaction mid={mid} reactions={reactions} readOnly={readOnly} />}
|
||||
</div>
|
||||
</div>
|
||||
</ContextMenu>
|
||||
|
||||
@@ -6,17 +6,24 @@ import { useLazyGetUsersQuery } from "../../app/services/user";
|
||||
import { useLazyGetServerQuery } from "../../app/services/server";
|
||||
import useStreaming from "./useStreaming";
|
||||
import { useAppSelector } from "../../app/store";
|
||||
import { useLazyGetHistoryMessagesQuery } from "../../app/services/channel";
|
||||
// type Props={
|
||||
// guest?:boolean
|
||||
// }
|
||||
let preloadChannelMsgs = false;
|
||||
export default function usePreload() {
|
||||
const [preloadChannelMessages] = useLazyGetHistoryMessagesQuery();
|
||||
const { rehydrate, rehydrated } = useRehydrate();
|
||||
const {
|
||||
loginUid,
|
||||
token,
|
||||
expireTime = +new Date()
|
||||
expireTime = +new Date(),
|
||||
channelMessageData,
|
||||
channelIds
|
||||
} = useAppSelector((store) => {
|
||||
return {
|
||||
channelIds: store.channels.ids,
|
||||
channelMessageData: store.channelMessage,
|
||||
loginUid: store.authData.user?.uid,
|
||||
token: store.authData.token,
|
||||
expireTime: store.authData.expireTime
|
||||
@@ -48,6 +55,16 @@ export default function usePreload() {
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (channelIds.length > 0 && !preloadChannelMsgs) {
|
||||
const tmps = channelIds.filter((cid) => !channelMessageData[cid]);
|
||||
console.log("tmpss", tmps);
|
||||
tmps.forEach((id) => {
|
||||
preloadChannelMessages({ id, limit: 50 });
|
||||
});
|
||||
preloadChannelMsgs = true;
|
||||
}
|
||||
}, [channelIds, channelMessageData]);
|
||||
useEffect(() => {
|
||||
if (rehydrated) {
|
||||
getUsers();
|
||||
|
||||
Reference in New Issue
Block a user