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

refactor: useLanguagePicker warning and fallback #12208

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/components/LanguagePicker/useLanguagePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ export const useLanguagePicker = (
const targetName = i18nConfigTarget || fallbackTarget

if (!sourceName || !targetName) {
throw new Error(
"Missing language display name, locale: " + localeOption
)
console.warn("Missing language display name:", {
localeOption,
sourceName,
targetName,
})
}

// English will not have a dataItem
Expand All @@ -106,10 +108,9 @@ export const useLanguagePicker = (
(dataItem!.words.approved / dataItem!.words.total) * 100
) || 0

if (progressData.length === 0)
throw new Error(
"Missing translation progress data; check GitHub action"
)
if (progressData.length === 0) {
console.warn("Missing translation progress data; check GitHub action")
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at this one again, I think we can probably keep this error. It should never throw as long as there is translation progress data available... if not, we should let the build break. cc: @pettinarip Curious your thoughts

Copy link
Member

Choose a reason for hiding this comment

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

I've to disagree on that. This condition is inside a useEffect. It will never be checked at build time/server side. If it is ever checked, then it will be on client side, creating bad ux since it will make the app crash (unless we are catching that error and failing gracefully).

However, I hear you, it would be nice to have that type of check at build time.

And the change to use console.warn is ok I guess but its not doing anything. Shouldn't we return something else here? given that in the next line we are accessing it progressData[0].words.total.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay yeah, that makes sense, thanks

Shouldn't we return something else here? given that in the next line we are accessing it progressData[0].words.total.

I think we should... I can update this to return early in this case, and pass everything but the approvalProgress and wordsApproved. We can also use localeOption (Lang type) as the fallback for the source or target names... this means a language that failed this for whatever reason would just show the language code instead of an empty string.

Will push a patch

Copy link
Member

Choose a reason for hiding this comment

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

Makes more sense 👍🏼


const totalWords = progressData[0].words.total

Expand All @@ -123,8 +124,8 @@ export const useLanguagePicker = (
return {
localeOption,
approvalProgress,
sourceName,
targetName,
sourceName: sourceName ?? "",
targetName: targetName ?? "",
Copy link
Member Author

Choose a reason for hiding this comment

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

@pettinarip Seeing your comment here... I went with this approach to avoid using mutable variables, sticking with const... lemme know if you disagree

Copy link
Member

Choose a reason for hiding this comment

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

Wondering if we should fallback to default locale or something else. Not sure if "' isn't going to break something else in the chain tbh. If not, it ok.

Copy link
Member Author

Choose a reason for hiding this comment

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

It wouldn't break anything, but it would just render without a name... I switched this to fallback to its language code.

englishName,
wordsApproved,
isBrowserDefault,
Expand Down
Loading