-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swap): display order hooks details (#4925)
- Loading branch information
Showing
27 changed files
with
619 additions
and
274 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
apps/cowswap-frontend/src/common/containers/OrderHooksDetails/HookItem/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { HookToDappMatch } from '@cowprotocol/hook-dapp-lib' | ||
|
||
import { Item, Wrapper } from './styled' | ||
|
||
export function HookItem({ item }: { item: HookToDappMatch }) { | ||
return ( | ||
<Item> | ||
{item.dapp ? ( | ||
<Wrapper href={item.dapp.website} target="_blank"> | ||
<img src={item.dapp.image} alt={item.dapp.name} /> | ||
<p> | ||
<strong>{item.dapp.name}</strong> <span>({item.dapp.version})</span> | ||
</p> | ||
</Wrapper> | ||
) : ( | ||
<div>Unknown hook dapp</div> | ||
)} | ||
</Item> | ||
) | ||
} |
19 changes: 19 additions & 0 deletions
19
apps/cowswap-frontend/src/common/containers/OrderHooksDetails/HookItem/styled.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import styled from 'styled-components/macro' | ||
|
||
export const Item = styled.li` | ||
list-style: none; | ||
margin: 0; | ||
padding: 0; | ||
` | ||
|
||
export const Wrapper = styled.a` | ||
display: flex; | ||
flex-direction: row; | ||
gap: 10px; | ||
align-items: center; | ||
> img { | ||
width: 30px; | ||
height: 30px; | ||
} | ||
` |
77 changes: 77 additions & 0 deletions
77
apps/cowswap-frontend/src/common/containers/OrderHooksDetails/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { ReactElement, useMemo, useState } from 'react' | ||
|
||
import { latest } from '@cowprotocol/app-data' | ||
import { HookToDappMatch, matchHooksToDappsRegistry } from '@cowprotocol/hook-dapp-lib' | ||
|
||
import { ChevronDown, ChevronUp } from 'react-feather' | ||
|
||
import { AppDataInfo, decodeAppData } from 'modules/appData' | ||
|
||
import { HookItem } from './HookItem' | ||
import { HooksList, InfoWrapper, ToggleButton, Wrapper } from './styled' | ||
|
||
interface OrderHooksDetailsProps { | ||
appData: string | AppDataInfo | ||
children: (content: ReactElement) => ReactElement | ||
} | ||
|
||
export function OrderHooksDetails({ appData, children }: OrderHooksDetailsProps) { | ||
const [isOpen, setOpen] = useState(false) | ||
const appDataDoc = useMemo(() => { | ||
return typeof appData === 'string' ? decodeAppData(appData) : appData.doc | ||
}, [appData]) | ||
|
||
if (!appDataDoc) return null | ||
|
||
const metadata = appDataDoc.metadata as latest.Metadata | ||
|
||
const preHooksToDapp = matchHooksToDappsRegistry(metadata.hooks?.pre || []) | ||
const postHooksToDapp = matchHooksToDappsRegistry(metadata.hooks?.post || []) | ||
|
||
if (!preHooksToDapp.length && !postHooksToDapp.length) return null | ||
|
||
return children( | ||
isOpen ? ( | ||
<Wrapper> | ||
<ToggleButton onClick={() => setOpen(false)}> | ||
<ChevronUp /> | ||
</ToggleButton> | ||
<HooksInfo data={preHooksToDapp} title="Pre Hooks" /> | ||
<HooksInfo data={postHooksToDapp} title="Post Hooks" /> | ||
</Wrapper> | ||
) : ( | ||
<Wrapper> | ||
<span> | ||
{preHooksToDapp.length ? `Pre: ${preHooksToDapp.length}` : ''} | ||
{preHooksToDapp.length && postHooksToDapp.length ? ' | ' : ''} | ||
{postHooksToDapp.length ? `Post: ${postHooksToDapp.length}` : ''} | ||
</span> | ||
<ToggleButton onClick={() => setOpen(true)}> | ||
<ChevronDown /> | ||
</ToggleButton> | ||
</Wrapper> | ||
), | ||
) | ||
} | ||
|
||
interface HooksInfoProps { | ||
data: HookToDappMatch[] | ||
title: string | ||
} | ||
|
||
function HooksInfo({ data, title }: HooksInfoProps) { | ||
return ( | ||
<> | ||
{data.length ? ( | ||
<InfoWrapper> | ||
<h3>{title}</h3> | ||
<HooksList> | ||
{data.map((item) => { | ||
return <HookItem key={item.hook.callData + item.hook.target + item.hook.gasLimit} item={item} /> | ||
})} | ||
</HooksList> | ||
</InfoWrapper> | ||
) : null} | ||
</> | ||
) | ||
} |
33 changes: 33 additions & 0 deletions
33
apps/cowswap-frontend/src/common/containers/OrderHooksDetails/styled.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import styled from 'styled-components/macro' | ||
|
||
export const Wrapper = styled.div` | ||
position: relative; | ||
padding-right: 30px; | ||
` | ||
|
||
export const HooksList = styled.ul` | ||
margin: 0; | ||
padding: 0; | ||
padding-left: 10px; | ||
` | ||
|
||
export const ToggleButton = styled.button` | ||
cursor: pointer; | ||
background: none; | ||
border: 0; | ||
outline: 0; | ||
padding: 0; | ||
margin: 0; | ||
position: absolute; | ||
right: 0; | ||
top: -4px; | ||
&:hover { | ||
opacity: 0.7; | ||
} | ||
` | ||
export const InfoWrapper = styled.div` | ||
h3 { | ||
margin: 0; | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/cowswap-frontend/src/modules/hooksStore/hooks/useAddCustomHookDapp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.