Skip to content

Commit 629a755

Browse files
committed
wip: update
1 parent 2c56a5d commit 629a755

File tree

22 files changed

+390
-417
lines changed

22 files changed

+390
-417
lines changed

packages/neuron-ui/src/components/PaymentChannel/components/Console/index.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { useState as useGlobalState } from 'states'
22
import { useTranslation } from 'react-i18next'
33
import { useInterval, useRequest } from 'ahooks'
4-
import { getChannels, openChannel } from '../../api'
4+
import { getChannels, openChannel, restoreChannels } from '../../api'
55
import { useEffect, useMemo, useState } from 'react'
6-
import { getCurrentWalletAccountExtendedPubKey, showErrorMessage } from 'services/remote'
6+
import { getCurrentWalletAccountExtendedPubKey, perunServiceAction, showErrorMessage } from 'services/remote'
77
import styles from '../../perun.module.scss'
88
import {
99
AddSimple,
@@ -46,21 +46,22 @@ enum DialogType {
4646
type PerunConsoleProps = {
4747
publicKey: string
4848
address: string
49-
onClose?: () => void
5049
}
5150

5251
export default function PerunConsole(props: PerunConsoleProps) {
53-
const { publicKey: myPubKey, address: myAddress, onClose } = props
52+
const { publicKey: myPubKey, address: myAddress } = props
5453
const {
5554
wallet,
5655
perun: { requests }, // channels
5756
} = useGlobalState()
5857
const [t] = useTranslation()
5958
const [dialogType, setDialogType] = useState<DialogType | undefined>(undefined)
6059
const channelInfoMap = useChannelInfoMap()
60+
6161
// const [pendingChannels, setPendingChannels] = useState<ChannelInfo[]>([])
6262
// const [channelMap, setChannelMap] = useState<Record<string, ChannelInfo>>({})
6363
const { data: channelStates = [], run: syncChannels } = useRequest(async () => {
64+
console.log("?")
6465
const list = await getChannels(myPubKey, myAddress)
6566
return list;
6667
}, {
@@ -70,14 +71,21 @@ export default function PerunConsole(props: PerunConsoleProps) {
7071

7172
useInterval(syncChannels, !!myPubKey ? 5000 : 0)
7273

74+
75+
useEffect(() => {
76+
restoreChannels()
77+
}, [])
78+
7379
// const assets = ['CKB']
7480
console.log("perun requests", requests);
7581
console.log("perun channle states", channelStates);
7682

7783
return (
7884
<div className={styles.container}>
7985
<div className='flex flex-row justify-between items-center mb-4'>
80-
<Button type="danger" className={styles.createBtn} onClick={() => { }}>
86+
<Button type="danger" className={styles.createBtn} onClick={() => {
87+
perunServiceAction({ type: "stop-runner" })
88+
}}>
8189
Exit
8290
</Button>
8391
<Button type="primary" className={styles.createBtn} onClick={() => setDialogType(DialogType.openChannel)}>

packages/neuron-ui/src/components/PaymentChannel/demo.tsx

Lines changed: 0 additions & 78 deletions
This file was deleted.

packages/neuron-ui/src/components/PaymentChannel/index.tsx

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
isSuccessResponse,
4747
clsx,
4848
getParticipantByAddressAndPubkey,
49+
isMainnet,
4950
} from 'utils'
5051
import { PasswordDialog } from 'components/SignAndVerify'
5152

@@ -55,6 +56,7 @@ import { useTranslation } from 'react-i18next'
5556
import Button from 'widgets/Button'
5657
import PerunConsole from './components/Console'
5758
import AddressSelector from './components/AddressSelector'
59+
import { useRequest } from 'ahooks'
5860

5961

6062

@@ -64,11 +66,34 @@ const PaymentChannel = () => {
6466
chain: { networkID },
6567
settings: { networks },
6668
wallet,
69+
perun: { runnerState }
6770
} = useGlobalState()
6871
const [t, _] = useTranslation()
69-
const [enable, setEnable] = useState(false);
70-
const [publicKey, setMyPubKey] = useState('')
71-
const [address, setMyAddress] = useState('')
72+
const [enable, setEnable] = useState(runnerState.running);
73+
const [publicKey, setMyPubKey] = useState(runnerState.context?.publicKey ?? "")
74+
const [address, setMyAddress] = useState(runnerState.context?.address ?? "")
75+
const isTestnet = !isMainnet(networks, networkID)
76+
77+
const { loading, run: startRunner } = useRequest(async (cfg: NonNullable<Perun.RunnerStatus['context']>) => {
78+
const res = await perunServiceAction({ type: "start-runner", payload: cfg })
79+
return res;
80+
}, {
81+
manual: true,
82+
})
83+
84+
useEffect(() => {
85+
if(!enable && runnerState.running) {
86+
setEnable(true)
87+
}
88+
if (enable && !runnerState.running) {
89+
setEnable(false)
90+
}
91+
if (enable && !runnerState.running && runnerState.message) {
92+
showErrorMessage('Error', runnerState.message);
93+
}
94+
}, [enable, runnerState.running, runnerState.message])
95+
96+
7297
return (
7398
<PageContainer
7499
head={
@@ -103,10 +128,20 @@ const PaymentChannel = () => {
103128

104129
<Button
105130
type="primary"
131+
loading={loading}
106132
className={styles.startButton}
107-
// todo start channel-service-runner
108-
// then invoke restore-channel
109-
onClick={() => { setEnable(true) }}
133+
onClick={async () => {
134+
if (!isTestnet) {
135+
showErrorMessage('Error', t('perun.testnet-only'))
136+
return;
137+
}
138+
await startRunner({
139+
walletId: wallet.id,
140+
publicKey: publicKey,
141+
address: address,
142+
network: isTestnet ? 'testnet' : 'mainnet',
143+
})
144+
}}
110145
>
111146
Start
112147
</Button>
@@ -116,8 +151,6 @@ const PaymentChannel = () => {
116151
<PerunConsole
117152
publicKey={publicKey}
118153
address={address}
119-
// todo
120-
onClose={() =>{}}
121154
/>
122155
)
123156
}

packages/neuron-ui/src/containers/Navbar/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
AppUpdater as AppUpdaterSubject,
2121
PerunRequest as PerunRequestSubject,
2222
PerunChannel as PerunChannelSubject,
23+
PerunRunnerState as PerunRunnerStateSubject,
2324
} from 'services/subjects'
2425

2526
import Badge from 'widgets/Badge'
@@ -105,10 +106,15 @@ const Navbar = () => {
105106
dispatch({ type: PaymentChannelActions.UpdatePerunChannel, payload })
106107
})
107108

109+
const perunRunnerStateSubscription = PerunRunnerStateSubject.subscribe((payload: Perun.RunnerStatus) => {
110+
dispatch({ type: PaymentChannelActions.UpdateRunnerState, payload })
111+
})
112+
108113
return () => {
109114
appUpdaterSubscription.unsubscribe()
110115
perunRequestSubscription.unsubscribe()
111116
perunChannelSubscription.unsubscribe()
117+
perunRunnerStateSubscription.unsubscribe()
112118
}
113119
}, [dispatch])
114120

packages/neuron-ui/src/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@
837837
"export": "Export Tx"
838838
},
839839
"perun": {
840+
"testnet-only": "Perun is only available on testnet",
840841
"no-data": "No data",
841842
"create-new-channel": "Create New Channel",
842843
"of-open-channels": "#of open channels",

packages/neuron-ui/src/services/remote/wallets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ export const verifyMessage = remoteApi<Controller.VerifyMessageParams, 'old-sign
4242

4343
// Perun
4444
export const respondPerunRequest = remoteApi<Controller.RespondPerunRequestParams>('respond-perun-request')
45-
export const perunServiceAction = remoteApi<Controller.PerunServiceActionParams>('perun-service-action')
45+
export const perunServiceAction = remoteApi<Perun.ServiceActionParams>('perun-service-action')

packages/neuron-ui/src/services/subjects.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const SubjectConstructor = <T>(
3232
| 'show-global-dialog'
3333
| 'no-disk-space'
3434
| 'perun-requests'
35-
| 'perun-channels',
35+
| 'perun-channels'
36+
| 'perun-runner-state',
3637
isMulti?: boolean
3738
) => {
3839
return ipcRenderer
@@ -73,6 +74,7 @@ export const Migrate = SubjectConstructor<'need-migrate' | 'migrating' | 'failed
7374
export const NoDiskSpace = SubjectConstructor<boolean>('no-disk-space')
7475
export const PerunRequest = SubjectConstructor<Subject.PerunRequest>('perun-requests')
7576
export const PerunChannel = SubjectConstructor<Subject.PerunChannel[]>('perun-channels')
77+
export const PerunRunnerState = SubjectConstructor<Perun.RunnerStatus>('perun-runner-state')
7678

7779
export default {
7880
DataUpdate,
@@ -91,4 +93,5 @@ export default {
9193
Migrate,
9294
PerunRequest,
9395
PerunChannel,
96+
PerunRunnerState,
9497
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export const perunState: State.Perun = {
2-
channels: [],
2+
// channels: [],
33
requests: [],
4+
runnerState: {
5+
running: false,
6+
}
47
}
58

69
export default perunState

packages/neuron-ui/src/states/stateProvider/reducer.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export enum PaymentChannelActions {
7777

7878
UpdatePerunRequest = 'UpdatePerunRequest',
7979
UpdatePerunChannel = 'UpdatePerunChannel',
80+
UpdateRunnerState = 'UpdateRunnerState',
8081
}
8182

8283
export type StateAction =
@@ -129,18 +130,22 @@ export type StateAction =
129130
| { type: AppActions.SignVerify; payload: string }
130131
| { type: AppActions.UpdateConsumeCells; payload?: { outPoint: OutPoint; capacity: string }[] }
131132
| { type: AppActions.SetLockWindowInfo; payload: Required<State.App>['lockWindowInfo'] }
133+
// | {
134+
// type: PaymentChannelActions.UpdatePerunState
135+
// payload: State.Perun
136+
// }
132137
| {
133-
type: PaymentChannelActions.UpdatePerunState
134-
payload: State.Perun
135-
}
138+
type: PaymentChannelActions.UpdatePerunRequest
139+
payload: Perun.ReadableMessage.Request[]
140+
}
136141
| {
137-
type: PaymentChannelActions.UpdatePerunRequest
138-
payload: Perun.ReadableMessage.Request[]
139-
}
142+
type: PaymentChannelActions.UpdatePerunChannel
143+
payload: State.PerunChannel[]
144+
}
140145
| {
141-
type: PaymentChannelActions.UpdatePerunChannel
142-
payload: State.PerunChannel[]
143-
}
146+
type: PaymentChannelActions.UpdateRunnerState
147+
payload: Perun.RunnerStatus
148+
}
144149

145150
export type StateDispatch = React.Dispatch<StateAction> // TODO: add type of payload
146151

@@ -341,7 +346,7 @@ export const reducer = produce((state: Draft<State.AppWithNeuronWallet>, action:
341346
*/
342347
state.app.showTopAlert =
343348
state.app.notifications.findIndex(message => message.timestamp === action.payload) ===
344-
state.app.notifications.length - 1
349+
state.app.notifications.length - 1
345350
? false
346351
: state.app.showTopAlert
347352
state.app.notifications = state.app.notifications.filter(({ timestamp }) => timestamp !== action.payload)
@@ -416,9 +421,9 @@ export const reducer = produce((state: Draft<State.AppWithNeuronWallet>, action:
416421
case AppActions.SetPageNotice: {
417422
state.app.pageNotice = action.payload
418423
? {
419-
...action.payload,
420-
index: (state.app.pageNotice?.index ?? 0) + 1,
421-
}
424+
...action.payload,
425+
index: (state.app.pageNotice?.index ?? 0) + 1,
426+
}
422427
: action.payload
423428
break
424429
}
@@ -441,9 +446,12 @@ export const reducer = produce((state: Draft<State.AppWithNeuronWallet>, action:
441446
state.perun.requests = action.payload
442447
break
443448
}
444-
445-
case PaymentChannelActions.UpdatePerunChannel: {
446-
state.perun.channels = action.payload
449+
// case PaymentChannelActions.UpdatePerunChannel: {
450+
// state.perun.channels = action.payload
451+
// break
452+
// }
453+
case PaymentChannelActions.UpdateRunnerState: {
454+
state.perun.runnerState = action.payload
447455
break
448456
}
449457

packages/neuron-ui/src/types/App/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,9 @@ declare namespace State {
378378
}
379379

380380
interface Perun {
381-
channels: PerunChannel[]
381+
// channels: PerunChannel[]
382382
requests: Perun.ReadableMessage.Request[]
383+
runnerState: Perun.RunnerStatus
383384
}
384385

385386
interface AppWithNeuronWallet {

0 commit comments

Comments
 (0)