Files
ThreadNet-Web/src/components/views/rooms/SearchResultTile.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
5.5 KiB
TypeScript
Raw Normal View History

2015-12-23 10:10:08 +00:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019-2023 The Matrix.org Foundation C.I.C.
2015-12-23 10:10:08 +00:00
Copyright 2015 OpenMarket Ltd
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2015-12-23 10:10:08 +00:00
*/
import React from "react";
2025-02-05 13:25:06 +00:00
import { type MatrixEvent } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
import RoomContext, { TimelineRenderingType } from "../../../contexts/RoomContext";
2020-09-16 11:26:15 +01:00
import SettingsStore from "../../../settings/SettingsStore";
2025-02-05 13:25:06 +00:00
import { type RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
import DateSeparator from "../messages/DateSeparator";
import EventTile from "./EventTile";
import { shouldFormContinuation } from "../../structures/MessagePanel";
import { wantsDateSeparator } from "../../../DateUtils";
2025-02-05 13:25:06 +00:00
import type LegacyCallEventGrouper from "../../structures/LegacyCallEventGrouper";
import { buildLegacyCallEventGroupers } from "../../structures/LegacyCallEventGrouper";
import { haveRendererForEvent } from "../../../events/EventTileFactory";
import { MatrixClientPeg } from "../../../MatrixClientPeg";
interface IProps {
2021-07-01 11:17:18 +01:00
// a list of strings to be highlighted in the results
searchHighlights?: string[];
// href for the highlights in this result
resultLink?: string;
// timeline of the search result
timeline: MatrixEvent[];
// indexes of the matching events (not contextual ones)
ourEventsIndexes: number[];
2021-07-01 11:17:18 +01:00
onHeightChanged?: () => void;
permalinkCreator?: RoomPermalinkCreator;
}
2015-12-23 10:10:08 +00:00
export default class SearchResultTile extends React.Component<IProps> {
public static contextType = RoomContext;
2024-12-02 09:39:36 +00:00
declare public context: React.ContextType<typeof RoomContext>;
// A map of <callId, LegacyCallEventGrouper>
private callEventGroupers = new Map<string, LegacyCallEventGrouper>();
2023-02-13 11:39:16 +00:00
public constructor(props: IProps, context: React.ContextType<typeof RoomContext>) {
super(props, context);
this.buildLegacyCallEventGroupers(this.props.timeline);
}
private buildLegacyCallEventGroupers(events?: MatrixEvent[]): void {
this.callEventGroupers = buildLegacyCallEventGroupers(this.callEventGroupers, events);
}
public render(): React.ReactNode {
const timeline = this.props.timeline;
const resultEvent = timeline[this.props.ourEventsIndexes[0]];
const eventId = resultEvent.getId();
2015-12-23 10:10:08 +00:00
const ts1 = resultEvent.getTs();
const ret = [<DateSeparator key={ts1 + "-search"} roomId={resultEvent.getRoomId()!} ts={ts1} />];
const layout = SettingsStore.getValue("layout");
const isTwelveHour = SettingsStore.getValue("showTwelveHourTimestamps");
2021-06-06 01:55:01 -04:00
const alwaysShowTimestamps = SettingsStore.getValue("alwaysShowTimestamps");
2015-12-23 10:10:08 +00:00
const cli = MatrixClientPeg.safeGet();
2020-09-16 11:26:15 +01:00
for (let j = 0; j < timeline.length; j++) {
const mxEv = timeline[j];
2023-04-25 09:30:32 +01:00
let highlights: string[] | undefined;
const contextual = !this.props.ourEventsIndexes.includes(j);
2015-12-23 10:10:08 +00:00
if (!contextual) {
highlights = this.props.searchHighlights;
}
if (haveRendererForEvent(mxEv, cli, this.context?.showHiddenEvents)) {
// do we need a date separator since the last event?
const prevEv = timeline[j - 1];
// is this a continuation of the previous message?
const continuation =
prevEv &&
!wantsDateSeparator(prevEv.getDate() || undefined, mxEv.getDate() || undefined) &&
shouldFormContinuation(
prevEv,
mxEv,
cli,
this.context?.showHiddenEvents,
TimelineRenderingType.Search,
);
let lastInSection = true;
const nextEv = timeline[j + 1];
if (nextEv) {
const willWantDateSeparator = wantsDateSeparator(
mxEv.getDate() || undefined,
nextEv.getDate() || undefined,
);
lastInSection =
willWantDateSeparator ||
mxEv.getSender() !== nextEv.getSender() ||
!shouldFormContinuation(
mxEv,
nextEv,
cli,
this.context?.showHiddenEvents,
TimelineRenderingType.Search,
);
}
ret.push(
2020-09-16 11:26:15 +01:00
<EventTile
key={`${eventId}+${j}`}
mxEvent={mxEv}
layout={layout}
2020-09-16 11:26:15 +01:00
contextual={contextual}
highlights={highlights}
permalinkCreator={this.props.permalinkCreator}
highlightLink={this.props.resultLink}
onHeightChanged={this.props.onHeightChanged}
isTwelveHour={isTwelveHour}
2021-06-06 01:55:01 -04:00
alwaysShowTimestamps={alwaysShowTimestamps}
lastInSection={lastInSection}
continuation={continuation}
callEventGrouper={this.callEventGroupers.get(mxEv.getContent().call_id)}
/>,
);
2015-12-23 10:10:08 +00:00
}
}
return (
<li data-scroll-tokens={eventId}>
<ol>{ret}</ol>
</li>
);
2020-08-29 12:14:16 +01:00
}
}