Skip to content

Commit 077078e

Browse files
committed
Add RampPluginManager service and RampPluginReducer
1 parent dfa155a commit 077078e

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

src/__tests__/reducers/__snapshots__/RootReducer.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ exports[`initialState 1`] = `
6060
"contacts": "denied",
6161
"location": "denied",
6262
},
63+
"rampPlugins": {
64+
"isLoading": true,
65+
"plugins": {},
66+
},
6367
"sortedWalletList": [],
6468
"staking": {
6569
"walletStakingMap": {},
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react'
2+
3+
import { ENV } from '../../env'
4+
import { pluginFactories } from '../../plugins/ramps/allRampPlugins'
5+
import type { RampPlugin } from '../../plugins/ramps/rampPluginTypes'
6+
import { useDispatch } from '../../types/reactRedux'
7+
8+
export const RampPluginManager: React.FC = () => {
9+
const dispatch = useDispatch()
10+
11+
React.useEffect(() => {
12+
const loadPlugins = async (): Promise<void> => {
13+
const plugins: Record<string, RampPlugin> = {}
14+
15+
for (const [pluginId, factory] of Object.entries(pluginFactories)) {
16+
const initOptions = ENV.RAMP_PLUGIN_INITS[pluginId]
17+
if (initOptions == null) continue
18+
try {
19+
const plugin = factory({
20+
initOptions
21+
})
22+
plugins[plugin.pluginId] = plugin
23+
} catch (error) {
24+
console.error(`Failed to load plugin ${pluginId}:`, error)
25+
}
26+
}
27+
28+
dispatch({
29+
type: 'RAMP_PLUGINS/LOADING_COMPLETE',
30+
data: { plugins }
31+
})
32+
}
33+
34+
loadPlugins().catch(console.error)
35+
}, [dispatch])
36+
37+
return null
38+
}

src/components/services/Services.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { NetworkActivity } from './NetworkActivity'
4444
import { NotificationService } from './NotificationService'
4545
import { PasswordReminderService } from './PasswordReminderService'
4646
import { PermissionsManager } from './PermissionsManager'
47+
import { RampPluginManager } from './RampPluginManager'
4748
import { SortedWalletList } from './SortedWalletList'
4849
import { WalletConnectService } from './WalletConnectService'
4950
import { WalletLifecycle } from './WalletLifecycle'
@@ -241,6 +242,7 @@ export function Services(props: Props) {
241242
<FioService account={account} navigation={navigation} />
242243
)}
243244
<PermissionsManager />
245+
<RampPluginManager />
244246
{startLoanManager ? <LoanManagerService account={account} /> : null}
245247
<NetworkActivity />
246248
<PasswordReminderService />

src/reducers/RampPluginReducer.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { RampPlugin } from '../plugins/ramps/rampPluginTypes'
2+
import type { Action } from '../types/reduxTypes'
3+
4+
export interface RampPluginState {
5+
readonly isLoading: boolean
6+
readonly plugins: Record<string, RampPlugin>
7+
}
8+
9+
const initialState: RampPluginState = {
10+
isLoading: true,
11+
plugins: {}
12+
}
13+
14+
export const rampPlugins = (
15+
state: RampPluginState = initialState,
16+
action: Action
17+
): RampPluginState => {
18+
switch (action.type) {
19+
case 'RAMP_PLUGINS/LOADING_COMPLETE':
20+
return {
21+
...state,
22+
isLoading: false,
23+
plugins: action.data.plugins
24+
}
25+
26+
case 'LOGOUT':
27+
return initialState
28+
29+
default:
30+
return state
31+
}
32+
}

src/reducers/RootReducer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { account, type AccountState } from './AccountReducer'
2121
import { core, type CoreState } from './CoreReducer'
2222
import { network, type NetworkState } from './NetworkReducer'
2323
import { permissions, type PermissionsState } from './PermissionsReducer'
24+
import { rampPlugins, type RampPluginState } from './RampPluginReducer'
2425
import { staking, type StakingState } from './StakingReducer'
2526
import { ui, type UiState } from './uiReducer'
2627

@@ -47,6 +48,7 @@ export interface RootState {
4748
readonly actionQueue: ActionQueueState
4849
readonly core: CoreState
4950
readonly loanManager: LoanManagerState
51+
readonly rampPlugins: RampPluginState
5052
readonly staking: StakingState
5153
readonly permissions: PermissionsState
5254
readonly ui: UiState
@@ -137,6 +139,7 @@ export const rootReducer = combineReducers<RootState, Action>({
137139
core,
138140
loanManager,
139141
permissions,
142+
rampPlugins,
140143
staking,
141144
ui,
142145
network

src/types/reduxActions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
import type { SortOption } from '../components/modals/WalletListSortModal'
1717
import type { ActionQueueAction } from '../controllers/action-queue/redux/actions'
1818
import type { LoanManagerActions } from '../controllers/loan-manager/redux/actions'
19+
import type { RampPlugin } from '../plugins/ramps/rampPluginTypes'
1920
import type { CcWalletMap } from '../reducers/FioReducer'
2021
import type { PermissionsState } from '../reducers/PermissionsReducer'
2122
import type {
@@ -186,6 +187,10 @@ export type Action =
186187
data: { fioAddress: string; ccWalletMap: CcWalletMap }
187188
}
188189
| { type: 'FIO/SET_FIO_DOMAINS'; data: { fioDomains: FioDomain[] } }
190+
| {
191+
type: 'RAMP_PLUGINS/LOADING_COMPLETE'
192+
data: { plugins: Record<string, RampPlugin> }
193+
}
189194
/*
190195
Self-Contained Package Actions:
191196

0 commit comments

Comments
 (0)