Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove custom sounds meteor.startup #30659

Merged
merged 9 commits into from
Oct 27, 2023
20 changes: 0 additions & 20 deletions apps/meteor/app/custom-sounds/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
import { Meteor } from 'meteor/meteor';

import { Notifications } from '../../notifications/client';
import { CachedCollectionManager } from '../../ui-cached-collection/client';
import { CustomSounds } from './lib/CustomSounds';

Meteor.startup(() => {
CachedCollectionManager.onLogin(() => {
Notifications.onAll('public-info', ([key, data]) => {
switch (key) {
case 'updateCustomSound':
CustomSounds.update(data[0].soundData);
break;
case 'deleteCustomSound':
CustomSounds.remove(data[0].soundData);
break;
}
});
});
});
export { CustomSounds } from './lib/CustomSounds';
23 changes: 13 additions & 10 deletions apps/meteor/app/custom-sounds/client/lib/CustomSounds.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { ICustomSound } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';

import { CachedCollectionManager } from '../../../ui-cached-collection/client';
import { getURL } from '../../../utils/client';
import { sdk } from '../../../utils/client/lib/SDKClient';

Expand All @@ -28,8 +26,11 @@ const defaultSounds = [
class CustomSoundsClass {
list: ReactiveVar<Record<string, ICustomSound>>;

initialFetchDone: boolean;

constructor() {
this.list = new ReactiveVar({});
this.initialFetchDone = false;
defaultSounds.forEach((sound) => this.add(sound));
}

Expand Down Expand Up @@ -130,15 +131,17 @@ class CustomSoundsClass {

return audio && audio.duration > 0 && !audio.paused;
};
}

export const CustomSounds = new CustomSoundsClass();

Meteor.startup(() =>
CachedCollectionManager.onLogin(async () => {
fetchCustomSoundList = async () => {
if (this.initialFetchDone) {
return;
}
const result = await sdk.call('listCustomSounds');
for (const sound of result) {
CustomSounds.add(sound);
this.add(sound);
}
}),
);
this.initialFetchDone = true;
};
}

export const CustomSounds = new CustomSoundsClass();
30 changes: 28 additions & 2 deletions apps/meteor/client/providers/CustomSoundProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
import { CustomSoundContext } from '@rocket.chat/ui-contexts';
import { CustomSoundContext, useUserId, useStream } from '@rocket.chat/ui-contexts';
import type { FC } from 'react';
import React from 'react';
import React, { useEffect } from 'react';

import { CustomSounds } from '../../app/custom-sounds/client/lib/CustomSounds';

const CustomSoundProvider: FC = ({ children }) => {
const userId = useUserId();
useEffect(() => {
if (!userId) {
return;
}
void CustomSounds.fetchCustomSoundList();
}, [userId]);

const streamAll = useStream('notify-all');

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

return streamAll('public-info', ([key, data]) => {
switch (key) {
case 'updateCustomSound':
CustomSounds.update(data[0].soundData);
break;
case 'deleteCustomSound':
CustomSounds.remove(data[0].soundData);
break;
}
});
}, [userId, streamAll]);
return <CustomSoundContext.Provider children={children} value={CustomSounds} />;
};

Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/client/providers/MeteorProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const MeteorProvider: FC = ({ children }) => (
<ToastMessagesProvider>
<LayoutProvider>
<AvatarUrlProvider>
<CustomSoundProvider>
<UserProvider>
<UserProvider>
<CustomSoundProvider>
<DeviceProvider>
<ModalProvider>
<AuthorizationProvider>
Expand All @@ -56,8 +56,8 @@ const MeteorProvider: FC = ({ children }) => (
</AuthorizationProvider>
</ModalProvider>
</DeviceProvider>
</UserProvider>
</CustomSoundProvider>
</CustomSoundProvider>
</UserProvider>
</AvatarUrlProvider>
</LayoutProvider>
</ToastMessagesProvider>
Expand Down
Loading