Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

refactor(app): simplify advanced settings rows and toggles

Hampus340d302109d0
diff --git a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedChatControls.tsx b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedChatControls.tsx
index 1d1e92518..40073dccd 100644
--- a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedChatControls.tsx
+++ b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedChatControls.tsx
@@ -46,7 +46,7 @@ import {msg} from '@lingui/core/macro';
import {Trans, useLingui} from '@lingui/react/macro';
import {GearIcon} from '@phosphor-icons/react';
import {observer} from 'mobx-react-lite';
-import {useCallback} from 'react';
+import {type ReactNode, useCallback} from 'react';
const SHOW_MESSAGE_ACTION_BAR_DESCRIPTOR = msg({
message: 'Show message action bar',
@@ -279,14 +279,18 @@ const SearchProviderSettingsModal = observer(({mode, title}: SearchProviderSetti
const handleClose = useCallback(() => {
ModalCommands.pop();
}, []);
- const content =
- mode === 'image' ? (
+ let content: ReactNode;
+ if (mode === 'image') {
+ content = (
<ReverseImageSearchContent data-flx="user.advanced-settings-tab.provider-settings-modal.reverse-image-search-content" />
- ) : mode === 'translate' ? (
- <TranslatorsContent data-flx="user.advanced-settings-tab.provider-settings-modal.translators-content" />
- ) : (
+ );
+ } else if (mode === 'translate') {
+ content = <TranslatorsContent data-flx="user.advanced-settings-tab.provider-settings-modal.translators-content" />;
+ } else {
+ content = (
<TextSearchEnginesContent data-flx="user.advanced-settings-tab.provider-settings-modal.search-engines-content" />
);
+ }
return (
<Modal.Root
size="medium"
@@ -457,16 +461,24 @@ const SwitchGroupSettingsModal = observer(({mode, title}: SwitchGroupSettingsMod
const handleClose = useCallback(() => {
ModalCommands.pop();
}, []);
- const content =
- mode === 'message-action-bar' ? (
+ let content: ReactNode;
+ if (mode === 'message-action-bar') {
+ content = (
<MessageActionBarContent data-flx="user.advanced-settings-tab.advanced-chat-controls.switch-group-settings-modal.message-action-bar-content" />
- ) : mode === 'media-buttons' ? (
+ );
+ } else if (mode === 'media-buttons') {
+ content = (
<MediaButtonsContent data-flx="user.advanced-settings-tab.advanced-chat-controls.switch-group-settings-modal.media-buttons-content" />
- ) : mode === 'expression-autocomplete' ? (
+ );
+ } else if (mode === 'expression-autocomplete') {
+ content = (
<ExpressionAutocompleteContent data-flx="user.advanced-settings-tab.advanced-chat-controls.switch-group-settings-modal.expression-autocomplete-content" />
- ) : (
+ );
+ } else {
+ content = (
<InputButtonsContent data-flx="user.advanced-settings-tab.advanced-chat-controls.switch-group-settings-modal.input-buttons-content" />
);
+ }
return (
<Modal.Root
size="medium"
diff --git a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedSettingRow.tsx b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedSettingRow.tsx
index 3c264a55d..f675347de 100644
--- a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedSettingRow.tsx
+++ b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedSettingRow.tsx
@@ -2,6 +2,7 @@
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
import {Button} from '@app/features/ui/button/Button';
+import {ArrowRightIcon} from '@app/features/ui/icons/DirectionalIcons';
import {
AdvancedSettingControl,
COMPACT_SWITCH_CONTROL_ITEM_IDS,
@@ -17,10 +18,9 @@ import type {SearchableSettingItem} from '@app/features/user/components/settings
import {SettingsItemStatusBadges} from '@app/features/user/components/settings_utils/SettingsStatusBadge';
import {msg} from '@lingui/core/macro';
import {useLingui} from '@lingui/react/macro';
-import {ArrowRightIcon} from '@phosphor-icons/react';
import {clsx} from 'clsx';
import {observer} from 'mobx-react-lite';
-import {useCallback, useRef} from 'react';
+import {type ReactNode, useCallback, useRef} from 'react';
const OPEN_DESCRIPTOR = msg({
message: 'Open',
@@ -50,53 +50,57 @@ export const AdvancedSettingRow = observer(
switchButton?.focus();
}, [canToggleFromMain]);
const MainComponent = canToggleFromMain ? 'button' : 'div';
- return (
- <div
- ref={rowRef}
- id={getAdvancedSettingRowElementId(item.id)}
- className={clsx(styles.settingRow, hasFullWidthControl && styles.settingRowFullWidth)}
- data-flx="user.advanced-settings-tab.setting-row"
+ const rowId = getAdvancedSettingRowElementId(item.id);
+ const rowClassName = clsx(styles.settingRow, hasFullWidthControl && styles.settingRowFullWidth);
+ const main = (
+ <MainComponent
+ {...(canToggleFromMain ? {type: 'button', onClick: handleMainClick} : {})}
+ className={clsx(styles.settingMain, canToggleFromMain && styles.settingMainInteractive)}
+ data-flx="user.advanced-settings-tab.setting-main"
>
- <MainComponent
- {...(canToggleFromMain ? {type: 'button', onClick: handleMainClick} : {})}
- className={clsx(styles.settingMain, canToggleFromMain && styles.settingMainInteractive)}
- data-flx="user.advanced-settings-tab.setting-main"
- >
- <div className={styles.settingTitleRow} data-flx="user.advanced-settings-tab.setting-title-row">
- <span className={styles.settingTitle} data-flx="user.advanced-settings-tab.setting-title">
- {item.label}
- </span>
- <SettingsItemStatusBadges
- item={item}
- userCreatedAt={userCreatedAt}
- data-flx="user.advanced-settings-tab.setting-badges"
- />
- </div>
- {item.description && (
- <p className={styles.settingDescription} data-flx="user.advanced-settings-tab.setting-description">
- {item.description}
- </p>
- )}
- </MainComponent>
- <div
- className={clsx(styles.settingAction, hasFullWidthControl && styles.settingActionFullWidth)}
- data-flx="user.advanced-settings-tab.setting-action"
+ <div className={styles.settingTitleRow} data-flx="user.advanced-settings-tab.setting-title-row">
+ <span className={styles.settingTitle} data-flx="user.advanced-settings-tab.setting-title">
+ {item.label}
+ </span>
+ <SettingsItemStatusBadges
+ item={item}
+ userCreatedAt={userCreatedAt}
+ data-flx="user.advanced-settings-tab.setting-badges"
+ />
+ </div>
+ {item.description && (
+ <p className={styles.settingDescription} data-flx="user.advanced-settings-tab.setting-description">
+ {item.description}
+ </p>
+ )}
+ </MainComponent>
+ );
+ const actionClassName = clsx(styles.settingAction, hasFullWidthControl && styles.settingActionFullWidth);
+ let action: ReactNode = null;
+ if (hasDirectControl) {
+ action = (
+ <AdvancedSettingControl
+ item={item}
+ data-flx="user.advanced-settings-tab.advanced-setting-row.advanced-setting-control"
+ />
+ );
+ } else if (canOpen) {
+ action = (
+ <Button
+ variant="secondary"
+ rightIcon={<ArrowRightIcon size={remFromPx(14)} data-flx="user.advanced-settings-tab.arrow-right-icon" />}
+ onClick={() => onOpen(item)}
+ data-flx="user.advanced-settings-tab.button.open-source"
>
- {hasDirectControl ? (
- <AdvancedSettingControl
- item={item}
- data-flx="user.advanced-settings-tab.advanced-setting-row.advanced-setting-control"
- />
- ) : canOpen ? (
- <Button
- variant="secondary"
- rightIcon={<ArrowRightIcon size={remFromPx(14)} data-flx="user.advanced-settings-tab.arrow-right-icon" />}
- onClick={() => onOpen(item)}
- data-flx="user.advanced-settings-tab.button.open-source"
- >
- {i18n._(OPEN_DESCRIPTOR)}
- </Button>
- ) : null}
+ {i18n._(OPEN_DESCRIPTOR)}
+ </Button>
+ );
+ }
+ return (
+ <div ref={rowRef} id={rowId} className={rowClassName} data-flx="user.advanced-settings-tab.setting-row">
+ {main}
+ <div className={actionClassName} data-flx="user.advanced-settings-tab.setting-action">
+ {action}
</div>
</div>
);
diff --git a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedVideoControls.tsx b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedVideoControls.tsx
index 86ecae8aa..69ac21766 100644
--- a/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedVideoControls.tsx
+++ b/fluxer_app/src/features/user/components/modals/tabs/advanced_settings_tab/AdvancedVideoControls.tsx
@@ -150,16 +150,15 @@ export const ScreenShareCodecControl = observer(() => {
const codecCapabilities = getCodecCapabilityReport();
const options: ReadonlyArray<ComboboxOption<CodecPreference>> = [
{value: 'auto', label: i18n._(AUTOMATIC_CODEC_OPTION_DESCRIPTOR, {codec: CODEC_DISPLAY_LABEL[automaticCodec]})},
- ...SCREEN_SHARE_CODEC_OPTION_ORDER.map((codec) => ({
- value: codec,
- label:
- codec === 'av1' && !av1OptIn
- ? i18n._(AV1_REQUIRES_OPT_IN_OPTION_DESCRIPTOR)
- : codec === 'h265' && !hevcOptIn
- ? i18n._(H265_REQUIRES_OPT_IN_OPTION_DESCRIPTOR)
- : CODEC_DISPLAY_LABEL[codec],
- isDisabled: !codecCapabilities[codec].supported,
- })),
+ ...SCREEN_SHARE_CODEC_OPTION_ORDER.map((codec) => {
+ let label: string = CODEC_DISPLAY_LABEL[codec];
+ if (codec === 'av1' && !av1OptIn) {
+ label = i18n._(AV1_REQUIRES_OPT_IN_OPTION_DESCRIPTOR);
+ } else if (codec === 'h265' && !hevcOptIn) {
+ label = i18n._(H265_REQUIRES_OPT_IN_OPTION_DESCRIPTOR);
+ }
+ return {value: codec, label, isDisabled: !codecCapabilities[codec].supported};
+ }),
];
return (
<Combobox<CodecPreference>
diff --git a/fluxer_app/src/features/user/components/modals/tabs/chat_settings_tab/AddCustomSearchEngineModal.tsx b/fluxer_app/src/features/user/components/modals/tabs/chat_settings_tab/AddCustomSearchEngineModal.tsx
index dd97ae089..ddbcbe124 100644
--- a/fluxer_app/src/features/user/components/modals/tabs/chat_settings_tab/AddCustomSearchEngineModal.tsx
+++ b/fluxer_app/src/features/user/components/modals/tabs/chat_settings_tab/AddCustomSearchEngineModal.tsx
@@ -3,9 +3,7 @@
import * as Modal from '@app/features/app/components/dialogs/Modal';
import {EXAMPLE_URL} from '@app/features/app/config/I18nDisplayConstants';
import {CANCEL_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
-import Translation from '@app/features/messaging/state/Translation';
-import ReverseImageSearch from '@app/features/search/state/ReverseImageSearch';
-import SearchEngine from '@app/features/search/state/SearchEngine';
+import {getSearchProviderStore} from '@app/features/search/utils/SearchProviderUtils';
import {Button} from '@app/features/ui/button/Button';
import * as ModalCommands from '@app/features/ui/commands/ModalCommands';
import {Input} from '@app/features/ui/components/form/FormInput';
@@ -99,6 +97,30 @@ const ADD_DESCRIPTOR = msg({
export type SearchEngineMode = 'text' | 'image' | 'translate';
+const SEARCH_ENGINE_MESSAGES = {
+ text: {
+ addTitle: ADD_SEARCH_ENGINE_DESCRIPTOR,
+ editTitle: EDIT_SEARCH_ENGINE_DESCRIPTOR,
+ examplePath: '/search?q=',
+ hint: USE_WHERE_THE_SEARCH_TEXT_SHOULD_BE_INSERTED_DESCRIPTOR,
+ namePlaceholder: MY_SEARCH_ENGINE_DESCRIPTOR,
+ },
+ image: {
+ addTitle: ADD_REVERSE_IMAGE_SEARCH_ENGINE_DESCRIPTOR,
+ editTitle: EDIT_REVERSE_IMAGE_SEARCH_ENGINE_DESCRIPTOR,
+ examplePath: '/searchbyimage?url=',
+ hint: USE_WHERE_THE_IMAGE_URL_SHOULD_BE_INSERTED_DESCRIPTOR,
+ namePlaceholder: MY_REVERSE_IMAGE_SEARCH_DESCRIPTOR,
+ },
+ translate: {
+ addTitle: ADD_TRANSLATION_PROVIDER_DESCRIPTOR,
+ editTitle: EDIT_TRANSLATION_PROVIDER_DESCRIPTOR,
+ examplePath: '/translate?text=',
+ hint: USE_WHERE_THE_TEXT_TO_TRANSLATE_SHOULD_BE_DESCRIPTOR,
+ namePlaceholder: MY_TRANSLATOR_DESCRIPTOR,
+ },
+};
+
interface AddCustomSearchEngineModalProps {
mode?: SearchEngineMode;
editingEngineId?: string;
@@ -116,27 +138,17 @@ export const AddCustomSearchEngineModal: React.FC<AddCustomSearchEngineModalProp
const isImageMode = mode === 'image';
const isTranslateMode = mode === 'translate';
const placeholder = isImageMode ? '{url}' : '{query}';
- const store = isImageMode ? ReverseImageSearch : isTranslateMode ? Translation : SearchEngine;
- const titleAdd = isImageMode
- ? i18n._(ADD_REVERSE_IMAGE_SEARCH_ENGINE_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(ADD_TRANSLATION_PROVIDER_DESCRIPTOR)
- : i18n._(ADD_SEARCH_ENGINE_DESCRIPTOR);
- const titleEdit = isImageMode
- ? i18n._(EDIT_REVERSE_IMAGE_SEARCH_ENGINE_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(EDIT_TRANSLATION_PROVIDER_DESCRIPTOR)
- : i18n._(EDIT_SEARCH_ENGINE_DESCRIPTOR);
- const exampleUrl = isImageMode
- ? `${EXAMPLE_URL}/searchbyimage?url=${placeholder}`
- : isTranslateMode
- ? `${EXAMPLE_URL}/translate?text=${placeholder}`
- : `${EXAMPLE_URL}/search?q=${placeholder}`;
- const hint = isImageMode
- ? i18n._(USE_WHERE_THE_IMAGE_URL_SHOULD_BE_INSERTED_DESCRIPTOR, {url: '{url}'})
- : isTranslateMode
- ? i18n._(USE_WHERE_THE_TEXT_TO_TRANSLATE_SHOULD_BE_DESCRIPTOR, {query: '{query}'})
- : i18n._(USE_WHERE_THE_SEARCH_TEXT_SHOULD_BE_INSERTED_DESCRIPTOR, {query: '{query}'});
+ const store = getSearchProviderStore(mode);
+ let messages = SEARCH_ENGINE_MESSAGES.text;
+ if (isImageMode) {
+ messages = SEARCH_ENGINE_MESSAGES.image;
+ } else if (isTranslateMode) {
+ messages = SEARCH_ENGINE_MESSAGES.translate;
+ }
+ const titleAdd = i18n._(messages.addTitle);
+ const titleEdit = i18n._(messages.editTitle);
+ const exampleUrl = `${EXAMPLE_URL}${messages.examplePath}${placeholder}`;
+ const hint = i18n._(messages.hint, isImageMode ? {url: '{url}'} : {query: '{query}'});
const placeholderRequiredError = isImageMode
? i18n._(URL_PATTERN_MUST_CONTAIN_PLACEHOLDER_DESCRIPTOR, {url: '{url}'})
: i18n._(URL_PATTERN_MUST_CONTAIN_PLACEHOLDER_2_DESCRIPTOR, {query: '{query}'});
@@ -192,13 +204,7 @@ export const AddCustomSearchEngineModal: React.FC<AddCustomSearchEngineModalProp
setName(event.target.value);
setError('');
}}
- placeholder={
- isImageMode
- ? i18n._(MY_REVERSE_IMAGE_SEARCH_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(MY_TRANSLATOR_DESCRIPTOR)
- : i18n._(MY_SEARCH_ENGINE_DESCRIPTOR)
- }
+ placeholder={i18n._(messages.namePlaceholder)}
autoFocus
data-flx="user.chat-settings-tab.add-custom-search-engine-modal.input.set-name"
/>
diff --git a/fluxer_app/src/features/user/state/AdvancedSettings.ts b/fluxer_app/src/features/user/state/AdvancedSettings.ts
index 9d213136d..2a07682cf 100644
--- a/fluxer_app/src/features/user/state/AdvancedSettings.ts
+++ b/fluxer_app/src/features/user/state/AdvancedSettings.ts
@@ -1,20 +1,29 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import AppStorage from '@app/features/platform/state/PersistentStorage';
+import {Logger} from '@app/features/platform/utils/AppLogger';
import {makeAutoObservable} from 'mobx';
-const UNREAD_BADGE_CUSTOMIZATION_STORAGE_KEY = 'AdvancedSettings:unreadBadgeCustomizationEnabled';
+export const UNREAD_BADGE_CUSTOMIZATION_STORAGE_KEY = 'AdvancedSettings:unreadBadgeCustomizationEnabled';
const KEEP_ATTACHMENTS_ON_EMPTY_MESSAGE_EDIT_STORAGE_KEY = 'AdvancedSettings:keepAttachmentsOnEmptyMessageEdit';
const EXPRESSION_CLONE_SHORTCUTS_STORAGE_KEY = 'AdvancedSettings:expressionCloneShortcutsEnabled';
-function readStoredBoolean(key: string, defaultValue = false): boolean {
- const raw = AppStorage.getItem(key);
- if (raw == null) return defaultValue;
+const logger = new Logger('AdvancedSettings');
+
+function readStoredBoolean(key: string): boolean {
+ return parseStoredBoolean(AppStorage.getItem(key));
+}
+
+export function parseStoredBoolean(raw: string | null): boolean {
+ if (raw == null) return false;
try {
- const parsed = JSON.parse(raw);
- return typeof parsed === 'boolean' ? parsed : defaultValue;
- } catch (_error) {
- return raw === 'true' ? true : raw === 'false' ? false : defaultValue;
+ const parsed: unknown = JSON.parse(raw);
+ return parsed === true;
+ } catch (error) {
+ if (!(error instanceof SyntaxError)) {
+ logger.warn('Failed to decode a stored advanced setting', error);
+ }
+ return raw === 'true';
}
}