From 34c190c52a1ab8e0b0d975a37dd1c4f262276882 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 17 Oct 2023 13:59:51 -0300 Subject: [PATCH 1/2] Remove custom sounds meteor.startup --- apps/meteor/app/custom-sounds/client/index.ts | 20 ------------- .../custom-sounds/client/lib/CustomSounds.ts | 23 ++++++++------- .../client/providers/CustomSoundProvider.tsx | 29 +++++++++++++++++-- .../client/providers/MeteorProvider.tsx | 8 ++--- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/apps/meteor/app/custom-sounds/client/index.ts b/apps/meteor/app/custom-sounds/client/index.ts index d1154824c78c..95992988ccfb 100644 --- a/apps/meteor/app/custom-sounds/client/index.ts +++ b/apps/meteor/app/custom-sounds/client/index.ts @@ -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'; diff --git a/apps/meteor/app/custom-sounds/client/lib/CustomSounds.ts b/apps/meteor/app/custom-sounds/client/lib/CustomSounds.ts index f881c15f9886..f925caf7f809 100644 --- a/apps/meteor/app/custom-sounds/client/lib/CustomSounds.ts +++ b/apps/meteor/app/custom-sounds/client/lib/CustomSounds.ts @@ -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'; @@ -28,8 +26,11 @@ const defaultSounds = [ class CustomSoundsClass { list: ReactiveVar>; + initialFetchDone: boolean; + constructor() { this.list = new ReactiveVar({}); + this.initialFetchDone = false; defaultSounds.forEach((sound) => this.add(sound)); } @@ -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(); diff --git a/apps/meteor/client/providers/CustomSoundProvider.tsx b/apps/meteor/client/providers/CustomSoundProvider.tsx index b383ad830012..2b9f4d47e27c 100644 --- a/apps/meteor/client/providers/CustomSoundProvider.tsx +++ b/apps/meteor/client/providers/CustomSoundProvider.tsx @@ -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 ; }; diff --git a/apps/meteor/client/providers/MeteorProvider.tsx b/apps/meteor/client/providers/MeteorProvider.tsx index dadb47caead4..aa12af905521 100644 --- a/apps/meteor/client/providers/MeteorProvider.tsx +++ b/apps/meteor/client/providers/MeteorProvider.tsx @@ -35,8 +35,8 @@ const MeteorProvider: FC = ({ children }) => ( - - + + @@ -56,8 +56,8 @@ const MeteorProvider: FC = ({ children }) => ( - - + + From d4c8d5825ab1cb24d77db49db7b212a42ac175bc Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Fri, 20 Oct 2023 11:36:13 -0300 Subject: [PATCH 2/2] review --- apps/meteor/client/providers/CustomSoundProvider.tsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/meteor/client/providers/CustomSoundProvider.tsx b/apps/meteor/client/providers/CustomSoundProvider.tsx index 2b9f4d47e27c..cb2d2933117f 100644 --- a/apps/meteor/client/providers/CustomSoundProvider.tsx +++ b/apps/meteor/client/providers/CustomSoundProvider.tsx @@ -1,9 +1,8 @@ -import { CustomSoundContext, useUserId } from '@rocket.chat/ui-contexts'; +import { CustomSoundContext, useUserId, useStream } from '@rocket.chat/ui-contexts'; import type { FC } 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(); @@ -14,12 +13,14 @@ const CustomSoundProvider: FC = ({ children }) => { void CustomSounds.fetchCustomSoundList(); }, [userId]); + const streamAll = useStream('notify-all'); + useEffect(() => { if (!userId) { return; } - return sdk.stream('notify-all', ['public-info'], ([key, data]) => { + return streamAll('public-info', ([key, data]) => { switch (key) { case 'updateCustomSound': CustomSounds.update(data[0].soundData); @@ -28,8 +29,8 @@ const CustomSoundProvider: FC = ({ children }) => { CustomSounds.remove(data[0].soundData); break; } - }).stop; - }, [userId]); + }); + }, [userId, streamAll]); return ; };