Skip to content
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(web3Hub): fix state management with jotai #8069

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Canestin
Copy link

@Canestin Canestin commented Oct 10, 2024

βœ… Checklist

  • npx changeset was attached.
  • Covered by automatic tests.
  • Impact of the changes:

πŸ“ Description

Problem

Currently, the way we update the app storage causes issues because the data state isn't shared between instances.
Specifically, when we modify a variable with instance X, the state of instance Y isn't updated (as the state isn't shared). As a result, if two different instances update the app storage at the same time, only the last modification is applied, leading to potential data loss.

Solution

Ensure that the database state is shared across all instances. This way, a modification in instance Z will automatically reflect in instance Y, by using Jotai npm lib, a lightweight library that simplifies state management across an application.

demo.mp4

🧐 Checklist for the PR Reviewers

  • The code aligns with the requirements described in the linked JIRA or GitHub issue.
  • The PR description clearly documents the changes made and explains any technical trade-offs or design decisions.
  • There are no undocumented trade-offs, technical debt, or maintainability issues.
  • The PR has been tested thoroughly, and any potential edge cases have been considered and handled.
  • Any new dependencies have been justified and documented.
  • Performance considerations have been taken into account. (changes have been profiled or benchmarked if necessary)

@Canestin Canestin requested a review from a team as a code owner October 10, 2024 14:56
Copy link

vercel bot commented Oct 10, 2024

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

5 Skipped Deployments
Name Status Preview Comments Updated (UTC)
ledger-live-docs ⬜️ Ignored (Inspect) Oct 10, 2024 2:56pm
ledger-live-github-bot ⬜️ Ignored (Inspect) Oct 10, 2024 2:56pm
native-ui-storybook ⬜️ Ignored (Inspect) Oct 10, 2024 2:56pm
react-ui-storybook ⬜️ Ignored (Inspect) Oct 10, 2024 2:56pm
web-tools ⬜️ Ignored (Inspect) Oct 10, 2024 2:56pm

@live-github-bot live-github-bot bot added the mobile Has changes in LLM label Oct 10, 2024
Comment on lines +9 to +22
export const storage = createJSONStorage<Web3HubDB>(() => AsyncStorage);

export const web3hubAtom = atomWithStorage<Web3HubDB>(
WEB3HUB_STORE_KEY,
INITIAL_WEB3HUB_STATE,
storage,
);

type NewState = Web3HubDB | ((s: Web3HubDB) => Web3HubDB);

export function useWeb3HubDB<Selected>(
selector: (state: Web3HubDB) => Selected,
): [Selected, (v: NewState) => void] {
const [state, setState] = useAtom(web3hubAtom);
Copy link
Author

@Canestin Canestin Oct 10, 2024

Choose a reason for hiding this comment

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

Ideally I would like to use this logic in live-common (by generalizing) and replace the one in useDB.

@@ -154,6 +154,7 @@
"hoist-non-react-statics": "3.3.2",
"i18next": "20.6.1",
"invariant": "2.2.4",
"jotai": "^2.7.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Small detail but we could update jotai to the latest version, it includes some fixes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mobile Has changes in LLM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants