Skip to content

Commit

Permalink
refactor: major refactoring of automation
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzkirstein committed Sep 28, 2023
1 parent f5295dd commit 7b17c43
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 306 deletions.
247 changes: 86 additions & 161 deletions src/@context/Automation/AutomationProvider.tsx

Large diffs are not rendered by default.

33 changes: 9 additions & 24 deletions src/@context/UserPreferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import React, {
import { LoggerInstance, LogLevel } from '@oceanprotocol/lib'
import { isBrowser } from '@utils/index'
import { useMarketMetadata } from './MarketMetadata'
import { AutomationMessage } from './Automation/AutomationProvider'

interface UserPreferencesValue {
debug: boolean
Expand All @@ -28,9 +27,8 @@ interface UserPreferencesValue {
allowExternalContent: boolean
setAllowExternalContent: (value: boolean) => void
locale: string
automationMessages: AutomationMessage[]
addAutomationMessage: (message: AutomationMessage) => void
removeAutomationMessage: (address: string) => void
automationWalletJSON: string
setAutomationWalletJSON: (encryptedWallet: string) => void
automationWalletMode: string
setAutomationWalletMode: (mode: string) => void
}
Expand Down Expand Up @@ -83,9 +81,9 @@ function UserPreferencesProvider({
localStorage?.allowExternalContent || false
)

const [automationMessages, setAutomationMessages] = useState<
AutomationMessage[]
>(localStorage?.automationMessages || [])
const [automationWallet, setAutomationWallet] = useState<string>(
localStorage?.automationWalletJSON || ''
)

const [automationWalletMode, setAutomationWalletMode] = useState<string>(
localStorage?.automationWalletMode || 'advanced'
Expand All @@ -101,7 +99,7 @@ function UserPreferencesProvider({
privacyPolicySlug,
showPPC,
allowExternalContent,
automationMessages,
automationWalletJSON: automationWallet,
automationWalletMode
})
}, [
Expand All @@ -112,7 +110,7 @@ function UserPreferencesProvider({
privacyPolicySlug,
showPPC,
allowExternalContent,
automationMessages,
automationWallet,
automationWalletMode
])

Expand Down Expand Up @@ -151,18 +149,6 @@ function UserPreferencesProvider({
setBookmarks(newPinned)
}, [bookmarks])

function addAutomationMessage(message: AutomationMessage) {
const newMessages = [...automationMessages, message]
setAutomationMessages(newMessages)
}

function removeAutomationMessage(address: string) {
const newMessages = automationMessages.filter(
(message) => message.address.toLowerCase() !== address.toLowerCase()
)
setAutomationMessages(newMessages)
}

// chainIds old data migration
// remove deprecated networks from user-saved chainIds
useEffect(() => {
Expand Down Expand Up @@ -191,9 +177,8 @@ function UserPreferencesProvider({
setShowPPC,
allowExternalContent,
setAllowExternalContent,
automationMessages,
addAutomationMessage,
removeAutomationMessage,
automationWalletJSON: automationWallet,
setAutomationWalletJSON: setAutomationWallet,
automationWalletMode,
setAutomationWalletMode
} as UserPreferencesValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.delete > svg {
fill: var(--brand-alert-red);
}

.address {
display: flex;
justify-content: space-between;
font-size: var(--font-size-base);
width: 100%;
padding-bottom: calc(var(--spacer) / 4);
}

.address strong {
color: var(--brand-blue);
}

.address button {
margin-left: calc(var(--spacer) / 4);
}
37 changes: 37 additions & 0 deletions src/components/Header/UserPreferences/Automation/Address.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { ReactElement } from 'react'
import { accountTruncate } from '../../../../@utils/wallet'
import { useAutomation } from '../../../../@context/Automation/AutomationProvider'
import { useUserPreferences } from '../../../../@context/UserPreferences'
import Copy from '../../../@shared/atoms/Copy'
import Cross from '@images/cross.svg'
import styles from './Address.module.css'
import Button from '../../../@shared/atoms/Button'

export default function Address({
showDelete
}: {
showDelete: boolean
}): ReactElement {
const { autoWalletAddress } = useAutomation()
const { setAutomationWalletJSON } = useUserPreferences()

return (
<div className={styles.address}>
<span>
Address: <strong>{accountTruncate(autoWalletAddress)}</strong>
<Copy text={autoWalletAddress} />
</span>
{showDelete && (
<Button
onClick={() => setAutomationWalletJSON(undefined)}
className={styles.delete}
title="Delete"
style="text"
size="small"
>
<Cross />
</Button>
)}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.wrapper {
width: 100%;
}

.button {
width: 100%;
}

.form {
display: flex;
}

.form > * {
margin: 0;
}

.form > button {
margin-left: calc(var(--spacer) / 4);
padding: calc(var(--spacer) / 4) calc(var(--spacer) / 2);
}

.help {
font-size: var(--font-size-small);
}
63 changes: 63 additions & 0 deletions src/components/Header/UserPreferences/Automation/Decrypt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { ReactElement, useEffect, useRef, useState } from 'react'
import styles from './Decrypt.module.css'
import Button from '../../../@shared/atoms/Button'
import { toast } from 'react-toastify'
import { useAutomation } from '../../../../@context/Automation/AutomationProvider'
import Loader from '../../../@shared/atoms/Loader'
import InputElement from '../../../@shared/FormInput/InputElement'
import Input from '../../../@shared/FormInput'
import { Field } from 'formik'

export default function Decrypt(): ReactElement {
const { isLoading, decryptPercentage, decryptAutomationWallet } =
useAutomation()

const decrpytToastRef = useRef(null)
const [showPasswordInput, setShowPasswordInput] = useState<boolean>()
const passwordInputRef = useRef(null)

useEffect(() => {
toast.update(decrpytToastRef.current, { progress: decryptPercentage })
}, [decrpytToastRef, decryptPercentage])

return (
<div className={styles.wrapper}>
{isLoading ? (
<Loader message="Decrypting..." />
) : showPasswordInput ? (
<>
<form
onSubmit={async (e) => {
e.preventDefault()
decrpytToastRef.current = toast.info(`Decrypting Wallet...`)
await decryptAutomationWallet(passwordInputRef.current.value)
toast.done(decrpytToastRef.current)
setShowPasswordInput(false)
}}
className={styles.form}
>
<InputElement
name="password"
placeholder="Password"
label="Password"
type="password"
ref={passwordInputRef}
/>
<Button type="submit">Decrypt</Button>
</form>
<span className={styles.help}>
Enter the password that was used to encrypt this wallet.
</span>
</>
) : (
<Button
onClick={() => setShowPasswordInput(true)}
disabled={isLoading}
className={styles.button}
>
Decrypt Wallet
</Button>
)}
</div>
)
}
27 changes: 11 additions & 16 deletions src/components/Header/UserPreferences/Automation/Details.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
padding: calc(var(--spacer) / 2);
}

.tokenActions {
display: flex;
.title {
font-size: var(--font-size-label);
font-weight: var(--font-weight-base);
margin-bottom: var(--spacer);
}

.toggleBtn {
width: 100%;
.help {
font-size: var(--font-size-small);
margin-bottom: calc(var(--spacer) / 2);
}

.walletAddress {
display: block;
font-size: var(--font-size-base);
width: 100%;
padding-bottom: calc(var(--spacer) / 4);
border-bottom: 1px solid var(--border-color);
}

.walletAddress > strong {
color: var(--brand-blue);
.tokenActions {
display: flex;
}

.walletAddress > button {
margin-left: calc(var(--spacer) / 4);
.toggleBtn {
width: 100%;
margin-bottom: calc(var(--spacer) / 2);
}

.simple,
Expand Down
Loading

0 comments on commit 7b17c43

Please sign in to comment.