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

Introduce ComponentState #4386

Merged
merged 9 commits into from
Mar 9, 2024
Merged

Introduce ComponentState #4386

merged 9 commits into from
Mar 9, 2024

Conversation

charlesBochet
Copy link
Member

@charlesBochet charlesBochet commented Mar 8, 2024

Summary:

This PR introduces significant refinements to our state management strategy, aimed at simplifying the use of Recoil states within our application. By streamlining the API, we enhance both the developer experience and the readability of our codebase. The changes include the consolidation of State and ComponentState management, elimination of the StateScopeMap naming, and introduction of a unified approach for handling Recoil states (aka States) and component-specific states. (aka ComponentStates)

Rationale:

The core motivation behind these changes is to make state management more intuitive and maintainable. Developers can now use classical Recoil states (State) for general state management and ComponentStates for states that are internal to reusable components. This approach simplifies the API by reducing it to two main concepts: States (classical Recoil states) and ComponentStates (with a slightly different API).

Both will share the same API: useRecoilState(myState())

For ComponentStates, there will be an additional step: myState = extractState(myComponentState, componentId)

Detailed API

State

Declaration:

export const recordIndexSortsState = createState<Sort[]>({
  key: 'recordIndexSortsState',
  defaultValue: [],
});

Usage:
const recordIndexSorts = useRecoilValue(recordIndexSortsState());

ComponentState

Declaration:

export const selectableItemIdsComponentState = createComponentState<string[][]>(
  {
    key: 'selectableItemIdsComponentState',
    defaultValue: [[]],
  },
);

Usage:

const selectableItemIdsState = extractComponentState(
      selectableItemIdsComponentState,
      scopeId,
    );
const selectableItemIds = useRecoilValue(selectableItemIdsState());

How to review

See first commit for POC
The other commits refactor the whole app

@charlesBochet charlesBochet changed the title Proof of concept ComponentState Introduce ComponentState Mar 8, 2024
Copy link
Contributor

github-actions bot commented Mar 9, 2024

Warnings
⚠️

Changes were made to package.json, but not to yarn.lock - Perhaps you need to run yarn install?

Generated by 🚫 dangerJS against e7c5e43

@charlesBochet charlesBochet merged commit 86c0f31 into main Mar 9, 2024
15 checks passed
@charlesBochet charlesBochet deleted the idea-component-state branch March 9, 2024 10:31
arnavsaxena17 pushed a commit to arnavsaxena17/twenty that referenced this pull request Oct 6, 2024
* Proof of concept ComponentState

* Migrate to createState and createFamilyState

* Refactor

* Fix

* Fix tests

* Fix lint

* Fix tests

* Re-enable coverage
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.

None yet

1 participant