Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jan 16, 2025
1 parent ee4ef64 commit 9fc832c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
21 changes: 17 additions & 4 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import {
import { ThemeType } from '../../shared/constants/preferences';
import { FirstTimeFlowType } from '../../shared/constants/onboarding';
import { getMethodDataAsync } from '../../shared/lib/four-byte';
import { BackgroundStateProxy } from '../../shared/types/metamask';
import { BackgroundStateProxy } from '../../shared/types/background';
import { DecodedTransactionDataResponse } from '../../shared/types/transaction-decode';
import { LastInteractedConfirmationInfo } from '../pages/confirmations/types/confirm';
import { EndTraceRequest } from '../../shared/lib/trace';
Expand Down Expand Up @@ -1713,7 +1713,17 @@ export function updateMetamaskState(
updateCustodyState(dispatch, newState, getState());
///: END:ONLY_INCLUDE_IF

return newState;
const newFlattenedState = Object.values(newState).reduce(
(flattenedState, controllerState) => {
Object.entries(controllerState).forEach((propertyName, stateValue) => {
flattenedState[propertyName] = stateValue;
});
return flattenedState;
},
{},
);

return { newState, newFlattenedState };
};
}

Expand Down Expand Up @@ -6087,27 +6097,30 @@ export async function endBackgroundTrace(request: EndTraceRequest) {
*
* @param oldState - The current state.
* @param patches - The patches to apply.
* @param isFlattened - 'false' if the input state object is keyed by controller name.
* 'true' if it has been flattened so that controller state properties are at the top level.
* Only supports 'replace' operations with at most 2 path elements.
* Properties that are nested at deeper levels cannot be updated in isolation.
* @returns The new state.
*/
function applyPatches<State extends Record<string, unknown>>(
oldState: State,
patches: Patch[],
isFlattened: boolean = false,
): State {
const newState = { ...oldState };

for (const patch of patches) {
const { op, path, value } = patch;

if (op === 'replace') {
if (path.length === 2) {
if (!isFlattened && path.length === 2) {
const [controllerKey, key] = path;
if (!(controllerKey in newState)) {
newState[controllerKey] = {};
}
newState[controllerKey][key] = value;
} else if (path.length === 1) {
} else if (isFlattened || path.length === 1) {
newState[path[0]] = value;
}
} else {
Expand Down
60 changes: 58 additions & 2 deletions ui/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { configureStore as baseConfigureStore } from '@reduxjs/toolkit';
import devtoolsEnhancer from 'remote-redux-devtools';
import rootReducer from '../ducks';
import { AppSliceState } from '../ducks/app/app';
import { MetaMaskSliceState } from '../ducks/metamask/metamask';
import { BackgroundSliceState } from '../ducks/background/background';
import { BackgroundStateProxy } from '../../shared/types/background';

/**
* This interface is temporary and is copied from the message-manager.js file
Expand Down Expand Up @@ -34,6 +35,60 @@ export type MessagesIndexedById = {

type RootReducerReturnType = ReturnType<typeof rootReducer>;

type _$toIntersection2<T> = (
T extends unknown ? (x: T) => unknown : never
) extends (x: infer X) => void
? X
: never;

type _$toIntersection<T> = boolean extends T
? boolean & _$toIntersection2<Exclude<T, boolean>>
: _$toIntersection2<T>;

type _$toList<T, O extends unknown[] = []> = _$toIntersection<
T extends unknown ? (t: T) => T : never
> extends (x: never) => infer X
? _$toList<Exclude<T, X>, [X, ...O]>
: O;

export type _$cast<T, U> = [T] extends [U] ? T : U;

export type _$keys<T extends Record<string, unknown>> = _$toList<keyof T>;

type _PrependKeyToEntries<K extends PropertyKey, E extends unknown[][]> = {
[I in keyof E]: E[I] extends unknown[] ? [K, ...E[I]] : never;
};

type _DeepEntriesKey<
O extends Record<PropertyKey, unknown>,
K extends PropertyKey,
> = O[K] extends Record<PropertyKey, unknown>
? _PrependKeyToEntries<K, _$deepEntries<O[K]>>
: [[K, O[K]]];

export type _$deepEntries<
O extends Record<PropertyKey, unknown>,
Keys extends unknown[] = _$keys<O>,
Acc extends unknown[][] = [],
DEEP_KEYS = _DeepEntriesKey<O, _$cast<Keys[0], PropertyKey>>,
> = Keys extends [infer K, ...infer Rest]
? K extends keyof O
? _$deepEntries<O, Rest, [...Acc, ..._$cast<DEEP_KEYS, unknown[][]>]>
: never
: Acc;

type _$display<T extends Record<PropertyKey, unknown>> = {
[key in keyof T]: T[key];
};

export type _ = _DeepEntriesKey<
_$display<BackgroundStateProxy>,
keyof _$display<BackgroundStateProxy>
>;

type FlattenedBackgroundStateProxy =
FlattenObjectOneLevel<BackgroundStateProxy>;

/**
* `ReduxState` overrides incorrectly typed properties of `RootReducerReturnType`, and is only intended to be used as an input for `configureStore`.
* The `MetaMaskReduxState` type (derived from the returned output of `configureStore`) is to be used consistently as the single source-of-truth and representation of Redux state shape.
Expand All @@ -45,7 +100,8 @@ type ReduxState = {
activeTab: {
origin: string;
};
metamask: MetaMaskSliceState['metamask'];
metamask: FlattenedBackgroundStateProxy;
background: BackgroundSliceState['background'];
appState: AppSliceState['appState'];
} & Omit<RootReducerReturnType, 'activeTab' | 'metamask' | 'appState'>;

Expand Down

0 comments on commit 9fc832c

Please sign in to comment.