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

When switching themes switch the figma native theme mode selector if possible #2336

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

Conversation

robinhoodie0823
Copy link
Contributor

@robinhoodie0823 robinhoodie0823 commented Oct 25, 2023

20231025_133845.mp4

Closes #2187

@changeset-bot
Copy link

changeset-bot bot commented Oct 25, 2023

🦋 Changeset detected

Latest commit: 21f2ae5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tokens-studio/figma-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

PR Analysis

  • 🎯 Main theme: The main theme of this PR is to switch the Figma native theme mode selector when switching themes in the plugin.
  • 📝 PR summary: This PR introduces changes to the AsyncMessageChannel and setActiveTheme files. It adds functionality to switch the Figma native theme mode selector when switching themes in the plugin. It also updates the state when a new active theme is set.
  • 📌 Type of PR: Enhancement
  • 🧪 Relevant tests added: No
  • ⏱️ Estimated effort to review [1-5]: 3, The PR introduces a new feature and modifies the state management, which requires a good understanding of the existing codebase and the new feature being introduced. However, the changes are not extensive and should be manageable to review.
  • 🔒 Security concerns: No

PR Feedback

  • 💡 General suggestions: The code changes seem to be well implemented, but it would be beneficial to add error handling for the new code blocks. Also, it would be good to consider the performance implications of the forEach loop on figma.currentPage.children.

  • 🤖 Code feedback:

    • relevant file: src/AsyncMessageChannel.ts
      suggestion: Consider adding error handling for the new code block. If there's an error while setting the explicit variable mode for a collection, it should be caught and handled appropriately. [important]
      relevant line: figma.currentPage.children.forEach((frame) => {

    • relevant file: src/AsyncMessageChannel.ts
      suggestion: The forEach loop on figma.currentPage.children could potentially be expensive if there are many children. Consider if there's a more efficient way to achieve the same result. [medium]
      relevant line: figma.currentPage.children.forEach((frame) => {

How to use

To invoke the PR-Agent, add a comment using one of the following commands:
/review [-i]: Request a review of your Pull Request. For an incremental review, which only considers changes since the last review, include the '-i' option.
/describe: Modify the PR title and description based on the contents of the PR.
/improve [--extended]: Suggest improvements to the code in the PR. Extended mode employs several calls, and provides a more thorough feedback.
/ask <QUESTION>: Pose a question about the PR.
/update_changelog: Update the changelog based on the PR's contents.

To edit any configuration parameter from configuration.toml, add --config_path=new_value
For example: /review --pr_reviewer.extra_instructions="focus on the file: ..."
To list the possible configuration parameters, use the /config command.

@@ -122,6 +122,16 @@ export class AsyncMessageChannel {
this.attachMessageListener((msg: IncomingMessageEvent<AsyncMessageResults & { type: Message['type'] }>['data']['pluginMessage']) => {
if (msg.id === messageId) {
if ('message' in msg) {
if (msg.message.type === 'async/get-theme-info') {
if ('activeTheme' in msg.message && 'themes' in msg.message) {
const { activeTheme } = msg.message;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const { activeTheme } = msg.message;
const { activeTheme, themes } = msg.message;

if (msg.message.type === 'async/get-theme-info') {
if ('activeTheme' in msg.message && 'themes' in msg.message) {
const { activeTheme } = msg.message;
const { themes } = msg.message;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const { themes } = msg.message;
const themeToFind = Object.values(activeTheme)[0];

if ('activeTheme' in msg.message && 'themes' in msg.message) {
const { activeTheme } = msg.message;
const { themes } = msg.message;
const activeMode = themes.filter((theme) => theme.id === Object.values(activeTheme)[0])[0];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const activeMode = themes.filter((theme) => theme.id === Object.values(activeTheme)[0])[0];
const activeMode = themes.find((theme) => theme.id === themeToFind);

@SorsOps
Copy link
Member

SorsOps commented Oct 26, 2023

Can you add a changeset so we can inform the users about this change?

Check to see if the fixes for the lookup fix the OOM error, otherwise we'll have to double check why that's occurring

@six7
Copy link
Collaborator

six7 commented Nov 10, 2023

Tests are failing here

…into 2187-when-switching-themes-switch-the-figma-native-theme-mode-selector-if-possible
…into 2187-when-switching-themes-switch-the-figma-native-theme-mode-selector-if-possible
Copy link
Contributor

github-actions bot commented Jan 10, 2024

Commit SHA:4c507f93be36defe7933184695662c0f75d72629

Test coverage results 🧪

Code coverage diff between base branch:main and head branch: 2187-when-switching-themes-switch-the-figma-native-theme-mode-selector-if-possible 
Status File % Stmts % Branch % Funcs % Lines
🔴 total 70.73 (0.01) 61.08 (-0.02) 68.84 (-0.02) 70.98 (0.01)
🔴 src/AsyncMessageChannel.ts 94.91 (-3.09) 82.6 (-14.27) 92.85 (-7.15) 94.64 (-3.27)

Copy link
Contributor

github-actions bot commented Jan 10, 2024

Commit SHA:4c507f93be36defe7933184695662c0f75d72629
Current PR reduces the test coverage percentage by 100 for some tests

@@ -0,0 +1,5 @@
---
Copy link
Member

Choose a reason for hiding this comment

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

This looks redundant?

const activeMode = themes.find((theme) => theme.id === themeToFind);
if (figma.currentPage.children && figma.currentPage.children.length > 0) {
figma.currentPage.children.forEach((frame) => {
frame.setExplicitVariableModeForCollection(activeMode?.$figmaCollectionId ?? '', activeMode?.$figmaModeId ?? '');
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we be using an if around this and checking that the string id's exist? What happens when we these are invalid or null?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah it could be that the information is outdated and then this will error. they'll likely not be invalid or null but it could be that it doesnt exist anymore causing an error.

let's check for if a) those are set and b) wrap so that we dont crash

@@ -122,6 +122,18 @@ export class AsyncMessageChannel {
this.attachMessageListener((msg: IncomingMessageEvent<AsyncMessageResults & { type: Message['type'] }>['data']['pluginMessage']) => {
if (msg.id === messageId) {
if ('message' in msg) {
if (msg.message.type === 'async/get-theme-info') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

i dont think this belongs here. this file doesnt have any of the other message handlers, this needs to go elsewhere where we define those.

switch (pluginMessage.type) {

this feels like the right place

const activeMode = themes.find((theme) => theme.id === themeToFind);
if (figma.currentPage.children && figma.currentPage.children.length > 0) {
figma.currentPage.children.forEach((frame) => {
frame.setExplicitVariableModeForCollection(activeMode?.$figmaCollectionId ?? '', activeMode?.$figmaModeId ?? '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah it could be that the information is outdated and then this will error. they'll likely not be invalid or null but it could be that it doesnt exist anymore causing an error.

let's check for if a) those are set and b) wrap so that we dont crash

const themeToFind = Object.values(activeTheme)[0];
const activeMode = themes.find((theme) => theme.id === themeToFind);
if (figma.currentPage.children && figma.currentPage.children.length > 0) {
figma.currentPage.children.forEach((frame) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this ignores the issue requirement completely and sets an active variable mode for every frame on the current page. this is not what we need. we need to apply way more logic here than that.

#2187

the issue states that it should have different behavior depending on the apply mode

if (msg.message.type === 'async/get-theme-info') {
if ('activeTheme' in msg.message && 'themes' in msg.message) {
const { activeTheme, themes } = msg.message;
const themeToFind = Object.values(activeTheme)[0];
Copy link
Collaborator

Choose a reason for hiding this comment

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

we do not just want to switch the first theme, but all theme groups that are active

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.

When switching Themes, switch the Figma native theme mode selector if possible
3 participants