Add client context.

This commit is contained in:
Paul
2021-06-18 20:07:26 +01:00
parent e7d1ada13d
commit aa81ebb298
12 changed files with 260 additions and 33 deletions

View File

@@ -4,13 +4,6 @@ import { Provider } from "react-redux";
import { Children } from "../types/Preact";
import { useEffect, useState } from "preact/hooks";
async function loadState() {
const state = await localForage.getItem("state");
if (state) {
store.dispatch({ type: "__INIT", state });
}
}
interface Props {
children: Children;
}
@@ -19,10 +12,16 @@ export default function State(props: Props) {
const [loaded, setLoaded] = useState(false);
useEffect(() => {
loadState().then(() => setLoaded(true));
localForage.getItem("state")
.then(state => {
if (state !== null) {
store.dispatch({ type: "__INIT", state });
}
setLoaded(true);
});
}, []);
if (!loaded) return null;
return <Provider store={store}>{props.children}</Provider>;
}

View File

@@ -2,6 +2,7 @@ import { createStore } from "redux";
import rootReducer from "./reducers";
import localForage from "localforage";
import { Core } from "revolt.js/dist/api/objects";
import { Typing } from "./reducers/typing";
import { Drafts } from "./reducers/drafts";
import { AuthState } from "./reducers/auth";
@@ -13,6 +14,7 @@ import { QueuedMessage } from "./reducers/queue";
import { ExperimentOptions } from "./reducers/experiments";
export type State = {
config: Core.RevoltNodeConfiguration,
locale: Language;
auth: AuthState;
settings: Settings;
@@ -40,6 +42,7 @@ export const store = createStore((state: any, action: any) => {
// Save state using localForage.
store.subscribe(() => {
const {
config,
locale,
auth,
settings,
@@ -51,6 +54,7 @@ store.subscribe(() => {
} = store.getState() as State;
localForage.setItem("state", {
config,
locale,
auth,
settings,

View File

@@ -1,6 +1,7 @@
import { State } from "..";
import { combineReducers } from "redux";
import { config, ConfigAction } from "./server_config";
import { settings, SettingsAction } from "./settings";
import { locale, LocaleAction } from "./locale";
import { auth, AuthAction } from "./auth";
@@ -12,6 +13,7 @@ import { sync, SyncAction } from "./sync";
import { experiments, ExperimentsAction } from "./experiments";
export default combineReducers({
config,
locale,
auth,
settings,
@@ -24,6 +26,7 @@ export default combineReducers({
});
export type Action =
| ConfigAction
| LocaleAction
| AuthAction
| SettingsAction

View File

@@ -0,0 +1,20 @@
import { Core } from "revolt.js/dist/api/objects";
export type ConfigAction =
| { type: undefined }
| {
type: "SET_CONFIG";
config: Core.RevoltNodeConfiguration;
};
export function config(
state = { } as Core.RevoltNodeConfiguration,
action: ConfigAction
): Core.RevoltNodeConfiguration {
switch (action.type) {
case "SET_CONFIG":
return action.config;
default:
return state;
}
}