Skip to content

fix(mobile): optimize translation query #3450

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged

Conversation

hyoban
Copy link
Member

@hyoban hyoban commented Apr 10, 2025

Description

PR Type

  • Feature
  • Bugfix
  • Hotfix
  • Other (please describe):

Screenshots (if UI change)

Demo Video (if new feature)

Linked Issues

Additional context

Changelog

  • I have updated the changelog/next.md with my changes.

Copy link

vercel bot commented Apr 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
follow ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 10, 2025 6:07am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
follow-external-ssr ⬜️ Ignored (Inspect) Visit Preview Apr 10, 2025 6:07am

@follow-reviewer-bot
Copy link

Suggested PR Title:

feat(translation): enhance translation services and hooks

Change Summary:
This pull request adds a new translation library for mobile, refactors translation hooks, and integrates prefetching for translations in various entry list components. It includes a significant update to the action language mapping, improving user experience by optimizing language checks and dynamically fetching translations based on content.

Code Review:

Code Review: Change Requests

  1. apps/desktop/src/renderer/src/lib/translate.ts

    • Lines 3 and 17: The type SupportedActionLanguage has replaced SupportedLanguages. Ensure that SupportedActionLanguage covers all the cases that were handled by SupportedLanguages. If these are different types, it could lead to potential type mismatches.
  2. apps/mobile/src/lib/translation.ts

    • Line 7: In the duplicateIfLengthLessThan function, consider handling edge cases where the text parameter might be an empty string, as text.repeat() will throw an error if text is empty.
    • Line 22: The ACTION_LANGUAGE_MAP[language].code is accessed without validation that the language key exists in ACTION_LANGUAGE_MAP. Add a fallback or validation to ensure stability in unforeseen cases.
  3. apps/mobile/src/modules/entry-content/EntryContentHeaderRightActions.tsx

    • Lines 131–134: The toggleAITranslation function uses the getGeneralSettings().actionLanguage cast as SupportedLanguages, but SupportedLanguages seems to have been replaced with SupportedActionLanguage elsewhere. Update the cast to the latest type to maintain consistency.
  4. apps/mobile/src/modules/entry-list/EntryListContentSocial.tsx, EntryListContentArticle.tsx, EntryListContentPicture.tsx, EntryListContentVideo.tsx

    • Lines in each of these files where usePrefetchEntryTranslation() is used (e.g., Line 35 in EntryListContentArticle.tsx): Ensure that viewableItems.map((item) => item.key) always resolves to valid IDs and is not undefined or null. Add validation before mapping to avoid runtime errors.
  5. apps/mobile/src/modules/entry-list/hooks.ts

    • Line 30: viewableItems state is updated inside the useNonReactiveCallback but is directly used in a useMemo dependency array (Line 89). This could lead to stale references if viewableItems changes. Consider validating potential re-renders or using a useRef to track it without causing unnecessary re-renders.
  6. apps/mobile/src/screens/(stack)/entries/[entryId]/index.tsx

    • Line 29: In usePrefetchEntryTranslation([entryId], true), ensure entryId is always defined, as spreading an undefined or invalid array could break the query.
  7. apps/mobile/src/store/translation/hooks.ts

    • Lines 4–7: usePrefetchEntryTranslation filters entry IDs based on certain conditions. However, it currently assumes that entryList has valid IDs (filter on translation || !!entry.settings.translation). Add extra validation to filter out undefined or invalid IDs before proceeding to map to queries.
    • Lines 14–15: The useQueries implementation lacks an enabled flag, which means queries could unintentionally fire even when unnecessary. Add an enabled condition based on the entryList length to optimize API calls.
  8. apps/mobile/src/store/translation/store.ts

    • Line 66: In the generateTranslation function, when calling checkLanguage (Line 78), make sure that all required fields are properly sanitized or defaulted to prevent throwing errors in edge cases.
    • Lines 70–79: The fields array is dynamically created, but the checkLanguage function is tightly coupled to its contents. A safeguard on unsupported fields or a fallback mechanism should be implemented to handle unexpected input variations.
  9. General Observations:

    • Verify that modular components like usePrefetchEntryTranslation are properly unit tested, especially given their extensive usage across modules.
    • With multiple additions and edits to the files, ensure that thorough integration testing is conducted to confirm interaction points between modules remain intact and functional.

Take action on these identified issues to improve code stability, maintainability, and runtime performance.

@hyoban hyoban merged commit 8dd6d5c into dev Apr 10, 2025
14 of 16 checks passed
@hyoban hyoban deleted the fix/04-10-tranlate-query branch April 10, 2025 06:05
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.

1 participant