Authentication: Optimizing the initial load of langpack (#2862)
This commit is contained in:
parent
e3dd98882b
commit
f2e9970077
@ -70,7 +70,7 @@ const AuthPhoneNumber: FC<StateProps> = ({
|
|||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
const suggestedLanguage = getSuggestedLanguage();
|
const suggestedLanguage = getSuggestedLanguage();
|
||||||
|
|
||||||
const continueText = useLangString(suggestedLanguage, 'ContinueOnThisLanguage');
|
const continueText = useLangString(suggestedLanguage, 'ContinueOnThisLanguage', true);
|
||||||
const [country, setCountry] = useState<ApiCountryCode | undefined>();
|
const [country, setCountry] = useState<ApiCountryCode | undefined>();
|
||||||
const [phoneNumber, setPhoneNumber] = useState<string | undefined>();
|
const [phoneNumber, setPhoneNumber] = useState<string | undefined>();
|
||||||
const [isTouched, setIsTouched] = useState(false);
|
const [isTouched, setIsTouched] = useState(false);
|
||||||
|
|||||||
@ -58,7 +58,7 @@ const AuthCode: FC<StateProps> = ({
|
|||||||
const lang = useLang();
|
const lang = useLang();
|
||||||
// eslint-disable-next-line no-null/no-null
|
// eslint-disable-next-line no-null/no-null
|
||||||
const qrCodeRef = useRef<HTMLDivElement>(null);
|
const qrCodeRef = useRef<HTMLDivElement>(null);
|
||||||
const continueText = useLangString(suggestedLanguage, 'ContinueOnThisLanguage');
|
const continueText = useLangString(suggestedLanguage, 'ContinueOnThisLanguage', true);
|
||||||
const [isLoading, markIsLoading, unmarkIsLoading] = useFlag();
|
const [isLoading, markIsLoading, unmarkIsLoading] = useFlag();
|
||||||
const [isQrMounted, markQrMounted, unmarkQrMounted] = useFlag();
|
const [isQrMounted, markQrMounted, unmarkQrMounted] = useFlag();
|
||||||
|
|
||||||
|
|||||||
@ -116,8 +116,7 @@ const useEditing = (
|
|||||||
noWebPage: false,
|
noWebPage: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// eslint-disable-next-line react-hooks-static-deps/exhaustive-deps -- `as const` not yet supported by linter
|
}, [editedMessage, chatId, getHtml, threadId, getShouldResetNoWebPageDebounced]);
|
||||||
}, [editedMessage, chatId, getHtml, threadId] as const);
|
|
||||||
|
|
||||||
const restoreNewDraftAfterEditing = useCallback(() => {
|
const restoreNewDraftAfterEditing = useCallback(() => {
|
||||||
if (!draft) return;
|
if (!draft) return;
|
||||||
|
|||||||
@ -1,18 +1,21 @@
|
|||||||
import * as langProvider from '../util/langProvider';
|
import * as langProvider from '../util/langProvider';
|
||||||
import { useState } from '../lib/teact/teact';
|
import { useState } from '../lib/teact/teact';
|
||||||
|
|
||||||
const useLangString = (langCode: string | undefined, key: string): string | undefined => {
|
const useLangString = (
|
||||||
|
langCode: string | undefined,
|
||||||
|
key: string,
|
||||||
|
shouldIgnoreSameValue = false,
|
||||||
|
): string | undefined => {
|
||||||
const [translation, setTranslation] = useState<string>();
|
const [translation, setTranslation] = useState<string>();
|
||||||
|
|
||||||
if (langCode) {
|
if (langCode) {
|
||||||
langProvider
|
langProvider
|
||||||
.getTranslationForLangString(langCode, key)
|
.getTranslationForLangString(langCode, key)
|
||||||
.then((value) => {
|
.then((value) => {
|
||||||
// The string is not translated, maybe the language pack was not loaded due to network or a timeout errors
|
// The string is not translated, maybe the language pack was not loaded due to network errors or a timeout
|
||||||
if (value === key) {
|
if (shouldIgnoreSameValue && value === key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setTranslation(value);
|
setTranslation(value);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import type { ApiLangPack } from '../api/types';
|
import type { ApiLangPack } from '../api/types';
|
||||||
|
|
||||||
export const authLangPack = {
|
export const fallbackLangPackInitial = {
|
||||||
WrongNumber: {
|
WrongNumber: {
|
||||||
key: 'WrongNumber',
|
key: 'WrongNumber',
|
||||||
value: 'Wrong number?',
|
value: 'Wrong number?',
|
||||||
@ -10,7 +10,7 @@ import * as cacheApi from './cacheApi';
|
|||||||
import { callApi } from '../api/gramjs';
|
import { callApi } from '../api/gramjs';
|
||||||
import { createCallbackManager } from './callbacks';
|
import { createCallbackManager } from './callbacks';
|
||||||
import { formatInteger } from './textFormat';
|
import { formatInteger } from './textFormat';
|
||||||
import { authLangPack } from './authLangPack';
|
import { fallbackLangPackInitial } from './fallbackLangPackInitial';
|
||||||
|
|
||||||
export interface LangFn {
|
export interface LangFn {
|
||||||
(key: string, value?: any, format?: 'i', pluralValue?: number): string;
|
(key: string, value?: any, format?: 'i', pluralValue?: number): string;
|
||||||
@ -105,16 +105,11 @@ function createLangFn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!langPack && !fallbackLangPack && !authLangPack[key]) {
|
if (!fallbackLangPack) {
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
const shouldImportFallback = !fallbackLangPack && !(langPack?.[key] || fallbackLangPack?.[key]);
|
|
||||||
if (shouldImportFallback) {
|
|
||||||
void importFallbackLangPack();
|
void importFallbackLangPack();
|
||||||
}
|
}
|
||||||
|
|
||||||
const langString = (langPack?.[key]) || (fallbackLangPack?.[key]) || authLangPack[key];
|
const langString = langPack?.[key] || fallbackLangPack?.[key] || fallbackLangPackInitial[key];
|
||||||
if (!langString) {
|
if (!langString) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user