1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/shared/lib/error-utils.js
Filip Sekulic 6e13524bcd
Remove related UI code from the app dir (#15384)
Co-authored-by: metamaskbot <metamaskbot@users.noreply.github.com>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
Co-authored-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-09-16 14:05:21 -05:00

76 lines
2.2 KiB
JavaScript

import { memoize } from 'lodash';
import getFirstPreferredLangCode from '../../app/scripts/lib/get-first-preferred-lang-code';
import {
fetchLocale,
loadRelativeTimeFormatLocaleData,
} from '../../ui/helpers/utils/i18n-helper';
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(supportLink, metamaskState) {
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);
return `
<div class="critical-error">
<div class="critical-error__alert">
<p class="critical-error__alert__message">
${t('troubleStarting')}
</p>
<button id='critical-error-button' class="critical-error__alert__button">
${t('restartMetamask')}
</button>
</div>
<p class="critical-error__paragraph">
${t('stillGettingMessage')}
<a
href=${supportLink}
class="critical-error__paragraph__link"
target="_blank"
rel="noopener noreferrer">
${t('sendBugReport')}
</a>
</p>
</div>
`;
}