Skip to content

feat: Add methods to store & restore UI component state#3208

Open
tobezdev wants to merge 12 commits intoPycord-Development:masterfrom
tobezdev:master
Open

feat: Add methods to store & restore UI component state#3208
tobezdev wants to merge 12 commits intoPycord-Development:masterfrom
tobezdev:master

Conversation

@tobezdev
Copy link
Copy Markdown

Caution

Feature freeze is active, as described by our Release Schedule.

Summary

Addresses issue #2385 by adding methods to store and restore UI component states via new methods ui.View.to_component_instances() & ui.View.from_components(). The method View.from_components() has also been edited to preserve row indexes by using enumerate() rather than _walk_all_components().

Note: I have intentionally not updated any formal documentation (markdown files) as I do not believe this is required. Any new functions have docstrings explaining their exact behaviour (and their code is very readable anyways). Anything existing has not been fundamentally changed so there will be no difference in terms of how people will use those methods. Please let me know if a documentation change is required and I will be happy to add this into the PR.

The Copilot CLI was used in the development of this PR, although no code was generated by it. It was used simply to identify an issue on this repo that I could have an attempt at using the GitHub MCP server, and I used it to understand the structure and standards of the already-existing code (for example, the format of docstrings).

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.
  • I have updated the changelog to include these changes.
  • AI Usage has been disclosed.
    • If AI has been used, I understand fully what the code does

Add to_component_instances() and from_components() to View and DesignerView for component-instance state round-tripping.
When importing ActionRow component instances, assign the source row index to items before adding them so exported View layouts round-trip accurately.
Add View and DesignerView tests for to_component_instances()/from_components().
@tobezdev tobezdev requested review from a team and Copilot April 18, 2026 20:01
@github-project-automation github-project-automation Bot moved this to Todo in Pycord Apr 18, 2026
@tobezdev tobezdev requested a review from a team as a code owner April 18, 2026 20:01
@pycord-app
Copy link
Copy Markdown

pycord-app Bot commented Apr 18, 2026

Thanks for opening this pull request!
Please make sure you have read the Contributing Guidelines and Code of Conduct.

This pull request can be checked-out with:

git fetch origin pull/3208/head:pr-3208
git checkout pr-3208

This pull request can be installed with:

pip install git+https://github.com/Pycord-Development/pycord@refs/pull/3208/head

@tobezdev tobezdev changed the title Add method to store & restore UI component state (addresses #2385) Add method to store & restore UI component state Apr 18, 2026
@Paillat-dev Paillat-dev requested review from NeloBlivion and removed request for Copilot April 18, 2026 20:03
@tobezdev tobezdev changed the title Add method to store & restore UI component state feat: Add methods to store & restore UI component state Apr 18, 2026
Comment thread tests/ui/test_view_component_instances.py Outdated
Co-authored-by: Paillat <jeremiecotti@ik.me>
Signed-off-by: Toby <83095177+tobezdev@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 18, 2026 20:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a way to snapshot and restore discord.ui view state using component instances (rather than raw payload dicts), addressing the need described in #2385.

Changes:

  • Add to_component_instances() to View and DesignerView for exporting in-memory Component instances.
  • Add from_components() to View and DesignerView for restoring views from component instances (with row preservation for legacy View ActionRows).
  • Add unit tests covering round-tripping for both View and DesignerView.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
discord/ui/view.py Introduces component-instance export/import APIs for View and DesignerView.
tests/ui/test_view_component_instances.py Adds round-trip tests for the new APIs and confirms existing dict-based behavior remains unchanged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread discord/ui/view.py Outdated
Comment thread tests/ui/test_view_component_instances.py
@Lulalaby Lulalaby added the hold: changelog This pull request is missing a changelog entry label Apr 18, 2026
@tobezdev tobezdev requested a review from a team as a code owner April 18, 2026 20:25
@tobezdev tobezdev requested review from a team and EmmmaTech April 18, 2026 20:25
Copy link
Copy Markdown
Member

@NeloBlivion NeloBlivion left a comment

Choose a reason for hiding this comment

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

Misc.

  • This should target the base ItemInterface class to support modals as well.
  • I'm not sure if a testing implementation is necessary here considering we do not have any tests implemented besides voice
  • This should be considered after #3093 is merged

Comment thread discord/ui/view.py Outdated
Comment thread discord/ui/view.py
Comment thread discord/ui/view.py
Comment thread discord/ui/view.py Outdated
tobezdev and others added 5 commits April 18, 2026 22:35
Add to_component_instances() method which returns the underlying instances from a list of components
to_component_instances() now targets the shared base layer & added on ItemInterface using each item’s _generate_underlying(), and exposed from BaseView so DesignerView no longer duplicates it
Adjusted _generate_underlying() to allow already-backed items and allows usage on ItemInterface
Nelo asked for support for `ItemInterface` so dont ask me ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hold: changelog This pull request is missing a changelog entry

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

5 participants