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

refactor: link types #367

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

refactor: link types #367

wants to merge 6 commits into from

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Dec 17, 2024

Resolves: https://prismic-team.slack.com/archives/C02L3FN3AJK/p1734036664049009?thread_ts=1734004341.679669&cid=C02L3FN3AJK

Description

This PR refactors how we handle link fields values.

Prismic is beginning to separate the link field type from the content relationship and link to media field types. Link fields can now be configured to include optional properties, like text and variant. We don't want these link-specific fields to appear in content relationship and link to media fields.

Before this PR, the LinkField and EmptyLinkField types were tightly coupled with the content relationship and link to media fields. The coupling made it difficult to add optional properties only to link fields.

In this PR, we decouple the fields so we can more easily add new properties. Link fields have the same shape as content relationship and link to media fields when a document or media item is linked, so there is still some connection.

Notable changes

  • EmptyLinkField, an exported type, has been corrected to always use { "link_type": "Any" }. Before this PR, the field could incorrectly be configured to use "Document", "Media", or "Web". It's possible those were valid values before the Page Builder.
  • The migration API now correctly includes optional link properties only on link fields, not content relationship or link to media fields.

How to add additional optional link properties

This refactoring should make it easier to add or modify optional link properties. These properties are scoped to link fields only and will not appear on content relationship or link to media fields.

  1. Add the optional property to the OptionalLinkProperties type.
  2. Conditionally add the optional property in src/lib/getOptionalLinkProperties.ts.
  3. Update tests.

See #368 for an example.

Checklist

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

How to QA 1

Footnotes

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a compliment.

@angeloashmore angeloashmore mentioned this pull request Dec 17, 2024
4 tasks
@angeloashmore angeloashmore marked this pull request as ready for review December 17, 2024 02:49
TDocuments | PrismicMigrationDocument<TDocuments> | undefined
>
})
| (Pick<ContentRelationshipField, "link_type"> & {
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ Why did you remove text here? It's used in resolveMigrationContentRelationship and from what I understood with Lucie will still be used while handling link to document that can be from a generic link.

Copy link
Member Author

Choose a reason for hiding this comment

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

text was removed because this type defines how users specify a content relationship value. text is invalid for content relationship fields.

Internally, we needed to widen the types after verifying the shape, not before. You can see the & OptionalLinkProperties in src/lib/isValue.ts and src/lib/isMigrationValue.ts.

These changes support the following behavior when migrating a document:

  • Content relationship fields will not include text or variant.
  • Link to media fields will not include variant. It includes text only because we explicitly added it to the field type. We can easily remove it for this field type only if we decide to do so.
  • Link fields include text and variant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hum, I'm not sure to get everything.

Content relationship fields will not include text or variant.

That I understand, but what's the difference with link to document? What I understood from Lucie is that the type is used also for migrating link to document implying that text and variant are supported.
There is no difference between link to document and content relationship behind the scene. Right?

Link to media fields will not include variant. It includes text only because we explicitly added it to the field type. We can easily remove it for this field type only if we decide to do so.

For me, it's either both text + variant or nothing. We are waiting for the product but meanwhile, I would go for both. (details we can take care of course)

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.

3 participants