lbry-desktop/ui/redux/selectors/settings.js
infinite-persistence 81e4730037 Simplify language lists
- SUPPORTED_SUB_LANGUAGE_CODES[] that I introduced was pretty redundant when SUPPORTED_LANGUAGES[] already hold the information. The logic to ignore sub-languages (i.e. reduce the locale's "en-GB" to "en" is now located in getDefaultLanguage()).

- SUPPORTED_BROWSER_LANGUAGES[] and SUPPORTED_LANGUAGES[] look so similar and hard to tell what the former is for at first glance. The functionality to map 'zh-CN' to 'zh-Hans' is now handled by resolveLanguageAlias(), which makes the intention clearer.

This leaves us with a single list -- SUPPORTED_LANGUAGES[], whose key also tells us the desired language code to use.
Also, clients now need to call `resolveLanguageAlias` to map any language code aliases, as they differ depending on how it is queried (e.g. `navigator.language` vs. `app.getLocal()` uses different standards).

I think we no longer need to explicitly migrate existing user's 'zh-CN' into 'zh-Hans' because the rest of the system will always use the desired language code as long as 'resolveLanguageAlias' is called appropriately. e.g. the system uses `selectLanguage` and `selectLanguage` calls `resolveLanguageAlias`.
2021-04-04 22:54:33 -04:00

78 lines
3.1 KiB
JavaScript

import { SETTINGS, DAEMON_SETTINGS } from 'lbry-redux';
import { createSelector } from 'reselect';
import homepages from 'homepages';
import { SIMPLE_SITE } from 'config';
import { resolveLanguageAlias, getDefaultHomepageKey, getDefaultLanguage } from 'util/default-languages';
const selectState = (state) => state.settings || {};
export const selectDaemonSettings = createSelector(selectState, (state) => state.daemonSettings);
export const selectDaemonStatus = createSelector(selectState, (state) => state.daemonStatus);
export const selectFfmpegStatus = createSelector(selectDaemonStatus, (status) => status.ffmpeg_status);
export const selectFindingFFmpeg = createSelector(selectState, (state) => state.findingFFmpeg || false);
export const selectClientSettings = createSelector(selectState, (state) => state.clientSettings || {});
export const selectLoadedLanguages = createSelector(selectState, (state) => state.loadedLanguages || {});
export const makeSelectClientSetting = (setting) =>
createSelector(selectClientSettings, (settings) => (settings ? settings[setting] : undefined));
// refactor me
export const selectShowMatureContent = SIMPLE_SITE
? createSelector(() => false)
: makeSelectClientSetting(SETTINGS.SHOW_MATURE);
// and me
export const selectShowRepostedContent = makeSelectClientSetting(SETTINGS.HIDE_REPOSTS);
export const selectTheme = makeSelectClientSetting(SETTINGS.THEME);
export const selectAutomaticDarkModeEnabled = makeSelectClientSetting(SETTINGS.AUTOMATIC_DARK_MODE_ENABLED);
export const selectIsNight = createSelector(selectState, (state) => state.isNight);
export const selectSavedWalletServers = createSelector(selectState, (state) => state.customWalletServers);
export const selectSharedPreferences = createSelector(selectState, (state) => state.sharedPreferences);
export const makeSelectSharedPreferencesForKey = (key) =>
createSelector(selectSharedPreferences, (prefs) => (prefs ? prefs[key] : undefined));
export const selectHasWalletServerPrefs = createSelector(
makeSelectSharedPreferencesForKey(DAEMON_SETTINGS.LBRYUM_SERVERS),
(servers) => {
return !!(servers && servers.length);
}
);
export const selectThemePath = createSelector(
selectTheme,
selectAutomaticDarkModeEnabled,
selectIsNight,
(theme, automaticDarkModeEnabled, isNight) => {
const dynamicTheme = automaticDarkModeEnabled && isNight ? 'dark' : theme;
return dynamicTheme || 'light';
}
);
export const selectHomepageCode = createSelector(makeSelectClientSetting(SETTINGS.HOMEPAGE), (setting) => {
return homepages[setting] ? setting : getDefaultHomepageKey();
});
export const selectLanguage = createSelector(makeSelectClientSetting(SETTINGS.LANGUAGE), (setting) => {
const lang = resolveLanguageAlias(setting) || getDefaultLanguage();
return lang;
});
export const selectHomepageData = createSelector(
// using homepage setting,
selectHomepageCode,
(homepageCode) => {
// homepages = { 'en': homepageFile, ... }
return homepages[homepageCode];
}
);
export const selectosNotificationsEnabled = makeSelectClientSetting(SETTINGS.OS_NOTIFICATIONS_ENABLED);