Skip to content

Commit

Permalink
Remove custom sounds meteor.startup
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed Oct 17, 2023
1 parent dd5b236 commit 34c190c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
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();
29 changes: 27 additions & 2 deletions apps/meteor/client/providers/CustomSoundProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { CustomSoundContext } from '@rocket.chat/ui-contexts';
import { CustomSoundContext, useUserId } 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';
import { sdk } from '../../app/utils/client/lib/SDKClient';

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

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

return sdk.stream('notify-all', ['public-info'], ([key, data]) => {
switch (key) {
case 'updateCustomSound':
CustomSounds.update(data[0].soundData);
break;
case 'deleteCustomSound':
CustomSounds.remove(data[0].soundData);
break;
}
}).stop;
}, [userId]);
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

0 comments on commit 34c190c

Please sign in to comment.