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

Improve search result counts #2221

Merged
merged 6 commits into from
Feb 21, 2025
Merged
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
20 changes: 8 additions & 12 deletions src/app/components/editor/autocomplete/EmoticonAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { Room } from 'matrix-js-sdk';
import { AutocompleteQuery } from './autocompleteQuery';
import { AutocompleteMenu } from './AutocompleteMenu';
import { useMatrixClient } from '../../../hooks/useMatrixClient';
import {
SearchItemStrGetter,
UseAsyncSearchOptions,
useAsyncSearch,
} from '../../../hooks/useAsyncSearch';
import { UseAsyncSearchOptions, useAsyncSearch } from '../../../hooks/useAsyncSearch';
import { onTabPress } from '../../../utils/keyboard';
import { createEmoticonElement, moveCursor, replaceWithElement } from '../utils';
import { useRecentEmoji } from '../../../hooks/useRecentEmoji';
Expand All @@ -20,6 +16,7 @@ import { useKeyDown } from '../../../hooks/useKeyDown';
import { mxcUrlToHttp } from '../../../utils/matrix';
import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
import { ImageUsage, PackImageReader } from '../../../plugins/custom-emoji';
import { getEmoticonSearchStr } from '../../../plugins/utils';

type EmoticonCompleteHandler = (key: string, shortcode: string) => void;

Expand All @@ -33,16 +30,11 @@ type EmoticonAutocompleteProps = {
};

const SEARCH_OPTIONS: UseAsyncSearchOptions = {
limit: 20,
matchOptions: {
contain: true,
},
};

const getEmoticonStr: SearchItemStrGetter<EmoticonSearchItem> = (emoticon) => [
`:${emoticon.shortcode}:`,
];

