From f1b96c13fd71f884661212e60fd8f4cfcaa9a6a2 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Sun, 17 Mar 2024 20:20:20 +0530 Subject: [PATCH] add thirdparty instance filter in server explore --- src/app/pages/client/explore/Server.tsx | 126 ++++++++++++++++++++++++ src/app/pages/paths.ts | 1 + 2 files changed, 127 insertions(+) diff --git a/src/app/pages/client/explore/Server.tsx b/src/app/pages/client/explore/Server.tsx index 144811ee..a339aa67 100644 --- a/src/app/pages/client/explore/Server.tsx +++ b/src/app/pages/client/explore/Server.tsx @@ -15,7 +15,9 @@ import { Icon, Icons, Input, + Line, Menu, + MenuItem, PopOut, Scroll, Spinner, @@ -37,12 +39,14 @@ import { getExploreServerPath, withSearchParam } from '../../pathUtils'; import * as css from './style.css'; import { allRoomsAtom } from '../../../state/room-list/roomList'; import { useRoomNavigate } from './hooks'; +import { getMxIdServer } from '../../../utils/matrix'; const getServerSearchParams = (searchParams: URLSearchParams): ExploreServerPathSearchParams => ({ limit: searchParams.get('limit') ?? undefined, since: searchParams.get('since') ?? undefined, term: searchParams.get('term') ?? undefined, type: searchParams.get('type') ?? undefined, + instance: searchParams.get('instance') ?? undefined, }); type RoomTypeFilter = { @@ -132,6 +136,106 @@ function Search({ active, loading, searchInputRef, onSearch, onReset }: SearchPr ); } +const DEFAULT_INSTANCE_NAME = 'Matrix'; +function ThirdPartyProtocolsSelector({ + instanceId, + onChange, +}: { + instanceId?: string; + onChange: (instanceId?: string) => void; +}) { + const mx = useMatrixClient(); + const [menu, setMenu] = useState(false); + + const { data } = useQuery({ + queryKey: ['thirdparty', 'protocols'], + queryFn: () => mx.getThirdpartyProtocols(), + }); + + const handleInstanceSelect: MouseEventHandler = (evt): void => { + const insId = evt.currentTarget.getAttribute('data-instance-id') ?? undefined; + onChange(insId); + setMenu(false); + }; + + const instances = data && Object.keys(data).flatMap((protocol) => data[protocol].instances); + if (!instances || instances.length === 0) return null; + const selectedInstance = instances.find((instance) => instanceId === instance.instance_id); + + return ( + setMenu(false), + clickOutsideDeactivates: true, + }} + > + + + + Protocols + + + + + {DEFAULT_INSTANCE_NAME} + + + {instances.map((instance) => ( + + + {instance.desc} + + + ))} + + + + + } + > + {(anchorRef) => ( + setMenu(!menu)} + aria-pressed={menu} + radii="Pill" + size="400" + variant={instanceId ? 'Success' : 'SurfaceVariant'} + after={} + > + + {selectedInstance?.desc ?? DEFAULT_INSTANCE_NAME} + + + )} + + ); +} + type LimitButtonProps = { limit: number; onLimitChange: (limit: string) => void; @@ -227,6 +331,8 @@ function LimitButton({ limit, onLimitChange }: LimitButtonProps) { export function PublicRooms() { const { server } = useParams(); const mx = useMatrixClient(); + const userId = mx.getUserId(); + const userServer = userId && getMxIdServer(userId); const allRooms = useAtomValue(allRoomsAtom); const { navigateSpace, navigateRoom } = useRoomNavigate(); @@ -270,6 +376,7 @@ export function PublicRooms() { generic_search_term: serverSearchParams.term, room_types: roomType !== undefined ? [roomType] : undefined, }, + third_party_instance_id: serverSearchParams.instance, } ); }, [mx, server, serverSearchParams]); @@ -282,6 +389,7 @@ export function PublicRooms() { serverSearchParams.since, serverSearchParams.term, serverSearchParams.type, + serverSearchParams.instance, ], queryFn: fetchPublicRooms, }); @@ -343,6 +451,10 @@ export function PublicRooms() { explore({ limit }); }; + const handleInstanceIdChange = (instanceId?: string) => { + explore({ instance: instanceId }); + }; + return ( @@ -415,6 +527,20 @@ export function PublicRooms() { {filter.title} ))} + {userServer === server && ( + <> + + + + )} diff --git a/src/app/pages/paths.ts b/src/app/pages/paths.ts index 6f3d18a4..4540d2b7 100644 --- a/src/app/pages/paths.ts +++ b/src/app/pages/paths.ts @@ -50,6 +50,7 @@ export type ExploreServerPathSearchParams = { since?: string; term?: string; type?: string; + instance?: string; }; export const EXPLORE_SERVER_PATH = `/explore/${_SERVER_PATH}`;