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

DataViews: remove the AnyItem type #62856

Merged
merged 1 commit into from
Jun 27, 2024
Merged

DataViews: remove the AnyItem type #62856

merged 1 commit into from
Jun 27, 2024

Conversation

jsnajdr
Copy link
Member

@jsnajdr jsnajdr commented Jun 26, 2024

Removes the AnyItem type from dataviews, as it turns out that Item can be completely generic without any constraints.

Fixes several type errors that the removal revealed. Correct types for the getItemId and getValue getters that can be optional only for some Item subtypes because the default getter is not 100% generic. Also uses the getItemId function instead of non-generic item.id in ViewList.

Inspired by this discussion: #62647 (comment)

My plan is that we can finish and merge this PR after #62647 is landed.

@jsnajdr jsnajdr added [Type] Code Quality Issues or PRs that relate to code quality [Feature] Data Views Work surrounding upgrading and evolving views in the site editor and beyond labels Jun 26, 2024
@jsnajdr jsnajdr self-assigned this Jun 26, 2024
interface DataViewsProps< Item extends AnyItem > {
type ItemWithId = { id: string };

type DataViewsProps< Item > = {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not really familiar with any of this. Where is Item defined? Below in Item extends ItemWithId right? I thought an Item was an item in the data array? Those don't have a getItemId function?

Copy link
Member

Choose a reason for hiding this comment

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

I guess I'm not getting this part

( Item extends ItemWithId
 	? { getItemId?: ( item: Item ) => string }
 	: { getItemId: ( item: Item ) => string } )

Copy link
Member Author

Choose a reason for hiding this comment

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

Item is a type parameter, it's completely generic. DataViews can work with any data type whatsoever, you can have a dataview of numbers. As long as you provide functions to extract IDs and field values from the item.

The Item extends ItemWithId part says that if the Item is an object that has an id field, the getItemId prop is optional, because we have a suitable default for it.

But if Item is not compatible with ItemWithId, then getItemId is mandatory. Because the default (item => item.id) is not going to work with such a type.

isLoading?: boolean;
paginationInfo: {
totalItems: number;
totalPages: number;
};
supportedLayouts: string[];
onSelectionChange?: ( items: Item[] ) => void;
}
} & ( Item extends ItemWithId
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL conditions wow :P

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, with this the type system is Turing complete and you can build any program with it 😄

@@ -318,7 +316,7 @@ export default function ViewList< Item extends AnyItem >(
} = props;
const baseId = useInstanceId( ViewList, 'view-list' );
const selectedItem = data?.findLast( ( item ) =>
selection.includes( item.id )
selection.includes( getItemId( item ) )
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess that's a bug fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. Another little suspicious thing is the optional chaining in data?.findLast. The types say that data is mandatory. If that's really true, the optional chaining is not needed. But that's a very minor issue compared with other problems.

@youknowriad
Copy link
Contributor

@jsnajdr #62647 This PR is blocked because of this #62505 (comment) We need to find a good API/way to access/check permissions in dataviews while allowing global registration of actions... (Any suggestions here are welcome)

Maybe, we can try to land the current PR without 62647

@jsnajdr
Copy link
Member Author

jsnajdr commented Jun 26, 2024

OK I can try to rebase it onto trunk.

@jsnajdr jsnajdr changed the base branch from update/allow-removing-permanently-delete-action to trunk June 26, 2024 10:42
@jsnajdr jsnajdr marked this pull request as ready for review June 26, 2024 10:42
Copy link

github-actions bot commented Jun 26, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: sirreal <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@jsnajdr
Copy link
Member Author

jsnajdr commented Jun 26, 2024

OK now this PR is independent from #62647 and can be merged directly into trunk.

header: string;
getValue: ( args: { item: Item } ) => any;
render: ( args: { item: Item } ) => ReactNode;
};
Copy link
Member Author

Choose a reason for hiding this comment

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

I had to do this because the Required<Pick> construct doesn't work with the conditional type where getValue is defined twice.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is actually nicer and easier to understand

@jsnajdr jsnajdr enabled auto-merge (squash) June 26, 2024 11:04
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

Nice simplification here, thank you. I did leave one question I'd like to understand before this is merged.

@jsnajdr jsnajdr merged commit 2e41a46 into trunk Jun 27, 2024
64 checks passed
@jsnajdr jsnajdr deleted the try/remove-anyitem branch June 27, 2024 06:34
@github-actions github-actions bot added this to the Gutenberg 18.8 milestone Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Data Views Work surrounding upgrading and evolving views in the site editor and beyond [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants