Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: initialization messages and ui improvements #256

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions src/chrome/background/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { notificationDispatcher } from './notification-dispatcher'
import { getExtensionOptions } from 'options'
import { chromeLocalStore } from 'chrome/helpers/chrome-local-store'
import { RadixNetworkConfigById } from '@radixdlt/babylon-gateway-api-sdk'
import { ConnectionsClient } from 'pairing/state/connections'
import { Connections, ConnectionsClient } from 'pairing/state/connections'
import { WalletInteraction } from '@radixdlt/radix-dapp-toolkit'
import { getSessionRouterData } from 'chrome/offscreen/session-router'

export type BackgroundMessageHandler = ReturnType<
typeof BackgroundMessageHandler
Expand All @@ -38,7 +39,7 @@ export const BackgroundMessageHandler =
}: Partial<{
logger?: AppLogger
ledgerTabWatcher: ReturnType<typeof LedgerTabWatcher>
getConnections: () => ResultAsync<any, Error>
getConnections: () => ResultAsync<Connections, never>
closePopup: () => ResultAsync<any, Error>
openParingPopup: () => ResultAsync<any, Error>
}>) =>
Expand All @@ -58,25 +59,15 @@ export const BackgroundMessageHandler =

switch (message?.discriminator) {
case messageDiscriminator.getExtensionOptions:
return getExtensionOptions()
.mapErr((error) => ({
reason: 'failedToGetExtensionOptions',
jsError: Error('failedToGetExtensionOptions'),
}))
.map((options) => ({
sendConfirmation: true,
data: { options },
}))
return getExtensionOptions().map((options) => ({
sendConfirmation: true,
data: { options },
}))
case messageDiscriminator.getConnections:
return getConnections()
.mapErr((error) => ({
reason: 'failedToGetConnections',
jsError: error,
}))
.map((data) => ({
sendConfirmation: true,
data,
}))
return getConnections().map((data) => ({
sendConfirmation: true,
data,
}))

case messageDiscriminator.openParingPopup:
return openParingPopup()
Expand Down Expand Up @@ -202,15 +193,10 @@ export const BackgroundMessageHandler =
}

case messageDiscriminator.getSessionRouterData: {
return chromeLocalStore
.getItem('sessionRouter')
.map((data) => ({
sendConfirmation: true,
data,
}))
.mapErr(() => ({
reason: 'failedToGetSessionRouterData',
}))
return getSessionRouterData().map((data) => ({
sendConfirmation: true,
data,
}))
}

case messageDiscriminator.dAppRequest: {
Expand Down
5 changes: 3 additions & 2 deletions src/chrome/helpers/get-connections.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Connections } from 'pairing/state/connections'
import { chromeLocalStore } from './chrome-local-store'
import { ResultAsync } from 'neverthrow'
import { ResultAsync, ok } from 'neverthrow'

export const getConnections = (): ResultAsync<Connections, Error> =>
export const getConnections = (): ResultAsync<Connections, never> =>
chromeLocalStore
.getItem('connections')
.map(({ connections }) => connections || {})
.orElse(() => ok({}))

