Skip to content

frontend: convert sidebar to functional component #2314

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

Merged

Conversation

thisconnect
Copy link
Collaborator

@thisconnect thisconnect commented Oct 25, 2023

No description provided.

@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from 17c7cf6 to e9dd306 Compare October 25, 2023 14:23
@thisconnect thisconnect changed the title Frontend sidebar functional frontend: convert sidebar to functional component Oct 25, 2023
@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from e9dd306 to 276abad Compare October 25, 2023 14:24
};
};
}, [sidebarStatus]);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole effect could probably become a hook, but keeping it here for now to not have too many changes.

@benma
Copy link
Contributor

benma commented Oct 25, 2023

This is nice but there will be some conflicts with #2283 and #2308.

If you plan more refactorings, let's check first if there will be conflicts in the staging branches to avoid needless rebasing effort.

Now that this is done, of course feel free to merge it and I'll try to resolve the conflicts when pulling master to staging-watchonly.

@thisconnect
Copy link
Collaborator Author

There will also be conflicts in staging-ln and staging-bitsurance, both each add another link in the sidebar afaik.

This is nice but there will be some conflicts with #2283 and #2308.

@thisconnect
Copy link
Collaborator Author

thisconnect commented Oct 25, 2023

I think we should refactor and cleanup, before starting new features. Reason that sidebar should be functional is so that we can finally get rid of the share HOC decorators (2 shared stores are left, rates and sidebar state).

EDIT: lets wait for bitsurance and watch-only before we merge this PR. I'll rebase it accordingly once that all is merged.

@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from 276abad to 16e0a10 Compare December 14, 2023 13:26
@thisconnect
Copy link
Collaborator Author

rebased

@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch 2 times, most recently from f550c2c to 855186d Compare December 19, 2023 10:19
@thisconnect
Copy link
Collaborator Author

rebased

@thisconnect thisconnect requested review from shonsirsha and removed request for shonsirsha December 19, 2023 10:19
@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from eb78710 to 855186d Compare December 20, 2023 16:24
setKeystores(keystores);

});
return subscribeKeystores(setKeystores);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`subscribeKeystore should be called without the return, otherwise the subscription only happens when unmounting the component, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no quiet. But I did a short cut here, so it looks a bit confusing. This is the same as:

const unsubscribeFn = subscribeKeystores(keystores => setKeystores(keystores));

return () => {
  unsibscribeFn();
};

the returned function is only called when the dependency-list (2nd arg of useEffect) is changing or on unmount. In this case (with empty deplist) only on unmount.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh looking at the code I see your point, sorry. But I tested it and it seemed to me that the sidebar didn't update when connecting/disconneting a new keystore.. I'll check that again.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You were right, it works nicely. Don't know why it seemed different before

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want I can also rewrite and store the unsubscribe fn in a variable like I did in the code example, for better readability... but I think we do use the shortcut way in a few other places already... let me know.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no no, this is nice!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a comment would be nice for readability, but I think this is fine either way

</div>
const hidden = sidebarStatus === 'forceHidden';
const hasOnlyBTCAccounts = accounts.every(({ coinCode }) => isBitcoinOnly(coinCode));
const accountsByKeystore = getAccountsByKeystore(accounts);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these don't need a `useEffect' hook as the whole component is re-rendered when the account list changes, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes these lines are currently executed on every render, like before this commit (those lines were just in render function).

But there is some potential for optimization here, as hasOnlyBTCAccounts and accountsByKeystore only depend on accounts. So we probably should move this so that accounts.every( and getAccountsByKeystore(accounts) only is executed when accounts change. This way it would not do the operations on every render.

title={t('accountSummary.title')}
onClick={handleSidebarItemClick}>
<div className={style.single}>
<img draggable={false} src={info} alt={t('sidebar.addAccount')} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt text looks wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for catching this, I accidentally re-added during rebase. I'll remove the alt attribute and fixup the last commit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

<img draggable={false} src={settings} alt={t('sidebar.settings')} />
to="/buy/info">
<div className={style.single}>
<img draggable={false} src={coins} alt={t('sidebar.exchanges')} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sidebar.exchanges doesn't seem to exist in the locales file. This was already like this on master, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from 855186d to c165f87 Compare December 21, 2023 10:34
Copy link
Collaborator

@Beerosagos Beerosagos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK! Very nice

Note the Sidebar component is still using share HOC decorator,
but this can now be easier replaced with react context.

Also remove 2 unused translation strings on image alt attributes
that had no corresponding translations.
@thisconnect thisconnect force-pushed the frontend-sidebar-functional branch from c165f87 to a8be91a Compare December 21, 2023 13:35
@thisconnect thisconnect merged commit 6a07734 into BitBoxSwiss:master Dec 21, 2023
@thisconnect thisconnect deleted the frontend-sidebar-functional branch April 18, 2025 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants