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

Improve pagination documentation #9488

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

Conversation

ManorSailor
Copy link
Contributor

@ManorSailor ManorSailor commented Sep 25, 2024

Description (required)

Following the discussion at #9442; This PR does the following:

  1. Adds type info to Page prop.
    • As per Page page is a generic type. As a tradeoff, TData
    • Where should we mention TData is of type any? Type? Or in description? Should we mention it at all? TData is generic enough...
      name was decided to balance the documentation for TS & non-TS folks alike.
  2. Adds the same type to page.data property
  3. Adds type information for the getStaticPaths function
    • As the said function can be used not only for content collection, but also for data fetched via APIs... I believe adding it under the current section makes sense. I have a doubt though, how will it infer the types for data returned via API?
    • Added the block just above the caution block. Let me know how it looks.
  4. & 5. The paginate function's type definition was too complex to be easily explained in the documentation. Therefore, I thought of describing the function signature in the docs instead.
  5. Removed redundant Page type reference from the Routing guide
  6. As suggested by @ArmandPhilippot, each mention of CollectionEntry<collection> has been replaced with CollectionEntry<TCollectionName> as specified by its documentation.

Anyways. That's all. Feel free to provide any inputs!

Special thanks to @ArmandPhilippot for his detailed responses. It cleared a lot of confusion.

Related issues & labels (optional)

Copy link

netlify bot commented Sep 25, 2024

Deploy Preview for astro-docs-2 ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit b694fb6
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/66f6dee71d2e110008f130a3
😎 Deploy Preview https://deploy-preview-9488--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@astrobot-houston
Copy link
Contributor

astrobot-houston commented Sep 25, 2024

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

Locale File Note
en guides/routing.mdx Source changed, localizations will be marked as outdated.
en reference/api-reference.mdx Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@ManorSailor ManorSailor changed the title Patch api reference pagination Improve pagination documentation Sep 25, 2024
ManorSailor and others added 7 commits September 26, 2024 20:08
As per https://github.com/withastro/astro/blob/8d4eb95086ae79339764d02215f84f4f21b96edc/packages/astro/src/%40types/astro.ts#L2804
`page` is a generic type. As a tradeoff, `TData`
name was decided to balance the documentation for TS & non-TS folks alike.
The `paginate` function's type definition was too complex to be easily explained in the documentation.
Hence, this commit simplifies things by just describing the expected arguments.
This makes the documentation easier to follow while still providing the necessary information.
Some parts of the docs were using `CollectionEntry<collection>`
while others were using `TCollectionName`.
This commit cleans things up by sticking to `TCollectionName`,
making everything consistent and easier to follow.
@ManorSailor ManorSailor marked this pull request as ready for review September 26, 2024 14:45
@@ -459,41 +459,6 @@ const { page } = Astro.props;
{page.url.last ? <a href={page.url.last}>Last</a> : null}
```


#### Complete API reference
Copy link
Member

@sarah11918 sarah11918 Sep 26, 2024

Choose a reason for hiding this comment

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

While I agree with not having this on the routing page, do you think we lose anything by not putting this e.g. right before all the individual reference items? I mean, yeah it's nice that they are all listed out individually, but many of these do fall into the "self-explanatory" category, so only needing to glance at this to know what all's available seems helpful? Otherwise, it's scrolling through 3 pages on my monitor to see everything.

otherwise, I might be tempted to leave something like this here, even if not claiming to be fully typed, but more to show available properties?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You raise a good point. We lose the convenience of glancing at all of the available properties, & having to navigate through the wall of text in the API Reference section... I agree that most of the properties are self explanatory. I can imagine that the some users may find it inconvenient to scroll all that just to see the available properties.

Considering the repercussions, keeping that block there seems warranted even though it adds to the maintenance costs.... Although, it still irks me. 😓 Surely, we can do better....

In hindsight, I realize that the actual purpose of that code block there was to serve as an At a glance section to showcase all of the supported properties by Page.

I was going to suggest that we simplify the type to a block which only mentions all of the available properties, but I realize that it would be much more work than simply copy-pasting the updated type from the codebase.

Should I revert back to its original state then?

Copy link
Member

Choose a reason for hiding this comment

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

I made a commit with an example of what I think could be a helpful structure to work from. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

though, we did lose this text which might go in the block itself for data

array containing the page’s slice of data that you passed to the paginate() function

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems we are coming back to #8929 (comment) 😅 So, it's probably better to leave the block here. And, I agree with Sarah, we could replace result which is not descriptive enough with the sentence quoted!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that adding the description for data property in the block would work perfectly fine.

@@ -1116,6 +1116,10 @@ The `getStaticPaths()` function should return an array of objects to determine w

It can also be used in static file endpoints for [dynamic routing](/en/guides/endpoints/#params-and-dynamic-routing).

:::tip
When using TypeScript, use the [`InferGetStatic*`](/en/guides/typescript/#infer-getstaticpaths-types) type utilities to ensure type-safe access of your `params` and `props`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little late, I was waiting until the PR was no longer a draft to comment.

InferGetStatic does not exist and I don't think the asterisk is understandable to everyone. It seems to me that GetStaticPaths is enough to have both Astro.params and Astrop.props inferred properly. The two other helpers (InferGetStaticParamsType and InferGetStaticPropsType) can be useful in some cases but I don't think they are required here.
So I would suggest:

Suggested change
When using TypeScript, use the [`InferGetStatic*`](/en/guides/typescript/#infer-getstaticpaths-types) type utilities to ensure type-safe access of your `params` and `props`.
When using TypeScript, use the [`GetStaticPaths`](/en/guides/typescript/#infer-getstaticpaths-types) type utility to ensure type-safe access of your `params` and `props`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! How about the following phrasing:

When using TypeScript, follow the [Infer `getStaticPaths` types](/en/guides/typescript/#infer-getstaticpaths-types) guide to ensure type-safe access of your `params` and `props`.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not, all types are useful. If I insisted on GetStaticPaths it is perhaps because I find that this section of the Typescript guide seems to insist on the other two types (InferGetStaticParamsType and InferGetStaticPropsType) when only one (GetStaticPaths) is enough for most cases. But that might just be my perception...
So I'm fine with your suggestion, if you think it's more helpful!

Copy link
Contributor Author

@ManorSailor ManorSailor Sep 27, 2024

Choose a reason for hiding this comment

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

After looking into it, it appears that GetStaticPaths is sufficient for type safety. However, this raises the question of why there are additional type utilities included. Additionally, that section does not highlight the line where we add satisfies GetStaticPaths; it only mentions the InferGetStatic* types. It seems like there’s a new area for improvement here? Though I believe it falls outside the scope of this PR.

Considering this, I will go with your suggestion. It seems satisfactory.

Copy link
Contributor

Choose a reason for hiding this comment

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

Um yes I think there is a missing line among those highlighted. Maybe it was forgotten because the opening parenthesis is also important in this case. As for the usefulness of the other types... I don't know. I've never used them, but no doubt some people need them. Better excess types than missing types! 😄
But you're right, it doesn't fit into this PR.

@sarah11918
Copy link
Member

Just updated branch to re-trigger the Netlify deploy. We had some random wonkiness yesterday.

Copy link
Contributor

@ArmandPhilippot ArmandPhilippot left a comment

Choose a reason for hiding this comment

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

Some nitpicks about the wording. But I'm not the best person to judge so I'm not blocking the PR for that, LGTM!

@@ -494,6 +469,25 @@ interface Page<T = any> {
}
```

The following example template displays current information for the page along with links to navigate between pages:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure template is needed here... I feel like it makes the sentence heavier... (I don't know if it makes sense in English) but I'm not the best person to discuss the wording.

Suggested change
The following example template displays current information for the page along with links to navigate between pages:
The following example displays current information for the page along with links to navigate between pages:

@@ -1217,28 +1221,35 @@ export async function getStaticPaths({ paginate }) {
const { page } = Astro.props;
```

`paginate()` has the following arguments:
- `data` - Array of data to paginate
- `options` - Optional object with following properties:
Copy link
Contributor

@ArmandPhilippot ArmandPhilippot Sep 27, 2024

Choose a reason for hiding this comment

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

Suggested change
- `options` - Optional object with following properties:
- `options` - Optional object with the following properties:

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.

Improvements to Pagination Documentation: Type Information, Argument Descriptions, and Redundant Sections
4 participants