Skip to content

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

TypeScript fluxerfluxerapp

refactor(app): flatten search provider and filter choices

Hampus5c346178eb3c
diff --git a/fluxer_app/src/features/search/components/modals/SearchProviderPickerModal.tsx b/fluxer_app/src/features/search/components/modals/SearchProviderPickerModal.tsx
index d5817c771..5e9ff49f4 100644
--- a/fluxer_app/src/features/search/components/modals/SearchProviderPickerModal.tsx
+++ b/fluxer_app/src/features/search/components/modals/SearchProviderPickerModal.tsx
@@ -3,9 +3,7 @@
import * as Modal from '@app/features/app/components/dialogs/Modal';
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import {CANCEL_DESCRIPTOR, TRANSLATE_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 {Combobox, type ComboboxOption} from '@app/features/ui/components/form/FormCombobox';
@@ -14,6 +12,7 @@ import {msg, ph} from '@lingui/core/macro';
import {Trans, useLingui} from '@lingui/react/macro';
import {observer} from 'mobx-react-lite';
import type React from 'react';
+import type {ReactNode} from 'react';
import {useCallback, useMemo, useState} from 'react';
const PICK_A_REVERSE_IMAGE_SEARCH_PROVIDER_DESCRIPTOR = msg({
@@ -51,6 +50,24 @@ const SEARCH_THE_WEB_DESCRIPTOR = msg({
type SearchProviderPickerMode = 'text' | 'image' | 'translate';
+const PICKER_MESSAGES = {
+ text: {
+ title: PICK_A_WEB_SEARCH_PROVIDER_DESCRIPTOR,
+ selectLabel: WEB_SEARCH_PROVIDER_DESCRIPTOR,
+ confirmLabel: SEARCH_THE_WEB_DESCRIPTOR,
+ },
+ image: {
+ title: PICK_A_REVERSE_IMAGE_SEARCH_PROVIDER_DESCRIPTOR,
+ selectLabel: REVERSE_IMAGE_SEARCH_PROVIDER_DESCRIPTOR,
+ confirmLabel: SEARCH_DESCRIPTOR,
+ },
+ translate: {
+ title: PICK_A_TRANSLATION_PROVIDER_DESCRIPTOR,
+ selectLabel: TRANSLATION_PROVIDER_DESCRIPTOR,
+ confirmLabel: TRANSLATE_DESCRIPTOR,
+ },
+};
+
interface SearchProviderPickerModalProps {
mode: SearchProviderPickerMode;
onPick: (engineId: string) => void;
@@ -61,7 +78,7 @@ export const SearchProviderPickerModal: React.FC<SearchProviderPickerModalProps>
const isImageMode = mode === 'image';
const isTranslateMode = mode === 'translate';
const searchEnginesSettingsPath = formatUserSettingsPath(i18n, 'advanced_settings');
- const store = isImageMode ? ReverseImageSearch : isTranslateMode ? Translation : SearchEngine;
+ const store = getSearchProviderStore(mode);
const engines = store.engines;
const options = useMemo<ReadonlyArray<ComboboxOption>>(
() => engines.map((engine) => ({value: engine.id, label: engine.name})),
@@ -86,37 +103,38 @@ export const SearchProviderPickerModal: React.FC<SearchProviderPickerModalProps>
ModalCommands.pop();
onPick(engineId);
}, [engines, onPick, selectedEngineId, store]);
- const title = isImageMode
- ? i18n._(PICK_A_REVERSE_IMAGE_SEARCH_PROVIDER_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(PICK_A_TRANSLATION_PROVIDER_DESCRIPTOR)
- : i18n._(PICK_A_WEB_SEARCH_PROVIDER_DESCRIPTOR);
- const description = isImageMode ? (
- <Trans>
- Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should send images for reverse image search. We'll
- remember your pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
- </Trans>
- ) : isTranslateMode ? (
- <Trans>
- Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should send highlighted text for translation. We'll
- remember your pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
- </Trans>
- ) : (
- <Trans>
- Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should search highlighted text. We'll remember your
- pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
- </Trans>
- );
- const selectLabel = isImageMode
- ? i18n._(REVERSE_IMAGE_SEARCH_PROVIDER_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(TRANSLATION_PROVIDER_DESCRIPTOR)
- : i18n._(WEB_SEARCH_PROVIDER_DESCRIPTOR);
- const confirmLabel = isImageMode
- ? i18n._(SEARCH_DESCRIPTOR)
- : isTranslateMode
- ? i18n._(TRANSLATE_DESCRIPTOR)
- : i18n._(SEARCH_THE_WEB_DESCRIPTOR);
+ let messages = PICKER_MESSAGES.text;
+ if (isImageMode) {
+ messages = PICKER_MESSAGES.image;
+ } else if (isTranslateMode) {
+ messages = PICKER_MESSAGES.translate;
+ }
+ const title = i18n._(messages.title);
+ let description: ReactNode;
+ if (isImageMode) {
+ description = (
+ <Trans>
+ Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should send images for reverse image search. We'll
+ remember your pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
+ </Trans>
+ );
+ } else if (isTranslateMode) {
+ description = (
+ <Trans>
+ Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should send highlighted text for translation. We'll
+ remember your pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
+ </Trans>
+ );
+ } else {
+ description = (
+ <Trans>
+ Choose where {ph({PRODUCT_NAME: RuntimeConfig.productName})} should search highlighted text. We'll remember your
+ pick. You can change it or add your own provider later in {searchEnginesSettingsPath}.
+ </Trans>
+ );
+ }
+ const selectLabel = i18n._(messages.selectLabel);
+ const confirmLabel = i18n._(messages.confirmLabel);
const hasSelectedEngine = engines.some((engine) => engine.id === selectedEngineId);
return (
<Modal.Root size="small" onClose={handleClose} data-flx="search.search-provider-picker-modal.modal-root">
diff --git a/fluxer_app/src/features/search/utils/SearchProviderUtils.ts b/fluxer_app/src/features/search/utils/SearchProviderUtils.ts
new file mode 100644
index 000000000..fc4bde4dd
--- /dev/null
+++ b/fluxer_app/src/features/search/utils/SearchProviderUtils.ts
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import Translation from '@app/features/messaging/state/Translation';
+import ReverseImageSearch from '@app/features/search/state/ReverseImageSearch';
+import SearchEngine from '@app/features/search/state/SearchEngine';
+
+export function getSearchProviderStore(mode: 'text' | 'image' | 'translate') {
+ if (mode === 'image') {
+ return ReverseImageSearch;
+ }
+ if (mode === 'translate') {
+ return Translation;
+ }
+ return SearchEngine;
+}
diff --git a/fluxer_app/src/features/search/utils/SearchQueryParser.ts b/fluxer_app/src/features/search/utils/SearchQueryParser.ts
index 3f226e72e..7b43fd5d6 100644
--- a/fluxer_app/src/features/search/utils/SearchQueryParser.ts
+++ b/fluxer_app/src/features/search/utils/SearchQueryParser.ts
@@ -14,7 +14,7 @@ export interface SearchHints {
channelsByName?: Record<string, string>;
}
-export interface ParsedToken {
+interface ParsedToken {
key: string;
value: string;
start: number;
@@ -32,7 +32,7 @@ export const SearchChipRole = Object.freeze({
export type SearchChipRole = (typeof SearchChipRole)[keyof typeof SearchChipRole];
-export interface SearchChip {
+interface SearchChip {
role: SearchChipRole;
key: string;
exclude: boolean;
@@ -384,7 +384,7 @@ const tryResolveChannel = (name: string, guildId?: string | null, hints?: Search
return partial?.id ?? null;
};
-export interface ParseContext {
+interface ParseContext {
guildId?: string | null;
}
@@ -643,13 +643,11 @@ export function parseQuery(query: string, hints?: SearchHints, ctx?: ParseContex
const values = splitCSV(tok.value);
for (const tag of values) {
const id = tryResolveUser(tag, hints);
- if (id) {
- if (isExcludeKey) {
- params.excludeAuthorId = add(params.excludeAuthorId, id);
- } else {
- params.authorId = add(params.authorId, id);
- }
+ if (!id) {
+ continue;
}
+ const destination = isExcludeKey ? 'excludeAuthorId' : 'authorId';
+ params[destination] = add(params[destination], id);
}
break;
}
@@ -659,16 +657,14 @@ export function parseQuery(query: string, hints?: SearchHints, ctx?: ParseContex
const lower = v.toLowerCase();
if (lower === 'everyone' || lower === 'here') {
params.mentionEveryone = true;
- } else {
- const id = tryResolveUser(v, hints);
- if (id) {
- if (isExcludeKey) {
- params.excludeMentions = add(params.excludeMentions, id);
- } else {
- params.mentions = add(params.mentions, id);
- }
- }
+ continue;
+ }
+ const id = tryResolveUser(v, hints);
+ if (!id) {
+ continue;
}
+ const destination = isExcludeKey ? 'excludeMentions' : 'mentions';
+ params[destination] = add(params[destination], id);
}
break;
}
@@ -676,13 +672,11 @@ export function parseQuery(query: string, hints?: SearchHints, ctx?: ParseContex
const values = splitCSV(tok.value);
for (const v of values) {
const id = tryResolveChannel(v, ctx?.guildId ?? null, hints);
- if (id) {
- if (isExcludeKey) {
- params.excludeChannelId = add(params.excludeChannelId, id);
- } else {
- params.channelId = add(params.channelId, id);
- }
+ if (!id) {
+ continue;
}
+ const destination = isExcludeKey ? 'excludeChannelId' : 'channelId';
+ params[destination] = add(params[destination], id);
}
break;
}
@@ -695,12 +689,11 @@ export function parseQuery(query: string, hints?: SearchHints, ctx?: ParseContex
.filter(Boolean);
for (const value of values) {
const normalized = normalizeHasValue(value);
- if (!normalized) continue;
- if (isExcludeKey) {
- params.excludeHas = add(params.excludeHas, normalized);
- } else {
- params.has = add(params.has, normalized);
+ if (!normalized) {
+ continue;
}
+ const destination = isExcludeKey ? 'excludeHas' : 'has';
+ params[destination] = add(params[destination], normalized);
}
break;
}
@@ -744,13 +737,11 @@ export function parseQuery(query: string, hints?: SearchHints, ctx?: ParseContex
case 'embed-type': {
const values = splitCSV(tok.value).map((v) => v.toLowerCase());
for (const v of values) {
- if (isExcludeKey) {
- if (isEmbedTypeFilter(v)) {
- params.excludeEmbedType = add(params.excludeEmbedType, v);
- }
- } else if (isEmbedTypeFilter(v)) {
- params.embedType = add(params.embedType, v);
+ if (!isEmbedTypeFilter(v)) {
+ continue;
}
+ const destination = isExcludeKey ? 'excludeEmbedType' : 'embedType';
+ params[destination] = add(params[destination], v);
}
break;
}