Files
element-web/src/hooks/usePermalinkEvent.ts
David Langley 69ee8fd96a Change License: AGPL + Element Commercial (#28856)
* Add commercial licence and update config files

* Update license in headers

* Revert "Update license in headers"

This reverts commit 7ed7949485.

* Update only spdx id

* Remove LicenseRef- from package.json

LicenseRef- no longer allowed in npm v3 package.json
This fixes the warning in the logs and failing build check.
2025-01-06 11:18:54 +00:00

82 lines
3.0 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2023 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { useEffect, useState } from "react";
import { PillType } from "../components/views/elements/Pill";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { PermalinkParts } from "../utils/permalinks/PermalinkConstructor";
/**
* Tries to find the initial event.
* If the event should not be looked up or there is no target room it returns null.
* Otherwise it tries to get the event from the target room.
*
* @param shouldLookUpEvent - whether the parmalink event should be looked up
* @param Room | null targetRoom - target room of the permalink
* @param parseResult - permalink parse result
* @returns The event if found or null if it should not be looked up or was not found.
*/
const determineInitialEvent = (
shouldLookUpEvent: boolean,
targetRoom: Room | null,
parseResult: PermalinkParts | null,
): MatrixEvent | null => {
if (!shouldLookUpEvent || !targetRoom || !parseResult?.eventId) return null;
return targetRoom.findEventById(parseResult.eventId) || null;
};
/**
* Hook to get a permalink target event
*
* @param type - Permalink type
* @param parseResult - Permalink parse result
* @param targetRoom - Target room of the permalink {@link ./usePermalinkTargetRoom.ts}
* @returns The permalink event if it targets an event and it can be loaded.
* Else null.
*/
export const usePermalinkEvent = (
type: PillType | null,
parseResult: PermalinkParts | null,
targetRoom: Room | null,
): MatrixEvent | null => {
// Event permalinks require to know the event.
// If it cannot be initially determined, it will be looked up later by a memo hook.
const shouldLookUpEvent =
!!type &&
!!parseResult?.roomIdOrAlias &&
!!parseResult?.eventId &&
[PillType.EventInSameRoom, PillType.EventInOtherRoom].includes(type);
const eventId = parseResult?.eventId;
const eventInRoom = determineInitialEvent(shouldLookUpEvent, targetRoom, parseResult);
const [event, setEvent] = useState<MatrixEvent | null>(eventInRoom);
useEffect(() => {
if (!shouldLookUpEvent || !eventId || event || !parseResult?.roomIdOrAlias || !parseResult.eventId) {
// nothing to do here
return;
}
const fetchRoomEvent = async (): Promise<void> => {
try {
const eventData = await MatrixClientPeg.safeGet().fetchRoomEvent(
parseResult.roomIdOrAlias!,
parseResult.eventId!,
);
setEvent(new MatrixEvent(eventData));
} catch {}
};
fetchRoomEvent();
}, [event, eventId, parseResult?.eventId, parseResult?.roomIdOrAlias, shouldLookUpEvent]);
return event;
};