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

fix: Support undefined text resource in text resource selector #14495

Merged
merged 5 commits into from
Feb 3, 2025

Conversation

TomasEng
Copy link
Contributor

@TomasEng TomasEng commented Jan 23, 2025

Description

Added support for undefined text resources in the text resource input component. This includes the following:

  • Support null and undefined values for the currentId prop
  • Make onChangeCurrentId call null when the user deconnects a text resource
  • Select search mode as default when no text resource is set
  • Disable the value field when no text resource is set

Default mode with no text resource:
image

Edit mode with no text resource:
image

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced text resource input component with improved ID handling
    • Added support for optional current ID in text resource management
  • Bug Fixes

    • Improved mode determination for text resource input
    • Fixed edge cases related to text resource ID handling
  • Tests

    • Expanded test coverage for text resource input component
    • Added new test scenarios for different ID states

The updates provide more flexible text resource input functionality with better error handling and mode selection.

Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-storybook".

(The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-storybook@latest --save-dev

The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-storybook".

(The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-storybook@latest --save-dev

The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-storybook".

(The package "eslint-plugin-storybook" was not found when loaded as a Node module from the directory "/frontend/libs/studio-components".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-storybook@latest --save-dev

The plugin "eslint-plugin-storybook" was referenced from the config file in "frontend/libs/studio-components/.eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

  • 2 others
📝 Walkthrough

Walkthrough

The pull request focuses on enhancing the StudioTextResourceInput component across multiple files. The changes introduce more flexible handling of the currentId prop, allowing for null and undefined values. A new utility function determineDefaultMode is added to manage the component's initial mode based on the current ID. The modifications improve the component's robustness by updating type definitions, test cases, and rendering logic to handle scenarios with and without a text resource.

Changes

File Change Summary
frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx - Renamed Preview story to WithId
- Added new WithoutId story
frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx - Added noCurrentIdCases constant
- Introduced switchToEditMode helper function
- Added new test cases for currentId scenarios
frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx - Updated currentId type to string | null
- Made textResource optional
- Added EnabledValueField and DisabledValueField components
frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.test.ts - Added test suite for determineDefaultMode function
frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.ts - Updated getTextResourceById return type
- Added determineDefaultMode function

Possibly related PRs

Suggested labels

quality/testing, skip-manual-testing

Suggested reviewers

  • Jondyr
  • lassopicasso
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added area/ui-editor Area: Related to the designer tool for assembling app UI in Altinn Studio. solution/studio/designer Issues related to the Altinn Studio Designer solution. frontend labels Jan 23, 2025
Copy link

codecov bot commented Jan 23, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.71%. Comparing base (80f442d) to head (a97deca).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14495   +/-   ##
=======================================
  Coverage   95.71%   95.71%           
=======================================
  Files        1902     1903    +1     
  Lines       24788    24797    +9     
  Branches     2839     2841    +2     
=======================================
+ Hits        23726    23735    +9     
  Misses        801      801           
  Partials      261      261           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

…t-resource-id

# Conflicts:
#	frontend/language/src/nb.json
#	frontend/libs/studio-components/src/components/StudioCodelistEditor/test-data/texts.ts
#	frontend/libs/studio-components/src/components/StudioInputTable/test-data/testTableData.ts
#	frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx
#	frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx
#	frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx
#	frontend/libs/studio-components/src/components/StudioTextResourceInput/types/TextResourceInputTexts.ts
#	frontend/libs/studio-components/src/components/StudioTextResourcePicker/StudioTextResourcePicker.stories.tsx
#	frontend/libs/studio-components/src/components/StudioTextResourcePicker/StudioTextResourcePicker.test.tsx
#	frontend/libs/studio-components/src/components/StudioTextResourcePicker/StudioTextResourcePicker.tsx
#	frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage/hooks/useCodeListEditorTexts.ts
#	frontend/packages/ux-editor/src/components/config/editModal/EditOptions/OptionTabs/hooks/useOptionListEditorTexts.ts
@TomasEng TomasEng added team/studio-domain1 skip-releasenotes Issues that do not make sense to list in our release notes skip-documentation Issues where updating documentation is not relevant labels Jan 28, 2025
@TomasEng TomasEng marked this pull request as ready for review January 29, 2025 06:07
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx (1)

53-53: Consider adding type conversion safety.

The currentId state management could benefit from explicit type handling.

-const [currentId, setCurrentId] = usePropState<string | null | undefined>(givenCurrentId);
+const [currentId, setCurrentId] = usePropState<string | null | undefined>(
+  givenCurrentId === '' ? null : givenCurrentId
+);

Also applies to: 55-55

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx (1)

93-104: Consider adding error case tests.

While the happy path is well tested, consider adding tests for error scenarios:

  • Invalid text resource format
  • Network errors in resource loading
frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx (1)

49-54: Consider adding more edge case stories.

While the basic cases are covered, consider adding stories for:

  • Error states
  • Loading states
  • Invalid text resource format
frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.test.ts (1)

59-73: Well-structured test suite with room for additional edge cases.

The test suite effectively covers the basic scenarios including empty string, null, and undefined values. Consider adding tests for these edge cases:

  • Whitespace-only strings (e.g., ' ', '\t', '\n')
  • Very long IDs
  • IDs with special characters
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1571041 and e2f8a3e.

📒 Files selected for processing (5)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx (2 hunks)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx (5 hunks)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx (4 hunks)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.test.ts (2 hunks)
  • frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build environment and run e2e test
  • GitHub Check: Testing
  • GitHub Check: CodeQL
🔇 Additional comments (8)
frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.ts (2)

5-8: LGTM! Good type safety improvement.

The updated return type TextResource | undefined better reflects the function's behavior when no matching resource is found.


30-32: LGTM! Clean implementation of mode determination.

The function is well-focused and handles null/undefined cases appropriately.

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.tsx (2)

26-26: LGTM! Type definitions align with requirements.

The updated types properly support undefined/null text resources.

Also applies to: 29-29


161-172: LGTM! Good component separation.

Clean separation of enabled/disabled states improves code organization and maintainability.

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.test.tsx (1)

38-42: LGTM! Comprehensive test cases for edge scenarios.

Good coverage of different null/undefined cases using test.each.

frontend/libs/studio-components/src/components/StudioTextResourceInput/StudioTextResourceInput.stories.tsx (1)

34-34: LGTM! Clear story naming.

Renaming to WithId provides better clarity about the story's purpose.

Also applies to: 48-48

frontend/libs/studio-components/src/components/StudioTextResourceInput/utils.test.ts (2)

1-6: LGTM! Clean import addition.

The new determineDefaultMode import is properly added alongside related utility functions.


59-73: Verify component behavior alignment.

Let's verify that the test cases align with how determineDefaultMode is used in the component:

✅ Verification successful

✓ Component behavior is correctly aligned

The test cases perfectly match how determineDefaultMode is used in the component, covering all possible input scenarios (valid ID, empty string, null, undefined) and their expected behavior (EditValue/Search modes).

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usage of determineDefaultMode in the component
echo "Searching for determineDefaultMode usage in StudioTextResourceInput component..."
rg -A 5 "determineDefaultMode" frontend/libs/studio-components/src/components/StudioTextResourceInput/

Length of output: 4441

@ErlingHauan ErlingHauan self-assigned this Jan 31, 2025
@ErlingHauan ErlingHauan removed their assignment Jan 31, 2025
@Konrad-Simso Konrad-Simso self-assigned this Feb 3, 2025
Copy link
Contributor

@Konrad-Simso Konrad-Simso left a comment

Choose a reason for hiding this comment

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

Tested locally in Storybook.

StudioTextResourceInput, spesifically it's child component StudioTextResourcePicker has 2 unexpected behaviours when removing the text resource key.

Removing the text resouce key results in "ikke oppgitt" being selected automatically when the first letter is removed, or if the user pauses while removing the line and continues to hold down backspace afterwords.

Here is an example:

Skjermopptak.2025-02-03.101207.mp4

I know this issue is not directly related to this PR as i've tested this on both this branch and main, it exists in both. However thought i should mention it here as it's an issue with removing the key and that is part of the goal with this PR.

@Konrad-Simso Konrad-Simso assigned TomasEng and unassigned Konrad-Simso Feb 3, 2025
@TomasEng
Copy link
Contributor Author

TomasEng commented Feb 3, 2025

Great testing, @Konrad-Simso! I'll have a look at the problem you found, but since it is not caused by this pull request, I suggest we merge it and create another one for fixing the bug. What do you think?

@Konrad-Simso
Copy link
Contributor

Konrad-Simso commented Feb 3, 2025

Sounds good

@TomasEng TomasEng merged commit 2b3dfb4 into main Feb 3, 2025
11 checks passed
@TomasEng TomasEng deleted the support-undefined-text-resource-id branch February 3, 2025 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ui-editor Area: Related to the designer tool for assembling app UI in Altinn Studio. frontend skip-documentation Issues where updating documentation is not relevant skip-releasenotes Issues that do not make sense to list in our release notes solution/studio/designer Issues related to the Altinn Studio Designer solution. team/studio-domain1
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants