Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

116 lines
4.1 KiB
TypeScript

import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import { escapeRegExp, isTruthy } from '@rocket.chat/tools';
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
import { useEndpoint, useUserSubscriptions } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { getConfig } from '../../../lib/utils/getConfig';
const LIMIT = parseInt(String(getConfig('Sidebar_Search_Spotlight_LIMIT', 20)));
const options = {
sort: {
lm: -1,
name: 1,
},
limit: LIMIT,
} as const;
type AISearchRoomResult = {
_id: string;
name: string;
fname?: string;
t: string;
rid?: string;
status?: string;
avatarETag?: string;
outside?: boolean;
uids?: string[];
};
export const useAISearchRooms = (filterText = ''): { items: SubscriptionWithRoom[]; isLoading: boolean } => {
const [, mention, name] = useMemo(() => filterText.match(/(@|#)?(.*)/i) || [], [filterText]);
const query = useMemo(() => {
const filterRegex = new RegExp(escapeRegExp(name), 'i');
return {
...(mention && {
t: mention === '@' ? 'd' : { $ne: 'd' },
}),
$or: [{ name: filterRegex }, { fname: filterRegex }],
};
}, [name, mention]);
const localRooms = useUserSubscriptions(query, options);
const debouncedName = useDebouncedValue(name, 500);
const usernamesFromClient = localRooms.map(({ t, name }) => (t === 'd' ? name : null)).filter(isTruthy);
const searchForChannels = mention === '#';
const searchForDMs = mention === '@';
const type = useMemo(() => {
if (searchForChannels) {
return { users: false, rooms: true, includeFederatedRooms: true };
}
if (searchForDMs) {
return { users: true, rooms: false };
}
return { users: true, rooms: true, includeFederatedRooms: true };
}, [searchForChannels, searchForDMs]);
const getSpotlight = useEndpoint('GET', '/v1/spotlight');
const { data: serverResults, isFetching } = useQuery({
queryKey: ['sidebar/ai-search/spotlight', debouncedName, mention, type],
enabled: localRooms.length < LIMIT,
queryFn: async () => {
const spotlight = await getSpotlight({
query: debouncedName,
usernames: usernamesFromClient.join(','),
type: JSON.stringify(type),
});
const filterUsersUnique = ({ _id }: { _id: string }, index: number, users: { _id: string }[]): boolean =>
index === users.findIndex((user) => _id === user._id);
const userMap = (user: { _id: string; name: string; username: string; status?: string }): AISearchRoomResult => ({
_id: user._id,
name: user.username,
fname: user.name,
t: 'd',
rid: user._id,
status: user.status,
});
const results: AISearchRoomResult[] = spotlight.users.filter(filterUsersUnique).map(userMap);
results.push(...spotlight.rooms);
return results;
},
staleTime: 60_000,
});
const items = useMemo(() => {
const filterRegex = new RegExp(escapeRegExp(name), 'i');
const matchesFilter = ({ name, fname }: { name?: string; fname?: string }) =>
(name && filterRegex.test(name)) || (fname && filterRegex.test(fname));
const isLocalDuplicate = (item: { _id: string; t?: string; uids?: string[]; name: string }): boolean =>
localRooms.some((room) => {
const sameRoom = [room.rid, room._id].includes(item._id);
const sameGroupDM = item.t === 'd' && !!item.uids && item.uids.length > 1 && item.uids.includes(room._id);
const sameDirectDM = item.t === 'd' && room.t === 'd' && !!room.uids && room.uids.length === 2 && room.uids.includes(item._id);
const sameUserDM = item.t === 'd' && room.t === 'd' && item.name === room.name;
return sameRoom || sameGroupDM || sameDirectDM || sameUserDM;
});
const candidates = name === debouncedName && localRooms.length < LIMIT ? (serverResults ?? []) : [];
const fromServer = candidates.filter((item) => matchesFilter(item) && !isLocalDuplicate(item));
const exact = fromServer.filter((item) => [item.name, item.fname].includes(name));
return [...exact, ...localRooms, ...fromServer.filter((item) => !exact.includes(item))] as SubscriptionWithRoom[];
}, [debouncedName, localRooms, name, serverResults]);
const isLoading = isFetching && serverResults === undefined;
return { items, isLoading };
};