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

[IndexTable]: optimizes and add more stuff to use-index-resource-state #11657

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lbenie
Copy link
Contributor

@lbenie lbenie commented Feb 27, 2024

WHY are these changes introduced?

Closes: https://github.com/Shopify/admin-comms/issues/1261

WHAT is this pull request doing?

This PR introduces 2 new states for the use-index-resource-state hook.

  1. Dirty -- A boolean that reflects the dirty state of the IndexTable
  2. UnselectedResources -- A string array of IDs. This array adds/removes IDs that were previously selected. It's initially set to an empty array.

Also, there were some performance improvements by using a Set instead of an Array. In a lot of cases we are concerned with the lookup access and using a Set gives us a O(1) access time compared to O(N) on the array since we were using operations like filter.

The hook's API returned object still exposes an array and not a Set.

How to 🎩

🖥 Local development instructions
🗒 General tophatting guidelines
📄 Changelog guidelines

🎩 checklist

@lbenie lbenie self-assigned this Feb 27, 2024
@lbenie lbenie changed the title [IndexTable]: optimizes and add more stuff to [IndexTable]: optimizes and add more stuff to use-index-resource-state Feb 27, 2024
);
}

function MockClearComponent<T extends {[key: string]: unknown}>({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was unnecessary since we can do everything from the "main" component

const [unselectedResources, setUnselectedResources] = useState<
ReadonlySet<string>
>(new Set());
const [dirty, setDirty] = useState(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This state is needed for Test drive.

We need to know if the user made some changes on the table and we display some text based on this condition

const [allResourcesSelected, setAllResourcesSelected] = useState(
initAllResourcesSelected,
);
const [unselectedResources, setUnselectedResources] = useState<
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This state is needed for Test drive.

We need to know all the "stores" that were unselected. In our UX, we pre-check every store and if they become unselected we must update them on the backend.

Comment on lines +58 to +71
useEffect(() => {
if (!isEqual(prevPreCheckedResourcesRef.current, preCheckedResources)) {
const filteredResources = dirty
? preCheckedResources.filter(
(resource) => !unselectedResources.has(resource),
)
: preCheckedResources;

setSelectedResources(new Set(filteredResources));
prevPreCheckedResourcesRef.current = preCheckedResources;
}
}, [dirty, preCheckedResources, unselectedResources]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sure that preCheckedResources are synced on pagination.

The current behavior does not re-hydrates on pagination since the state is never updated.

Comment on lines +71 to +87
useEffect(() => {
return () => {
setDirty(false);
};
}, []);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sure the state is cleaned when the component unmounts

? [...newSelectedResources, selection as string]
: newSelectedResources.filter((id) => id !== selection),
);
if (typeof selection !== 'string') break;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It felt odd to use selection as String when the Types allowed for more.

@lbenie
Copy link
Contributor Author

lbenie commented Feb 27, 2024

/snapit

@lbenie lbenie marked this pull request as ready for review February 27, 2024 19:18
Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/polaris-icons@0.0.0-snapshot-20240227192044
yarn add @shopify/polaris@0.0.0-snapshot-20240227192044

@boyutf boyutf self-assigned this Feb 27, 2024
@lbenie lbenie force-pushed the test-drive/use-index-resouce-state-fast-follow branch 2 times, most recently from 91df46b to e082aef Compare February 27, 2024 21:39
Copy link
Contributor

@mrcthms mrcthms left a comment

Choose a reason for hiding this comment

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

Code changes are looking good to me! Would love to get a Spin URL to test this snapshot with in the PR description before approving

@lbenie
Copy link
Contributor Author

lbenie commented Feb 28, 2024

Code changes are looking good to me! Would love to get a Spin URL to test this snapshot with in the PR description before approving

Yes I spun an environment last night but kids prevented me from working haha, will get something up in a few!

@mrcthms

@lbenie
Copy link
Contributor Author

lbenie commented Feb 28, 2024

Here's the Product Index

Here's Test drive

For test drive you need to click on manage so show the modal and see the Index Table.

One thing I've noticed is we are still bound to pass a bulk/promoted actions in order to show the paginated text.

@mrcthms
Copy link
Contributor

mrcthms commented Feb 28, 2024

@lbenie Created a PR for the paginated select all action text issue – #11670 My apologies on that one, thought we'd get that for free if we enabled the regular select actions in the bulk actions, but evidently not!

@mrcthms
Copy link
Contributor

mrcthms commented Feb 28, 2024

Also just want to confirm, taking a look at the two URLs there, I can discern no difference from the behaviour that we currently have in production – is that what we expect?

@lbenie
Copy link
Contributor Author

lbenie commented Feb 28, 2024

Also just want to confirm, taking a look at the two URLs there, I can discern no difference from the behaviour that we currently have in production – is that what we expect?

Yes that was the goal, API is retro-compatible meaning that it shouldn't break any IndexTable using this hook.

Also it adds some perf/stuff to the API (dirty/unselected) so it's "opt-in" from a consumer perspective.

On a side note, I began investigating last night about this design.

Here's my plan to achieve this

  1. We'd need to call the clearSelection callback from the use-index-resource-state. In order to achieve to we'd need to pass this as a prop to IndexTable (same way it's done for handleChange)
  2. Not sure if we need to bind it to the context
  3. If all is selected, call clear selection

Thoughts on ^

I think as a follow-up on web for test drive, I can check if unselected.length > 0 && all test drive are enabled then fully delete them with the API.

@lbenie
Copy link
Contributor Author

lbenie commented Feb 28, 2024

/snapit

Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshot has been published to npm.

Test the snapshot by updating your package.json with the newly published version:

yarn add @shopify/polaris@0.0.0-snapshot-20240228183806

@lbenie lbenie force-pushed the test-drive/use-index-resouce-state-fast-follow branch 2 times, most recently from 839f0a7 to 3e69e73 Compare February 28, 2024 18:53
@lbenie lbenie force-pushed the test-drive/use-index-resouce-state-fast-follow branch from 3e69e73 to 12e0ddb Compare March 18, 2024 20:42
@lbenie
Copy link
Contributor Author

lbenie commented Mar 18, 2024

/snapit

Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/polaris-migrator@0.0.0-snapshot-20240318204532
yarn add @shopify/polaris@0.0.0-snapshot-20240318204532
yarn add @shopify/stylelint-polaris@0.0.0-snapshot-20240318204532

@lbenie lbenie force-pushed the test-drive/use-index-resouce-state-fast-follow branch from 2968e00 to 82f1811 Compare March 19, 2024 18:21
@lbenie
Copy link
Contributor Author

lbenie commented Mar 19, 2024

Spin:

What to expect, no changes and regressions on products. For test drive, if you select/unselect it should send a different payload to the API with the proper resources.

@mrcthms @alex-page

Here's the diff with web: https://github.com/Shopify/web/compare/main...polaris-index-state

Can I get this merged for tomorrow?

@alex-page alex-page requested review from a team and removed request for alex-page March 20, 2024 00:02
@lbenie
Copy link
Contributor Author

lbenie commented Mar 21, 2024

/snapit

Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/polaris-migrator@0.0.0-snapshot-20240321175357
yarn add @shopify/polaris@0.0.0-snapshot-20240321175357
yarn add @shopify/stylelint-polaris@0.0.0-snapshot-20240321175357

@lbenie
Copy link
Contributor Author

lbenie commented Mar 21, 2024

/snapit

Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/polaris-migrator@0.0.0-snapshot-20240321181526
yarn add @shopify/polaris@0.0.0-snapshot-20240321181526
yarn add @shopify/stylelint-polaris@0.0.0-snapshot-20240321181526

@lbenie
Copy link
Contributor Author

lbenie commented Mar 25, 2024

/snapit

Copy link
Contributor

🫰✨ Thanks @lbenie! Your snapshots have been published to npm.

Test the snapshots by updating your package.json with the newly published versions:

yarn add @shopify/polaris-migrator@0.0.0-snapshot-20240325145122
yarn add @shopify/polaris@0.0.0-snapshot-20240325145122
yarn add @shopify/stylelint-polaris@0.0.0-snapshot-20240325145122

Copy link
Member

@chloerice chloerice left a comment

Choose a reason for hiding this comment

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

Hey @lbenie 👋🏽 Changes overall look good, but I noticed while tophatting that when SelectionType is Range (e.g., with subheaders, with nested rows) the "All" deselection breaks. 👀

@lbenie
Copy link
Contributor Author

lbenie commented Apr 15, 2024

thanks @chloerice I'll investigate

@aaronccasanova aaronccasanova force-pushed the test-drive/use-index-resouce-state-fast-follow branch from 7fe360a to b029416 Compare April 19, 2024 21:52
@github-actions github-actions bot added the cla-needed Added by a bot. Contributor needs to sign the CLA Agreement. label Apr 19, 2024
@translation-platform
Copy link
Contributor

Localization quality issues found

The following issues may affect the quality of localized translations if they are not addressed:

  • The value Clear for key Polaris.ActionList.SearchField.clearButtonLabel is very short. Short strings are more likely to be misunderstood by translators without context. Please provide additional context for the translators if possible.
  • The value Search for key Polaris.ActionList.SearchField.search is very short. Short strings are more likely to be misunderstood by translators without context. Please provide additional context for the translators if possible.
  • The value Search actions for key Polaris.ActionList.SearchField.placeholder is very short. Short strings are more likely to be misunderstood by translators without context. Please provide additional context for the translators if possible.

Please look out for other instances of this issue in your PR and fix them as well if possible.

Questions about these messages? Hop in the #help-localization Slack channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-needed Added by a bot. Contributor needs to sign the CLA Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants