chore: deprecate RevoltClient context

This commit is contained in:
Paul Makles
2022-06-29 16:02:35 +01:00
parent 0e86f19da2
commit 0261fec676
13 changed files with 118 additions and 108 deletions

View File

@@ -1,6 +1,7 @@
import { observer } from "mobx-react-lite";
import { Redirect } from "react-router-dom";
import { useSession } from "../../controllers/client/ClientController";
import { clientController } from "../../controllers/client/ClientController";
interface Props {
auth?: boolean;
@@ -9,16 +10,17 @@ interface Props {
children: Children;
}
export const CheckAuth = (props: Props) => {
const session = useSession();
export const CheckAuth = observer((props: Props) => {
const loggedIn = clientController.isLoggedIn();
if (props.auth && !session?.ready) {
// Redirect if logged out on authenticated page or vice-versa.
if (props.auth && !loggedIn) {
if (props.blockRender) return null;
return <Redirect to="/login" />;
} else if (!props.auth && session?.ready) {
} else if (!props.auth && loggedIn) {
if (props.blockRender) return null;
return <Redirect to="/" />;
}
return <>{props.children}</>;
};
});

View File

@@ -1,27 +0,0 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { observer } from "mobx-react-lite";
import { useEffect } from "preact/hooks";
import { Preloader } from "@revoltchat/ui";
import { useApplicationState } from "../../mobx/State";
import { clientController } from "../../controllers/client/ClientController";
type Props = {
children: Children;
};
export default observer(({ children }: Props) => {
const session = clientController.getActiveSession();
if (session) {
if (!session.ready) return <Preloader type="spinner" />;
const client = session.client!;
const state = useApplicationState();
useEffect(() => state.registerListeners(client), [state, client]);
}
return <>{children}</>;
});