Skip to content

Commit

Permalink
chore: add server information to ServiceInfoContext
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Aug 29, 2023
1 parent 2c4efd3 commit 2baab6c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/components/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import App from './App'

jest.mock('../libs/JmWalletApi', () => ({
...jest.requireActual('../libs/JmWalletApi'),
getGetinfo: jest.fn(),
getSession: jest.fn(),
}))

describe('<App />', () => {
beforeEach(() => {
const neverResolvingPromise = new Promise(() => {})
;(apiMock.getGetinfo as jest.Mock).mockResolvedValue(neverResolvingPromise)
;(apiMock.getSession as jest.Mock).mockResolvedValue(neverResolvingPromise)
})

Expand Down
2 changes: 2 additions & 0 deletions src/components/CreateWallet.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CreateWallet from './CreateWallet'

jest.mock('../libs/JmWalletApi', () => ({
...jest.requireActual('../libs/JmWalletApi'),
getGetinfo: jest.fn(),
getSession: jest.fn(),
postWalletCreate: jest.fn(),
}))
Expand All @@ -31,6 +32,7 @@ describe('<CreateWallet />', () => {

beforeEach(() => {
const neverResolvingPromise = new Promise(() => {})
apiMock.getGetinfo.mockResolvedValue(neverResolvingPromise)
apiMock.getSession.mockReturnValue(neverResolvingPromise)
})

Expand Down
4 changes: 3 additions & 1 deletion src/components/Wallet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Wallet, { WalletProps } from './Wallet'

jest.mock('../libs/JmWalletApi', () => ({
...jest.requireActual('../libs/JmWalletApi'),
getGetinfo: jest.fn(),
getSession: jest.fn(),
}))

Expand Down Expand Up @@ -43,7 +44,8 @@ describe('<Wallet />', () => {

beforeEach(() => {
const neverResolvingPromise = new Promise<Response>(() => {})
jest.mocked(apiMock.getSession).mockReturnValue(neverResolvingPromise)
;(apiMock.getGetinfo as jest.Mock).mockResolvedValue(neverResolvingPromise)
;(apiMock.getSession as jest.Mock).mockResolvedValue(neverResolvingPromise)
})

it('should render inactive wallet without errors', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/components/Wallets.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Wallets from './Wallets'

jest.mock('../libs/JmWalletApi', () => ({
...jest.requireActual('../libs/JmWalletApi'),
getGetinfo: jest.fn(),
getSession: jest.fn(),
getWalletAll: jest.fn(),
postWalletUnlock: jest.fn(),
Expand Down Expand Up @@ -37,6 +38,7 @@ describe('<Wallets />', () => {

beforeEach(() => {
const neverResolvingPromise = new Promise(() => {})
apiMock.getGetinfo.mockResolvedValue(neverResolvingPromise)
apiMock.getSession.mockResolvedValue(neverResolvingPromise)
})

Expand Down
72 changes: 63 additions & 9 deletions src/context/ServiceInfoContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,39 @@ interface JmSessionData {
nickname: string | null
}

interface JmGetInfoData {
version: string
}

type SemVer = { major: number; minor: number; patch: number; raw: string }
const UNKNOWN_VERSION: SemVer = { major: 0, minor: 0, patch: 0, raw: 'unknown' }

type SessionFlag = { sessionActive: boolean }
type MakerRunningFlag = { makerRunning: boolean }
type CoinjoinInProgressFlag = { coinjoinInProgress: boolean }
type SessionInfo = {
walletName: Api.WalletName | null
schedule: Schedule | null
offers: Offer[] | null
nickname: string | null
}
type ServerInfo = {
server?: {
version?: SemVer
}
}

type ServiceInfo = SessionFlag & MakerRunningFlag & CoinjoinInProgressFlag & SessionInfo & ServerInfo
type ServiceInfoUpdate = ServiceInfo | MakerRunningFlag | CoinjoinInProgressFlag | ServerInfo

type ServiceInfo = SessionFlag &
MakerRunningFlag &
CoinjoinInProgressFlag & {
walletName: Api.WalletName | null
schedule: Schedule | null
offers: Offer[] | null
nickname: string | null
const toSemVer = (data: JmGetInfoData): SemVer => {
return {
major: 0,
minor: 0,
patch: 0,
raw: data.version,
}
type ServiceInfoUpdate = ServiceInfo | MakerRunningFlag | CoinjoinInProgressFlag
}

interface ServiceInfoContextEntry {
serviceInfo: ServiceInfo | null
Expand All @@ -86,6 +106,34 @@ const ServiceInfoProvider = ({ children }: React.PropsWithChildren<{}>) => {
)
const [connectionError, setConnectionError] = useState<Error>()

useEffect(() => {
const abortCtrl = new AbortController()

Api.getGetinfo({ signal: abortCtrl.signal })
.then((res) => (res.ok ? res.json() : Api.Helper.throwError(res)))
.then((data: JmGetInfoData) => {
dispatchServiceInfo({
server: {
version: toSemVer(data),
},
})
})
.catch((err) => {
const notFound = err.response.status === 404
if (notFound) {
dispatchServiceInfo({
server: {
version: UNKNOWN_VERSION,
},
})
}
})

return () => {
abortCtrl.abort()
}
}, [connectionError])

useEffect(() => {
if (connectionError) {
// Just reset the wallet info, not the session storage (token),
Expand Down Expand Up @@ -210,7 +258,13 @@ const ServiceInfoProvider = ({ children }: React.PropsWithChildren<{}>) => {
}, [websocket, onWebsocketMessage])

return (
<ServiceInfoContext.Provider value={{ serviceInfo, reloadServiceInfo, connectionError }}>
<ServiceInfoContext.Provider
value={{
serviceInfo,
reloadServiceInfo,
connectionError,
}}
>
{children}
</ServiceInfoContext.Provider>
)
Expand Down

0 comments on commit 2baab6c

Please sign in to comment.