export function EmoticonAutocomplete({
imagePackRooms,
editor,
Expand All @@ -63,8 +55,12 @@ export function EmoticonAutocomplete({
);
}, [imagePacks]);

const [result, search, resetSearch] = useAsyncSearch(searchList, getEmoticonStr, SEARCH_OPTIONS);
const autoCompleteEmoticon = result ? result.items : recentEmoji;
const [result, search, resetSearch] = useAsyncSearch(
searchList,
getEmoticonSearchStr,
SEARCH_OPTIONS
);
const autoCompleteEmoticon = result ? result.items.slice(0, 20) : recentEmoji;

useEffect(() => {
if (query.text) search(query.text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ type RoomMentionAutocompleteProps = {
};

const SEARCH_OPTIONS: UseAsyncSearchOptions = {
limit: 20,
matchOptions: {
contain: true,
},
Expand Down Expand Up @@ -97,7 +96,7 @@ export function RoomMentionAutocomplete({
SEARCH_OPTIONS
);

const autoCompleteRoomIds = result ? result.items : allRooms.slice(0, 20);
const autoCompleteRoomIds = result ? result.items.slice(0, 20) : allRooms.slice(0, 20);

useEffect(() => {
if (query.text) search(query.text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const withAllowedMembership = (member: RoomMember): boolean =>
member.membership === Membership.Knock;

const SEARCH_OPTIONS: UseAsyncSearchOptions = {
limit: 20,
limit: 1000,
matchOptions: {
contain: true,
},
Expand All @@ -97,7 +97,7 @@ export function UserMentionAutocomplete({
const members = useRoomMembers(mx, roomId);

const [result, search, resetSearch] = useAsyncSearch(members, getRoomMemberStr, SEARCH_OPTIONS);
const autoCompleteMembers = (result ? result.items : members.slice(0, 20)).filter(
const autoCompleteMembers = (result ? result.items.slice(0, 20) : members.slice(0, 20)).filter(
withAllowedMembership
);

Expand Down
20 changes: 8 additions & 12 deletions src/app/components/emoji-board/EmojiBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { addRecentEmoji } from '../../plugins/recent-emoji';
import { mobileOrTablet } from '../../utils/user-agent';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
import { ImagePack, ImageUsage, PackImageReader } from '../../plugins/custom-emoji';
import { getEmoticonSearchStr } from '../../plugins/utils';

const RECENT_GROUP_ID = 'recent_group';
const SEARCH_GROUP_ID = 'search_group';
Expand Down Expand Up @@ -636,15 +637,8 @@ export const NativeEmojiGroups = memo(
)
);

const getSearchListItemStr = (item: PackImageReader | IEmoji) => {
const shortcode = `:${item.shortcode}:`;
if ('body' in item) {
return [shortcode, item.body ?? ''];
}
return shortcode;
};
const SEARCH_OPTIONS: UseAsyncSearchOptions = {
limit: 26,
limit: 1000,
matchOptions: {
contain: true,
},
Expand Down Expand Up @@ -696,10 +690,12 @@ export function EmojiBoard({

const [result, search, resetSearch] = useAsyncSearch(
searchList,
getSearchListItemStr,
getEmoticonSearchStr,
SEARCH_OPTIONS
);

const searchedItems = result?.items.slice(0, 100);

const handleOnChange: ChangeEventHandler<HTMLInputElement> = useDebounce(
useCallback(
(evt) => {
Expand Down Expand Up @@ -920,13 +916,13 @@ export function EmojiBoard({
direction="Column"
gap="200"
>
{result && (
{searchedItems && (
<SearchEmojiGroup
mx={mx}
tab={tab}
id={SEARCH_GROUP_ID}
label={result.items.length ? 'Search Results' : 'No Results found'}
emojis={result.items}
label={searchedItems.length ? 'Search Results' : 'No Results found'}
emojis={searchedItems}
useAuthentication={useAuthentication}
/>
)}
Expand Down
27 changes: 15 additions & 12 deletions src/app/features/room/MembersDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export type MembersFilterOptions = {
};

const SEARCH_OPTIONS: UseAsyncSearchOptions = {
limit: 100,
limit: 1000,
matchOptions: {
contain: true,
},
Expand Down Expand Up @@ -428,8 +428,9 @@ export function MembersDrawer({ room, members }: MembersDrawerProps) {
}}
after={<Icon size="50" src={Icons.Cross} />}
>
<Text size="B300">{`${result.items.length || 'No'} ${result.items.length === 1 ? 'Result' : 'Results'
}`}</Text>
<Text size="B300">{`${result.items.length || 'No'} ${
result.items.length === 1 ? 'Result' : 'Results'
}`}</Text>
</Chip>
)
}
Expand Down Expand Up @@ -485,15 +486,17 @@ export function MembersDrawer({ room, members }: MembersDrawerProps) {
const member = tagOrMember;
const name = getName(member);
const avatarMxcUrl = member.getMxcAvatarUrl();
const avatarUrl = avatarMxcUrl ? mx.mxcUrlToHttp(
avatarMxcUrl,
100,
100,
'crop',
undefined,
false,
useAuthentication
) : undefined;
const avatarUrl = avatarMxcUrl
? mx.mxcUrlToHttp(
avatarMxcUrl,
100,
100,
'crop',
undefined,
false,
useAuthentication
)
: undefined;

return (
<MenuItem
Expand Down
19 changes: 19 additions & 0 deletions src/app/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SearchItemStrGetter } from '../hooks/useAsyncSearch';
import { PackImageReader } from './custom-emoji';
import { IEmoji } from './emoji';

export const getEmoticonSearchStr: SearchItemStrGetter<PackImageReader | IEmoji> = (item) => {
const shortcode = `:${item.shortcode}:`;
if (item instanceof PackImageReader) {
if (item.body) {
return [shortcode, item.body];
}
return shortcode;
}

const names = [shortcode, item.label];
if (Array.isArray(item.shortcodes)) {
return names.concat(item.shortcodes);
}
return names;
};