Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nested settings support #487

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/renderer/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { React, channels, fluxDispatcher, guilds } from "@common";
import { React, channels, fluxDispatcher, guilds, lodash } from "@common";
import type { Fiber } from "react-reconciler";
import type { ReactElement } from "react";
import type { Jsonifiable } from "type-fest";
import type { ObjectExports } from "../types";
import { SettingsManager } from "./apis/settings";
Expand Down Expand Up @@ -182,7 +183,7 @@ export function useSetting<
F extends T[K] | undefined,
>(
settings: SettingsManager<T, D>,
key: K,
key: keyof T & string extends K ? string : K,
fallback?: F,
): {
value: K extends D
Expand All @@ -192,8 +193,10 @@ export function useSetting<
: NonNullable<T[K]> | F;
onChange: (newValue: ValType<T[K]>) => void;
} {
const initial = settings.get(key, fallback);
const [value, setValue] = React.useState(initial);
const [initialKey, ...pathArray] = key.split(".") as K[];
const path = pathArray.join(".");
const initial = settings.get(initialKey, fallback);
const [value, setValue] = React.useState(path.length ? lodash.get(initial, path) : initial);

return {
value,
Expand All @@ -209,9 +212,11 @@ export function useSetting<
const targetChecked = target && "checked" in target ? target.checked : undefined;
const finalValue = (checked ?? targetChecked ?? targetValue ?? value ?? newValue) as T[K];

// @ts-expect-error dumb
setValue(finalValue);
settings.set(key, finalValue);
settings.set(
key as K,
path.length ? (lodash.set(initial as object, path, finalValue) as T[K]) : finalValue,
);
},
};
}
Expand Down Expand Up @@ -353,11 +358,11 @@ export function findInTree(
* @returns The component you are looking for
*/
export function findInReactTree(
tree: Tree,
tree: Tree | ReactElement,
searchFilter: TreeFilter,
maxRecursion = 100,
): Tree | null | undefined {
return findInTree(tree, searchFilter, {
): Tree | ReactElement | null | undefined {
return findInTree(tree as Tree, searchFilter, {
walkable: ["props", "children", "child", "sibling"],
maxRecursion,
});
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
Expand Down