From 2d44766dc684037a39ad90b3f1c3a43635682083 Mon Sep 17 00:00:00 2001 From: MartinSchoeler Date: Tue, 21 Jan 2025 15:41:11 -0300 Subject: [PATCH] refactor: remove meteor startup from livechat-enterprise --- .../app/livechat-enterprise/client/index.ts | 1 - .../app/livechat-enterprise/client/startup.ts | 22 ----------------- .../hooks/useLivechatEnterprise.ts | 24 +++++++++++++++++++ apps/meteor/client/views/root/AppLayout.tsx | 3 +++ 4 files changed, 27 insertions(+), 23 deletions(-) delete mode 100644 apps/meteor/app/livechat-enterprise/client/startup.ts create mode 100644 apps/meteor/app/livechat-enterprise/hooks/useLivechatEnterprise.ts diff --git a/apps/meteor/app/livechat-enterprise/client/index.ts b/apps/meteor/app/livechat-enterprise/client/index.ts index 7be870039276..dad541510b4d 100644 --- a/apps/meteor/app/livechat-enterprise/client/index.ts +++ b/apps/meteor/app/livechat-enterprise/client/index.ts @@ -1,5 +1,4 @@ import { hasLicense } from '../../license/client'; -import './startup'; void hasLicense('livechat-enterprise').then((enabled) => { if (!enabled) { diff --git a/apps/meteor/app/livechat-enterprise/client/startup.ts b/apps/meteor/app/livechat-enterprise/client/startup.ts deleted file mode 100644 index bdf6c2549816..000000000000 --- a/apps/meteor/app/livechat-enterprise/client/startup.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { hasLicense } from '../../license/client'; -import { businessHourManager } from '../../livechat/client/views/app/business-hours/BusinessHours'; -import type { IBusinessHourBehavior } from '../../livechat/client/views/app/business-hours/IBusinessHourBehavior'; -import { SingleBusinessHourBehavior } from '../../livechat/client/views/app/business-hours/Single'; -import { settings } from '../../settings/client'; -import { MultipleBusinessHoursBehavior } from './views/business-hours/Multiple'; - -const businessHours: Record = { - multiple: new MultipleBusinessHoursBehavior(), - single: new SingleBusinessHourBehavior(), -}; - -Meteor.startup(() => { - Tracker.autorun(async () => { - const bhType = settings.get('Livechat_business_hour_type'); - if (bhType && (await hasLicense('livechat-enterprise'))) { - businessHourManager.registerBusinessHourBehavior(businessHours[bhType.toLowerCase()]); - } - }); -}); diff --git a/apps/meteor/app/livechat-enterprise/hooks/useLivechatEnterprise.ts b/apps/meteor/app/livechat-enterprise/hooks/useLivechatEnterprise.ts new file mode 100644 index 000000000000..e9bb5a5f35f7 --- /dev/null +++ b/apps/meteor/app/livechat-enterprise/hooks/useLivechatEnterprise.ts @@ -0,0 +1,24 @@ +import { useSetting } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { useHasLicenseModule } from '../../../client/hooks/useHasLicenseModule'; +import { businessHourManager } from '../../livechat/client/views/app/business-hours/BusinessHours'; +import type { IBusinessHourBehavior } from '../../livechat/client/views/app/business-hours/IBusinessHourBehavior'; +import { SingleBusinessHourBehavior } from '../../livechat/client/views/app/business-hours/Single'; +import { MultipleBusinessHoursBehavior } from '../client/views/business-hours/Multiple'; + +const businessHours: Record = { + multiple: new MultipleBusinessHoursBehavior(), + single: new SingleBusinessHourBehavior(), +}; + +export const useLivechatEnterprise = () => { + const businessHourType = useSetting('Livechat_business_hour_type') as string; + const hasLicense = useHasLicenseModule('livechat-enterprise'); + + useEffect(() => { + if (businessHourType && hasLicense) { + businessHourManager.registerBusinessHourBehavior(businessHours[businessHourType.toLowerCase()]); + } + }, [businessHourType, hasLicense]); +}; diff --git a/apps/meteor/client/views/root/AppLayout.tsx b/apps/meteor/client/views/root/AppLayout.tsx index 8a4485ceef7b..28eb06135b7d 100644 --- a/apps/meteor/client/views/root/AppLayout.tsx +++ b/apps/meteor/client/views/root/AppLayout.tsx @@ -7,6 +7,7 @@ import { useEscapeKeyStroke } from './hooks/useEscapeKeyStroke'; import { useGoogleTagManager } from './hooks/useGoogleTagManager'; import { useMessageLinkClicks } from './hooks/useMessageLinkClicks'; import { useAnalytics } from '../../../app/analytics/client/loadScript'; +import { useLivechatEnterprise } from '../../../app/livechat-enterprise/hooks/useLivechatEnterprise'; import { useAnalyticsEventTracking } from '../../hooks/useAnalyticsEventTracking'; import { useLoadRoomForAllowedAnonymousRead } from '../../hooks/useLoadRoomForAllowedAnonymousRead'; import { useNotifyUser } from '../../hooks/useNotifyUser'; @@ -29,6 +30,8 @@ const AppLayout = () => { useLoadRoomForAllowedAnonymousRead(); useNotifyUser(); + useLivechatEnterprise(); + const layout = useSyncExternalStore(appLayout.subscribe, appLayout.getSnapshot); return (