-
Notifications
You must be signed in to change notification settings - Fork 102
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
feat: prevent adding unsupported hooks for your wallet #5020
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e60e9bf
feat: prevent adding unsupported hooks for your wallet
fairlighteth d54c9e4
feat: make walletType optional
fairlighteth 94f8d95
Merge branch 'develop' into hooks-styling-3
fairlighteth f44cae3
feat: address comments
fairlighteth 9491f96
Merge branch 'develop' into hooks-styling-3
fairlighteth 9afa90c
Merge branch 'develop' into hooks-styling-3
fairlighteth 61535fb
Merge branch 'develop' of https://github.com/cowprotocol/cowswap into…
shoom3301 7e40342
chore: fix code style
shoom3301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { useCallback, useEffect, useMemo, useState } from 'react' | ||
|
||
import ICON_HOOK from '@cowprotocol/assets/cow-swap/hook.svg' | ||
import { HookDappWalletCompatibility } from '@cowprotocol/hook-dapp-lib' | ||
import { Command } from '@cowprotocol/types' | ||
import { BannerOrientation, DismissableInlineBanner } from '@cowprotocol/ui' | ||
import { useIsSmartContractWallet } from '@cowprotocol/wallet' | ||
|
@@ -33,62 +34,48 @@ interface HookStoreModal { | |
export function HookRegistryList({ onDismiss, isPreHook, hookToEdit }: HookStoreModal) { | ||
const [selectedDapp, setSelectedDapp] = useState<HookDapp | null>(null) | ||
const [dappDetails, setDappDetails] = useState<HookDapp | null>(null) | ||
|
||
const [isAllHooksTab, setIsAllHooksTab] = useState<boolean>(true) | ||
const [searchQuery, setSearchQuery] = useState<string>('') | ||
|
||
const isSmartContractWallet = useIsSmartContractWallet() | ||
const walletType = isSmartContractWallet | ||
? HookDappWalletCompatibility.SMART_CONTRACT | ||
: HookDappWalletCompatibility.EOA | ||
const addCustomHookDapp = useAddCustomHookDapp(isPreHook) | ||
const removeCustomHookDapp = useRemoveCustomHookDapp() | ||
const customHookDapps = useCustomHookDapps(isPreHook) | ||
const hookToEditDetails = useHookById(hookToEdit, isPreHook) | ||
|
||
// State for Search Input | ||
const [searchQuery, setSearchQuery] = useState<string>('') | ||
|
||
// Clear search input handler | ||
const handleClearSearch = useCallback(() => { | ||
setSearchQuery('') | ||
}, []) | ||
|
||
const internalHookDapps = useInternalHookDapps(isPreHook) | ||
|
||
const currentDapps = useMemo(() => { | ||
return isAllHooksTab ? internalHookDapps.concat(customHookDapps) : customHookDapps | ||
}, [isAllHooksTab, internalHookDapps, customHookDapps]) | ||
const currentDapps = useMemo( | ||
() => (isAllHooksTab ? [...internalHookDapps, ...customHookDapps] : customHookDapps), | ||
[isAllHooksTab, internalHookDapps, customHookDapps], | ||
) | ||
|
||
// Compute filteredDapps based on searchQuery | ||
const filteredDapps = useMemo(() => { | ||
if (!searchQuery) return currentDapps | ||
|
||
const lowerQuery = searchQuery.toLowerCase() | ||
|
||
return currentDapps.filter((dapp) => { | ||
const name = dapp.name?.toLowerCase() || '' | ||
const description = dapp.descriptionShort?.toLowerCase() || '' | ||
|
||
return name.includes(lowerQuery) || description.includes(lowerQuery) | ||
}) | ||
return currentDapps.filter(({ name = '', descriptionShort = '' }) => | ||
[name, descriptionShort].some((text) => text.toLowerCase().includes(lowerQuery)), | ||
) | ||
}, [currentDapps, searchQuery]) | ||
|
||
const sortedFilteredDapps = useMemo(() => { | ||
const isCompatible = (dapp: HookDapp) => | ||
!dapp.conditions?.walletCompatibility || dapp.conditions.walletCompatibility.includes(walletType) | ||
return filteredDapps.sort((a, b) => (isCompatible(a) === isCompatible(b) ? 0 : isCompatible(a) ? -1 : 1)) | ||
}, [filteredDapps, isSmartContractWallet]) | ||
|
||
const customHooksCount = customHookDapps.length | ||
const allHooksCount = internalHookDapps.length + customHooksCount | ||
|
||
// Compute title based on selected dapp or details | ||
const title = useMemo(() => { | ||
if (selectedDapp) return selectedDapp.name | ||
if (dappDetails) return 'Hook description' | ||
return 'Hook Store' | ||
}, [selectedDapp, dappDetails]) | ||
const title = selectedDapp?.name || (dappDetails ? 'Hook description' : 'Hook Store') | ||
|
||
// Handle modal dismiss | ||
const onDismissModal = useCallback(() => { | ||
if (hookToEdit) { | ||
setSelectedDapp(null) | ||
onDismiss() | ||
return | ||
} | ||
|
||
if (dappDetails) { | ||
} else if (dappDetails) { | ||
setDappDetails(null) | ||
} else if (selectedDapp) { | ||
setSelectedDapp(null) | ||
|
@@ -97,33 +84,27 @@ export function HookRegistryList({ onDismiss, isPreHook, hookToEdit }: HookStore | |
} | ||
}, [onDismiss, selectedDapp, dappDetails, hookToEdit]) | ||
|
||
// Handle hookToEditDetails | ||
useEffect(() => { | ||
if (!hookToEditDetails) { | ||
setSelectedDapp(null) | ||
if (hookToEditDetails) { | ||
const foundDapp = findHookDappById(currentDapps, hookToEditDetails) | ||
setSelectedDapp(foundDapp || null) | ||
} else { | ||
setSelectedDapp(findHookDappById(currentDapps, hookToEditDetails) || null) | ||
setSelectedDapp(null) | ||
} | ||
}, [hookToEditDetails, currentDapps]) | ||
|
||
// Reset dappDetails when tab changes | ||
useEffect(() => { | ||
setDappDetails(null) | ||
}, [isAllHooksTab]) | ||
|
||
// Handle add custom hook button | ||
const handleAddCustomHook = useCallback(() => { | ||
setIsAllHooksTab(false) | ||
}, [setIsAllHooksTab]) | ||
const handleAddCustomHook = () => setIsAllHooksTab(false) | ||
const handleClearSearch = () => setSearchQuery('') | ||
|
||
// Determine the message for EmptyList based on the active tab and search query | ||
const emptyListMessage = useMemo(() => { | ||
if (isAllHooksTab) { | ||
return searchQuery ? 'No hooks match your search.' : 'No hooks available.' | ||
} else { | ||
return "You haven't added any custom hooks yet. Add a custom hook to get started." | ||
} | ||
}, [isAllHooksTab, searchQuery]) | ||
const emptyListMessage = isAllHooksTab | ||
? searchQuery | ||
? 'No hooks match your search.' | ||
: 'No hooks available.' | ||
: "You haven't added any custom hooks yet. Add a custom hook to get started." | ||
|
||
const DappsListContent = ( | ||
<> | ||
|
@@ -153,13 +134,16 @@ export function HookRegistryList({ onDismiss, isPreHook, hookToEdit }: HookStore | |
onClear={handleClearSearch} | ||
/> | ||
|
||
{filteredDapps.length > 0 ? ( | ||
{sortedFilteredDapps.length > 0 ? ( | ||
<HookDappsList> | ||
{filteredDapps.map((dapp) => ( | ||
{sortedFilteredDapps.map((dapp) => ( | ||
<HookListItem | ||
key={isHookDappIframe(dapp) ? dapp.url : dapp.name} | ||
dapp={dapp} | ||
onRemove={isAllHooksTab ? undefined : () => removeCustomHookDapp(dapp as HookDappIframe)} | ||
walletType={ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
isSmartContractWallet ? HookDappWalletCompatibility.SMART_CONTRACT : HookDappWalletCompatibility.EOA | ||
} | ||
onRemove={!isAllHooksTab ? () => removeCustomHookDapp(dapp as HookDappIframe) : undefined} | ||
onSelect={() => setSelectedDapp(dapp)} | ||
onOpenDetails={() => setDappDetails(dapp)} | ||
/> | ||
|
@@ -189,37 +173,29 @@ export function HookRegistryList({ onDismiss, isPreHook, hookToEdit }: HookStore | |
onAddCustomHook={handleAddCustomHook} | ||
/> | ||
)} | ||
{(() => { | ||
if (selectedDapp) { | ||
return ( | ||
<> | ||
<HookDetailHeader dapp={selectedDapp} iconSize={58} gap={12} padding="24px 10px" /> | ||
<HookDappContainer | ||
isPreHook={isPreHook} | ||
onDismiss={onDismiss} | ||
dapp={selectedDapp} | ||
hookToEdit={hookToEdit} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
if (dappDetails) { | ||
return <HookDappDetails dapp={dappDetails} onSelect={() => setSelectedDapp(dappDetails)} /> | ||
} | ||
|
||
return isAllHooksTab ? ( | ||
DappsListContent | ||
) : ( | ||
<AddCustomHookForm | ||
{selectedDapp ? ( | ||
<> | ||
<HookDetailHeader dapp={selectedDapp} iconSize={58} gap={12} padding="24px 10px" walletType={walletType} /> | ||
<HookDappContainer | ||
isPreHook={isPreHook} | ||
isSmartContractWallet={isSmartContractWallet} | ||
addHookDapp={addCustomHookDapp} | ||
> | ||
{DappsListContent} | ||
</AddCustomHookForm> | ||
) | ||
})()} | ||
onDismiss={onDismiss} | ||
dapp={selectedDapp} | ||
hookToEdit={hookToEdit} | ||
/> | ||
</> | ||
) : dappDetails ? ( | ||
<HookDappDetails dapp={dappDetails} onSelect={() => setSelectedDapp(dappDetails)} walletType={walletType} /> | ||
) : isAllHooksTab ? ( | ||
DappsListContent | ||
) : ( | ||
<AddCustomHookForm | ||
isPreHook={isPreHook} | ||
isSmartContractWallet={isSmartContractWallet} | ||
addHookDapp={addCustomHookDapp} | ||
> | ||
{DappsListContent} | ||
</AddCustomHookForm> | ||
)} | ||
</NewModal> | ||
</Wrapper> | ||
) | ||
|
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
22 changes: 20 additions & 2 deletions
22
apps/cowswap-frontend/src/modules/hooksStore/pure/HookDetailHeader/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 |
---|---|---|
@@ -1,25 +1,43 @@ | ||
import { HookDappWalletCompatibility } from '@cowprotocol/hook-dapp-lib' | ||
|
||
import * as styled from './styled' | ||
|
||
import { HookDapp } from '../../types/hooks' | ||
|
||
interface HookDetailHeaderProps { | ||
dapp: HookDapp | ||
walletType?: HookDappWalletCompatibility | ||
onSelect?: () => void | ||
iconSize?: number | ||
gap?: number | ||
padding?: string | ||
} | ||
|
||
export function HookDetailHeader({ dapp, onSelect, iconSize, gap, padding }: HookDetailHeaderProps) { | ||
export function HookDetailHeader({ dapp, walletType, onSelect, iconSize, gap, padding }: HookDetailHeaderProps) { | ||
const { name, image, descriptionShort } = dapp | ||
|
||
const isCompatible = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code is duplicated in |
||
!dapp.conditions?.walletCompatibility || | ||
dapp.conditions.walletCompatibility.includes( | ||
walletType === HookDappWalletCompatibility.EOA | ||
? HookDappWalletCompatibility.EOA | ||
: HookDappWalletCompatibility.SMART_CONTRACT, | ||
) | ||
|
||
return ( | ||
<styled.Header iconSize={iconSize} gap={gap} padding={padding}> | ||
<img src={image} alt={name} /> | ||
<styled.Content> | ||
<h3>{name}</h3> | ||
<styled.Description>{descriptionShort}</styled.Description> | ||
{onSelect && <styled.AddButton onClick={onSelect}>Add</styled.AddButton>} | ||
{onSelect && | ||
(isCompatible ? ( | ||
<styled.AddButton onClick={onSelect}>Add</styled.AddButton> | ||
) : ( | ||
<styled.AddButton disabled title="Not compatible with current wallet type"> | ||
n/a | ||
</styled.AddButton> | ||
))} | ||
</styled.Content> | ||
</styled.Header> | ||
) | ||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
walletType
is not present inuseMemo()
depsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And
isSmartContractWallet
is not needed there