diff --git a/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts b/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts index aab037a16435d..fd88f0237d297 100644 --- a/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts +++ b/apps/meteor/client/hooks/useFeaturePreviewEnableQuery.ts @@ -1,27 +1,28 @@ import type { FeaturePreviewProps } from '@rocket.chat/ui-client'; +import { useMemo } from 'react'; -export const useFeaturePreviewEnableQuery = (features: FeaturePreviewProps[]) => { - return Object.entries( - features - .map((item) => { - if (!item.enableQuery) { - return item; - } - - const expected = item.enableQuery.value; - const received = features.find((el) => el.name === item.enableQuery?.name)?.value; - if (expected !== received) { - item.disabled = true; - item.value = false; - } else { - item.disabled = false; - } +const handleFeaturePreviewEnableQuery = (item: FeaturePreviewProps, _: any, features: FeaturePreviewProps[]) => { + if (item.enableQuery) { + const expected = item.enableQuery.value; + const received = features.find((el) => el.name === item.enableQuery?.name)?.value; + if (expected !== received) { + item.disabled = true; + item.value = false; + } else { + item.disabled = false; + } + } + return item; +}; - return item; - }) - .reduce((result, currentValue) => { - (result[currentValue.group] = result[currentValue.group] || []).push(currentValue); - return result; - }, {} as Record), +const groupFeaturePreview = (features: FeaturePreviewProps[]) => + Object.entries( + features.reduce((result, currentValue) => { + (result[currentValue.group] = result[currentValue.group] || []).push(currentValue); + return result; + }, {} as Record), ); + +export const useFeaturePreviewEnableQuery = (features: FeaturePreviewProps[]) => { + return useMemo(() => groupFeaturePreview(features.map(handleFeaturePreviewEnableQuery)), [features]); }; diff --git a/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx b/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx index ffa7c3d08cd6b..eece30cc72803 100644 --- a/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx +++ b/packages/ui-client/src/components/FeaturePreview/FeaturePreviewBadge.tsx @@ -1,7 +1,7 @@ import { Badge } from '@rocket.chat/fuselage'; import { useTranslation } from '@rocket.chat/ui-contexts'; -import { usePreferenceFeaturePreviewList } from '../../hooks/useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from '../../hooks/usePreferenceFeaturePreviewList'; const FeaturePreviewBadge = () => { const t = useTranslation(); diff --git a/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts b/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts new file mode 100644 index 0000000000000..373862379cc1e --- /dev/null +++ b/packages/ui-client/src/hooks/useDefaultSettingFeaturePreviewList.ts @@ -0,0 +1,12 @@ +import { useSetting } from '@rocket.chat/ui-contexts'; +import { useMemo } from 'react'; + +import { parseSetting, useFeaturePreviewList } from './useFeaturePreviewList'; + +export const useDefaultSettingFeaturePreviewList = () => { + const featurePreviewSettingJSON = useSetting('Accounts_Default_User_Preferences_featuresPreview'); + + const settingFeaturePreview = useMemo(() => parseSetting(featurePreviewSettingJSON), [featurePreviewSettingJSON]); + + return useFeaturePreviewList(settingFeaturePreview ?? []); +}; diff --git a/packages/ui-client/src/hooks/useFeaturePreview.ts b/packages/ui-client/src/hooks/useFeaturePreview.ts index 05d27879990a3..bd46adfdefff5 100644 --- a/packages/ui-client/src/hooks/useFeaturePreview.ts +++ b/packages/ui-client/src/hooks/useFeaturePreview.ts @@ -1,4 +1,5 @@ -import { type FeaturesAvailable, usePreferenceFeaturePreviewList } from './useFeaturePreviewList'; +import { type FeaturesAvailable } from './useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from './usePreferenceFeaturePreviewList'; export const useFeaturePreview = (featureName: FeaturesAvailable) => { const { features } = usePreferenceFeaturePreviewList(); diff --git a/packages/ui-client/src/hooks/useFeaturePreviewList.ts b/packages/ui-client/src/hooks/useFeaturePreviewList.ts index c568f41321d88..08bda4ff81ffe 100644 --- a/packages/ui-client/src/hooks/useFeaturePreviewList.ts +++ b/packages/ui-client/src/hooks/useFeaturePreviewList.ts @@ -1,6 +1,4 @@ import type { TranslationKey } from '@rocket.chat/ui-contexts'; -import { useUserPreference, useSetting } from '@rocket.chat/ui-contexts'; -import { useMemo } from 'react'; export type FeaturesAvailable = | 'quickReactions' @@ -88,36 +86,17 @@ export const defaultFeaturesPreview: FeaturePreviewProps[] = [ export const enabledDefaultFeatures = defaultFeaturesPreview.filter((feature) => feature.enabled); // TODO: Remove this logic after we have a way to store object settings. -const parseSetting = (setting: FeaturePreviewProps[] | string) => { +export const parseSetting = (setting?: FeaturePreviewProps[] | string) => { if (typeof setting === 'string') { - return JSON.parse(setting); + try { + return JSON.parse(setting) as FeaturePreviewProps[]; + } catch (_) { + return; + } } return setting; }; -export const usePreferenceFeaturePreviewList = () => { - const featurePreviewEnabled = useSetting('Accounts_AllowFeaturePreview'); - const userFeaturesPreviewPreference = useUserPreference('featuresPreview'); - const userFeaturesPreview = useMemo( - () => parseSetting(userFeaturesPreviewPreference!), - [userFeaturesPreviewPreference], - ); - const { unseenFeatures, features } = useFeaturePreviewList(userFeaturesPreview); - - if (!featurePreviewEnabled) { - return { unseenFeatures: 0, features: [] as FeaturePreviewProps[], featurePreviewEnabled }; - } - return { unseenFeatures, features, featurePreviewEnabled }; -}; - -export const useDefaultSettingFeaturePreviewList = () => { - const featurePreviewSettingJSON = useSetting('Accounts_Default_User_Preferences_featuresPreview'); - - const settingFeaturePreview = useMemo(() => parseSetting(featurePreviewSettingJSON!), [featurePreviewSettingJSON]); - - return useFeaturePreviewList(settingFeaturePreview); -}; - export const useFeaturePreviewList = (featuresList: Pick[]) => { const unseenFeatures = enabledDefaultFeatures.filter( (defaultFeature) => !featuresList?.find((feature) => feature.name === defaultFeature.name), diff --git a/packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx similarity index 94% rename from packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx rename to packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx index b8782b0a554f6..ac3d6f92d51aa 100644 --- a/packages/ui-client/src/hooks/useFeaturePreviewList.spec.tsx +++ b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.spec.tsx @@ -1,7 +1,8 @@ import { mockAppRoot } from '@rocket.chat/mock-providers'; import { renderHook } from '@testing-library/react'; -import { usePreferenceFeaturePreviewList, enabledDefaultFeatures } from './useFeaturePreviewList'; +import { enabledDefaultFeatures } from './useFeaturePreviewList'; +import { usePreferenceFeaturePreviewList } from './usePreferenceFeaturePreviewList'; it('should return the number of unseen features and Accounts_AllowFeaturePreview enabled ', () => { const { result } = renderHook(() => usePreferenceFeaturePreviewList(), { diff --git a/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts new file mode 100644 index 0000000000000..d7c4c13417d28 --- /dev/null +++ b/packages/ui-client/src/hooks/usePreferenceFeaturePreviewList.ts @@ -0,0 +1,16 @@ +import { useSetting, useUserPreference } from '@rocket.chat/ui-contexts'; +import { useMemo } from 'react'; + +import { FeaturePreviewProps, parseSetting, useFeaturePreviewList } from './useFeaturePreviewList'; + +export const usePreferenceFeaturePreviewList = () => { + const featurePreviewEnabled = useSetting('Accounts_AllowFeaturePreview'); + const userFeaturesPreviewPreference = useUserPreference('featuresPreview'); + const userFeaturesPreview = useMemo(() => parseSetting(userFeaturesPreviewPreference), [userFeaturesPreviewPreference]); + const { unseenFeatures, features } = useFeaturePreviewList(userFeaturesPreview ?? []); + + if (!featurePreviewEnabled) { + return { unseenFeatures: 0, features: [] as FeaturePreviewProps[], featurePreviewEnabled }; + } + return { unseenFeatures, features, featurePreviewEnabled }; +};