Skip to content

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

Closed
ultraprinse wants to merge 12 commits intomainfrom
2187-when-switching-themes-switch-the-figma-native-theme-mode-selector-if-possible
Closed

When switching themes switch the figma native theme mode selector if possible#2336
ultraprinse wants to merge 12 commits intomainfrom
2187-when-switching-themes-switch-the-figma-native-theme-mode-selector-if-possible

Conversation

@ultraprinse
Copy link
Copy Markdown
Contributor

@ultraprinse ultraprinse commented Oct 25, 2023

20231025_133845.mp4

Closes #2187

@changeset-bot
Copy link
Copy Markdown

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

@ultraprinse ultraprinse self-assigned this Oct 25, 2023
@ultraprinse ultraprinse requested review from SorsOps and six7 October 25, 2023 17:39
@github-actions
Copy link
Copy Markdown
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.

Comment thread src/AsyncMessageChannel.ts Outdated
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
Copy Markdown
Contributor

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;

Comment thread src/AsyncMessageChannel.ts Outdated
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
Copy Markdown
Contributor

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];

Comment thread src/AsyncMessageChannel.ts Outdated
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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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

@ultraprinse ultraprinse requested a review from SorsOps November 7, 2023 13:17
@six7
Copy link
Copy Markdown
Collaborator

six7 commented Nov 10, 2023

Tests are failing here

hiroshi added 5 commits December 5, 2023 04:08
…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
@github-actions
Copy link
Copy Markdown
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)

@github-actions
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
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

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@six7 six7 closed this Sep 16, 2025
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