Skip to content

Commit

Permalink
fix: more common modules
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeIlLeone committed Oct 28, 2023
1 parent 860e24c commit 4ec2cfb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 63 deletions.
3 changes: 3 additions & 0 deletions src/renderer/modules/common/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import type { TextAreaType } from "../components/TextArea";
import type { TextInputType } from "../components/TextInput";
import type { OriginalTooltipType } from "../components/Tooltip";
import { waitForProps } from "../webpack";
import type { CreateToast, ShowToast } from "./toast";

// Expand this as needed
interface DiscordComponents {
Button: ButtonType;
Checkbox: CheckboxType;
Clickable: ClickableCompType;
createToast: CreateToast;
FormDivider: DividerType;
FormItem: FormItemCompType;
FormNotice: FormNoticeType;
Expand All @@ -43,6 +45,7 @@ interface DiscordComponents {
ModalRoot: ModalType["ModalRoot"];
RadioGroup: RadioType;
Select: SelectCompType;
showToast: ShowToast;
Slider: SliderCompType;
Spinner: LoaderType;
Switch: SwitchType;
Expand Down
66 changes: 27 additions & 39 deletions src/renderer/modules/common/flux.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { filters, getFunctionBySource, waitForModule, waitForProps } from "../webpack";
import { filters, waitForModule, waitForProps } from "../webpack";
import type { DispatchBand, FluxDispatcher as Dispatcher } from "./fluxDispatcher";

type DispatchToken = string;
Expand Down Expand Up @@ -187,46 +187,34 @@ const SnapshotStoreClass = await waitForModule<typeof SnapshotStore>(
filters.bySource("SnapshotStores"),
);

type useStateFromStores = <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
compare?:
| (<T extends []>(a: T, b: T) => boolean)
| (<T extends Record<string, unknown>>(a: T, b: T) => boolean),
) => T;
type statesWillNeverBeEqual = <T>(a: T, b: T) => boolean;
type useStateFromStoresArray = <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
) => T;
type useStateFromStoresObject = <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
) => T;

const FluxHooksMod = await waitForModule(filters.bySource("useStateFromStores"));

const useStateFromStores = getFunctionBySource<useStateFromStores>(
FluxHooksMod,
"useStateFromStores",
)!;
const statesWillNeverBeEqual = getFunctionBySource<statesWillNeverBeEqual>(
FluxHooksMod,
"return!1",
)!;
const useStateFromStoresArray: useStateFromStoresArray = (stores, callback, deps) =>
useStateFromStores(stores, callback, deps, _.isEqual);
const useStateFromStoresObject: useStateFromStoresObject = (stores, callback, deps) =>
useStateFromStores(stores, callback, deps, _.isEqual);
interface FluxHooks {
useStateFromStores: <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
compare?:
| (<T extends []>(a: T, b: T) => boolean)
| (<T extends Record<string, unknown>>(a: T, b: T) => boolean),
) => T;
statesWillNeverBeEqual: <T>(a: T, b: T) => boolean;
useStateFromStoresArray: <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
) => T;
useStateFromStoresObject: <T>(
stores: Store[],
callback: () => T,
deps?: React.DependencyList,
) => T;
}

const FluxHooksMod = await waitForProps<FluxHooks>("useStateFromStores");
const FluxHooks = {
useStateFromStores,
statesWillNeverBeEqual,
useStateFromStoresArray,
useStateFromStoresObject,
useStateFromStores: FluxHooksMod.useStateFromStores,
statesWillNeverBeEqual: FluxHooksMod.statesWillNeverBeEqual,
useStateFromStoresArray: FluxHooksMod.useStateFromStoresArray,
useStateFromStoresObject: FluxHooksMod.useStateFromStoresObject,
};

export type Flux = FluxMod & { SnapshotStore: typeof SnapshotStore } & typeof FluxHooks;
Expand Down
24 changes: 14 additions & 10 deletions src/renderer/modules/common/guilds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { waitForProps } from "../webpack";
import type { Guild } from "discord-types/general";
import { virtualMerge } from "src/renderer/util";
import { waitForProps } from "../webpack";
import type { Store } from "./flux";

