2025-01-15 09:56:50 +01:00
|
|
|
import React, { useCallback, useMemo } from 'react';
|
2025-01-05 10:31:20 +01:00
|
|
|
import { Room } from 'matrix-js-sdk';
|
|
|
|
import { usePowerLevels, usePowerLevelsAPI } from '../../hooks/usePowerLevels';
|
|
|
|
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
|
|
|
import { ImagePackContent } from './ImagePackContent';
|
2025-01-15 09:56:50 +01:00
|
|
|
import { ImagePack, PackContent } from '../../plugins/custom-emoji';
|
2025-01-05 10:31:20 +01:00
|
|
|
import { StateEvent } from '../../../types/matrix/room';
|
2025-01-15 09:56:50 +01:00
|
|
|
import { useRoomImagePack } from '../../hooks/useImagePacks';
|
|
|
|
import { randomStr } from '../../utils/common';
|
2025-01-05 10:31:20 +01:00
|
|
|
|
|
|
|
type RoomImagePackProps = {
|
|
|
|
room: Room;
|
2025-01-15 09:56:50 +01:00
|
|
|
stateKey: string;
|
2025-01-05 10:31:20 +01:00
|
|
|
};
|
|
|
|
|
2025-01-15 09:56:50 +01:00
|
|
|
export function RoomImagePack({ room, stateKey }: RoomImagePackProps) {
|
2025-01-05 10:31:20 +01:00
|
|
|
const mx = useMatrixClient();
|
|
|
|
const userId = mx.getUserId()!;
|
|
|
|
const powerLevels = usePowerLevels(room);
|
|
|
|
|
|
|
|
const { getPowerLevel, canSendStateEvent } = usePowerLevelsAPI(powerLevels);
|
|
|
|
const canEditImagePack = canSendStateEvent(StateEvent.PoniesRoomEmotes, getPowerLevel(userId));
|
|
|
|
|
2025-01-15 09:56:50 +01:00
|
|
|
const fallbackPack = useMemo(() => {
|
|
|
|
const fakePackId = randomStr(4);
|
|
|
|
return new ImagePack(
|
|
|
|
fakePackId,
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
roomId: room.roomId,
|
|
|
|
stateKey,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, [room.roomId, stateKey]);
|
|
|
|
const imagePack = useRoomImagePack(room, stateKey) ?? fallbackPack;
|
|
|
|
|
|
|
|
const handleUpdate = useCallback(
|
|
|
|
async (packContent: PackContent) => {
|
|
|
|
const { address } = imagePack;
|
|
|
|
if (!address) return;
|
|
|
|
|
|
|
|
await mx.sendStateEvent(
|
|
|
|
address.roomId,
|
|
|
|
'StateEvent.PoniesRoomEmotes',
|
|
|
|
packContent,
|
|
|
|
address.stateKey
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[mx, imagePack]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ImagePackContent imagePack={imagePack} canEdit={canEditImagePack} onUpdate={handleUpdate} />
|
|
|
|
);
|
2025-01-05 10:31:20 +01:00
|
|
|
}
|