-
Notifications
You must be signed in to change notification settings - Fork 112
(refactor) O3-4964 : Refactor Translation Builder - preserve edits & sync keys incrementally #825
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
Open
Bharath-K-Shetty
wants to merge
16
commits into
openmrs:main
Choose a base branch
from
Bharath-K-Shetty:fix/O3-4964
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
00df1a0
Refactor Translation Builder — preserve edits & sync keys incrementally
Bharath-K-Shetty ac3b41c
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty 778ead0
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty b0c9c56
Merge branch 'main' of https://github.com/Bharath-K-Shetty/openmrs-es…
Bharath-K-Shetty 5851466
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty ea25695
Merge branch 'main' of https://github.com/Bharath-K-Shetty/openmrs-es…
Bharath-K-Shetty 78122ec
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty c4fd84d
Merge branch 'main' of https://github.com/Bharath-K-Shetty/openmrs-es…
Bharath-K-Shetty bd785f4
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty 11add43
Merge branch 'main' into fix/O3-4964
NethmiRodrigo b4d9b39
Merge branch 'main' of https://github.com/Bharath-K-Shetty/openmrs-es…
Bharath-K-Shetty 30ceb82
Fixed the failing e2e tests
Bharath-K-Shetty 8e8c477
Merge branch 'fix/O3-4964' of https://github.com/Bharath-K-Shetty/ope…
Bharath-K-Shetty 411e4d0
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty 16d02e8
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty 69c696f
Merge branch 'main' into fix/O3-4964
Bharath-K-Shetty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,7 @@ interface TranslationBuilderProps { | |||||||||
| const TranslationBuilder: React.FC<TranslationBuilderProps> = ({ formSchema, onUpdateSchema }) => { | ||||||||||
| const { t } = useTranslation(); | ||||||||||
| const languageOptions = useLanguageOptions(); | ||||||||||
| const [localTranslations, setLocalTranslations] = useState<Record<string, Record<string, string>>>({}); | ||||||||||
| const [selectedLanguageCode, setSelectedLanguageCode] = useState(() => languageOptions[0]?.code ?? 'en'); | ||||||||||
| const [translations, setTranslations] = useState<Record<string, string>>({}); | ||||||||||
| const [loading, setLoading] = useState(false); | ||||||||||
|
|
@@ -29,6 +30,7 @@ const TranslationBuilder: React.FC<TranslationBuilderProps> = ({ formSchema, onU | |||||||||
| const [searchQuery, setSearchQuery] = useState(''); | ||||||||||
| const [debouncedQuery, setDebouncedQuery] = useState(searchQuery); | ||||||||||
| const { formUuid } = useParams<{ formUuid?: string }>(); | ||||||||||
|
|
||||||||||
| useEffect(() => { | ||||||||||
| const handler = setTimeout(() => { | ||||||||||
| setDebouncedQuery(searchQuery); | ||||||||||
|
|
@@ -42,10 +44,42 @@ const TranslationBuilder: React.FC<TranslationBuilderProps> = ({ formSchema, onU | |||||||||
| return formSchema ? extractTranslatableStrings(formSchema) : {}; | ||||||||||
| }, [formSchema]); | ||||||||||
|
|
||||||||||
| useEffect(() => { | ||||||||||
| if (!formSchema) return; | ||||||||||
|
|
||||||||||
| const fallback = fallbackStrings; | ||||||||||
| const current = translations; | ||||||||||
|
|
||||||||||
| const updated: Record<string, string> = {}; | ||||||||||
| Object.keys(fallback).forEach((key) => { | ||||||||||
| updated[key] = current[key] ?? fallback[key] ?? ''; | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| const changed = | ||||||||||
| Object.keys(updated).length !== Object.keys(current).length || | ||||||||||
| Object.keys(updated).some((k) => updated[k] !== current[k]); | ||||||||||
|
|
||||||||||
| if (changed) { | ||||||||||
| setTranslations(updated); | ||||||||||
| setLocalTranslations((prev) => ({ | ||||||||||
| ...prev, | ||||||||||
| [selectedLanguageCode]: updated, | ||||||||||
| })); | ||||||||||
|
|
||||||||||
| // Removed the onUpdateSchema call from here | ||||||||||
| // The schema should only be updated on user-initiated actions | ||||||||||
|
Comment on lines
+69
to
+70
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: We can remove this comment since it doesn't explain the functionality. Lets instead add a comment at the top of this useEffect to explain its purpose
Suggested change
|
||||||||||
| } | ||||||||||
| }, [formSchema, fallbackStrings, selectedLanguageCode, translations]); | ||||||||||
|
|
||||||||||
| const handleUpdateValue = useCallback( | ||||||||||
| (key: string, newValue: string) => { | ||||||||||
| const updatedTranslations = { ...translations, [key]: newValue }; | ||||||||||
| setTranslations(updatedTranslations); | ||||||||||
| setLocalTranslations((prev) => ({ | ||||||||||
| ...prev, | ||||||||||
| [langCode]: updatedTranslations, | ||||||||||
| })); | ||||||||||
|
|
||||||||||
| if (formSchema) { | ||||||||||
| const updatedSchema = { ...formSchema }; | ||||||||||
| if (!updatedSchema.translations) { | ||||||||||
|
|
@@ -108,22 +142,49 @@ const TranslationBuilder: React.FC<TranslationBuilderProps> = ({ formSchema, onU | |||||||||
| [fallbackStrings], | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| useEffect(() => { | ||||||||||
| if (selectedLanguageCode === 'en' && formSchema) { | ||||||||||
| setTranslations(fallbackStrings); | ||||||||||
| } | ||||||||||
| }, [selectedLanguageCode, formSchema, fallbackStrings]); | ||||||||||
| function mergeTranslations( | ||||||||||
| local: Record<string, string> = {}, | ||||||||||
| backend: Record<string, string> = {}, | ||||||||||
| fallback: Record<string, string> = {}, | ||||||||||
| ): Record<string, string> { | ||||||||||
| const merged: Record<string, string> = {}; | ||||||||||
| const allKeys = new Set([...Object.keys(fallback), ...Object.keys(backend), ...Object.keys(local)]); | ||||||||||
| allKeys.forEach((key) => { | ||||||||||
| if (local[key] !== undefined) { | ||||||||||
| merged[key] = local[key]; | ||||||||||
| } else if (backend[key] !== undefined) { | ||||||||||
| merged[key] = backend[key]; | ||||||||||
| } else { | ||||||||||
| merged[key] = fallback[key] ?? ''; | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| return merged; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const languageChanger = async (newLangCode: string) => { | ||||||||||
| setSelectedLanguageCode(newLangCode); | ||||||||||
| if (!formSchema || newLangCode === 'en') { | ||||||||||
| setTranslations(fallbackStrings); | ||||||||||
|
|
||||||||||
| if (!formSchema) { | ||||||||||
| setTranslations({}); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
| if (localTranslations[newLangCode]) { | ||||||||||
| setTranslations(localTranslations[newLangCode]); | ||||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| setLoading(true); | ||||||||||
| try { | ||||||||||
| const merged = await fetchBackendTranslations(formUuid, newLangCode, fallbackStrings); | ||||||||||
| let backend = await fetchBackendTranslations(formUuid, newLangCode, fallbackStrings); | ||||||||||
| let schemaTranslations = formSchema.translations?.[newLangCode] || {}; | ||||||||||
|
|
||||||||||
| const merged = mergeTranslations(schemaTranslations, backend || {}, fallbackStrings); | ||||||||||
|
|
||||||||||
| setLocalTranslations((prev) => ({ | ||||||||||
| ...prev, | ||||||||||
| [newLangCode]: merged, | ||||||||||
| })); | ||||||||||
|
|
||||||||||
| setTranslations(merged); | ||||||||||
|
|
||||||||||
| const updatedSchema = { | ||||||||||
|
|
@@ -133,7 +194,6 @@ const TranslationBuilder: React.FC<TranslationBuilderProps> = ({ formSchema, onU | |||||||||
| [newLangCode]: merged, | ||||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| onUpdateSchema(updatedSchema); | ||||||||||
| } catch (err) { | ||||||||||
| setError('Failed to load backend translations.'); | ||||||||||
|
|
||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: Seems like this is something that needs to be run whenever either a translation string gets updated or the language gets changed. In that case can't we extract this into a function and just call it in
hamdleUpdateValueandlanguageChanger. That way we can avoid using auseEffectaltogether, they can be cause infinite renders and would be very hard to debug.