-
Notifications
You must be signed in to change notification settings - Fork 473
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
Refactor sidebar hooks [SW-647] #4641
Conversation
Branch preview✅ Deploy successful! Storybook: |
📦 Next.js Bundle Analysis for safe-wallet-webThis analysis was generated by the Next.js Bundle Analysis action. 🤖 🎉 Global Bundle Size Decreased
DetailsThe global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster. Any third party scripts you have added directly to your app using the If you want further insight into what is behind the changes, give @next/bundle-analyzer a try! One Page Changed SizeThe following page changed size from the code in this PR compared to its base branch:
DetailsOnly the gzipped size is provided here based on an expert tip. First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If Any third party scripts you have added directly to your app using the Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 20% or more, there will be a red status indicator applied, indicating that special attention should be given to this. |
Coverage report
Test suite run success1696 tests passing in 229 suites. Report generated by 🧪jest coverage report action from 03c8136 |
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.
Code review by ChatGPT
})) | ||
}) | ||
|
||
it('returns an empty array if there is no wallet and allOwned is undefined', () => { |
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.
Consider implementing the buildSafeItem
and prepareAddresses
test cases soon to ensure the new functionality is correctly tested. Additionally, review current integration coverage to verify any untested edge cases.
const addedOnChain = Object.keys(allAdded[chainId] || {}) | ||
const ownedOnChain = allOwned[chainId] || [] | ||
const undeployedOnChain = Object.keys(allUndeployed[chainId] || {}) | ||
|
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.
- Use consistent naming conventions for exported functions. Prefixing with an underscore is unconventional and might confuse maintainers.
- Consider memoization for
_prepareAddresses
and_buildSafeItem
to avoid recomputation on rerender. - Inline logic inside
useAllSafes
can be encapsulated into a separate utility function to enhance readability and maintainability.
const isPinned = Boolean(addedSafe) // Pinning a safe means adding it to the added safes storage | ||
|
||
// Determine if the user is an owner | ||
const isOwnerFromAdded = addedSafeOwners.some(({ value }) => sameAddress(walletAddress, value)) |
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.
This is just for CF safes, right?
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.
Good question. From what I can find is that we are using the isReadOnly
flag for two things:
- To show the Add network button
- To show the queue actions
With that in mind I would expect the following behaviour:
CF Safes:
- Show the Add network button if the connected wallet is an owner regardless of pinned state 🔴
- No queue actions required
Owned Safes:
- Show the Add network button regardless of pinned state ✅
- Show the queue actions regardless of pinned state ✅
Added but not owned safes:
- Show neither the Add network button nor the queue actions ✅
Anything I missed? I will implement the CF Safes case
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.
That behaviour makes sense, thanks for clarifying!
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.
Good question. From what I can find is that we are using the isReadOnly flag for two things:
It's also used to display the read-only chip for the item in the safe list
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.
Code review by ChatGPT
|
||
const { result } = renderHook(() => useAllSafes(), { | ||
initialReduxState: { | ||
addedSafes: { |
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.
Consider factoring out repetitive mock object creation in your tests into reusable shared functions to adhere to DRY principles. This will streamline your test setup and improve maintainability, especially if more tests are added in the future.
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.
Code review by ChatGPT
'1': ['0x456', '0x789'], | ||
} | ||
jest.spyOn(allOwnedSafes, 'default').mockReturnValue([mockOwnedSafes, undefined, false]) | ||
|
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.
Add mockAllUndeployed
to the declarations at the start of the test suite if it's used across multiple tests for consistency. Extract repeated configuration and props setups into a helper function to adhere to DRY principles. Check if casting to UndeployedSafe['props']
is necessary.
export const useHasSafes = () => { | ||
const { address = '' } = useWallet() || {} | ||
const allAdded = useAddedSafes() | ||
const hasAdded = !isEmpty(allAdded) |
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.
-
Combining
isOwnerFromAdded
andisOwnerFromCF
: Merge the logic of these two conditions if possible since both check ownership; this reduces redundancy. -
Consistent Naming: Ensure the term "CF" is well-documented or renamed for clarity if it's not a commonly understood term in this context.
-
Function Complexity:
_buildSafeItem
contains several logic checks; consider breaking it into smaller functions for better readability and testability.
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.
Code review by ChatGPT
const ownedOnChain = allOwned[chainId] || [] | ||
const undeployedOnChain = Object.keys(allUndeployed[chainId] || {}) | ||
|
||
const combined = [...addedOnChain, ...ownedOnChain, ...undeployedOnChain] |
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.
- The initial setting of
allOwned = {}
implies it should never beundefined
; verify thatuseAllOwnedSafes
handles all cases to ensure reliability. - Consider defining
_prepareAddresses
and_buildSafeItem
as standalone functions outside the return statement to align with DRY and improve readability.
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.
nice work 🚀
const isOwnedSafe = (allOwned[chainId] || []).includes(address) | ||
const isOwned = isOwnedSafe || isOwnerFromAdded | ||
const isOwned = isOwnedSafe || isOwnerFromAdded || isOwnerFromCF |
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.
I think isOwnerFromAdded
is not needed anymore. If it's deployed we check ownership from allOwned, if it's CF we check ownership from allUndeployed, so isOwnerFromAdded is redundant
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.
Very true, nice catch!
# Conflicts: # src/features/myAccounts/hooks/useAllOwnedSafes.ts
What it solves
Part of SW-647
How this PR fixes it
useAllSafes
useAllOwnedSafes
as it is not needed anymoreuseAllSafes
since it is already done inMyAccounts
useHasSafes
into its own fileToDos
useAllSafes
buildSafeItem
prepareAddresses
useAllSafes
and split up the logic