Skip to content

Conversation

@Anty0
Copy link
Collaborator

@Anty0 Anty0 commented Dec 18, 2025

Fixes #3353

Summary by CodeRabbit

  • Refactor
    • Improved machine translation settings validation to evaluate each target language using its specific language info, enhancing accuracy of service applicability checks and null-safety during updates.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 18, 2025

Walkthrough

Updated Machine Translation settings validation to use full language info objects per target language during validation and updates, ensuring each settings entry is validated against its specific language's supported services rather than a shared language ID lookup.

Changes

Cohort / File(s) Summary
Machine Translation validation logic
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
Replaced isSupported(languageId) usage with isSupported(langInfo) and changed validateService to accept langInfo. In applyUpdate, resolve targetLangInfo per updated settings entry and use it to validate primaryServiceInfo and enabledServicesInfo, preserving per-language validation context and adjusting null/undefined guards accordingly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Check targetLangInfo resolution for missing language IDs and fallback behavior
  • Verify the updated validateService and isSupported calls are consistently passed the correct langInfo
  • Confirm null/undefined guards and non-null assertions are safe in all update paths

Poem

🐰 I hopped through code by moonlit logs,

Saved each tongue from settings' fogs,
Now every language keeps its place,
No more lost config in the race,
Hooray — each setting holds its space!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically identifies the main fix: using correct languageInfo when validating MT settings, which directly addresses the bug in issue #3353.
Linked Issues check ✅ Passed The code changes directly address the root cause of issue #3353 by fixing the validation logic to use the correct languageInfo (targetLangInfo) for each target language instead of a shared outer scope variable.
Out of Scope Changes check ✅ Passed All changes are focused on the Machine Translation settings validation logic in a single file with no unrelated modifications detected.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch jirikuchynka/use-correct-lang-info-for-validating-mt-settings

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9e3222b and 82f04f4.

📒 Files selected for processing (1)
  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
webapp/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

