mirror of
https://github.com/stoatchat/for-legacy-web.git
synced 2026-03-06 17:11:55 +00:00
Add client context.
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
import State from "../redux/State";
|
||||
import { Children } from "../types/Preact";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
|
||||
import ClientContext from './revoltjs/RevoltClient';
|
||||
import Locale from "./Locale";
|
||||
import Theme from "./Theme";
|
||||
|
||||
export default function Context({ children }: { children: Children }) {
|
||||
return (
|
||||
<State>
|
||||
<Locale>
|
||||
<Theme>{children}</Theme>
|
||||
</Locale>
|
||||
</State>
|
||||
<BrowserRouter>
|
||||
<State>
|
||||
<ClientContext>
|
||||
<Locale>
|
||||
<Theme>{children}</Theme>
|
||||
</Locale>
|
||||
</ClientContext>
|
||||
</State>
|
||||
</BrowserRouter>
|
||||
);
|
||||
}
|
||||
|
||||
22
src/context/revoltjs/CheckAuth.tsx
Normal file
22
src/context/revoltjs/CheckAuth.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ReactNode } from "react";
|
||||
import { useContext } from "preact/hooks";
|
||||
import { Redirect } from "react-router-dom";
|
||||
|
||||
import { AppContext } from "./RevoltClient";
|
||||
|
||||
interface Props {
|
||||
auth?: boolean;
|
||||
children: ReactNode | ReactNode[];
|
||||
}
|
||||
|
||||
export const CheckAuth = (props: Props) => {
|
||||
const { operations } = useContext(AppContext);
|
||||
|
||||
if (props.auth && !operations.ready()) {
|
||||
return <Redirect to="/login" />;
|
||||
} else if (!props.auth && operations.ready()) {
|
||||
return <Redirect to="/" />;
|
||||
}
|
||||
|
||||
return <>{props.children}</>;
|
||||
};
|
||||
@@ -1,4 +1,12 @@
|
||||
import { Client } from "revolt.js";
|
||||
import { createContext } from "preact";
|
||||
import { useState } from "preact/hooks";
|
||||
import { Children } from "../../types/Preact";
|
||||
import { Route } from "revolt.js/dist/api/routes";
|
||||
import { connectState } from "../../redux/connector";
|
||||
import { WithDispatcher } from "../../redux/reducers";
|
||||
import { AuthState } from "../../redux/reducers/auth";
|
||||
import { SyncOptions } from "../../redux/reducers/sync";
|
||||
|
||||
export enum ClientStatus {
|
||||
LOADING,
|
||||
@@ -10,10 +18,60 @@ export enum ClientStatus {
|
||||
ONLINE,
|
||||
}
|
||||
|
||||
export const RevoltJSClient = new Client({
|
||||
export interface ClientOperations {
|
||||
login: (data: Route<"POST", "/auth/login">["data"]) => Promise<void>;
|
||||
logout: (shouldRequest?: boolean) => Promise<void>;
|
||||
loggedIn: () => boolean;
|
||||
ready: () => boolean;
|
||||
}
|
||||
|
||||
export interface AppState {
|
||||
status: ClientStatus;
|
||||
operations: ClientOperations;
|
||||
}
|
||||
|
||||
export const AppContext = createContext<AppState>(undefined as any);
|
||||
|
||||
export const RevoltClient = new Client({
|
||||
autoReconnect: false,
|
||||
apiURL: process.env.API_SERVER,
|
||||
apiURL: import.meta.env.VITE_API_URL,
|
||||
debug: process.env.NODE_ENV === "development",
|
||||
// Match sw.js#13
|
||||
// db: new Db("state", 3, ["channels", "servers", "users", "members"])
|
||||
});
|
||||
|
||||
type Props = WithDispatcher & {
|
||||
auth: AuthState;
|
||||
sync: SyncOptions;
|
||||
children: Children;
|
||||
};
|
||||
|
||||
function Context({ auth, sync, children, dispatcher }: Props) {
|
||||
const [status, setStatus] = useState(ClientStatus.LOADING);
|
||||
|
||||
const value: AppState = {
|
||||
status,
|
||||
operations: {
|
||||
login: async data => {},
|
||||
logout: async shouldRequest => {},
|
||||
loggedIn: () => false,
|
||||
ready: () => false
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<AppContext.Provider value={value}>
|
||||
{ children }
|
||||
</AppContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export default connectState<{ children: Children }>(
|
||||
Context,
|
||||
state => {
|
||||
return {
|
||||
auth: state.auth,
|
||||
sync: state.sync
|
||||
};
|
||||
},
|
||||
true
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user