-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[core-data] Document and add types for dynamic actions and selectors. #67668
Changes from 17 commits
d06bc58
b3778a3
0b72ca3
3928817
9280cce
9f6b4e3
c583805
8bf0ea5
0de4023
0096e00
a6612e6
bea1374
5293486
105423a
1a45990
fa2a98b
5a09cec
1f5bd67
ca6550a
7b23643
465521b
a4fea0b
70f681e
1af6f51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { GetRecordsHttpQuery, State } from './selectors'; | ||
import type * as ET from './entity-types'; | ||
|
||
export type WPEntityTypes< C extends ET.Context = 'edit' > = { | ||
Comment: ET.Comment< C >; | ||
Media: ET.Attachment< C >; | ||
Menu: ET.NavMenu< C >; | ||
MenuItem: ET.NavMenuItem< C >; | ||
MenuLocation: ET.MenuLocation< C >; | ||
Plugin: ET.Plugin< C >; | ||
PostType: ET.Type< C >; | ||
Revision: ET.PostRevision< C >; | ||
Sidebar: ET.Sidebar< C >; | ||
Site: ET.Settings< C >; | ||
Status: ET.PostStatusObject< C >; | ||
Taxonomy: ET.Taxonomy< C >; | ||
Theme: ET.Theme< C >; | ||
UnstableBase: ET.UnstableBase< C >; | ||
User: ET.User< C >; | ||
Widget: ET.Widget< C >; | ||
WidgetType: ET.WidgetType< C >; | ||
}; | ||
|
||
/** | ||
* A simple utility that pluralizes a string. | ||
* Converts: | ||
* - "post" to "posts" | ||
* - "taxonomy" to "taxonomies" | ||
* - "media" to "mediaItems" | ||
* - "status" to "statuses" | ||
*/ | ||
type PluralizeEntity< T extends string > = T extends 'Media' | ||
? 'MediaItems' | ||
: T extends 'Status' | ||
? 'Statuses' | ||
: T extends `${ infer U }y` | ||
? `${ U }ies` | ||
: `${ T }s`; | ||
|
||
/** | ||
* A simple utility that singularizes a string. | ||
* | ||
* Converts: | ||
* - "posts" to "post" | ||
* - "taxonomies" to "taxonomy" | ||
* - "mediaItems" to "media" | ||
* - "statuses" to "status" | ||
*/ | ||
type SingularizeEntity< T extends string > = T extends 'MediaItems' | ||
? 'Media' | ||
: T extends 'Statuses' | ||
? 'Status' | ||
: T extends `${ infer U }ies` | ||
? `${ U }y` | ||
: T extends `${ infer U }s` | ||
? U | ||
: T; | ||
|
||
export type SingularGetters = { | ||
[ Key in `get${ keyof WPEntityTypes }` ]: ( | ||
state: State, | ||
id: number | string, | ||
query?: GetRecordsHttpQuery | ||
) => WPEntityTypes[ Key extends `get${ infer E }` ? E : never ] | undefined; | ||
}; | ||
|
||
export type PluralGetters = { | ||
[ Key in `get${ PluralizeEntity< keyof WPEntityTypes > }` ]: ( | ||
state: State, | ||
query?: GetRecordsHttpQuery | ||
) => Array< | ||
WPEntityTypes[ Key extends `get${ infer E }` | ||
? SingularizeEntity< E > | ||
: never ] | ||
> | null; | ||
}; | ||
|
||
type ActionOptions = { | ||
throwOnError?: boolean; | ||
}; | ||
|
||
type DeleteRecordsHttpQuery = Record< string, any >; | ||
|
||
export type SaveActions = { | ||
[ Key in `save${ keyof WPEntityTypes }` ]: ( | ||
manzoorwanijk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: Partial< | ||
WPEntityTypes[ Key extends `save${ infer E }` ? E : never ] | ||
>, | ||
options?: ActionOptions | ||
) => Promise< void >; | ||
}; | ||
|
||
export type DeleteActions = { | ||
[ Key in `delete${ keyof WPEntityTypes }` ]: ( | ||
id: number | string, | ||
query?: DeleteRecordsHttpQuery, | ||
options?: ActionOptions | ||
) => Promise< void >; | ||
}; | ||
|
||
export let dynamicActions: SaveActions & DeleteActions; | ||
|
||
export let dynamicSelectors: SingularGetters & PluralGetters; | ||
Comment on lines
+109
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Context, OmitNevers } from './helpers'; | ||
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records'; | ||
|
||
type TemplatePartArea = { | ||
area: string; | ||
label: string; | ||
icon: string; | ||
description: string; | ||
}; | ||
|
||
type TemplateType = { | ||
title: string; | ||
description: string; | ||
slug: string; | ||
}; | ||
|
||
declare module './base-entity-records' { | ||
export namespace BaseEntityRecords { | ||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ | ||
export interface Base< C extends Context > { | ||
/** | ||
* Site description. | ||
*/ | ||
description: string; | ||
|
||
/** | ||
* GMT offset for the site. | ||
*/ | ||
gmt_offset: string | number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't it always be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that is correct. I don't remember why I set it as a number in the first place. |
||
|
||
/** | ||
* Home URL. | ||
*/ | ||
home: string; | ||
|
||
/** | ||
* Site title | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* Site icon ID. | ||
*/ | ||
site_icon?: number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirming that regardless of its |
||
|
||
/** | ||
* Site icon URL. | ||
*/ | ||
site_icon_url: string; | ||
|
||
/** | ||
* Site logo ID. | ||
*/ | ||
site_logo?: number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirming that regardless of its |
||
|
||
/** | ||
* Site timezone string. | ||
*/ | ||
timezone_string: string; | ||
|
||
/** | ||
* Site URL. | ||
*/ | ||
url: string; | ||
|
||
/** | ||
* Default template part areas. | ||
*/ | ||
default_template_part_areas?: Array< TemplatePartArea >; | ||
|
||
/** | ||
* Default template types | ||
*/ | ||
default_template_types?: Array< TemplateType >; | ||
} | ||
} | ||
} | ||
|
||
export type Base< C extends Context = 'edit' > = OmitNevers< | ||
_BaseEntityRecords.Base< C > | ||
>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ import type { NavMenuItem } from './nav-menu-item'; | |
import type { Page } from './page'; | ||
import type { Plugin } from './plugin'; | ||
import type { Post } from './post'; | ||
import type { PostStatusObject } from './post-status'; | ||
import type { Base } from './base'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: might make sense to keep the alphabetic order here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks |
||
import type { PostRevision } from './post-revision'; | ||
import type { Settings } from './settings'; | ||
import type { Sidebar } from './sidebar'; | ||
|
@@ -27,6 +29,7 @@ export type { BaseEntityRecords } from './base-entity-records'; | |
|
||
export type { | ||
Attachment, | ||
Base as UnstableBase, | ||
Comment, | ||
Context, | ||
GlobalStylesRevision, | ||
|
@@ -36,6 +39,7 @@ export type { | |
Page, | ||
Plugin, | ||
Post, | ||
PostStatusObject, | ||
PostRevision, | ||
Settings, | ||
Sidebar, | ||
|
@@ -84,6 +88,7 @@ export type { | |
*/ | ||
export interface PerPackageEntityRecords< C extends Context > { | ||
core: | ||
| Base< C > | ||
| Attachment< C > | ||
| Comment< C > | ||
| GlobalStylesRevision< C > | ||
|
@@ -93,6 +98,7 @@ export interface PerPackageEntityRecords< C extends Context > { | |
| Page< C > | ||
| Plugin< C > | ||
| Post< C > | ||
| PostStatusObject< C > | ||
| PostRevision< C > | ||
Comment on lines
+103
to
104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, with regards to keeping the alphabetic order There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks |
||
| Settings< C > | ||
| Sidebar< C > | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { Context, OmitNevers } from './helpers'; | ||
import type { BaseEntityRecords as _BaseEntityRecords } from './base-entity-records'; | ||
|
||
declare module './base-entity-records' { | ||
export namespace BaseEntityRecords { | ||
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */ | ||
export interface PostStatusObject< C extends Context > { | ||
/** | ||
* The title for the status. | ||
*/ | ||
name: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't most (if not all) of these actually optional? See: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's the same for user object but those fields are actually present because those are actually present in the item schema. |
||
|
||
/** | ||
* Whether posts with this status should be private. | ||
*/ | ||
private: boolean; | ||
|
||
/** | ||
* Whether posts with this status should be protected. | ||
*/ | ||
protected: boolean; | ||
|
||
/** | ||
* Whether posts of this status should be shown in the front end of the site. | ||
*/ | ||
public: boolean; | ||
|
||
/** | ||
* Whether posts with this status should be publicly-queryable. | ||
*/ | ||
queryable: boolean; | ||
|
||
/** | ||
* Whether to include posts in the edit listing for their post type. | ||
*/ | ||
show_in_list: boolean; | ||
|
||
/** | ||
* An alphanumeric identifier for the status. | ||
*/ | ||
slug: string; | ||
|
||
/** | ||
* Whether posts of this status may have floating published dates. | ||
*/ | ||
date_floating: boolean; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Statuses can also have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have links defined in any of the objects here. |
||
} | ||
} | ||
|
||
export type PostStatusObject< C extends Context = 'edit' > = OmitNevers< | ||
_BaseEntityRecords.Type< C > | ||
>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { | |
__experimentalVStack as VStack, | ||
} from '@wordpress/components'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import type { UnstableBase } from '@wordpress/core-data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useState } from '@wordpress/element'; | ||
|
@@ -52,13 +53,6 @@ type CreateTemplatePartModalContentsProps = { | |
defaultTitle?: string; | ||
}; | ||
|
||
type TemplatePartArea = { | ||
area: string; | ||
label: string; | ||
icon: string; | ||
description: string; | ||
}; | ||
|
||
/** | ||
* A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part. | ||
* This component should not live in this package, it should be moved to a dedicated package responsible for managing template. | ||
|
@@ -73,7 +67,6 @@ export default function CreateTemplatePartModal( { | |
} & CreateTemplatePartModalContentsProps ) { | ||
const defaultModalTitle = useSelect( | ||
( select ) => | ||
// @ts-expect-error getPostType is not typed with 'wp_template_part' as argument. | ||
select( coreStore ).getPostType( 'wp_template_part' )?.labels | ||
?.add_new_item, | ||
[] | ||
|
@@ -135,9 +128,8 @@ export function CreateTemplatePartModalContents( { | |
|
||
const defaultTemplatePartAreas = useSelect( | ||
( select ) => | ||
// @ts-expect-error getEntityRecord is not typed with unstableBase as argument. | ||
select( coreStore ).getEntityRecord< { | ||
default_template_part_areas: Array< TemplatePartArea >; | ||
default_template_part_areas: UnstableBase[ 'default_template_part_areas' ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to export There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks |
||
} >( 'root', '__unstableBase' )?.default_template_part_areas, | ||
[] | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these all the entity types? Looks like Global Styles are missing for some reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for some reason, Global Styles seems to be broken. It always returns 404.
await wp.data.resolveSelect('core').getGlobalStylesVariations()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 1f5bd67
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how the plural one is supposed to work 🤔 Return all entries from the
wp_global_styles
post type? I'm not sure this will ever be needed. Maybe the plural just needs to be disabled?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that plural IS actually available on runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have disabled that plural for now.