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

import emoji async draft #41097

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
25da155
import emoji async draft
rinej Apr 26, 2024
3729e0e
refactor: remove redundant if statements
rinej Apr 26, 2024
f9a9f06
add dynamic emojiTrie creation based on locale variable
rinej May 6, 2024
67f7c37
Merge branch 'main' into Load-Emojis-async-depending-on-lang
rinej May 6, 2024
ead2673
use vm module for jest tests
rinej May 7, 2024
4e3e2f2
add proper error handling for dynamic imports
rinej May 8, 2024
fab626e
fix: fix lint
rinej May 8, 2024
6ad5f7a
add console logs for debug on cli
rinej May 8, 2024
1326dbc
add build stept to test to debug on cli
rinej May 8, 2024
04e5e36
fix lint
rinej May 8, 2024
09a780e
Merge branch 'main' into Load-Emojis-async-depending-on-lang
rinej May 8, 2024
dd38471
remove silent and add logs for debug
rinej May 8, 2024
7765f88
add cl fo debug
rinej May 8, 2024
f34d9e7
run test action from packagejson script
rinej May 8, 2024
cf4776e
add experimental flag to gh action
rinej May 8, 2024
2ac8e85
adjust action
rinej May 8, 2024
cb2a2e7
adjust jest action
rinej May 8, 2024
f39d4bc
cleanup
rinej May 8, 2024
3fbcbda
add comments
rinej May 8, 2024
f6d465d
cleanup
rinej May 8, 2024
9f4028f
adjust package json script
rinej May 8, 2024
776be3d
adjust imports for ios
rinej May 10, 2024
82ceac6
parse typescript types for jest tests
rinej May 10, 2024
5879397
Merge branch 'main' into Load-Emojis-async-depending-on-lang
rinej May 10, 2024
dfdce42
Merge branch 'main' into Load-Emojis-async-depending-on-lang
rinej May 13, 2024
060127c
fix failing workflow
rinej May 13, 2024
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: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
key: ${{ runner.os }}-jest

- name: Jest tests
run: npx jest --silent --shard=${{ fromJSON(matrix.chunk) }}/${{ strategy.job-total }} --max-workers ${{ steps.cpu-cores.outputs.count }}
run: NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules" npx jest --silent --shard=${{ fromJSON(matrix.chunk) }}/${{ strategy.job-total }} --max-workers ${{ steps.cpu-cores.outputs.count }}

storybookTests:
if: ${{ github.actor != 'OSBotify' && github.actor != 'imgbot[bot]' || github.event_name == 'workflow_call' }}
Expand Down
20 changes: 15 additions & 5 deletions assets/emojis/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type {Locale} from '@src/types/onyx';
import emojis from './common';
import enEmojis from './en';
import esEmojis from './es';
import type {Emoji, EmojisList} from './types';

type EmojiTable = Record<string, Emoji>;
Expand Down Expand Up @@ -30,10 +28,22 @@ const emojiCodeTableWithSkinTones = emojis.reduce<EmojiTable>((prev, cur) => {
}, {});

const localeEmojis: LocaleEmojis = {
en: enEmojis,
es: esEmojis,
en: undefined,
es: undefined,
};

const importEmojiLocale = (locale: Locale) => {
const normalizedLocale = locale.toLowerCase().split('-')[0] as Locale;
if (!localeEmojis[normalizedLocale]) {
const emojiImportPromise = normalizedLocale === 'en' ? import('./en') : import('./es');
return emojiImportPromise.then((esEmojiModule) => {
// it is needed because in jest test the modules are imported in double nested default object
localeEmojis[normalizedLocale] = esEmojiModule.default.default ? (esEmojiModule.default.default as unknown as EmojisList) : esEmojiModule.default;
});
}
return Promise.resolve();
};

export default emojis;
export {emojiNameTable, emojiCodeTableWithSkinTones, localeEmojis};
export {emojiNameTable, emojiCodeTableWithSkinTones, localeEmojis, importEmojiLocale};
export {skinTones, categoryFrequentlyUsed} from './common';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"android-build": "fastlane android build",
"android-build-e2e": "bundle exec fastlane android build_e2e",
"android-build-e2edelta": "bundle exec fastlane android build_e2edelta",
"test": "TZ=utc jest",
"test": "TZ=utc NODE_OPTIONS=--experimental-vm-modules jest",
"typecheck": "tsc",
"lint": "eslint . --max-warnings=0 --cache --cache-location=node_modules/.cache/eslint",
"lint-changed": "eslint --fix $(git diff --diff-filter=AM --name-only main -- \"*.js\" \"*.ts\" \"*.tsx\")",
Expand Down
19 changes: 18 additions & 1 deletion src/libs/EmojiTrie.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import emojis, {localeEmojis} from '@assets/emojis';
import type {Emoji, HeaderEmoji, PickerEmoji} from '@assets/emojis/types';
import CONST from '@src/CONST';
import type {Locale} from '@src/types/onyx';
import Timing from './actions/Timing';
import Trie from './Trie';

