///: BEGIN:ONLY_INCLUDE_IN(flask) import browser from 'webextension-polyfill'; ///: END:ONLY_INCLUDE_IN import { memoize } from 'lodash'; import getFirstPreferredLangCode from '../../app/scripts/lib/get-first-preferred-lang-code'; import { fetchLocale, loadRelativeTimeFormatLocaleData, } from '../../ui/helpers/utils/i18n-helper'; ///: BEGIN:ONLY_INCLUDE_IN(flask) import { renderDesktopError } from '../../ui/pages/desktop-error/render-desktop-error'; import { EXTENSION_ERROR_PAGE_TYPES } from '../constants/desktop'; import { openCustomProtocol } from './deep-linking'; ///: END:ONLY_INCLUDE_IN import switchDirection from './switch-direction'; const _setupLocale = async (currentLocale) => { const currentLocaleMessages = currentLocale ? await fetchLocale(currentLocale) : {}; const enLocaleMessages = await fetchLocale('en'); await loadRelativeTimeFormatLocaleData('en'); if (currentLocale) { await loadRelativeTimeFormatLocaleData(currentLocale); } return { currentLocaleMessages, enLocaleMessages }; }; export const setupLocale = memoize(_setupLocale); const getLocaleContext = (currentLocaleMessages, enLocaleMessages) => { return (key) => { let message = currentLocaleMessages[key]?.message; if (!message && enLocaleMessages[key]) { message = enLocaleMessages[key].message; } return message; }; }; export async function getErrorHtml( errorKey, supportLink, metamaskState, ///: BEGIN:ONLY_INCLUDE_IN(flask) err, ///: END:ONLY_INCLUDE_IN ) { let response, preferredLocale; if (metamaskState?.currentLocale) { preferredLocale = metamaskState.currentLocale; response = await setupLocale(metamaskState.currentLocale); } else { preferredLocale = await getFirstPreferredLangCode(); response = await setupLocale(preferredLocale); } const textDirection = ['ar', 'dv', 'fa', 'he', 'ku'].includes(preferredLocale) ? 'rtl' : 'auto'; switchDirection(textDirection); const { currentLocaleMessages, enLocaleMessages } = response; const t = getLocaleContext(currentLocaleMessages, enLocaleMessages); ///: BEGIN:ONLY_INCLUDE_IN(flask) const isDesktopEnabled = metamaskState?.desktopEnabled === true; if (isDesktopEnabled) { let errorType = EXTENSION_ERROR_PAGE_TYPES.CRITICAL_ERROR; if (err?.message.includes('No response from RPC')) { errorType = EXTENSION_ERROR_PAGE_TYPES.CONNECTION_LOST; } return renderDesktopError({ type: errorType, t, isHtmlError: true, }); } ///: END:ONLY_INCLUDE_IN /** * The pattern ${errorKey === 'troubleStarting' ? t('troubleStarting') : ''} * is neccessary because we we need linter to see the string * of the locale keys. If we use the variable directly, the linter will not * see the string and will not be able to check if the locale key exists. */ return `
${t('stillGettingMessage')} ${t('sendBugReport')}