feat(mobx): refactor and remove (react-)redux

This commit is contained in:
Paul
2021-12-23 21:43:11 +00:00
parent 6e1bcab92b
commit cc0e45526c
55 changed files with 249 additions and 1522 deletions

View File

@@ -5,7 +5,6 @@ import { createGlobalStyle } from "styled-components";
import { useEffect } from "preact/hooks";
import { useApplicationState } from "../mobx/State";
import { getState } from "../redux";
export type Variables =
| "accent"
@@ -280,28 +279,6 @@ export const PRESETS: Record<string, Theme> = {
},
};
// todo: store used themes locally
export function getBaseTheme(name: string): Theme {
if (name in PRESETS) {
return PRESETS[name];
}
// TODO: properly initialize `themes` in state instead of letting it be undefined
const themes = getState().themes ?? {};
if (name in themes) {
const { theme } = themes[name];
return {
...PRESETS[theme.light ? "light" : "dark"],
...theme,
};
}
// how did we get here
return PRESETS["dark"];
}
const keys = Object.keys(PRESETS.dark);
const GlobalTheme = createGlobalStyle<{ theme: Theme }>`
:root {

View File

@@ -1,7 +1,5 @@
import { BrowserRouter as Router } from "react-router-dom";
import State from "../redux/State";
import { Children } from "../types/Preact";
import Locale from "./Locale";
import Theme from "./Theme";
@@ -15,14 +13,12 @@ import Client from "./revoltjs/RevoltClient";
export default function Context({ children }: { children: Children }) {
return (
<Router basename={import.meta.env.BASE_URL}>
<State>
<Locale>
<Intermediate>
<Client>{children}</Client>
</Intermediate>
</Locale>
<Theme />
</State>
<Locale>
<Intermediate>
<Client>{children}</Client>
</Intermediate>
</Locale>
<Theme />
</Router>
);
}

View File

@@ -1,7 +1,6 @@
import { Text } from "preact-i18n";
import { useApplicationState } from "../../../mobx/State";
import { dispatch } from "../../../redux";
import Modal from "../../../components/ui/Modal";

View File

@@ -6,31 +6,28 @@ import { Message } from "revolt.js/dist/maps/Messages";
import { useContext, useEffect } from "preact/hooks";
import { useApplicationState } from "../../mobx/State";
import { connectState } from "../../redux/connector";
import { QueuedMessage } from "../../redux/reducers/queue";
import { setGlobalEmojiPack } from "../../components/common/Emoji";
import { AppContext } from "./RevoltClient";
type Props = {
messages: QueuedMessage[];
};
function StateMonitor(props: Props) {
export default function StateMonitor() {
const client = useContext(AppContext);
const state = useApplicationState();
useEffect(() => {
function add(msg: Message) {
if (!msg.nonce) return;
if (!props.messages.find((x) => x.id === msg.nonce)) return;
if (
!state.queue.get(msg.channel_id).find((x) => x.id === msg.nonce)
)
return;
state.queue.remove(msg.nonce);
}
client.addListener("message", add);
return () => client.removeListener("message", add);
}, [client, props.messages]);
}, [client]);
// Set global emoji pack.
useEffect(() => {
@@ -40,9 +37,3 @@ function StateMonitor(props: Props) {
return null;
}
export default connectState(StateMonitor, (state) => {
return {
messages: [...state.queue],
};
});

View File

@@ -1,25 +1,6 @@
/**
* This file monitors changes to settings and syncs them to the server.
*/
import isEqual from "lodash.isequal";
import { UserSettings } from "revolt-api/types/Sync";
import { ClientboundNotification } from "revolt.js/dist/websocket/notifications";
import { useCallback, useContext, useEffect, useMemo } from "preact/hooks";
import { dispatch } from "../../redux";
import { connectState } from "../../redux/connector";
import { Notifications } from "../../redux/reducers/notifications";
import { Settings } from "../../redux/reducers/settings";
import {
DEFAULT_ENABLED_SYNC,
SyncData,
SyncKeys,
SyncOptions,
} from "../../redux/reducers/sync";
import { Language } from "../Locale";
import { AppContext, ClientStatus, StatusContext } from "./RevoltClient";
/*type Props = {
settings: Settings;

View File

@@ -5,7 +5,6 @@ import { ClientboundNotification } from "revolt.js/dist/websocket/notifications"
import { StateUpdater } from "preact/hooks";
import Auth from "../../mobx/stores/Auth";
import { dispatch } from "../../redux";
import { ClientStatus } from "./RevoltClient";
@@ -46,29 +45,6 @@ export function registerEvents(
attemptReconnect();
},
packet: (packet: ClientboundNotification) => {
switch (packet.type) {
case "ChannelAck": {
dispatch({
type: "UNREADS_MARK_READ",
channel: packet.id,
message: packet.message_id,
});
break;
}
}
},
message: (message: Message) => {
if (message.mention_ids?.includes(client.user!._id)) {
dispatch({
type: "UNREADS_MENTION",
channel: message.channel_id,
message: message._id,
});
}
},
ready: () => setStatus(ClientStatus.ONLINE),
logout: () => {