Skip to content

Commit 376f13c

Browse files
vojtatrantapeter-sanderson
authored andcommitted
feat(suite): add option to pass extra dependencies factory to store and as extra thunk parameter and use it freely also from react context
fix: magic fix for extra in middlewares fix(suite): make sure that the thunk middleware is added first so that other middlewares also can use it
1 parent c48adfc commit 376f13c

File tree

98 files changed

+418
-329
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+418
-329
lines changed

packages/suite-desktop-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
},
1414
"dependencies": {
1515
"@sentry/electron": "^7.2.0",
16+
"@suite-common/redux-utils": "workspace:*",
1617
"@suite-common/sentry": "workspace:*",
1718
"@suite-common/suite-types": "workspace:*",
18-
"@suite/suite-sync": "workspace:*",
1919
"@trezor/components": "workspace:*",
2020
"@trezor/connect": "workspace:*",
2121
"@trezor/ipc-proxy": "workspace:*",

packages/suite-desktop-ui/src/MainDesktop.tsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import { DesktopUpdater } from './support/DesktopUpdater';
3030
import { TorLoadingScreen } from './support/screens/TorLoadingScreen';
3131
import { BioAuthGuard } from '../../suite/src/components/suite/BioAuthGuard/BioAuthGuard';
3232
import { desktopComponents } from './support/desktopComponents';
33-
import { initSuiteSyncDesktop } from '@suite/suite-sync';
3433
import { type History, createMemoryHistory } from 'history';
3534
import { createRouterServices } from 'src/support/extraDependencies';
3635
import { FindBar } from '../../suite/src/components/suite/FindBar/FindBar';
3736
import { GlobalStyle } from './GlobalStyle';
3837
import { initSentry } from './sentry';
38+
import { ServicesProvider } from '@suite-common/redux-utils';
3939

4040
const MainDesktop = ({ history }: { history: History }) => {
4141
useTor();
@@ -71,7 +71,7 @@ export const init = async (container: HTMLElement) => {
7171
const memoryHistory = createMemoryHistory();
7272
const preloadAction = await preloadStore();
7373
const { statePatch } = await desktopApi.handshake();
74-
const store = initStore(preloadAction, {
74+
const { store, extra } = initStore(preloadAction, {
7575
statePatch,
7676
additionalExtraDeps: { routerServices: createRouterServices(memoryHistory) },
7777
});
@@ -102,11 +102,13 @@ export const init = async (container: HTMLElement) => {
102102
if (shouldRunTor) {
103103
await new Promise(resolve => {
104104
root.render(
105-
<ReduxProvider store={store}>
106-
<ConnectedIntlProvider>
107-
<TorLoadingScreen callback={resolve} />
108-
</ConnectedIntlProvider>
109-
</ReduxProvider>,
105+
<ServicesProvider services={extra.services}>
106+
<ReduxProvider store={store}>
107+
<ConnectedIntlProvider>
108+
<TorLoadingScreen callback={resolve} />
109+
</ConnectedIntlProvider>
110+
</ReduxProvider>
111+
</ServicesProvider>,
110112
);
111113
desktopApi.toggleTor(true);
112114
});
@@ -136,13 +138,12 @@ export const init = async (container: HTMLElement) => {
136138
// TODO should it really be here instead of initAction.ts?
137139
await store.dispatch(initBluetoothThunk());
138140

139-
// Todo: run it from extra, shall be solved in: https://github.com/trezor/trezor-suite/pull/23356
140-
initSuiteSyncDesktop({ getState: store.getState }).init();
141-
142141
// finally render whole app
143142
root.render(
144-
<ReduxProvider store={store}>
145-
<MainDesktop history={memoryHistory} />
146-
</ReduxProvider>,
143+
<ServicesProvider services={extra.services}>
144+
<ReduxProvider store={store}>
145+
<MainDesktop history={memoryHistory} />
146+
</ReduxProvider>
147+
</ServicesProvider>,
147148
);
148149
};

packages/suite-desktop-ui/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"../suite/src/components/suite/BioAuthGuard"
1010
],
1111
"references": [
12+
{
13+
"path": "../../suite-common/redux-utils"
14+
},
1215
{ "path": "../../suite-common/sentry" },
1316
{
1417
"path": "../../suite-common/suite-types"
1518
},
16-
{ "path": "../../suite/suite-sync" },
1719
{ "path": "../components" },
1820
{ "path": "../connect" },
1921
{ "path": "../ipc-proxy" },

packages/suite-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"dependencies": {
1616
"@sentry/browser": "10.17.0",
17+
"@suite-common/redux-utils": "workspace:*",
1718
"@suite-common/sentry": "workspace:*",
1819
"@suite-common/suite-types": "workspace:*",
1920
"@trezor/connect": "workspace:*",
@@ -25,7 +26,6 @@
2526
"react-redux": "9.2.0"
2627
},
2728
"devDependencies": {
28-
"@suite/suite-sync": "workspace:*",
2929
"@trezor/connect": "workspace:*",
3030
"@trezor/eslint": "workspace:*",
3131
"@types/react": "^19.0.0",

packages/suite-web/src/MainWeb.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Provider as ReduxProvider } from 'react-redux';
66
import { History, createBrowserHistory } from 'history';
77
import { createRoot } from 'react-dom/client';
88

9-
import { initSuiteSyncDesktop } from '@suite/suite-sync';
9+
import { ServicesProvider } from '@suite-common/redux-utils';
1010

1111
import { AppRouter, BundleLoader, Metadata, Preloader, ToastContainer } from 'src/components/suite';
1212
import { useDebugLanguageShortcut } from 'src/hooks/suite';
@@ -53,18 +53,17 @@ export const init = async (container: HTMLElement) => {
5353
root.render(<LoadingScreen />);
5454

5555
const preloadAction = await preloadStore();
56-
const store = initStore(preloadAction, {
56+
const { store, extra } = initStore(preloadAction, {
5757
additionalExtraDeps: {
5858
routerServices: createRouterServices(browserHistory),
5959
},
6060
});
6161

62-
// Todo: run it from extra, shall be solved in: https://github.com/trezor/trezor-suite/pull/23356
63-
initSuiteSyncDesktop({ getState: store.getState }).init();
64-
6562
root.render(
66-
<ReduxProvider store={store}>
67-
<MainWeb history={browserHistory} />
68-
</ReduxProvider>,
63+
<ServicesProvider services={extra.services}>
64+
<ReduxProvider store={store}>
65+
<MainWeb history={browserHistory} />
66+
</ReduxProvider>
67+
</ServicesProvider>,
6968
);
7069
};

packages/suite-web/tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
},
77
"include": ["."],
88
"references": [
9+
{
10+
"path": "../../suite-common/redux-utils"
11+
},
912
{ "path": "../../suite-common/sentry" },
1013
{
1114
"path": "../../suite-common/suite-types"
1215
},
1316
{ "path": "../connect" },
1417
{ "path": "../suite" },
15-
{ "path": "../../suite/suite-sync" },
1618
{ "path": "../eslint" }
1719
]
1820
}

packages/suite/src/reducers/store.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { accountsActions } from '@suite-common/wallet-core';
1818
import { isCodesignBuild } from '@trezor/env-utils';
1919
import { mergeDeepObject } from '@trezor/utils';
2020
import { suiteSyncSlice } from 'src/actions/suiteSync/suiteSyncSlice';
21+
import { ExtraDependencies, createStoreWithExtraStoreMiddleware } from '@suite-common/redux-utils';
2122

2223
import backupMiddlewares from 'src/middlewares/backup';
2324
import onboardingMiddlewares from 'src/middlewares/onboarding';
@@ -32,14 +33,13 @@ import walletReducers from 'src/reducers/wallet';
3233
// toastMiddleware can be used only in suite-desktop and suite-web
3334
// it's not included into `@suite-middlewares` index
3435
import { geolocationReducer } from '@suite-common/geolocation';
35-
import { ExtraDependencies } from '@suite-common/redux-utils';
3636
import { prepareLabelingReducer } from '@suite-common/suite-sync';
3737
import { OPEN_USER_CONTEXT } from 'src/actions/suite/constants/modalConstants';
3838
import toastMiddleware from 'src/middlewares/suite/toastMiddleware';
3939
import { globalSendReceiveFilters } from 'src/slices/wallet/globalSendReceiveFilters';
4040
import type { PreloadStoreAction } from 'src/support/suite/preloadStore';
4141
import { bluetoothSlice } from '../actions/bluetooth/desktopBluetoothReducer';
42-
import { extraDependencies } from '../support/extraDependencies';
42+
import { extraDependencies, suiteExtraFactory } from '../support/extraDependencies';
4343
import { prepareBioAuthReducer } from './bioAuth';
4444
import { desktopReducer } from './desktop';
4545

@@ -138,7 +138,9 @@ export const initStore = <E extends Partial<ExtraDependencies>>(
138138
)
139139
: preloadedState;
140140

141-
return configureStore({
141+
let extra: ExtraDependencies | null = null;
142+
143+
const store = configureStore({
142144
reducer: rootReducer as Reducer<AppState, InferredAction, PreloadedState>,
143145
preloadedState: patchedState,
144146
middleware: getDefaultMiddleware =>
@@ -152,13 +154,27 @@ export const initStore = <E extends Partial<ExtraDependencies>>(
152154
'modal.payload.decision.reject',
153155
],
154156
},
155-
thunk: {
156-
extraArgument: {
157-
...extraDependencies,
158-
...(options.additionalExtraDeps ?? {}),
159-
},
160-
},
161-
}).concat(getCustomMiddleware()),
157+
})
158+
.prepend(
159+
createStoreWithExtraStoreMiddleware(
160+
api => ({
161+
...extraDependencies,
162+
...suiteExtraFactory(api),
163+
}),
164+
initializedExtra => {
165+
extra = initializedExtra;
166+
},
167+
),
168+
)
169+
.concat(getCustomMiddleware()),
162170
devTools,
163171
} as const);
172+
173+
if (!extra) {
174+
throw new Error(
175+
"This shouldn't happen: Extra dependencies not initialized, should be done in callback in createEnhancedStoreWithExtraStoreThunk",
176+
);
177+
}
178+
179+
return { store, extra };
164180
};

packages/suite/src/support/extraDependencies.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { PayloadAction } from '@reduxjs/toolkit';
22
import { saveAs } from 'file-saver';
33
import { type History, createMemoryHistory } from 'history';
44

5-
import { createSuiteSyncOwnerDesktop, initSuiteSyncDesktop } from '@suite/suite-sync';
5+
import { createSuiteSyncDesktop } from '@suite/suite-sync';
66
import { FW_HASH_CHECK_DEFAULT_TIMEOUTS } from '@suite-common/firmware-authenticity';
7-
import { ExtraDependencies, LocationPushState, To } from '@suite-common/redux-utils';
7+
import {
8+
ExtraDependenciesStatic,
9+
ExtraWithStoreFactory,
10+
LocationPushState,
11+
To,
12+
} from '@suite-common/redux-utils';
813
import {
914
subscribeSuiteSyncStorageThunk,
1015
unsubscribeAndDisposeSuiteSyncStorageThunk,
@@ -65,7 +70,13 @@ export const createRouterServices = (history: History) => ({
6570
navigate: (to: To, state?: LocationPushState) => history.push(to, state),
6671
});
6772

68-
export const extraDependencies: ExtraDependencies = {
73+
export const suiteExtraFactory: ExtraWithStoreFactory = store => ({
74+
services: {
75+
suiteSync: createSuiteSyncDesktop(store),
76+
},
77+
});
78+
79+
export const extraDependencies: ExtraDependenciesStatic = {
6980
thunks: {
7081
cardanoValidatePendingTxOnBlock: cardanoStakingActions.validatePendingTxOnBlock,
7182
initMetadata: metadataLabelingActions.init,
@@ -77,8 +88,6 @@ export const extraDependencies: ExtraDependencies = {
7788
// `@suite-common/suite-sync` depends on `wallet-core`
7889
subscribeSuiteSync: subscribeSuiteSyncStorageThunk,
7990
unsubscribeAndDisposeSuiteSyncStorage: unsubscribeAndDisposeSuiteSyncStorageThunk,
80-
initSuiteSync: () => (_, getState) => initSuiteSyncDesktop({ getState }).init(),
81-
createSuiteSyncOwner: params => () => createSuiteSyncOwnerDesktop(params),
8291
},
8392
selectors: {
8493
selectTokenDefinitionsEnabledNetworks: (state: AppState) =>

suite-common/redux-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react": "19.1.0",
2424
"react-redux": "9.2.0",
2525
"redux": "^5.0.1",
26+
"redux-thunk": "^3.1.0",
2627
"reselect": "5.1.1"
2728
}
2829
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
3+
import type { ExtraDependencies } from './extraDependenciesType';
4+
5+
const ServicesContext = React.createContext<ExtraDependencies['services'] | null>(null);
6+
7+
type ServicesProviderParams = {
8+
services: ExtraDependencies['services'];
9+
children: React.ReactNode;
10+
};
11+
12+
export const ServicesProvider = ({ services, children }: ServicesProviderParams) => (
13+
<ServicesContext.Provider value={services}>{children}</ServicesContext.Provider>
14+
);
15+
16+
export const useServices = () => {
17+
const services = React.useContext(ServicesContext);
18+
19+
if (!services) {
20+
throw new Error('useServices must be used within a ServicesProvider');
21+
}
22+
23+
return services;
24+
};

0 commit comments

Comments
 (0)