interface State {
selectedGuildTimestampMillis: Record<string, number>;
Expand All @@ -27,17 +28,20 @@ export interface GuildStore {

export type Guilds = SelectedGuildStore & GuildStore;

const guilds: Guilds = {
...(await waitForProps<GuildStore>("getGuild", "getGuilds").then(Object.getPrototypeOf)),
...(await waitForProps<SelectedGuildStore>("getGuildId", "getLastSelectedGuildId").then(
Object.getPrototypeOf,
)),
};
const GuildStore = await waitForProps<GuildStore & Store>("getGuild", "getGuildIds");
const SelectedGuildStore = await waitForProps<SelectedGuildStore & Store>(
"getGuildId",
"getLastSelectedGuildId",
);

export function getCurrentGuild(): Guild | undefined {
const guildId = guilds.getGuildId();
const guildId = SelectedGuildStore.getGuildId();
if (!guildId) return undefined;
return guilds.getGuild(guildId);
return GuildStore.getGuild(guildId);
}

export default virtualMerge(guilds, { getCurrentGuild });
export default virtualMerge(
Object.getPrototypeOf(GuildStore) as GuildStore,
Object.getPrototypeOf(SelectedGuildStore) as SelectedGuildStore,
{ getCurrentGuild },
);
2 changes: 2 additions & 0 deletions src/renderer/modules/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export type { API };
export let api: API;
importTimeout("api", import("./api"), (mod) => (api = mod.default));

import * as Components from "./components";
export type { Components };
export let components: typeof import("./components").default;
importTimeout("components", import("./components"), (mod) => (components = mod.default));

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/modules/common/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type Modal = {
const mod = await waitForModule(filters.bySource("onCloseRequest:null!="));
const alertMod = await waitForProps<AlertMod>("show", "close");

const classes = getBySource<ModalClasses>("().justifyStart")!;
const classes = getBySource<ModalClasses>(".justifyStart")!;

export default {
openModal: getFunctionBySource<Modal["openModal"]>(mod, "onCloseRequest:null!=")!,
Expand Down
33 changes: 20 additions & 13 deletions src/renderer/modules/common/toast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { filters, getFunctionBySource, waitForModule } from "../webpack";
import type React from "react";
import { components } from ".";

const Kind = {
MESSAGE: 0,
Expand All @@ -19,30 +20,36 @@ interface ToastOptions {
component?: React.ReactElement;
}

interface ToastProps {
message: string | React.ReactElement | null;
id: string;
type: (typeof Kind)[keyof typeof Kind];
options: ToastOptions;
}

export type CreateToast = (
content: string | React.ReactElement | null,
kind?: (typeof Kind)[keyof typeof Kind],
opts?: ToastOptions,
) => ToastProps;

export type ShowToast = (props: ToastProps) => void;

type ToastFn = (
content: string | React.ReactElement | null,
kind?: (typeof Kind)[keyof typeof Kind],
opts?: ToastOptions,
) => unknown;
) => void;

export interface Toast {
toast: ToastFn;
Kind: typeof Kind;
Position: typeof Position;
}

const mod = await waitForModule(filters.bySource("queuedToasts"));
const fn = getFunctionBySource<(props: ReturnType<ToastFn>) => void>(mod, "queuedToasts).concat")!;

const propGenMod = await waitForModule(filters.bySource(/case (\w+\.){1,2}FAILURE/));
const propGenFn = getFunctionBySource<ToastFn>(
propGenMod,
/options:{position:\w+,component:\w+,duration:\w+}/,
)!;

const toast: ToastFn = (content, kind = Kind.SUCCESS, opts = undefined) => {
const props = propGenFn(content, kind, opts);
fn(props);
const props = components.createToast(content, kind, opts);
components.showToast(props);
};

export default {
Expand Down

0 comments on commit 4ec2cfb

Please sign in to comment.