Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Convert JS files to TypeScript #496

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions packages/base-shell/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { AuthorizedRoute } from "./AuthorizedRoute";
export { UnauthorizedRoute } from "./UnauthorizedRoute";
7 changes: 7 additions & 0 deletions packages/base-shell/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "./Update";
export * from "./SimpleValues";
export * from "./Online";
export * from "./Locale";
export * from "./Config";
export * from "./Auth";
export * from "./AddToHomeScreen";
11 changes: 11 additions & 0 deletions packages/base-shell/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const merge = (obj1: Record<string, any>, obj2: Record<string, any>): Record<string, any> => {
let temp = { ...obj1, ...obj2 };

Object.keys(temp).forEach((key) => {
if (typeof temp[key] === "object" && !(temp[key] instanceof Array)) {
temp[key] = { ...obj1[key], ...obj2[key] };
}
});

return temp;
};
2 changes: 2 additions & 0 deletions packages/base-shell/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { merge } from "./config";
export * from "./locale";
76 changes: 76 additions & 0 deletions packages/base-shell/src/utils/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//import areIntlLocalesSupported from 'intl-locales-supported'
//import intl from 'intl'
import { defineMessages } from 'react-intl'

/*
const loadLocalePolyfill = (locale) => {
// START: Intl polyfill
// Required for working on Safari
// Code from here: https://formatjs.io/guides/runtime-environments/
let localesMyAppSupports = [locale]

if (global.Intl) {
// Determine if the built-in `Intl` has the locale data we need.
if (!areIntlLocalesSupported(localesMyAppSupports)) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and replace the constructors with need with the polyfill's.
let IntlPolyfill = intl
Intl.NumberFormat = IntlPolyfill.NumberFormat
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat
}
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = intl
}
// END: Intl polyfill
}
*/

const getUsersPreferredLanguages = (): string[] | undefined => {
if (navigator.languages !== undefined) {
return navigator.languages;
} else if (navigator.language !== undefined) {
return [navigator.language];
} else {
return undefined;
}
};

const parseLanguages = (acceptedLangs: string[], defaultLang: string | false = false): string | false | undefined => {
const userPref = getUsersPreferredLanguages();

const match = userPref
? userPref.find((lang) => acceptedLangs.includes(lang))
: undefined;

if (match === undefined && defaultLang !== false) {
return defaultLang;
}

return match;
};

const getLocaleMessages = async (l: string, ls: { locale: string; messages: any }[]): Promise<Record<string, any>> => {
if (ls) {
for (let i = 0; i < ls.length; i++) {
if (ls[i]['locale'] === l) {
const { default: messages } = await defineMessages(ls[i].messages);

return messages;
}
}
}

return {};
};

const formatMessage = (messages: Record<string, string> = {}, id: string): string => {
return messages[id] || id;
};

export {
formatMessage,
getLocaleMessages,
//loadLocalePolyfill
parseLanguages
};
Loading