Files
ThreadNet-Web/src/components/views/elements/EffectsOverlay.tsx
T

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

108 lines
3.8 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2020 Nurjin Jafar
Copyright 2020 Nordeck IT + Consulting GmbH.
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.
*/
2025-02-05 13:25:06 +00:00
import React, { type FunctionComponent, useEffect, useRef } from "react";
2021-10-22 17:23:32 -05:00
import { logger } from "matrix-js-sdk/src/logger";
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 dis from "../../../dispatcher/dispatcher";
2025-02-05 13:25:06 +00:00
import type ICanvasEffect from "../../../effects/ICanvasEffect";
2021-06-29 13:11:58 +01:00
import { CHAT_EFFECTS } from "../../../effects";
2021-05-25 09:53:22 +01:00
import UIStore, { UI_EVENTS } from "../../../stores/UIStore";
2020-10-19 21:25:01 +02:00
2020-12-07 15:12:26 -07:00
interface IProps {
2020-10-19 21:25:01 +02:00
roomWidth: number;
}
2020-12-07 15:12:26 -07:00
const EffectsOverlay: FunctionComponent<IProps> = ({ roomWidth }) => {
2020-10-19 21:25:01 +02:00
const canvasRef = useRef<HTMLCanvasElement>(null);
2020-10-21 17:58:54 +02:00
const effectsRef = useRef<Map<string, ICanvasEffect>>(new Map<string, ICanvasEffect>());
2020-10-19 21:25:01 +02:00
const lazyLoadEffectModule = async (name: string): Promise<ICanvasEffect | null> => {
2020-10-21 14:29:25 +02:00
if (!name) return null;
2023-02-13 11:39:16 +00:00
let effect: ICanvasEffect | null = effectsRef.current.get(name) || null;
2020-10-21 14:29:25 +02:00
if (effect === null) {
2021-06-29 13:11:58 +01:00
const options = CHAT_EFFECTS.find((e) => e.command === name)?.options;
2020-10-19 21:25:01 +02:00
try {
2020-12-07 15:12:26 -07:00
const { default: Effect } = await import(`../../../effects/${name}`);
2020-10-21 17:58:54 +02:00
effect = new Effect(options);
effectsRef.current.set(name, effect!);
2020-10-19 21:25:01 +02:00
} catch (err) {
2021-10-15 16:31:29 +02:00
logger.warn(`Unable to load effect module at '../../../effects/${name}.`, err);
2020-10-19 21:25:01 +02:00
}
}
return effect;
};
useEffect(() => {
const resize = (): void => {
2021-05-25 09:53:22 +01:00
if (canvasRef.current && canvasRef.current?.height !== UIStore.instance.windowHeight) {
canvasRef.current.height = UIStore.instance.windowHeight;
2020-12-01 17:15:02 +01:00
}
2020-10-21 14:43:09 +02:00
};
const onAction = (payload: { action: string; event?: MatrixEvent }): void => {
2020-10-21 14:43:09 +02:00
const actionPrefix = "effects.";
const isOutdated = isEventOutdated(payload.event);
if (canvasRef.current && payload.action.startsWith(actionPrefix) && !isOutdated) {
const effect = payload.action.slice(actionPrefix.length);
lazyLoadEffectModule(effect).then((module) => module?.start(canvasRef.current!));
2020-10-21 14:43:09 +02:00
}
2021-06-29 13:11:58 +01:00
};
2020-10-19 21:25:01 +02:00
const dispatcherRef = dis.register(onAction);
const canvas = canvasRef.current;
if (canvas) canvas.height = UIStore.instance.windowHeight;
2021-05-25 09:53:22 +01:00
UIStore.instance.on(UI_EVENTS.Resize, resize);
2020-10-21 14:29:25 +02:00
const currentEffects = effectsRef.current; // this is not a react node ref, warning can be safely ignored
2020-10-21 14:29:25 +02:00
return () => {
2020-10-19 21:25:01 +02:00
dis.unregister(dispatcherRef);
2021-05-25 09:53:22 +01:00
UIStore.instance.off(UI_EVENTS.Resize, resize);
2020-10-21 14:43:09 +02:00
for (const effect in currentEffects) {
const effectModule: ICanvasEffect = currentEffects.get(effect)!;
2020-10-21 14:56:04 +02:00
if (effectModule && effectModule.isRunning) {
2020-10-21 14:43:09 +02:00
effectModule.stop();
}
2020-10-19 21:25:01 +02:00
}
};
}, []);
return (
<canvas
ref={canvasRef}
2020-10-21 14:43:09 +02:00
width={roomWidth}
2020-10-19 21:25:01 +02:00
style={{
display: "block",
zIndex: 999999,
pointerEvents: "none",
position: "fixed",
top: 0,
right: 0,
}}
aria-hidden={true}
2020-10-19 21:25:01 +02:00
/>
2021-06-29 13:11:58 +01:00
);
};
2020-10-19 21:25:01 +02:00
2020-10-21 14:43:09 +02:00
export default EffectsOverlay;
// 48 hours
// 48h * 60m * 60s * 1000ms
const OUTDATED_EVENT_THRESHOLD = 48 * 60 * 60 * 1000;
/**
* Return true if the event is older than 48h.
* @param event
*/
function isEventOutdated(event?: MatrixEvent): boolean {
if (!event) return false;
const nowTs = Date.now();
const eventTs = event.getTs();
return nowTs - eventTs > OUTDATED_EVENT_THRESHOLD;
}