Expand Down Expand Up @@ -95,9 +96,25 @@ function createTrie(lang: SupportedLanguage = CONST.LOCALES.DEFAULT): Trie<Emoji
return trie;
}

const emojiTrie: EmojiTrie = supportedLanguages.reduce((prev, cur) => ({...prev, [cur]: createTrie(cur)}), {});
const emojiTrie: EmojiTrie = supportedLanguages.reduce((acc, lang) => {
acc[lang] = undefined;
return acc;
}, {} as EmojiTrie);

const buildEmojisTrie = (locale: Locale) => {
// Normalize the locale to lowercase and take the first part before any dash
const normalizedLocale = locale.toLowerCase().split('-')[0];
const localeToUse = supportedLanguages.includes(normalizedLocale as SupportedLanguage) ? (normalizedLocale as SupportedLanguage) : undefined;

if (!localeToUse || emojiTrie[localeToUse]) {
return; // Return early if the locale is not supported or the trie is already built
}
emojiTrie[localeToUse] = createTrie(localeToUse);
};

Timing.end(CONST.TIMING.TRIE_INITIALIZATION);

export default emojiTrie;
export {buildEmojisTrie};

export type {SupportedLanguage};
17 changes: 15 additions & 2 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {AppState} from 'react-native';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import {importEmojiLocale} from '@assets/emojis';
import * as API from '@libs/API';
import type {
GetMissingOnyxMessagesParams,
Expand All @@ -18,6 +19,7 @@ import type {
import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as Browser from '@libs/Browser';
import DateUtils from '@libs/DateUtils';
import {buildEmojisTrie} from '@libs/EmojiTrie';
import Log from '@libs/Log';
import getCurrentUrl from '@libs/Navigation/currentUrl';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -59,10 +61,17 @@ Onyx.connect({
initWithStoredValues: false,
});

let preferredLocale: string | null;
let preferredLocale: string | null = null;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (val) => (preferredLocale = val),
callback: (val) => {
preferredLocale = val;
if (preferredLocale) {
importEmojiLocale(preferredLocale as Locale).then(() => {
buildEmojisTrie(preferredLocale as Locale);
});
}
},
});

let priorityMode: ValueOf<typeof CONST.PRIORITY_MODE> | null;
Expand Down Expand Up @@ -150,6 +159,10 @@ function setLocale(locale: Locale) {
value: locale,
};

importEmojiLocale(locale).then(() => {
buildEmojisTrie(locale);
});

API.write(WRITE_COMMANDS.UPDATE_PREFERRED_LOCALE, parameters, {optimisticData});
}

Expand Down
10 changes: 9 additions & 1 deletion tests/unit/EmojiTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {getUnixTime} from 'date-fns';
import Onyx from 'react-native-onyx';
import Emojis from '@assets/emojis';
import Emojis, {importEmojiLocale} from '@assets/emojis';
import type {Emoji} from '@assets/emojis/types';
import * as User from '@libs/actions/User';
import {buildEmojisTrie} from '@libs/EmojiTrie';
import * as EmojiUtils from '@libs/EmojiUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand All @@ -11,6 +12,13 @@ import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

describe('EmojiTest', () => {
beforeAll(async () => {
await importEmojiLocale('en');
buildEmojisTrie('en');
await importEmojiLocale('es');
buildEmojisTrie('es');
});

it('matches all the emojis in the list', () => {
// Given the set of Emojis available in the application
const emojiMatched = Emojis.every((emoji) => {
Expand Down
8 changes: 4 additions & 4 deletions workflow_tests/assertions/platformDeployAssertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,10 @@ function assertPostGithubCommentJobExecuted(workflowResult: Step[], didExecute =
{key: 'IS_PRODUCTION_DEPLOY', value: isProduction ? 'true' : 'false'},
{key: 'DEPLOY_VERSION', value: '1.2.3'},
{key: 'GITHUB_TOKEN', value: '***'},
{key: 'ANDROID', value: didDeploy ? 'success' : ''},
{key: 'DESKTOP', value: didDeploy ? 'success' : ''},
{key: 'IOS', value: didDeploy ? 'success' : ''},
{key: 'WEB', value: didDeploy ? 'success' : ''},
{key: 'ANDROID', value: didDeploy ? 'success' : 'skipped'},
{key: 'DESKTOP', value: didDeploy ? 'success' : 'skipped'},
{key: 'IOS', value: didDeploy ? 'success' : 'skipped'},
{key: 'WEB', value: didDeploy ? 'success' : 'skipped'},
]),
] as const;

Expand Down
Loading