export const hasConnections = () =>
getConnections().map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ export const OffscreenInitializationMessages = (
},
sessionRouterData: () => {
messageClient
.sendMessageAndWaitForConfirmation<{
sessionRouter: Record<SessionId, WalletPublicKey>
}>(createMessage.getSessionRouterData())
.andThen(({ sessionRouter }) =>
.sendMessageAndWaitForConfirmation<Record<SessionId, WalletPublicKey>>(
createMessage.getSessionRouterData(),
)
.andThen((sessionRouter) =>
messageClient.handleMessage(
createMessage.setSessionRouterData(sessionRouter, 'offScreen'),
),
Expand Down
4 changes: 2 additions & 2 deletions src/chrome/offscreen/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ export const OffscreenMessageHandler = (input: {

case messageDiscriminator.setSessionRouterData: {
const { data } = message
sessionRouter.refreshStore(data)
logger?.info('setSessionRouterData', data)
logger?.info('setSessionRouterData', message)
sessionRouter.refreshStore(data || {})
return okAsync({ sendConfirmation: true })
}

Expand Down
14 changes: 14 additions & 0 deletions src/chrome/offscreen/session-router.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { chromeLocalStore } from 'chrome/helpers/chrome-local-store'
import { ResultAsync, ok } from 'neverthrow'

export type SessionRouter = ReturnType<typeof SessionRouter>

export type SessionId = string
Expand All @@ -22,3 +25,14 @@ export const SessionRouter = () => {
store,
}
}

export const sessionRouter = SessionRouter()

export const getSessionRouterData = (): ResultAsync<
Record<string, string>,
never
> =>
chromeLocalStore
.getItem('sessionRouter')
.map(({ sessionRouter }) => sessionRouter || {})
.orElse(() => ok({}))
4 changes: 4 additions & 0 deletions src/components/linked-wallet/linked-wallet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ button {
display: inline-flex;
transition: transform 0.3s ease-in-out;
}

.shared-accounts-collapse-button.rotate {
margin-top: 5px;
}
4 changes: 2 additions & 2 deletions src/components/linked-wallet/shared-accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ChevronDown from './chevron-down.svg'
import { Account as AccountType } from '@radixdlt/radix-dapp-toolkit'
import { Account } from 'components/account/account'
import { Box, Collapse } from '@mui/material'
import { useEffect, useState } from 'react'
import { useState } from 'react'

export const SharedAccounts = (props: {
accounts?: AccountType[]
Expand All @@ -11,7 +11,7 @@ export const SharedAccounts = (props: {
const [isCollapsed, setIsCollapsed] = useState(!props.isJustLinked)

return (
<Box sx={{ marginTop: '-5px', marginBottom: '5px' }}>
<Box>
<Collapse in={!isCollapsed}>
<Box display="flex" flexDirection="column" gap="8px">
{props.accounts?.map((account) => (
Expand Down
16 changes: 9 additions & 7 deletions src/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { chromeLocalStore } from 'chrome/helpers/chrome-local-store'
import { logger } from 'utils/logger'
import { defaultRadixConnectConfig } from 'config'
import { ResultAsync } from 'neverthrow'
import { ResultAsync, ok } from 'neverthrow'
import { ed25519 } from '@noble/curves/ed25519'

const privateKey = ed25519.utils.randomPrivateKey()
Expand All @@ -10,9 +10,9 @@ const publicKey = ed25519.getPublicKey(privateKey)
export type ConnectorExtensionOptions = {
publicKey: string
privateKey: string
showDAppRequestNotifications?: boolean
showTransactionResultNotifications?: boolean
radixConnectConfiguration: string
showDAppRequestNotifications: boolean
showTransactionResultNotifications: boolean
}

export const defaultConnectorExtensionOptions: ConnectorExtensionOptions = {
Expand All @@ -23,11 +23,13 @@ export const defaultConnectorExtensionOptions: ConnectorExtensionOptions = {
radixConnectConfiguration: defaultRadixConnectConfig,
}

export const getSingleOptionValue = (key: keyof ConnectorExtensionOptions) =>
export const getSingleOptionValue = <T extends keyof ConnectorExtensionOptions>(
key: T,
): ResultAsync<ConnectorExtensionOptions[T], never> =>
chromeLocalStore
.getSingleItem('options')
.map((options) => options?.[key] || defaultConnectorExtensionOptions[key])
.mapErr(() => defaultConnectorExtensionOptions[key])
.orElse(() => ok(defaultConnectorExtensionOptions[key]))

export const getShowDAppRequestNotifications = () =>
getSingleOptionValue('showDAppRequestNotifications')
Expand All @@ -37,15 +39,15 @@ export const getShowTransactionResultNotifications = () =>

export const getExtensionOptions = (): ResultAsync<
ConnectorExtensionOptions,
ConnectorExtensionOptions
never
> => {
return chromeLocalStore
.getSingleItem('options')
.map((options) => ({
...defaultConnectorExtensionOptions,
...options,
}))
.mapErr(() => defaultConnectorExtensionOptions)
.orElse(() => ok(defaultConnectorExtensionOptions))
}

export const setConnectorExtensionOptions = (
Expand Down
20 changes: 10 additions & 10 deletions src/pairing/components/connection-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ConnectionPassword = ({
<>
<PairingHeader
header={
connectionsClient.hasNoConnections()
!connectionsClient.hasConnections()
? 'Radix Wallet Connector'
: 'Link New Radix Wallet'
}
Expand All @@ -44,15 +44,7 @@ export const ConnectionPassword = ({
</Box>

<Box textAlign="center" mb="lg">
{connectionsClient.hasNoConnections() ? (
<LinkComponent
style={{ fontSize: '16px', color: 'white', fontWeight: 'bold' }}
href="https://wallet.radixdlt.com"
target="_blank"
>
{`Don't have Radix Wallet?`}
</LinkComponent>
) : (
{connectionsClient.hasConnections() ? (
<Link
style={{
fontSize: '16px',
Expand All @@ -64,6 +56,14 @@ export const ConnectionPassword = ({
>
Cancel
</Link>
) : (
<LinkComponent
style={{ fontSize: '16px', color: 'white', fontWeight: 'bold' }}
href="https://wallet.radixdlt.com"
target="_blank"
>
{`Don't have Radix Wallet?`}
</LinkComponent>
)}
</Box>
<RelinkWarning />
Expand Down
6 changes: 3 additions & 3 deletions src/pairing/components/connection-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ConnectionStatus = () => {

useEffect(() => {
if (connectionsClient.isLoading()) return
if (connectionsClient.hasNoConnections()) {
if (!connectionsClient.hasConnections()) {
navigate('/pairing')
}
}, [connections])
Expand Down Expand Up @@ -79,7 +79,7 @@ export const ConnectionStatus = () => {
)
}

return (
return connectionsClient.hasConnections() ? (
<>
<Box
py="small"
Expand Down Expand Up @@ -125,5 +125,5 @@ export const ConnectionStatus = () => {
{renderForgetWalletConfirmation()}
{renderChangeWalletName()}
</>
)
) : null
}
8 changes: 4 additions & 4 deletions src/pairing/state/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useConnections = () => {
useEffect(() => {
chromeLocalStore.getItem('connections').map((result) => {
if (JSON.stringify(result.connections) !== JSON.stringify(connections)) {
setConnections(result.connections || null)
setConnections(result.connections || {})
}
})
const listener = (
Expand Down Expand Up @@ -69,8 +69,8 @@ export const ConnectionsClient = (connections?: Connections | null) => {
})
}

const hasNoConnections = () => {
return connections && Object.keys(connections).length === 0
const hasConnections = () => {
return connections && Object.keys(connections).length > 0
}

const isLoading = () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ export const ConnectionsClient = (connections?: Connections | null) => {
entries,
isLoading,
updateName,
hasNoConnections,
hasConnections,
updateAccounts,
connections,
}
Expand Down
36 changes: 0 additions & 36 deletions src/queues/storage/chrome-local-storage.ts

This file was deleted.

Loading