Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useMergedRefs, useStableCallback } from '@rocket.chat/fuselage-hooks';
import { useStream, useUserId } from '@rocket.chat/ui-contexts';
import type { ReactNode } from 'react';
import { useCallback, useMemo, useRef, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import type { MediaPlayerContextValue, PersistentAudioTrack } from './MediaPlayerContext';
import { MediaPlayerContext } from './MediaPlayerContext';
import { useReloadOnError } from '../../components/message/content/attachments/file/hooks/useReloadOnError';
import { Messages } from '../../stores';

const PLAYBACK_RATES = [1, 1.5, 2] as const;

Expand All @@ -27,6 +29,7 @@ const MediaPlayerProvider = ({ children }: MediaPlayerProviderProps) => {
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
const [playbackRate, setPlaybackRate] = useState<number>(1);
const userId = useUserId();

const trackRef = useRef<PersistentAudioTrack | null>(null);
trackRef.current = track;
Expand Down Expand Up @@ -101,6 +104,78 @@ const MediaPlayerProvider = ({ children }: MediaPlayerProviderProps) => {
});

const isActive = useCallback((id: string) => trackRef.current?.id === id, []);
const subscribeToNotifyRoom = useStream('notify-room');
const subscribeToNotifyUser = useStream('notify-user');

//For hard message delete, when Message Message_ShowDeletedStatus is off
useEffect(() => {
if (!track) {
return;
}

const { rid, mid } = track;

if (!rid || !mid) {
return;
}

const unsubscribeFromDeleteMessage = subscribeToNotifyRoom(`${rid}/deleteMessage`, ({ _id }) => {
if (_id === mid) {
close();
}
});

const unsubscribeFromDeleteMessageBulk = subscribeToNotifyRoom(`${rid}/deleteMessageBulk`, ({ ids }) => {
if (ids?.includes(mid)) {
close();
}
});

return () => {
unsubscribeFromDeleteMessage();
unsubscribeFromDeleteMessageBulk();
};
}, [track, subscribeToNotifyRoom, close]);

//For soft message delete, when Message_ShowDeletedStatus is on and server updates message.t to rm
useEffect(() => {
if (!track) {
return;
}

const unsub = Messages.use.subscribe((state, prevState) => {
const { mid } = track;
if (!mid) {
return;
}

const message = state.records.get(mid);

if (message && message !== prevState.records.get(mid) && message.t === 'rm') {
close();
}
});
Comment on lines +111 to +157

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like for this to at least be isolated in it's own hook.


return unsub;
}, [track, close]);

useEffect(() => {
if (!track || !userId) {
return;
}

const { rid } = track;

if (!rid) {
return;
}

return subscribeToNotifyUser(`${userId}/subscriptions-changed`, (event, subscription) => {
if (event === 'removed' && subscription.rid === rid) {
close();
}
});
}, [userId, subscribeToNotifyUser, track, close]);
Comment on lines +162 to +178

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you leave a public room, you might still have access to the message, so listening for subscription removal would stop playing a sound even if you still have access to the message.

Another scenario is, you're listening to an audio in a public room, without joining it, and loosing the permission preview-c-room.


const value = useMemo<MediaPlayerContextValue>(
() => ({ track, playing, currentTime, duration, playbackRate, play, toggle, seek, cyclePlaybackRate, close, isActive }),
Expand Down
Loading