A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {Channel} from '@app/features/channel/models/Channel';
import type {Message} from '@app/features/messaging/models/MessagingMessage';
import {AccountScopedWork} from '@app/features/platform/state/AccountScopedWork';
import {makeAutoObservable, observableRef} from 'mobx';
export type MediaViewerItem = Readonly<{
src: string;
originalSrc: string;
naturalWidth: number;
naturalHeight: number;
type: 'image' | 'gif' | 'gifv' | 'video' | 'audio';
contentHash?: string | null;
attachmentId?: string;
embedIndex?: number;
filename?: string;
fileSize?: number;
contentType?: string;
duration?: number;
expiresAt?: string | null;
expired?: boolean;
animated?: boolean;
providerName?: string;
initialTime?: number;
}>;
function copyMediaViewerItems(items: ReadonlyArray<MediaViewerItem>): ReadonlyArray<MediaViewerItem> {
return Object.freeze(items.map((item) => Object.freeze({...item})));
}
class MediaViewer {
isOpen: boolean = false;
items: ReadonlyArray<MediaViewerItem> = [];
currentIndex: number = 0;
channelId?: string = undefined;
messageId?: string = undefined;
message?: Message = undefined;
sourceChannel?: Channel = undefined;
allowAttachmentDelete: boolean = false;
constructor() {
makeAutoObservable(
this,
{
items: observableRef,
message: observableRef,
sourceChannel: observableRef,
},
{autoBind: true},
);
}
open(
items: ReadonlyArray<MediaViewerItem>,
currentIndex: number,
channelId?: string,
messageId?: string,
message?: Message,
sourceChannel?: Channel | null,
allowAttachmentDelete: boolean = false,
): void {
this.isOpen = true;
this.items = copyMediaViewerItems(items);
this.currentIndex = currentIndex;
this.channelId = channelId;
this.messageId = messageId;
this.message = message;
this.sourceChannel = sourceChannel ?? undefined;
this.allowAttachmentDelete = allowAttachmentDelete;
}
close(): void {
this.isOpen = false;
this.items = [];
this.currentIndex = 0;
this.channelId = undefined;
this.messageId = undefined;
this.message = undefined;
this.sourceChannel = undefined;
this.allowAttachmentDelete = false;
}
navigate(index: number): void {
if (index < 0 || index >= this.items.length) {
return;
}
this.currentIndex = index;
}
}
const mediaViewer = new MediaViewer();
AccountScopedWork.registerCancellation(() => mediaViewer.close());
export default mediaViewer;