webapp/**/*.{ts,tsx}: Use TypeScript path aliases (tg.component/, tg.service/, tg.hooks/, tg.views/, tg.globalContext/*) instead of relative imports
After backend API changes, regenerate TypeScript types by running npm run schema (and npm run billing-schema if applicable) in the webapp directory
Use typed React Query hooks from useQueryApi.ts for API communication, not raw React Query
Use Tolgee-specific hooks useReportEvent and useReportOnce from 'tg.hooks/useReportEvent' for business event tracking and analytics

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
webapp/**/*.tsx

📄 CodeRabbit inference engine (AGENTS.md)

Always use data-cy attributes for E2E test selectors, never rely on text content

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
**/*.{ts,tsx,js,jsx,kt,kts}

⚙️ CodeRabbit configuration file

As part of review, please check if the file follows 'The Stepdown Rule': The most important, high-level concepts should be at the top (like a newspaper headline and opening paragraph). Details should increase as you read downward. Functions should be ordered so that a caller appears before the functions it calls. When working with JavaScript components, we allow the main component to live at the bottom of the file as an exception to the rule.

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
🧬 Code graph analysis (1)
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (1)
webapp/src/views/projects/languages/MachineTranslation/types.ts (2)
  • LanguageInfoModel (6-6)
  • MtServiceInfo (16-16)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: BT ‍🔎 (server-app:runStandardTests)
  • GitHub Check: BT ‍🔎 (server-app:runWebsocketTests)
  • GitHub Check: BT ‍🔎 (server-app:runWithoutEeTests)
  • GitHub Check: BT ‍🔎 (data:test)
  • GitHub Check: BT ‍🔎 (server-app:runContextRecreatingTests)
  • GitHub Check: BT ‍🔎 (ee-test:test)
  • GitHub Check: BT ‍🔎 (ktlint:test)
  • GitHub Check: BT ‍🔎 (security:test)
  • GitHub Check: Frontend static check 🪲
  • GitHub Check: Build frontend 🏗️
🔇 Additional comments (3)
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (3)

49-62: LGTM! Simplified validation logic.

The refactored isSupported function correctly validates services against language-specific info without requiring ID lookups. The graceful handling of undefined langInfo (treating it as "all services supported") is appropriate for global settings with targetLanguageId: null.


64-82: LGTM! Consistent parameter update.

The validateService signature update correctly aligns with the isSupported changes. The undefined guards ensure invalid services are cleared, and the formality/promptId validation logic remains sound.


103-121: Excellent fix for the critical MT settings bug—validates each language against its own language info.

This commit fixes a regression introduced in Nov 2024 (commit 9b87e2f) where all machine translation settings were validated against a single langInfo parameter instead of each language's specific language information. The fix correctly resolves targetLangInfo for each language (lines 105-108) before validating both primaryServiceInfo and enabledServicesInfo, ensuring that saving settings for one language no longer corrupts or resets settings for other languages.

Edge case note: If targetLangInfo is not found (undefined), validateService will clear both primaryServiceInfo and enabledServicesInfo for that language. This defensive behavior gracefully handles removed languages or stale data, which is appropriate for this scenario.


Comment @coderabbitai help to get the list of available commands and usage tips.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (2)

49-62: Type annotation should reflect nullable parameter.

The function checks !langInfo on line 55, implying the parameter can be undefined, but the type annotation is LanguageInfoModel without | undefined. This creates a type safety gap where TypeScript won't warn callers about passing undefined.

🔎 Suggested fix:
 function isSupported(
   service: MtServiceInfo | undefined,
-  langInfo: LanguageInfoModel
+  langInfo: LanguageInfoModel | undefined
 ) {

99-117: Add defensive handling for missing targetLangInfo.

The .find() on line 102 can return undefined if targetLanguageId has no matching entry in languageInfos. Using non-null assertions (targetLangInfo!) masks this from TypeScript but doesn't prevent runtime issues.

When targetLangInfo is undefined, the !langInfo check in isSupported causes all services to be considered "supported", potentially saving invalid service configurations.

🔎 Suggested fix - filter entries without valid language info:
 const mtSettingsValidated = updatedMtSettings
+  .filter(({ targetLanguageId }) => {
+    // Default settings (null targetLanguageId) or settings with valid language info
+    return (
+      targetLanguageId === null ||
+      languageInfos.data?._embedded?.languageInfos?.some(
+        (l) => l.languageId === targetLanguageId
+      )
+    );
+  })
   .map(
     ({ targetLanguageId, primaryServiceInfo, enabledServicesInfo }) => {
       const targetLangInfo =
-        languageInfos.data!._embedded!.languageInfos!.find(
+        languageInfos.data?._embedded?.languageInfos?.find(
           (l) => l.languageId === targetLanguageId
         );

       return {
         targetLanguageId,
         primaryServiceInfo: validateService(
-          targetLangInfo!,
+          targetLangInfo,
           primaryServiceInfo
         ),
         enabledServicesInfo: enabledServicesInfo
-          ?.map((service) => validateService(targetLangInfo!, service))
+          ?.map((service) => validateService(targetLangInfo, service))
           .filter((service) => Boolean(service)) as MtServiceInfo[],
       };
     }
   );

This also requires updating validateService to accept LanguageInfoModel | undefined.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7c5f0a3 and 9e3222b.

📒 Files selected for processing (1)
  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (3 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
webapp/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

webapp/**/*.{ts,tsx}: Use TypeScript path aliases (tg.component/, tg.service/, tg.hooks/, tg.views/, tg.globalContext/*) instead of relative imports
After backend API changes, regenerate TypeScript types by running npm run schema (and npm run billing-schema if applicable) in the webapp directory
Use typed React Query hooks from useQueryApi.ts for API communication, not raw React Query
Use Tolgee-specific hooks useReportEvent and useReportOnce from 'tg.hooks/useReportEvent' for business event tracking and analytics

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
webapp/**/*.tsx

📄 CodeRabbit inference engine (AGENTS.md)

Always use data-cy attributes for E2E test selectors, never rely on text content

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
**/*.{ts,tsx,js,jsx,kt,kts}

⚙️ CodeRabbit configuration file

As part of review, please check if the file follows 'The Stepdown Rule': The most important, high-level concepts should be at the top (like a newspaper headline and opening paragraph). Details should increase as you read downward. Functions should be ordered so that a caller appears before the functions it calls. When working with JavaScript components, we allow the main component to live at the bottom of the file as an exception to the rule.

Files:

  • webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx
🧬 Code graph analysis (1)
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (1)
webapp/src/views/projects/languages/MachineTranslation/types.ts (1)
  • LanguageInfoModel (6-6)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: BT ‍🔎 (ktlint:test)
  • GitHub Check: BT ‍🔎 (security:test)
  • GitHub Check: BT ‍🔎 (server-app:runWebsocketTests)
  • GitHub Check: BT ‍🔎 (ee-test:test)
  • GitHub Check: BT ‍🔎 (server-app:runContextRecreatingTests)
  • GitHub Check: BT ‍🔎 (server-app:runStandardTests)
  • GitHub Check: BT ‍🔎 (data:test)
  • GitHub Check: BT ‍🔎 (server-app:runWithoutEeTests)
  • GitHub Check: Build frontend 🏗️
  • GitHub Check: Frontend static check 🪲
🔇 Additional comments (2)
webapp/src/views/projects/languages/MachineTranslation/useMachineTranslationSettings.tsx (2)

64-78: LGTM!

The validateService function correctly passes the langInfo object to isSupported, aligning with the updated signature.


80-97: Core fix correctly addresses the bug.

The approach of filtering out the current language's settings from existing settings and then re-adding the updated settings ensures that changes to one language don't affect other languages' configurations. This directly addresses issue #3353.

@Anty0 Anty0 force-pushed the jirikuchynka/use-correct-lang-info-for-validating-mt-settings branch from 9e3222b to 82f04f4 Compare December 18, 2025 18:54
@Anty0 Anty0 marked this pull request as draft December 18, 2025 19:16
@Anty0
Copy link
Collaborator Author

Anty0 commented Dec 18, 2025

I need to test this locally – something is failing in the E2E tests. I'd also like to add an E2E test for this.

@Anty0
Copy link
Collaborator Author

Anty0 commented Dec 18, 2025

(Probably after the new year.)

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.

Machine Translation settings reset when saving configuration for another language

2 participants